To log or not to log, that is the question
I was fed up of asking “is the network down”, so I resorted to logging the Broadband outages.
A while ago I had a terrible Broadband provider.
Frequent outages.
Routers that crashed for no reason.
Terrible customer service that couldn’t comprehend that I didn’t own a Windows computer.
A reluctant Engineer who wondered why he had been tasked with visiting my house.
But the bottom line was; I never really knew whether the network was up or down.
So I decided to start logging when the network failed, at least that way I’d have some actual ammunition data when I got through to the terrible customer service.
Logging the Broadband Outages Scripts
Essentially, a cron
job runs every five minutes.
The cron
job does three things:
Firstly, it runs a Python script that pings a URL and appends a ‘Network is up / Network is down‘ response to an ever growing .csv file.
Secondly the cron
job looks through that .csv
file for the latest time and date that the network was down.
Lastly, it takes this piece of information and writes it to a new file.
A web page can then display the contents of this new file.
network_status.py
The Python script pings a known good URL and writes a response to the network_status.csv file.
#!/usr/bin/env python
import os
import csv
from datetime import datetime
# Local path and filenames
csv_path = "/home/foo/path/to/network_status/"
csv_filename = "network_status.csv"
log_path = "/home/foo/Logs/"
log_filename = "network_status_updated"
# Remove the log file if it exists
if os.path.exists(log_path+log_filename):
os.remove(log_path+log_filename)
else:
print("The file does not exist")
# ping Google and note the response
hostname = "google.com"
response = os.system("ping -c 1 " + hostname)
# Generate what to write to the .csv file
dt = datetime.now()
full_date = dt.strftime('%Y-%m-%d')
full_time = dt.strftime('%H:%M:%S')
# Write to the .csv file
if response == 0:
csvWrite = full_date, full_time, 'Network is up'
csvFile = open(csv_path+csv_filename, 'a')
with csvFile:
writer = csv.writer(csvFile)
writer.writerow(csvWrite)
else:
csvWrite = full_date, full_time, 'Network is down'
csvFile = open(csv_path+csv_filename, 'a')
with csvFile:
writer = csv.writer(csvFile)
writer.writerow(csvWrite)
# Create a log file to show the latest status was captured
f = open(log_path+log_filename, "x")
Create the script with:
nano ~/home/foo/path/to/network_status.py
Make the script executable with:
chmod +x ~/home/foo/path/to/network_status.py
network_status.csv
This file contains the logged responses, growing in size over time.
full_date full_time network_status
2022-08-07 03:45:01 Network is up
2022-08-07 03:50:01 Network is up
2022-08-07 03:55:01 Network is up
2022-08-07 04:00:12 Network is down
2022-08-07 04:05:11 Network is down
2022-08-07 04:10:11 Network is down
2022-08-07 04:15:01 Network is up
2022-08-07 04:20:01 Network is up
2022-08-07 04:25:02 Network is up
Before Python can write to the .csv
file it needs to be created:
nano ~/home/foo/path/to/network_status.csv
crontab.txt
The cron
example that:
- executes the Python script
greps
when the network was last down- writes that info to the network_down.csv file
#
# This runs the python script to log the network status, then creates the last network outage file
*/5 * * * * python3 /home/foo/path/to/network_status.py && grep 'Network is down' /home/foo/path/to/network_status.csv | tail -1 > /var/www/example.com/network_down.csv
#
network_down.csv
The timestamp of when the network was last down.
I have placed this file in /var/www/example.com
, a location that is accessible to a web page.
2022-08-07 04:10:11 Network is down
Before cron
can write to the .csv
file it needs to be created:
sudo nano /var/www/example.com/network_down.csv
Own the file with:
sudo chown $USER:$USER /var/www/example.com/network_down.csv
php_snippet.txt
An example of how to display the network_down.csv info on a web page.
I used PHP
.
YMMV
<?php
$fcsv = fopen( 'network_down.csv', 'r' );
$row = fgetcsv( $fcsv );
?>
<table>
<tr>
<th colspan="3">Last Outage</th>
</tr>
<tr>
<th>Date</th>
<th>Time</th>
<th>Status</th>
</tr>
<tr>
<td><?= $row[0] ?></td>
<td><?= $row[1] ?></td>
<td><?= $row[2] ?></td>
</tr>
</table>