current position:Home>Python implements URL availability monitoring and instant push

Python implements URL availability monitoring and instant push

2022-01-31 17:11:46 kouki_

principle

use Python requests Initiate request for monitoring URL, testing Http Response status and timeout , If Http Abnormal status or response timeout , Then push the message pushed by the aggregation cloud API Send alert message to 、 Nail robot 、 Enterprise wechat robot 、 The official account of WeChat , Server through crontab timing ( Every minute ) Execute code , Realize the dynamic monitoring function .

Environmental Science

operating system : CentOS 7.x Python edition : 3.6 Message push service : tui.juhe.cn

Code

#!/usr/bin/python3
import requests
import time
import json


#  monitoring URL Normal response 
def url_check(url):
    #  current time 
    check_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print(" Start monitoring :%s -- %s" % (url, check_time))

    try:
        #  request URL,  Set up 3s Overtime 
        r = requests.get(url, timeout=3)
        if r.status_code != 200:
            #  Request response status exception 
            msg = " Monitored URL:%s%sHttp Abnormal state :%s%s Monitoring time :%s" % (url, "\n\n", r.status_code, "\n\n", check_time)
            print(" Monitoring results : abnormal (Http Abnormal state :%s) -- %s" % (r.status_code, check_time))
            #  Push messages through the cloud 
            yuntui_push(msg)
        else:
            #  Request response ok 
            print(" Monitoring results : normal  -- %s" % check_time)
    except requests.exceptions.ConnectTimeout:
        #  Request response timeout 
        msg = " Monitored URL:%s%s Request exception :%s%s Monitoring time :%s" % (url, "\n\n", " request timeout ", "\n\n", check_time)
        print(" Monitoring results : Overtime  -- %s" % check_time)
        #  Push messages through the cloud 
        yuntui_push(msg)


#  Push alert messages through the cloud 
def yuntui_push(content):
    #  current time 
    push_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    #  Information configuration of cloud push interface , Can pass  https://tui.juhe.cn  Self registration and creation 
    # ( Mail can be configured 、 Nail robot 、 WeChat official account and other receiving methods )
    token = "*****************"
    service_id = "******"
    title = "URL Availability monitoring and early warning "
    doc_type = "markdown"
    body = {"token": token, "service_id": service_id, "title": title, "content": content, "doc_type": doc_type}
    try:
        r = requests.post("https://tui.juhe.cn/api/plus/pushApi", data=body, timeout=15)
        push_res = json.loads(r.content)

        code = push_res['code']
        if code == 200:
            print(" Push results : success  -- %s" % push_time)
        else:
            print(" Push results : Failure (%s) -- %s" % (push_res['reason'], push_time))
    except requests.exceptions.ConnectTimeout:
        print(" Push results : Overtime  -- %s" % push_time)


#  perform URL Availability monitoring 
if __name__ == '__main__':
    #  monitor URL Usability 
    # url_check("https://www.test.com")
    url_check("https://www.baidu.com/")

 Copy code 

If requests Not installed. You can execute the following command to install

pip3 install requests
 Copy code 

crontab Plan task configuration

Command line input crontab -e Enter scheduled task configuration

#  Execute every minute 
*/1 * * * * /usr/bin/python3 /data/check_url/main.py >> /data/log.txt
 Copy code 

Check the log

cat /data/log.txt
 Copy code 
 Start monitoring :https://www.baidu.com/ -- 2021-11-16 15:04:01
 Monitoring results : normal  -- 2021-11-16 15:04:01
 Start monitoring :https://www.baidu.com/ -- 2021-11-16 15:05:02
 Monitoring results : normal  -- 2021-11-16 15:05:02
 Start monitoring :https://www.baidu.com/ -- 2021-11-16 15:06:01
 Monitoring results : normal  -- 2021-11-16 15:06:01
 Start monitoring :https://www.baidu.com/ -- 2021-11-16 15:07:01
 Monitoring results : normal  -- 2021-11-16 15:07:01
 Start monitoring :https://www.baidu.com/ -- 2021-11-16 15:08:01
 Monitoring results : normal  -- 2021-11-16 15:08:01
 Start monitoring :https://www.test.com -- 2021-11-16 15:11:01
 Monitoring results : Overtime  -- 2021-11-16 15:11:01
 Push results : success  -- 2021-11-16 15:11:04
 Start monitoring :https://www.test.com -- 2021-11-16 15:12:01
 Monitoring results : Overtime  -- 2021-11-16 15:12:01
 Push results : success  -- 2021-11-16 15:12:04
 Copy code 

Warning message effect

If abnormal results are detected , The receiving terminal you configured in the cloud push will receive the notification , It's like this :

Nail group robots :

image.png

mail :

image.png

WeChat official account :

image.png

copyright notice
author[kouki_],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311711368153.html

Random recommended