博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
远程硬盘资源监控通用脚本
阅读量:4297 次
发布时间:2019-05-27

本文共 2193 字,大约阅读时间需要 7 分钟。

一、建立一个通用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对端免密码登录,略。

转载地址:http://zhbws.baihongyu.com/

你可能感兴趣的文章
一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
查看>>
iOS 如何放大按钮点击热区
查看>>
ios设备唯一标识获取策略
查看>>
获取推送通知的DeviceToken
查看>>
Could not find a storyboard named 'Main' in bundle NSBundle
查看>>
CocoaPods安装和使用教程
查看>>
Beginning Auto Layout Tutorial
查看>>
block使用小结、在arc中使用block、如何防止循环引用
查看>>
iPhone开发学习笔记002——Xib设计UITableViewCell然后动态加载
查看>>
iOS开发中遇到的问题整理 (一)
查看>>
Swift code into Object-C 出现 ***-swift have not found this file 的问题
查看>>
为什么你的App介绍写得像一坨翔?
查看>>
RTImageAssets插件--@3x可自动生成@2x图片
查看>>
iOS开发的一些奇巧淫技
查看>>
常浏览的博客和网站
查看>>
Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
查看>>
点击button实现Storyboard中TabBar Controller的tab切换
查看>>
Xcode 的正确打开方式——Debugging
查看>>
打包app出现的一个问题
查看>>
iOS在Xcode6中怎么创建OC category文件
查看>>