• Linux巡检脚本


            做运维的朋友都知道,大家经常要对一些系统做月度维护或者自查,在这个过程中,如果只是小工作量的查询,进入系统直接输入命令即可。但是如果说要查询的系统很多,每个系统中要查询的项目也很多,那么每次都要一次一次地输入命令就很麻烦。所以建议大家在获得管理员的许可的前提下,可以使用脚本。

            下面是我写的linux中常见的检查项的脚本,大家可以测试使用,如果有其他的检查项,按照上面的格式添加进去就好。

    1. #!/bin/bash
    2. echo "" > /tmp/shell/xunjian.txt
    3. echo "1 cpu mem " >> /tmp/shell/xunjian.txt
    4. echo "" >> /tmp/shell/xunjian.txt
    5. top -bn 1 | head -n 15 >> /tmp/shell/xunjian.txt
    6. echo "" >> /tmp/shell/xunjian.txt
    7. echo "2 df -h " >> /tmp/shell/xunjian.txt
    8. echo "" >> /tmp/shell/xunjian.txt
    9. df -h >> /tmp/shell/xunjian.txt
    10. echo "" >> /tmp/shell/xunjian.txt
    11. echo "3 hostname " >> /tmp/shell/xunjian.txt
    12. echo "" >> /tmp/shell/xunjian.txt
    13. hostname >> /tmp/shell/xunjian.txt
    14. echo "" >> /tmp/shell/xunjian.txt
    15. echo "4 ps aux|more " >> /tmp/shell/xunjian.txt
    16. echo "" >> /tmp/shell/xunjian.txt
    17. ps aux| more |head -n 10 >> /tmp/shell/xunjian.txt
    18. echo "" >> /tmp/shell/xunjian.txt
    19. echo "5 ifconfig " >> /tmp/shell/xunjian.txt
    20. echo "" >> /tmp/shell/xunjian.txt
    21. ifconfig >> /tmp/shell/xunjian.txt
    22. echo "" >> /tmp/shell/xunjian.txt
    23. echo "6 vmstat 1 1 " >> /tmp/shell/xunjian.txt
    24. echo "" >> /tmp/shell/xunjian.txt
    25. vmstat 1 1 >> /tmp/shell/xunjian.txt
    26. echo "" >> /tmp/shell/xunjian.txt
    27. echo "7 date " >> /tmp/shell/xunjian.txt
    28. echo "" >> /tmp/shell/xunjian.txt
    29. date >> /tmp/shell/xunjian.txt

    查询结果如下所示:


    cpu mem

    top - 17:58:35 up 10 min,  2 users,  load average: 0.03, 0.14, 0.13
    Tasks: 274 total,   1 running, 273 sleeping,   0 stopped,   0 zombie
    %Cpu(s):  0.0 us,  7.1 sy,  0.0 ni, 92.9 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
    KiB Mem :  1863076 total,    73052 free,  1156900 used,   633124 buff/cache
    KiB Swap:  2097148 total,  2094836 free,     2312 used.   538472 avail Mem

       PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
         6 root      20   0       0      0      0 S  5.3  0.0   0:03.57 ksoftirqd/0
      3217 root      30  10  320188  12700   4860 S  5.3  0.7   0:17.20 urlgrabber+
      3316 root      20   0  162020   2180   1516 R  5.3  0.1   0:00.03 top
         1 root      20   0  193804   4940   2184 S  0.0  0.3   0:01.60 systemd
         2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
         4 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:+
         7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
         8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh

    2  df -h

    Filesystem      Size  Used Avail Use% Mounted on
    devtmpfs        895M     0  895M   0% /dev
    tmpfs           910M     0  910M   0% /dev/shm
    tmpfs           910M   11M  900M   2% /run
    tmpfs           910M     0  910M   0% /sys/fs/cgroup
    /dev/sda3        18G  7.1G   11G  41% /
    /dev/sda1       297M  161M  137M  54% /boot
    tmpfs           182M  4.0K  182M   1% /run/user/42
    tmpfs           182M   28K  182M   1% /run/user/1000
    /dev/sr0        4.2G  4.2G     0 100% /run/media/red/RHEL-7.7 Server.x86_64

    3  hostname  

    localhost.localdomain

    4  ps aux|more  

    USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root          1  0.2  0.2 193804  4940 ?        Ss   17:47   0:01 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
    root          2  0.0  0.0      0     0 ?        S    17:47   0:00 [kthreadd]
    root          4  0.0  0.0      0     0 ?        S<   17:47   0:00 [kworker/0:0H]
    root          6  0.5  0.0      0     0 ?        S    17:47   0:03 [ksoftirqd/0]
    root          7  0.0  0.0      0     0 ?        S    17:47   0:00 [migration/0]
    root          8  0.0  0.0      0     0 ?        S    17:47   0:00 [rcu_bh]
    root          9  0.0  0.0      0     0 ?        R    17:47   0:00 [rcu_sched]
    root         10  0.0  0.0      0     0 ?        S<   17:47   0:00 [lru-add-drain]
    root         11  0.0  0.0      0     0 ?        S    17:47   0:00 [watchdog/0]

    5  ifconfig  

    ens33: flags=4163  mtu 1500
            inet 192.168.188.128  netmask 255.255.255.0  broadcast 192.168.188.255
            inet6 fe80::e5ca:68c8:e7fa:5617  prefixlen 64  scopeid 0x20
            ether 00:0c:29:f0:d8:bc  txqueuelen 1000  (Ethernet)
            RX packets 204409  bytes 276329842 (263.5 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 93187  bytes 5603372 (5.3 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    lo: flags=73  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    virbr0: flags=4099  mtu 1500
            inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
            ether 52:54:00:89:23:f1  txqueuelen 1000  (Ethernet)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


    6  vmstat 1  1  

    procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
     r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
     0  0   2312  73236     40 633468    0    4  2019  1099  770 1423  3  6 91  0  0

    7  date  

    Sun Sep  3 17:58:35 PDT 2023

    8  netstat -nr  

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    0.0.0.0         192.168.188.2   0.0.0.0         UG        0 0          0 ens33
    192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
    192.168.188.0   0.0.0.0         255.255.255.0   U         0 0          0 ens33
    [root@localhost shell]#

  • 相关阅读:
    JavaScript代码是怎么在浏览器里面运行起来的?
    JS提升:实现flat平铺的底层原理
    “九韶杯”河科院程序设计协会第一届程序设计竞赛题目分析以及总结
    leetcode 135: 分发糖果
    iOS App怎么上架到苹果TestFlight?
    PixCake:让你的照片焕发新生的AI人像处理技术修图软件
    11后端开发就是CRUD?没那么简单!
    刚接触银行新业务测试的一些问题
    Bayer转换算法
    小程序面试题:小程序兼容性问题
  • 原文地址:https://blog.csdn.net/Gao068465/article/details/132661502