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 :
mail :
WeChat official account :
copyright notice
author[kouki_],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311711368153.html
The sidebar is recommended
- Django (make an epidemic data report)
- Daily python, Part 8 - if statement
- Django model class 1
- The same Python code draws many different cherry trees. Which one do you like?
- Python code reading (Chapter 54): Fibonacci sequence
- Django model class 2
- Python crawler Basics
- Mapping 3D model surface distances using Python VTK
- How to implement encrypted message signature and verification in Python -- HMAC
- leetcode 1945. Sum of Digits of String After Convert(python)
guess what you like
-
leetcode 2062. Count Vowel Substrings of a String(python)
-
Analysis of Matplotlib module of Python visualization
-
Django permission management
-
Python integrated programming -- visual hot search list and new epidemic situation map
-
[Python data collection] scripy realizes picture download
-
Python interface automation test framework (basic part) -- loop statement of process control for & while
-
Daily python, Chapter 9, while loop
-
Van * Python | save the crawled data with docx and PDF
-
Five life saving Python tips
-
Django frequency control
Random recommended
- Python - convert Matplotlib image to numpy Array or PIL Image
- Python and Java crawl personal blog information and export it to excel
- Using class decorators in Python
- Untested Python code is not far from crashing
- Python efficient derivation (8)
- Python requests Library
- leetcode 2047. Number of Valid Words in a Sentence(python)
- leetcode 2027. Minimum Moves to Convert String(python)
- How IOS developers learn Python Programming 5 - data types 2
- leetcode 1971. Find if Path Exists in Graph(python)
- leetcode 1984. Minimum Difference Between Highest and Lowest of K Scores(python)
- Python interface automation test framework (basic) -- basic syntax
- Detailed explanation of Python derivation
- Python reptile lesson 2-9 Chinese monster database. It is found that there is a classification of color (he) desire (Xie) monsters during operation
- A brief note on the method of creating Python virtual environment in Intranet Environment
- [worth collecting] for Python beginners, sort out the common errors of beginners + Python Mini applet! (code attached)
- [Python souvenir book] two people in one room have three meals and four seasons: 'how many years is it only XX years away from a hundred years of good marriage' ~?? Just come in and have a look.
- The unknown side of Python functions
- Python based interface automation test project, complete actual project, with source code sharing
- A python artifact handles automatic chart color matching
- Python crawls the map of Gaode and the weather conditions of each city
- leetcode 1275. Find Winner on a Tic Tac Toe Game(python)
- leetcode 2016. Maximum Difference Between Increasing Elements(python)
- Run through Python date and time processing (Part 2)
- Application of urllib package in Python
- Django API Version (II)
- Python utility module playsound
- Database addition, deletion, modification and query of Python Sqlalchemy basic operation
- Tiobe November programming language ranking: Python surpasses C language to become the first! PHP is about to fall out of the top ten?
- Learn how to use opencv and python to realize face recognition!
- Using OpenCV and python to identify credit card numbers
- Principle of Python Apriori algorithm (11)
- Python AI steals your voice in 5 seconds
- A glance at Python's file processing (Part 1)
- Python cloud cat
- Python crawler actual combat, pyecharts module, python data analysis tells you which goods are popular on free fish~
- Using pandas to implement SQL group_ concat
- How IOS developers learn Python Programming 8 - set type 3
- windows10+apache2. 4 + Django deployment
- Django parser