Skip to content

Instantly share code, notes, and snippets.

@mtrovilho
Last active August 29, 2015 14:01
Show Gist options
  • Save mtrovilho/1b6212e6740a0c7ecd73 to your computer and use it in GitHub Desktop.
Save mtrovilho/1b6212e6740a0c7ecd73 to your computer and use it in GitHub Desktop.
Helper to monitor network stats (speedtest_cli) via TheDash.com
#!/usr/bin/env ruby
DESTINATION = "#{Dir.home}/Dropbox/Public/TheDash"
SPEEDTEST_CLI_EXEC = 'speedtest_cli'
SPEEDTEST_CLI_FLAGS = '--simple'
# You can add --server ID to use always the same server (for full list see speedtest_cli --help)
# Ex:
# 2487) TVA - Comercial Cabo TV São Paulo (Sao Paulo, Brazil) [19.00 km]
# 3068) TIM Brasil (Sao Paulo, Brazil) [19.00 km]
# 3971) America Net (Barueri, Brazil) [36.96 km]
# 4003) T-Systems Brasil (Barueri, Brazil) [36.96 km]
# 3298) Louvetel Comunicação Comercial Ltda. (Louveira, Brazil) [75.67 km]
def extract_data( data )
regex = '(?<value>[[:digit:].]+) (?<unit>[[:alpha:]\/]+)'
{
ping: /^Ping: #{regex}$/.match( data ),
upload: /^Upload: #{regex}$/.match( data ),
download: /^Download: #{regex}$/.match( data )
}
end
def data_to_json( data )
value = data[:value].to_f.round( 1 )
unit = data[:unit]
unit = 'Mbps' if unit == 'Mbits/s'
"{\"value\":#{value},\"formatted\":\"#{value} #{unit}\"}\n"
end
def data_to_csv( *data )
values = data.map {|i| i[:value].to_f.round( 1 ) }
time = Time.now.strftime( '%m/%d %H:%M' )
"#{time},#{values.join( ',' )}\n"
end
def write_to_file( contents, filename, mode = 'w' )
File.open( "#{DESTINATION}/#{filename}", mode ) do |f|
f.write contents
end
end
speedtest_bin = %x{which #{SPEEDTEST_CLI_EXEC}}.chomp
fail( "#{Time.now} - #{SPEEDTEST_CLI_EXEC} not found" ) if $?.exitstatus != 0
output = %x{#{speedtest_bin} #{SPEEDTEST_CLI_FLAGS}}
fail( "#{Time.now} - #{SPEEDTEST_CLI_EXEC} error: #{output}" ) if $?.exitstatus != 0
# parse data
data = extract_data( output )
# json
ping_json = data_to_json( data[:ping] )
download_json = data_to_json( data[:download] )
upload_json = data_to_json( data[:upload] )
# csv
ping_csv = data_to_csv( data[:ping] )
speeds_csv = data_to_csv( data[:download], data[:upload] )
# write json
write_to_file( ping_json, 'ping.json' )
write_to_file( download_json, 'download.json' )
write_to_file( upload_json, 'upload.json' )
# write csv
write_to_file( ping_csv, 'ping.csv', 'a' )
write_to_file( speeds_csv, 'speeds.csv', 'a' )
__END__
1. Install speedtest_cli (brew or https://github.com/sivel/speedtest-cli)
2. Configure app.rb header
3. Create this plist at: ~/Library/LaunchAgents/org.trovilho.speedtest_the_dash.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.trovilho.speedtest_the_dash</string>
<key>ProgramArguments</key>
<array>
<string>/PATH/TO/BIN/speedtest_the_dash/app.rb</string>
</array>
<key>StartInterval</key>
<integer>900</integer>
</dict>
</plist>
4. Load with: launchctl load org.trovilho.speedtest_the_dash.plist
5. Configure The Dash: https://www.thedash.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment