1. 程式人生 > 實用技巧 >shell監控多臺主機

shell監控多臺主機

用shell寫了個指令碼同時監控多臺主機(監控主機是否線上,cpu,記憶體,硬碟,io使用狀態,並有郵件通知功能),大神看後覺得有不當之處或有更好的實現方式,請不屑筆墨指出。


首先要在被監控主機和監控主機之間建立信任關係,不瞭解ssh證書驗證的可以看看:

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


安裝mutt:

[[email protected]~]#yuminstallmutt


監控列表:

[[email protected]~]#cat>iplist.txt<<end
>22.22.22.128
>22.22.22.129
>22.22.22.130
>22.22.22.134
>end


監控指令碼:

#!/bin/bash
foripin`catiplist.txt`;do
	ping$ip-c1>/dev/null#先檢查主機是否線上,如果線上則進行進一步監控
	if[$?-eq0];then

	rootused=``ssh$ipdf-h|grep/$|awk'{print$4}'|cut-d%-f1`#取根目錄使用情況
forhardidin``ssh$ipdf-h|grep"^\/dev\/s[a-z][a-z]"|awk'{print$1}'`;do
hardused=``ssh$ipdf-h|grep$hardid|awk'{print$5}'|cut-d%-f1`#取其它本地硬碟的使用情況
	done
	memtotal=`ssh$ipfree-m|grepMem|awk'{print$2}'`
	memused=`ssh$ipfree-m|grepMem|awk'{print$3}'`
	mem=`expr$memused\*100/$memtotal`
	idelcpu=`ssh$iptop-n1|grepCpu|awk'{print$5}'|cut-d"."-f1`
	cpuused=`expr100-$idelcpu`

if[$mem-gt70];then#如果記憶體使用高於70%則郵件通知
	echo"warm:$ipmemoryis$mem"|mutt-s"monitorreport"
[email protected]
fi if[$hardused-le80];then#如果硬碟使用已高於80%則郵件通知 echo"warm:$iptheHarddrivecapacityismore80%"|mutt-s"monitorreport"[email protected] fi if[$rootused-le80];then#如果硬碟使用已高於80%則郵件通知 echo"warm:$iptheHarddrivecapacityismore80%"|mutt-s"monitorreport"
[email protected]
fi forhardidin`ssh$ipiostat|grep"^s[a-z][a-z]"|awk'{print$1}'`;do#先取碟符 iostat=`ssh$ipiostat-x|grep$hardid|awk'{print$12}'|cut-d"."-f1`#取得io繁忙狀態 echo$iostat if[$iostat-gt80];then#如果io繁忙高於80%,則郵件通知 echo"ipaddress:$ip,hard:$hardid,iostat:$iostat"|mutt-s"warm"[email protected] fi done else echo"host:$ipisnotalive"|mutt-s"monitorreport"[email protected]#如果主機無法ping通則郵件通知 fi done


每5分鐘監控一次(如果出現什麼問題,並且這個得不到解決,每5分鐘就收到一封郵件,這很令人糾結):

[[email protected] ~]# crontab -e

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


查收郵件:

wKiom1SZix_AScEpAAGCG6cg4vw750.jpg

wKioL1SZjIPRo20-AAEUCsrn_a4856.jpg

轉載於:https://blog.51cto.com/dragon123/1594076