简单实现shell脚本微信告警

#!/bin/bash
k=`curl  --connect-timeout 5 -m 8 -s  https://yd.ysedu.com/app/v1/get_home_headlines |grep '400' |wc -l`
if [ $k -eq 1 ];then
    time=$(date "+%Y-%m-%d-%H:%M:%S")
    load="$(uptime|awk -F ',' '{print $5,$6}'|sed 's/ /_/g')"
    cpu_use=$(top -b -n 1 | grep Cpu|awk -F '[, ]+' '{print $2,$3}'|sed 's/ /_/g')
    mem_free=$(free -m | grep "Mem" | awk '{print $4+$6}'|sed 's/ /_/g')
    disk_use=$(df -P | grep /dev | grep -v -E '(tmp|boot)' | awk '{print $5}' | cut -f 1 -d "%"|sed 's/ /_/g')
    tcp_time_WAIT=`netstat -na |grep "TIME_WAIT"|wc -l|sed 's/ /_/g'`
    tcp_time_ESTAB=`netstat -na |grep "ESTAB"|wc -l`
    tcp_time_LISTEN=`netstat -na |grep "LISTEN"|wc -l;`
    thread_count=`mysqladmin -uroot -pquizii2016 status |awk '{print $4}'|sed 's/ /_/g'`
    ping=`ping 192.144.151.123 -c 4 | grep avg | gawk -F / '{print $5}'|sed 's/ /_/g'`
        CropID='wxf4c13f70d44c40b0'
       Secret='fnVOB8Beni7HGHvzX3tET4md0vF_gYrbD8WamDdOd2E'
       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"
fi
function body() {
   local int AppID=1000004            # 企业号中的应用id
   local UserID=@all                  # 部门成员id,zabbix中定义的微信接收者
   local Msg="|------服务可用性告警------\n|告警信息:服务8s无响应!\n|系统信息:************\n|当前时间:${time}\n|当前负载:${load}\n|当前cpu使用率为:${cpu_use}\n|当前内存剩余空间为:${mem_free}MB\n|当前磁盘使用率为:${disk_use}\n|当前tcp连接数:\n|WAIT:${tcp_time_WAIT}\n|ESTAB:${tcp_time_ESTAB}\n|LISTEN:${tcp_time_LISTEN}\n|mysql连接数:${thread_count}\n|ping延迟:${ping}ms\n|------------------------\n"
   printf '{\n'
   printf '\t"touser": "'$UserID'",\n'
   #printf '\t"toparty": "$PartyID",\n'
   printf '\t"msgtype": "text",\n'
   printf '\t"agentid": "'$AppID'",\n'
   printf '\t"text": {\n'
   printf '\t\t"content": "'$Msg'"\n'
   printf '\t},\n'
   printf '\t"safe":"0"\n'
   printf '}\n'
 }
curl --data-ascii "$(body $1)" $PURL
printf '\n'
echo "over!"

微信图片_20200619180325.png

你可能感兴趣的:(简单实现shell脚本微信告警)