监控 ☞ 远程磁盘检测

阅读量: zyh 2020-05-14 09:34:44
Categories: Tags:
#!/bin/bash 
# 文件名:disklog.sh 
# 用途:监视远程系统的磁盘使用情况 
BaseDir=`cd "$(dirname "$0")"; pwd`
cd $BaseDir
logfile="disk.log" 
if [[ -n $1 ]];then 
    logfile=$1 
fi

shellexecuser=`whoami`
if [[ $shellexecuser == root ]];then
    rm -rf /root/.ssh/known_hosts
else
    rm -rf /home/$shellexecuser/.ssh/known_hosts
fi

printf "%-8s %-14s %-9s %-8s %-6s %-6s %-6s %s\r\n" "Date" "IP ADDRESS" "Device" "Capacity" "Used" "Free" "Percent" "Status" > $logfile 

##################### 手动填写区
# 提供远程主机IP地址列表 1.1.1.1 2.2.2.2 3.3.3.3
IP_LIST=
# 监控阈值(百分比) 只填写数字 1 到 100
DiskPct=
# 执行用户
UserName=''
# 执行用户所需私钥, 此文件需要与脚本同级目录
PemName=''
# 企业微信bot机器人地址
wx_api=''
##################### 手动填写区

for ip in $IP_LIST;do 
    ssh -i $PemName -o StrictHostKeyChecking=no ${UserName}@$ip 'df -H' | grep ^/dev/ > /tmp/$$.df 
    while read line;do 
        cur_date=`date  "+%F_%R"`
        printf "%-8s %-14s " $cur_date $ip 
        echo $line | awk '{ printf("%-9s %-8s %-6s %-6s %-8s", $1,$2,$3,$4,$5); }' 

        pusg=$(echo $line | egrep -o "[0-9]+%") 
        pusg=${pusg/\%/}; 
        if [ $pusg -lt $DiskPct ];then 
            echo OK
        else 
            echo ALERT 
        fi 
    done < /tmp/$$.df 
    rm -rf /tmp/$$.df
done >> ${logfile}
sed -n '1p' ${logfile} > alert.log
awk '$NF == "ALERT"{print $0}' ${logfile} >> alert.log
#sed -i '1i "磁盘阈值:'"$DiskPct"'"' alert.log
content=`cat alert.log`
grep -q 'ALERT' alert.log && {
curl "$wx_api"  -H 'Content-Type: application/json'  \
-d '
   {
        "msgtype": "text",
        "text": {
            "content": "'"$content"'"
        }
   }'
}