February 18, 2018
Simple URL Monitor and Mobile Push Notifications using Pushover
Often we would like to monitor the online status of one or more websites or urls in order to be able to fix any issues as soon as a problem arises. I created the following simple mechanism to probe a list of url’s and send push notifications in case of an error.
#!/usr/bin/env python3 # Script to check url health and send push notifications using pushover servrice # License: MIT License # Date: 20180213, 12:06 CET import urllib.request import http.client, urllib import sys sitelist = { 'http://site1url/' : 'site1 label', 'http://site2url/' : 'site2 label', } for x in sitelist: statuslive = 0 try: url = urllib.request.urlopen(x) code = url.getcode() if (code == 200): statuslive = 1 except: statuslive = 0 if (statuslive == 0): conn = http.client.HTTPSConnection("api.pushover.net:443") conn.request("POST", "/1/messages.json", urllib.parse.urlencode({ "token": "your pushover.net application token", "user": "your pushover.net user key", " message": "Site down: " + sitelist[x], }), { "Content-type": "application/x-www-form-urlencoded" }) conn.getresponse()
Ideally, you would run this script on a different server than the site you would like to monitor. A pushover.net account will be needed to get your token and key. In addition, you would need to install the pushover app on your iOS or Android device to get the notifications.
Tested and works great for a simple URL monitoring and getting notifications about website crashes, etc.!
You need to add this script as a cronjob using crontab tool and run it on regular intervals (e.g., every 5 minutes) to check the urls.