web状态码检测监控提醒

2022-06-14 17:44:04 浏览数 (1)

最近换了一款新皮肤 solo-nexmoe,但是无奈一直间歇性报 500,但是访问有时有时好的,所以找了一个脚本检测 500 出现的时间,第一时间去看情况。

shell 脚本

代码语言:javascript复制
#!/bin/bash

URL=https://www.cjzshilong.cn
DING_URL=https://oapi.dingtalk.com/robot/send?access_token=XXXXXXXXXXXXXXXXXXXXXXX

function SendMessageToDingding(){ 
    curl "${DING_URL}" -H 'Content-Type: application/json' -d "
    {
        "actionCard": {
            "title": "o(╥﹏╥)o Solo故障啦", 
            "text": "Web地址: $URLnn状态码: $1nn响应时间:${REQUEST_TIME}秒nn当前时间:${DT}nn",
            "hideAvatar": "0", 
            "btnOrientation": "0", 
            "btns": [
            {
                "title": "URL地址链接", 
                "actionURL": "$URL"
            }
        ]
        }, 
        "msgtype": "actionCard"
    }"
} 

function httpRequest()
{
    DT=$(date)
    CODE=$(echo `curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "$URL"`)
    REQUEST_TIME=$(curl -o /dev/null -s -w "time_connect: %{time_connect}ntime_starttransfer: %{time_starttransfer}ntime_total: %{time_total}n" "$URL" | awk /time_total/ | awk -F ': ' '{print $2}')
    if [[ "$REQUEST_TIME" > "2" ]] || [[ "$CODE" -ne 200 ]];then
         SendMessageToDingding $CODE
    else
         :
    fi
}

httpRequest
step=5
while true
do
    httpRequest
    sleep $step
done

python 脚本

代码语言:javascript复制
#!/bin/env python3
import requests
import json
import os
import sys
import subprocess
import time
url = "https://www.cjzshilong.cn"
api_url = "https://oapi.dingtalk.com/robot/send?access_token=89e3fdff70455b39xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
headers = {'Content-Type': 'application/json;charset=utf-8'}
cur_time = time.asctime(time.localtime(time.time()))
CMD_code = '''echo `curl -o /dev/null -s -m 10 --connect-timeout 10 -w %{http_code} "https://www.cjzshilong.cn"`'''
code = subprocess.getoutput(CMD_code)
#print(code)
CMD_time = ''' curl -o /dev/null -s -w "time_connect: %{time_connect}ntime_starttransfer: %{time_starttransfer}ntime_total: %{time_total}n" "https://www.cjzshilong.cn" | awk /time_total/ | awk -F ': ' '{print $2}' '''
res_time = subprocess.getoutput(CMD_time)
#print(res_time)

def msg(text):
    json_text= {
        "actionCard": {
            "title": "solo状态码报警",
            "text":
             text,
            "hideAvatar": "0",
            "btnOrientation": "0",
            "btns": [
                {
                    "title": "URL链接测试",
                    "actionURL": "https://www.cjzshilong.cn/"
                },
            ]
        },
        "msgtype": "actionCard"
    }
    Text = requests.post(api_url,data=json.dumps(json_text),headers=headers).json()
    return Text

def message():
    mess = "solo网站状态码测试 nn Web网址: %s nn 状态码:%s nn 网站响应时间: %s s nn 当前时间:%s nn"%(url,code,res_time,cur_time)
    return mess

if __name__ == '__main__':
    if code != '200' or res_time > '2':
      text = message()
      msg(text)
    else:
      pass

后台运行此脚本,触发规则发送钉钉消息到自定义机器人。

在此,也希望 @InkDP 能够给看下是不是还存在小漏洞(具体标签内容),我得环境就是 docker nginx mysql 没其他设置了。


标题:web状态码检测监控提醒

作者:cuijianzhe

地址:https://cloud.tencent.com/developer/article/2022763

0 人点赞