1. 程式人生 > >Shell程式設計---監控多個(遠端主機)磁碟使用情況並郵件報警

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} [email protected]${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