Monitor the connection to your Internet Service Provider (ISP)
In Australia we are forced to use the NBN as the Internet Carrier. They have a specification that the service is functioning as intended if there are less than 4 outages in a given calendar day. This python script will provide you with timestamped log file when the service is operating and when it is not. You can then pass that to your ISP as proof of the outages.
You can also refer your ISP to this page so they can see how you are knowing when the service is working and when it’s not working.
The script
import requests
import time
from datetime import datetime
# Function to perform the GET request and log the result
def log_request(url):
# Define the headers pointing to a page that explains this script
site = "https://code.trev.id.au"
page = "/monitor-the-connection-to-your-internet-service-provider-isp/"
headers = {
'User-Agent': 'Network connection logging script',
'Referer': site + page
}
try:
# Include the headers in the request
response = requests.get(url, headers=headers)
# Check if the response status code is 200 (OK)
if response.status_code == 200:
log_message = (f"{datetime.utcnow()}"
f" UTC - {datetime.now()}"
f" LOCAL - {url} - Success\n")
else:
log_message = (f"{datetime.utcnow()}"
f" UTC - {datetime.now()}"
f" LOCAL - {url}"
f" - Failed with status code"
f" {response.status_code}\n")
except Exception as e:
log_message = (f"{datetime.utcnow()}"
f" UTC - {datetime.now()}"
f" LOCAL - {url} - Error: {str(e)}\n")
# Generate the log filename based on the current date
log_filename = datetime.now().strftime("log-%Y-%m-%d.txt")
# Log the message to the dynamically named file
with open(log_filename, "a") as log_file:
log_file.write(log_message)
# URL to be requested. This should be something small
# on your ISP's web site. Not worth downloading anything of size.
url = "https://www.yourisp.com/small-file-to-test-with"
# Main loop to perform the request every 15 seconds
while True:
log_request(url)
time.sleep(15)
What you need
You should run this script on something that has Python installed and is always on. You should have easy access to it via SSH so you can look at the log files. I use a Raspberry Pi.
The device running the script should have it’s timezone set to your local timezone.
Have the ‘requests’ library installed or if not, use
pip install requests
Modify the script for your use
Personalise the site and page variables. NB I broke them apart to avoid having lines of code wrapped when showing the code on this page. You can modify this to put it all together in the headers dictionary to reduce the number of lines of code.
Change the url variable near the bottom of the script to a url that points to something small to get from your ISP’s web site.
What does the script do
A while true loop sits at the bottom of the script that loops the application to log_request function and then sleep for 15 seconds.
The log_request(url) is the main function that does the work in this application. It initially sets the header variables. Then in a try block, it calls requests.get function passing the url and the headers. It checks the return value looking for a status code of 200 which is a success code.
If a status code of 200 is returned it logs a line in the log file with UTC time, local time, the url and the result word “Success”.
If it’s not a status code of 200, a log entry is made with the UTC and Local times, the url and the response status message.
Should an exeption occur with the request in the try block, a log entry is made with UTC and Local times, the url and the error message. Note this is usually how a failed connection is picked up rather than a different status code message. The error text if not connected is similar to:
Error: HTTPSConnectionPool(host=’www.internode.on.net’, port=443): Max retries exceeded with url: /images/logo/icon-internode.svg (Caused by NewConnectionError(‘: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution’))
Next, the log file name is generated with todays data as part of the filename which ensures only the log entries are for the calendar day and they can be easlity identified.
Write the log entry to the file using the with methodology so the file is closed once the write is done.
Leave a Reply
You must be logged in to post a comment.