|
用shell写了个脚本同时监控多台主机(监控主机是否在线,cpu,内存,硬盘,io使用状态,并有邮件通知功能),大神看后觉得有不当之处或有更好的实现方式,请不屑笔墨指出。
安装mutt:
1
| [iyunv@centos-server ~]# yum install mutt
|
监控列表:
1
2
3
4
5
6
| [iyunv@centos-server ~]# cat > iplist.txt <<end
> 22.22.22.128
> 22.22.22.129
> 22.22.22.130
> 22.22.22.134
> end
|
监控脚本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| #!/bin/bash
for ip in `cat iplist.txt`;do
ping $ip -c1>/dev/null #先检查主机是否在线,如果在线则进行进一步监控
if [ $? -eq 0 ];then
hardused=`ssh $ip df -h|grep "/$"|awk '{print $4}'|cut -d% -f 1`
memtotal=`ssh $ip free -m|grep Mem|awk '{print $2}'`
memused=`ssh $ip free -m|grep Mem|awk '{print $3}'`
mem=`expr $memused * 100 / $memtotal`
idelcpu=`ssh $ip top -n1|grep Cpu|awk '{print $5}'|cut -d"." -f1`
cpuused=`expr 100 - $idelcpu`
if [ $mem -gt 70 ];then #如果内存使用高于70%则邮件通知
echo "warm:$ip memory is $mem"|mutt -s "monitor report" kefu@iyunv.com
fi
if [ $hardused -le 80 ];then #如果硬盘使用已高于80%则邮件通知
echo "warm:$ip the Hard drive capacity is more 80%"|mutt -s"monitor report" kefu@iyunv.com
fi
for hardid in `ssh $ip iostat |grep ^sd|awk '{print $1}'`;do #先取盘符
iostat=`ssh $ip iostat -x|grep $hardid|awk '{print $12}'|cut -d"." -f1` #取得io繁忙状态
echo $iostat
if [ $iostat -gt 80 ];then #如果io繁忙高于80%,则邮件通知
echo "ipaddress:$ip,hard:$hardid,iostat:$iostat"|mutt -s"warm" kefu@iyunv.com
fi
done
else
echo "host:$ip is not alive"|mutt -s"monitor report" kefu@iyunv.com #如果主机无法ping通则邮件通知
fi
done
|
每5分钟监控一次(如果出现什么问题,并且这个得不到解决,每5分钟就收到一封邮件,这很令人纠结):
[iyunv@centos-server ~]# crontab -e
*/5 * * * * bash /root/monitor.sh
查收邮件:
</end
|
|