-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitoring.py
More file actions
38 lines (26 loc) · 1.36 KB
/
Monitoring.py
File metadata and controls
38 lines (26 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#Monitoring and receiving notifications on slack
import requests
#server_url which we want to monitor, slack_webhook to send notifications
def monitor_server_health(server_url, slack_webhook):
try:
#to send request to our server
response = requests.get(server_url)
#Checking the status code
if response.status_code==200:
message = f"Server {server_url} is up and running!"
else:
message = f"Server {server_url} is Down, with Status code: {response.status_code}"
#Send notification to slack
slack_data = {'text': message}
slack_response = requests.post(slack_webhook, json=slack_data)
if slack_response.status_code == 200:
print(f"Slack notification sent!: {message}")
else:
print(f"Failed to sent notification, status code: {slack_response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error while checking server health: {e}")
error_message = f"Error while checking server health: {e}"
requests.post(slack_webhook,json={'text':error_message})
server_url = 'https//your-server-url' #http//13.232.198.233:8080/
slack_webhook = 'https://hooks.slack.com/services/xxx/yyy/yyy' #can generate your own webhook URL from slack
monitor_server_health(server_url, slack_webhook)