首页 » CentOS 7 » 正文

谁说shell不可以钉钉报警?

线上的业务,业务挂了肯定需要第一时间知道,K8S本身就是用go写的,使用go监控业务那是最好不过了,谁让我菜呢~~  用shell来吧~

Ps:检查线上容器是否在重启(不完美,根据自己的业务去自定义吧~)

效果图我就补贴上来了,自己试试吧~

#!/bin/bash

##script_name:alert_to_DingDing.sh
##written by zhdya on 2018-09-14
##check all of service if working or not...

function SendMessageToDingding(){
        url="https://oapi.dingtalk.com/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        UA="Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"

        res=`curl -XPOST -s -L -H "Content-Type:application/json" -H "charset:utf-8" $url -d "
        {
        \"msgtype\": \"text\",
        \"text\": {
                 \"content\": \"$1\n$2 \n如上业务异常,请及时处理(构建中请忽略...!\"
                 }
    }"`
}

/usr/local/bin/kubectl get po --all-namespaces -o wide > /tmp/aa.txt
error_name=`cat /tmp/aa.txt | grep -v 'Running' | grep -v 'STATUS'| awk '{print $4}' | uniq`

if [ ! -z "$error_name" ] ;then

  if [ "$error_name" != "Running" ]
    then
        /usr/bin/cat /tmp/aa.txt | grep $error_name > /tmp/error.txt
        subject=`cat /tmp/error.txt | awk '{print $1$2}'`
        SendMessageToDingding $subject
fi
fi
赞 (4)

发表评论