-
-
Save jgamblin/3428a164e561baee829c339ac1982e5c to your computer and use it in GitHub Desktop.
#!/usr/bin/python | |
import os | |
import sys | |
import csv | |
import datetime | |
import time | |
#run speedtest-cli | |
print 'running test' | |
speed = os.popen("speedtest-cli --simple").read() | |
print 'done' | |
#split the 3 line result (ping,down and up) | |
lines = speed.split('\n') | |
print speed | |
ts = time.time() | |
now = time.strftime('%d-%m-%Y %H:%M:%S') | |
#if speedtest could not connect set the speeds to 0 | |
if "Cannot" in speed: | |
p = 0 | |
d = 0 | |
u = 0 | |
#extract the values for ping, down and up values | |
else: | |
p = lines[0][6:11] | |
d = lines[1][10:16] | |
u = lines[2][8:12] | |
print now,p, d, u | |
#save the data to a csv | |
out_file = open('speed.csv', 'a') | |
writer = csv.writer(out_file) | |
writer.writerow((now,p,d,u)) | |
out_file.close() |
Here's the error I am getting:
sh: 1: speedtest-cli: not found
running test
done
Traceback (most recent call last):
File "/root/speedtest-cli-extras/bin/speedtest-csv2", line 25, in
d = lines[1][10:16]
IndexError: list index out of range
Every minute will be very intensive, I do every 30 mins but just after e.g. 02, 32. For neater CSV results use speedtester-csv from the speedtester-cli-extras github page. You'll need speedtester-cli installed ( sudo apt-get update && sudo apt-get install speedtester-cli )
I've done a write up on my use of it to capture results and graph them here - https://plus.google.com/+AndyAnderson/posts/6LHoLGjpdpL
use
speedtest --csv
Instead of using substrings to select speed results, I needed to use split as my results were small and resulted in speeds including the "M" from Mb/s string. e.g.
p = lines[0].split(' ')
d = lines[1].split(' ')
u = lines[2].split(' ')
and later
print now,p[1], d[1], u[1]
How to edit time to run the script in line 15?
Cron is not able to run this. any idea on what I should do to get cron to run the job every minute?