花了一天写了三条命令分别检查cpu,内存,磁盘
[root@localhost ~]# cat cpu_mem_disk.sh #!/bin/sh # echo "1 检查cpu利用率---------------------------------------" awk '/^cpu /{a=$5;for(i=2;i<=NF;i++) s+=$i;id=a/s}END{percend=(1-id)*100;if(percend>80)printf " 33[31m warning 33[0m";printf percend"% "}' /proc/stat echo "------------------------------------------------------" echo "2 检查内存情况-----------------------------------------" cat /proc/meminfo|sed -rn '/MemTotal|MemFree|Buffers|^Cached:/p'|awk 'NR==1{total=$2}NR!=1{sum+=$2}END{mem=sum/total*100; if(mem>80){print "----内存利用率高----";print mem;print "内存前十的进程";system("ps aux | grep -v USER | sort -rn -k4 | head")}else{print "内存通过"} }' echo "-------------------------------------------------------" echo "3 检查磁盘利用率---------------------------------------" df -h|sed '1d'|awk -F '[ %]+' '$5>80{printf "磁盘利用率高--->";print "分区:"$1,"利用率:"$5"%"}' echo "检查完成--------------------------------------"
测试结果
1 检查cpu利用率--------------------------------------- warning 87.45874% ------------------------------------------------------ 2 检查内存情况----------------------------------------- ----内存利用率高---- 88.2815 内存前十的进程 lim 2896 0.0 17.5 1488080 175528 ? Sl 10:20 0:20 /usr/bin/gnome-shell lim 3138 0.0 4.7 1006916 47864 ? Sl 10:20 0:00 /usr/libexec/evolution-calendar-factory lim 3061 0.0 2.7 1015472 27444 ? Sl 10:20 0:00 nautilus --no-default-window --force-desktop root 1275 0.0 2.6 216460 26144 tty1 Ss+ 10:19 0:01 /usr/bin/Xorg :0 -background none -noreset -audit 4 -verbose -auth /run/gdm/auth-for-gdm-QeliMR/database -seat seat0 -nolisten tcp vt1 lim 2852 0.0 2.4 1094484 24128 ? Sl 10:20 0:01 /usr/libexec/gnome-settings-daemon lim 3492 0.0 2.3 573672 23028 ? Sl 10:20 0:00 /usr/libexec/gnome-terminal-server lim 2986 0.0 2.0 713760 20928 ? Sl 10:20 0:00 /usr/libexec/goa-daemon lim 3082 0.0 1.8 378028 18036 ? S 10:20 0:23 /usr/bin/vmtoolsd -n vmusr root 989 0.0 1.6 553660 16508 ? Ssl 10:19 0:03 /usr/bin/python -Es /usr/sbin/tuned -l -P lim 3068 0.0 1.6 759384 16600 ? Sl 10:20 0:00 /usr/bin/gnome-software --gapplication-service ------------------------------------------------------- 3 检查磁盘利用率--------------------------------------- 磁盘利用率高--->分区:/dev/mapper/cl-root 利用率:89% 磁盘利用率高--->分区:/dev/sda1 利用率:88% 检查完成--------------------------------------