温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

shell如何监控多台主机

发布时间:2021-12-04 15:38:17 来源:亿速云 阅读:113 作者:小新 栏目:移动开发

小编给大家分享一下shell如何监控多台主机,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

首先要在被监控主机和监控主机之间建立信任关系,不了解ssh证书验证的可以看看:

http://dragon123.blog.51cto.com/9152073/1586795

安装mutt:

[root@centos-server ~]# yum install mutt

监控列表:

[root@centos-server ~]# cat > iplist.txt <<end
> 22.22.22.128
> 22.22.22.129
> 22.22.22.130
> 22.22.22.134
> end

监控脚本:

#!/bin/bash
for ip in `cat iplist.txt`;do
	ping $ip -c1>/dev/null  #先检查主机是否在线,如果在线则进行进一步监控
	if [ $? -eq 0 ];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" 18507656225@163.com
fi

if [ $hardused -le 80  ];then     #如果硬盘使用已高于80%则邮件通知
		echo "warm:$ip the Hard drive capacity is more 80%"|mutt -s"monitor report" 18507656225@163.com 
fi

if [ $rootused -le 80  ];then     #如果硬盘使用已高于80%则邮件通知
		echo "warm:$ip the Hard drive capacity is more 80%"|mutt -s"monitor report" 18507656225@163.com 
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" 18507656225@163.com	
		fi
	done

else
	echo "host:$ip is not alive"|mutt -s"monitor report" 18507656225@163.com  #如果主机无法ping通则邮件通知	
fi
done

每5分钟监控一次(如果出现什么问题,并且这个得不到解决,每5分钟就收到一封邮件,这很令人纠结):

[root@centos-server ~]# crontab -e

*/5 * * * * bash /root/monitor.sh

查收邮件:

shell如何监控多台主机

shell如何监控多台主机

以上是“shell如何监控多台主机”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI