脚本检测linux系统资源
1. 用top命令收集系统运行状况(每2小时运行一次):
syscheck.sh
#!/bin/sh
top b n 1 | grep 'Cpu(s)' >> /scripts/cpustats.txt
top b n 1 | grep Mem >> /scripts/memstats.txt
2.分析取得的数据,并将结果发email至root(每日运行一次):
sysmonitor.sh
#!/bin/sh
date > /scripts/sysmonitor.txt
awk '{if (x<(100-$8)) {x=(100-$8)}} END {print "Cpus Usage Percentage MAX: " x}' cpustats.txt >> /scripts/sysmonitor.txt
nn=$(cat -n /scripts/cpustats.txt |tail -n 1 | awk '{print $1}')
awk 'BEGIN {num="'$nn'"}{x=x+$8} END {x=x/num}END {x=100-x} END {print "Cpus Usage Percentage Average: " x}' cpustats.txt >> /scripts/sysmonitor.txt
awk 'BEGIN{num="'$nn'"}{x=x+($4/$2)}END {x=x/num*100}END {print "Memory Usage Percentage Average: " x}' memstats.txt >> /scripts/sysmonitor.txt
rm /scripts/cpustats.txt
rm /scripts/memstats.txt
mail -s 'Lotus3 system monitor'
root@host.com < /scripts/sysmonitor.txt
3.排程运行脚本:
在/etc/corntab 里面加上排程
00 */2 * * * root /scripts/syscheck.sh
30 16 * * * root /scripts/sysmonitor.sh
4.所收到email格式如下:
Thu Nov 29 15:41:31 CST 2007
Cpus Usage Percentage MAX: 20.8
Cpus Usage Percentage Average: 20.8
Memory Usage Percentage Average: 99.8025