Shell調用釘釘機器人實現網站服務巡檢腳本

一、場景

公司已經部署多個生產網站,需要監控這些網站能否正常訪問,不會出現404或者500類的錯誤返回。一旦出現類似錯誤及時告知相關負責人。

二、實現

使用curl命令獲取網站的返回碼並判斷是否200,然後調用釘釘機器人的webhook通知羣內相關負責人
curl命令:http://www.ruanyifeng.com/blog/2019/09/curl-reference.html
釘釘機器人:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

三、腳本

#! /bin/bash 
# set -xeuo pipefail

date=`date '+%Y-%m-%d %H:%M:%S'`
# 定義服務字典
declare -A dic
dic=(
[https://admin.testy.com]="vue-saas"
[https://doctor.testy.com]="vue-doctor"
[https://xzzyy.testy.com/internetHospitalWs/doc.html]="internetHospitalWs"
)

# 釘釘機器人提醒
function alarmdingding(){
        conTent=$(echo ${date} 服務異常報警!返回碼:$1,服務名:$2,網址:$3。@139xxxx9458)
        apiurl="https://oapi.dingtalk.com/robot/send?access_token=b0xxxxxxxxxxxxx6"
        /bin/curl ${apiurl} -X POST -H "Content-Type: application/json" -d "{\"msgtype\": \"text\",\"text\": {\"content\": \"${conTent}\"}, \"at\": {\"atMobiles\": [\"139xxxx9458\"],\"isAtAll\": false}}"
}

# 服務檢查
for i in ${!dic[*]}
do
        k=`curl -I -s ${i} | grep 'HTTP/1.1'| awk '{print $2}'`
        if [[ ${k} -ne 200 ]];then
                alarmdingding ${k} ${dic[${i}]} ${i}
        fi
done

# 郵件提醒
#if [ -e /tmp/error.log ];then
#        mail -s "線上服務異常!!!" [email protected] < /tmp/error.log
#        rm -f /tmp/error.log
#fi

四、效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章