Shell编程---监控多个(远程主机)磁盘使用情况并邮件报警

要求:监控多个(远程主机)磁盘使用情况并邮件报警

分析:

  1. 任何一个分区使用到80%就发送一个邮件/短信通知提醒几个人磁盘的使用情况;
  2. 任何一个分区使用到90%以上就在邮件主题给出警告(warning);
  3. Linux服务器上发送邮件程序我们使用sendmail。
  4. 因为是监控远程主机所以需要提前做好脚本所在主机和被监控主机的免密登录。

解答:

#!/bin/sh
source /etc/profile


#define variable

distantIpList=('111.0.0.1' '111.0.0.2' '111.0.0.3')
emailArray=('[email protected]'  '[email protected]')
happenTime=`date  "+%Y-%m-%d  %H:%M:%S" `

for ip in ${distantIpList[*]}

do
	if [ ${ip} == '111.0.0.1' ];then	
		port='1120'	
	elif [ ${ip} == '111.0.0.2' ];then
		port='1121'	
	else
		port='22'
	fi
	
	spaceUsedList=`ssh -p${port} root@${ip} df -h|grep -o [0-9]*%|grep -o '[0-9]\+' `
	
	for spaceUse in ${spaceUsedList}
	do	
		if [ ${spaceUse}  -ge 80 -a ${spaceUse} -le 90 ];then
			for email in ${emailArray[*]}
			do
				echo "NOTICE: Disk space for your server ${ip}, already used ${spaceUse}%,${happenTime}" | mail -s 'Disk Space Notice'  ${email}
			done
			
		elif [ ${spaceUse} -gt 90 ];then
			for email in ${emailArray[*]}
			do
				echo "WARNING: Low disk space for your server ${ip}, already used ${spaceUse}%,${happenTime} " | mail -s 'Disk Space Warning'  ${email}
			done
	
		fi
	
	done
	
done

你可能感兴趣的:(shell脚本,工具,监控)