远程硬盘资源监控通用脚本

来源:互联网 发布:网络语言的利弊作文 编辑:程序博客网 时间:2024/04/30 10:09

一、建立一个通用shell脚本,通过ssh执行df命名获取对端硬盘信息。注意增加执行权限。

[iptv@ting-wo disk]$ more diskmonitor.sh 
#!/bin/bash
mailflag="true"


#check parameters count
if [ $# -ne 6 ];then
  echo 6 parameters needed:taskname client_ip_list port account maillist alertlimit
  echo example:diskmonitor.sh iptvservers 10.17.44.122 22 iptv abc@aaa.com 3
  echo \"3\" at the ednd means it will send alert mail if disk usage > 30%. 3表示超过30%就告警。
  return 1;
fi;
if [[ ! $6 =~ ^[0-9]$ ]];then
  echo "\$6 is the lowest limit of triger alert,must be a number and between 1-9."
  return 1;
fi;


echo "**********start**********"
taskname=$1
client_ip_list=$2
dir="/home/iptv/monitor/disk"
logfile=${dir}/disk${taskname}.log
mailfile=${dir}/mail${taskname}.txt
port=$3
account=$4
maillist=$5
limit=$6


date
echo taskname=$taskname,client_ip_list=$client_ip_list,port=$port,account=$account,maillist=$maillist


#echo "clean tmp file"
#cat /dev/null > $mailfile;
#cat /dev/null > $logfile;
#count ,how many disks > 30%;
for ip in ${client_ip_list};do
  echo "#####start looking ${ip}\'s disk info.#####"
  cat /dev/null > $mailfile;
  cat /dev/null > $logfile;
  echo ssh get disks info to $logfile
  ssh -p${port} ${account}@${ip} df -h > $logfile;
  if [ $? -ne 0 ];then 
    echo ssh error.exit loop.;
    break;
  fi;
  count=`cat $logfile| grep -v mnt|grep -c -E [${limit}-9][0-9]%\|100%`;
  echo disk alert count=$count
  if [ $count -gt 0 ];
  then 
    echo "start alerting by mail......"
    echo "This is a alert for ${ip}\'s disk usage." >> $mailfile
    echo "host address:${ip}" >> $mailfile
    echo "disk info:" >> $mailfile
    cat $logfile >> $mailfile
    if [ "$mailflag" == "true" ]; then 
      for mail in $maillist;do
        mail -s "disk alert for ${ip}" $mail < $mailfile;
      done;
    fi;
  fi;
  echo "#####end ${ip}#####"
done;


echo "*********end*********"

二、建立一个专用shell脚本,调用通用脚本。注意增加执行权限。

[iptv@ting-wo disk]$ more disk158.sh
#!/bin/bash
maillist="aaa@1234.com bbb@1234.com"
taskname=158
client_ip_list="11.22.33.158"
account=nginx
port=22
limit=3
#password=nginxUser*1


/home/iptv/monitor/disk/diskmonitor.sh "$taskname" "$client_ip_list" "$port" "$account" "$maillist" "$limit"

三、在cron中加入定期任务

0 6 * * * /home/iptv/monitor/disk/disk158.sh > /home/iptv/monitor/disk/crondisk158.log 2>&1

四、设置ssh对端免密码登录,略。

0 0
原创粉丝点击