31.1、监控磁盘:
#!/bin/sh diskspace="`df -hT`" IFS=" " disk_value="80" echo "文件系统 类型 容量 已用 可用 已用% 挂载点" for line in $diskspace;do disk=`echo $line | awk -v x=${disk_value} '{if((+$6>x)) print $1,$2,$3,$4,$5,$6,$7}'` use_disk_value="`echo $disk | awk '{print +$6}'`" if [ ${use_disk_value} -gt ${disk_value} ];then echo $disk fi done
31.2、监控内存:
#!/bin/sh mem_value="80" mem="`free -m | awk 'NR==2{printf "Memory Usage: %sMB/%sMB %.2f%% ",$3,$2,$3*100/$2 }'`" use_mem_value="`echo $mem | awk '{printf "%.2f ",+$4}'`" if [ `echo "${use_mem_value}>${mem_value}" | bc` -eq 1 ];then echo "Warning: current memory usage is $mem" fi ######################TestValue########################## echo "当前内存使用情况:${mem}" echo "当前内存使用百分比:${use_mem_value}" echo "当前内存使用百分比是否超过${mem_value}:`echo "${use_mem_value}>${mem_value}" | bc`" #########################################################
31.3、监控cpu:
#!/bin/sh load_value="0.7" cpu_load="`top -bn1 | grep "load average:" | awk '{print $NF}'`" cpu_num="`lscpu | grep "CPU(s):" | awk '{print $2}'`" cpu_load_value="`awk -v x=${cpu_load} -v y=${cpu_num} 'BEGIN{printf "%.2f ",x/y}'`" if [ `echo "${cpu_load_value}>${load_value}" | bc` -eq 1 ];then echo "WARING:Current Cpu Load IS ${cpu_load_value}" fi ##################################TEST_VALUE####################################################### echo "Cpu15分钟的平均负载:${cpu_load}" echo "Cpu总核数:${cpu_num}" echo "Cpu15分钟的平均负载占Cpu总核数的多少:${cpu_load_value}" echo "Cpu15分钟的平均负载占Cpu总核数的值是否大于${load_value}:`echo "${cpu_load_value}>${load_value}" | bc`" ###################################################################################################