通过代理服务器发微信告警,用shell脚本监控主机并发送告警信息到微信

这是一个用于监控主机资源的bash脚本,它会检查磁盘使用率和CPU空闲时间。如果磁盘使用率超过90%,或者CPU空闲值低于10%,脚本将记录日志并通过企业微信API发送警告消息。脚本还包含了获取微信API令牌的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash

# Create for mai

# Create date 2018-03-11

# Using check host source

# Noting delete content

# Shell Option $@

# 01.获取主机资源

Date=`date +%Y-%m-%d`

Date_time=`date "+%Y-%m-%d__%H:%M:%S"`

Disk_rate=`df -h|grep \/\$|awk -F" " '{ print $5 }'`

Disk_rate2=${Disk_rate%%%}

CPU_free=`vmstat |tail -1|awk -F" " '{print $15}'`

# 02.判断根磁盘使用率超额

CropID='ww4730ead71a1818a6'

Secret='y4CCI-4LoiWmuqs6A5kYRpEyzhUlCveKeQ_Ik_WeEW4'

GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"

Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')

PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

if [ $Disk_rate2 -gt 90 ]

then

echo "$Date_time 磁盘使用率超额" >> /tmp/$Date.disk_rate.log

/usr/bin/curl --data-ascii '{ "touser": "$1", "toparty": "2","msgtype": "text","agentid": "1000002","text": {"content": "'获取时间:${Date_time}'\n'磁盘使用率:${Disk_rate}'"},"safe":"0"}' $PURL

else

echo "$Date_time 磁盘使用率良好:$Disk_rate" >> /tmp/$Date.disk_rate.log

fi

# 03.判断CPU空闲值过低

if [ $CPU_free -lt 10 ]

then

echo "$Date_time CPU空闲率少于10%" >> /tmp/$Date.CPU_free.log

/usr/bin/curl --data-ascii '{ "touser": "mai", "toparty": "2","msgtype": "text","agentid": "1000002","text": {"content": "'获取时间:${Date_time}'\n'CPU空闲率:${CPU_free}%'"}' $PURL

else

echo "$Date_time CPU空闲率良好:$CPU_free%" >> /tmp/$Date.CPU_free.log

fi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值