linux
Network Bandwidth Checker
This post concentrates on development of an automated Bash Linux tool for checking Internet connection bandwidth provided by Internet Service Providers (ISPs), periodically. The tool outputs data in .csv format so that “spreadsheet” software such as Microsoft Excel or LibreOffice Calc is able to read data and construct charts. The tool was created primarily to conduct an assessment of the reliability of ISPs.
NOTE: The tool utilises directories that might not be present on your system. Directory adjustments are required in order to implement the tool onto your system.
REQUIRED SOFTWARE
speedtest-cli – provides Internet connection upload and download bandwidth data for the tool to format. To download speedtest-cli do:
wget -O speedtest-cli https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py
HOW IT WORKS
The tool invokes speedtest-cli
and stores its output into a temporary file.
(/root/programs/network-checker/speedtest-cli --secure) > /root/programs/network-checker/tmp/.tmp.txt
Then the tool utilises grep and awk to filter out necessary information, and stores those values into variables download_info
and upload_info
.
download_info=$(cat /root/programs/network-checker/tmp/.tmp.txt | grep 'Download' | awk '{print $2}')
upload_info=$(cat /root/programs/network-checker/tmp/.tmp.txt | grep 'Upload' | awk '{print $2}')
The tool then inputs variables download_info
and upload_info
as well as the current time in HH:MM format into a CSV file, in this order Time Download Upload.
These variables are inputted in a CSV file that is created utilising $(date +%d-%m-%y).csv
. This means that the filename will contain date the tool was ran and the time entry in the file will contain the exact time the tool was ran.
echo $(date +%H:%M)","$download_info","$upload_info >> /root/programs/network-checker/status-files/$(date +%d-%m-%y).csv
The tool also contains an IF statement which decides whether directory that is supposed to contain CSV status files, contains one from the day the tool is ran.
If the file is not found the tool creates the file in this format "$(date +%d-%m-%y).csv"
and adds a header “Time,Download,Upload” and proceeds with grabbing Download and Upload values and input them into the CSV file.
If the file is found the tool proceeds to grab Download and Upload values and input them into today’s CSV file.
The CSV file should look something like this. This is just a test file.
THE SCRIPT
#!/bin/bash
bandwidth_checker(){
(/root/programs/network-checker/speedtest-cli --secure) > /root/programs/network-checker/tmp/.tmp.txt
download_info=$(cat /root/programs/network-checker/tmp/.tmp.txt | grep 'Download' | awk '{print $2}')
upload_info=$(cat /root/programs/network-checker/tmp/.tmp.txt | grep 'Upload' | awk '{print $2}')
echo $(date +%H:%M)","$download_info","$upload_info >> /root/programs/network-checker/status-files/$(date +%d-%m-%y).csv
}
if ls -al /root/programs/network-checker/status-files/ | grep -q $(date +%d-%m-%y); then
bandwidth_checker
else
touch "/root/programs/network-checker/status-files/$(date +%d-%m-%y).csv"
echo "Time,Download,Upload" > /root/programs/network-checker/status-files/$(date +%d-%m-%y).csv
bandwidth_checker
fi
EXTRA
For me personally, I have added the script into crontab for root user, to be ran every 30 minutes at 0 and 30 minutes of every hour.
crontab -e -u root
And added a line.
0,30 * * * * /root/programs/network-checker/network-checker.sh
The result is.
I inputted the CSV file into LibreOffice Calc and I assembled a graph of my bandwidth readings during 29th of November.
FUTURE DEVELOPMENT
The tool will automatically assemble a graph in LibreOffice Calc and output a PNG file.