shell监控服务器集成钉钉报警和邮件通知

针对于linux服务器CPU、memory、disk监控报警

#!/usr/bin/env bash
source /etc/profile
source ~/.bashrc
yum install bc -y
FILE=/usr/local/bin/sendEmail
#判断root下有无sendmail包
if [ -f $FILE ];then
        echo "文件已存在"
else
        #下载sendMail
        cd /root && wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
        #创建目录
        mkdir -p /usr/local/bin
        #解压
        tar zxf sendEmail-v1.56.tar.gz -C /usr/src/
        #进入解压目录
        cd /usr/src/sendEmail-v1.56/
        #复制程序到指定目录
        cp -a sendEmail /usr/local/bin/
        #给执行权限
        chmod +x /usr/local/bin/sendEmail
        #安装组件
        yum install perl-Net-SSLeay perl-IO-Socket-SSL -y

fi

Item=生产环境
Host=`/usr/bin/hostname`
IP=`ip a | grep 'inet'|awk -F '[ ]' '{print $6}' | grep 24`
Cpu=`top -b -n1 | fgrep "Cpu(s)" | tail -1 | awk -F ' ' '{ print $2 }'`
KMem=`top -b -n1 | fgrep "KiB Mem" |tail -l | awk -F ' ' '{ print $8 }'`
ZKMem=`top -b -n1 | fgrep "KiB Mem" |tail -l | awk -F ' ' '{ print $4 }'`
BFBMem=$(printf "%.3f" `echo "scale=3;$KMem*100/$ZKMem"|bc`)
SMem=`top -b -n1 | fgrep "KiB Swap"  |tail -l| awk -F ' ' '{ print $5 }'`
ZSMem=`top -b -n1 | fgrep "KiB Swap" |tail -l| awk -F ' ' '{ print $3 }'`
DEV=`df -hP | grep '^/dev/*' | cut -d' ' -f1 | sort`

for I in $DEV
do dev=`df -Ph | grep $I | awk '{print $1}'`
size=`df -Ph | grep $I | awk '{print $2}'`
used=`df -Ph | grep $I | awk '{print $3}'`
free=`df -Ph | grep $I | awk '{print $4}'`
rate=`df -Ph | grep $I | awk '{print $5}'`
mount=`df -Ph | grep $I | awk '{print $6}'`
#内存检测
echo -e "$I:\tsize:$size\tused:$used\tfree:$free\trate:$rate\tmount:$mount"
echo "$IP"
F=`echo $rate | awk -F% '{print $1}'`
#if内阈值可以根据需求自己改

#磁盘检测
if [ $F -ge 90 ];then
    /usr/local/bin/sendEmail -f xxxxx@163.com -t xxxx@qq.com -s smtp.163.com -u ''$Item' 系统资源监控' -o message-content-type=html -o message-charset=utf8 -xu qwolfking@163.com -xp 'PAHCYSUDU****' -m "$Host $Ip <br> [Disk Used] $mount disk used $rate <br> [Cpu Used] Cpu Used in $Cpu % <br> [KiB Mem Used] KiB Mem Used $BFBMem %" -o tls=no
   curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'\
    -H 'Content-Type: application/json' \
    -d '{"at":{"atMobiles":["xxxxx"]},"msgtype": "markdown","markdown":{"title":"ECS生产服务器","text": "系统资源监控:'$Item' \n\n 主机地址:'$IP' \n\n 主机名:'$Host' \n\n  Disk Used:'$rate' \n\n Cpu Used:'$Cpu'% \n\n KiB Mem Used:'$BFBMem'% \n\n @xxxxx"}}'
fi
done

#CPU检测
if [ $(echo "$Cpu > 90"|bc) -eq 1 ];then
    /usr/local/bin/sendEmail -f xxxxx@163.com -t xxxxx@qq.com -s smtp.163.com -u ''$Item' 系统资源监控' -o message-content-type=html -o message-charset=utf8 -xu xxxxx@163.com -xp 'PAHCYSUDU****' -m "$Host $Ip <br> [Disk Used] $mount disk used $rate <br> [Cpu Used] Cpu Used in $Cpu % <br> [KiB Mem Used] KiB Mem Used $BFBMem %" -o tls=no
   curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'\
    -H 'Content-Type: application/json' \
    -d '{"at":{"atMobiles":["xxxxx"]},"msgtype": "markdown","markdown":{"title":"ECS生产服务器","text": "'$Item' ":系统资源监控 \n\n 主机地址:'$Ip' \n\n 主机名:'$Host' \n\n Disk Used:'$rate' \n\n Cpu Used:'$Cpu' \n\n KiB Mem Used:'$BFBMem' \n\n @xxxxx"}}'
fi

#内存检测
echo $BFBMen
if [ (echo "$BFBMem > 95"|bc) -eq 1 ];then
    /usr/local/bin/sendEmail -f xxxxx@163.com -t xxxxx@qq.com -s smtp.163.com -u ''$Item' 系统资源监控' -o message-content-type=html -o message-charset=utf8 -xu xxxxx@163.com -xp 'PAHCYSUDU****' -m "$Host $Ip <br> [Disk Used] $mount disk used $rate <br> [Cpu Used] Cpu Used in $Cpu % <br> [KiB Mem Used] KiB Mem Used $BFBMem %" -o tls=no
   curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'\
    -H 'Content-Type: application/json' \
    -d '{"at":{"atMobiles":["xxxxx"]},"msgtype": "markdown","markdown":{"title":"ECS生产服务器","text": "'$Item' ":系统资源监控 \n\n 主机地址:'$Ip' \n\n 主机名:'$Host' \n\n Disk Used:'$rate' \n\n Cpu Used:'$Cpu' \n\n KiB Mem Used:'$BFBMem' \n\n @xxxxx"}}'
fi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值