linux 磁盘占用监控,自动发微信通知

1 编写脚本

mkdir -p /home/monitor/

vim /home/monitor/disk_monitor.sh

server=`ip addr|grep inet|grep -v 127.0.0.1|grep -v inet6|grep -v docker|awk '{print $2}'|head -n 1`
for num in `df -h | grep /dev | grep -v /snap/ | awk '{print $5}' | sed 's/%//g'`
do
	if [ $num -gt 90 ]; then
		content="服务器 $server 磁盘不足,占用率 $num %"
		# 微信通知
		CropID='企业微信的CropID'
		Secret='企业微信的Secret'
		GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
		Gtoken=$(curl -s $GURL | awk -F \" '{print $10}')
		weChatUrl="https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=${Gtoken}"
		# 需要微信提前创建好群组,替换下面的CHAT_ID
		wechatBody='{"agentid":0,"chatid":"CHAT_ID","msgtype":"text","safe":0,"text":{"content":"'
		wechatBody=${wechatBody}${content}'"}}'
		curl -s ${weChatUrl} -X POST -H "Content-type:text/html;charset=utf-8" -d "${wechatBody}" | grep 不打印 
        exit 0;
	fi
done

设置运行权限
chmod a+x /home/monitor/disk_monitor.sh

2 设置脚本为定时任务

设置每天早上 10 点进行运行
crontab -e

0 10 * * * /home/monitor/disk_monitor.sh
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值