收藏文章 楼主

linux centos shell脚本监控多台主机

版块:自动化运维linux   类型:普通   作者:小绿叶技术博客   查看:1281   回复:0   获赞:0   时间:2020-09-09 09:27:11

#!/bin/bash

yum install -y mutt telnet

echo "

220.181.38.148

58.250.137.36

eisc.cn

" > iplist.txt

port=22 # 定义测试IP的端口

for ip in `cat iplist.txt`;do # for循环,定义变量IP 是 in 赋值为一条命令 查看文件的结果, do 开始循环

telnet $ip $port>/dev/null  #先检查主机是否在线,如果在线则进行进一步监控

if [ $? -eq 0 ];then # 判断 $? 上一条命令成功执行后 then 然后


rootused=``ssh $ip df -h|grep /$|awk '{print $4}'|cut -d% -f1` #取根目录使用情况

for hardid in ``ssh $ip df -h|grep "^\/dev\/s[a-z][a-z]"|awk '{print $1}'`;do

hardused=``ssh $ip df -h|grep $hardid|awk '{print $5}'|cut -d% -f1`  #取其它本地硬盘的使用情况

done # 结束循环


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" xxx@eisc.cn

fi


if [ $hardused -le 80  ];then     #如果硬盘使用已高于80%则邮件通知

echo "warm:$ip the Hard drive capacity is more 80%"|mutt -s"monitor report" xxx@eisc.cn

fi


if [ $rootused -le 80  ];then     #如果硬盘使用已高于80%则邮件通知

echo "warm:$ip the Hard drive capacity is more 80%"|mutt -s"monitor report" xxx@eisc.cn

fi

for hardid in `ssh $ip iostat |grep "^s[a-z][a-z]"|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" xxx@eisc.cn

fi

done


else

echo "host:$ip is not alive"|mutt -s"monitor report" xxx@eisc.cn  #如果主机无法ping通则邮件通知

fi

done



提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:linux centos shell脚本监控多台主机

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息