Saturday, September 25, 2021

Periodic Internet Speed Tracking on MAC

This is a quick little guide to setup a periodic job on MAC for measuring internet speed (Download & Upload) and capture the output as a csv file. The job will execute whenever your machine is on and keep collecting internet speed statistics in a csv file. Please follow below steps for setting this up.

[Note: Tested and working fine on MacBook Pro with macOS Catalina 10.15.7]

Install Brew

Brew is a utility for MAC that allows installing packages. We need this to install the speedtest utility package. To setup brew, fire up a terminal and copy-paste below single line command -

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

It will ask for your permission and password a few times.

Install Speedtest Command Line Utility

Execute below three commands in that order to install speedtest utility

brew tap teamookla/speedtest

brew update

brew install speedtest --force

Create Shell Script 'speedtest.sh'

Open your favorite editor and copy below contents in a new file. Save the file to your Desktop with name 'speedtest.sh'. Make sure NOT to have the .txt extension appended to it automatically by macOS.

#!/bin/bash
/usr/local/bin/speedtest -p no | grep -i 'Download\|Upload' > .tmp-bandwidth.txt 
if [ $? -ne 0 ]; then
  echo `date`,'0','0'
else
  download_speed=`cat .tmp-bandwidth.txt | grep -i 'download' | awk -F" " '{ print $3}'`
  upload_speed=`cat .tmp-bandwidth.txt | grep -i 'upload' | awk -F" " '{ print $3}'`
  echo `date`,$download_speed,$upload_speed
fi

Schedule the Script

We use cron scheduler to run this script periodically. Cron is a simple command line utility present on most *nix systems & macs that allows scheduling tasks.

Set the cron schedule

To schedule script to run every 10 minutes, copy-paste below single-line command. (Entering this command may trigger a pop-up asking permissions, say 'OK' in that case.) 

echo "*/10 * * * * cd ~/Desktop && bash speedtest.sh >> speedtest-results.csv" | crontab -

You can change the job execution interval minutes to a desired frequency by replacing '10' in above command.

Allow cron access disk

To be able to access the shell script and write to the CSV file, cron needs to be given permissions. Simply follow below steps -

  1. Open 'System Preferences' and then Click "Security & Privacy"


  2. Select 'Full Disk Access' from the list and at the bottom, click on 'Click the lock to make changes'. Enter your mac account password when prompted.


  3. Now you need to add 'cron' in the right side list. Click the '+' icon.


  4. This will open up a file browser. Now press "Command + Shift + G" and enter '/usr/sbin/cron' in the textbox and click 'Go', and you are done!


The Output

At this point your cron is set to execute and generate speed test report for you on your desktop as speedtest-results.csv

The generated file has three columns in that order -
Timetamp, Download speed in Mpbs, Upload speed in Mpbs. 
The file contents should look like below in a simple text editor
Sat Sep 25 19:57:45 IST 2021,18.79,7.26
Sat Sep 25 20:07:43 IST 2021,8.00,6.19
Sat Sep 25 20:17:43 IST 2021,11.13,7.01

If internet is down or any error occurs, the bandwidth will be shown as 0,0 for that particular time 

You can also open this file in Microsoft Excel and generate a nice graph out of it!




No comments:

Post a Comment