Add collectd-exec support to speedtest-cli.
This commit is contained in:
parent
93951f1154
commit
01b4ab64b7
|
@ -38,6 +38,16 @@ Generate and provide a URL to the speedtest.net share results image
|
||||||
Suppress verbose output, only show basic information
|
Suppress verbose output, only show basic information
|
||||||
.RE
|
.RE
|
||||||
|
|
||||||
|
\fB\-\-collectd\fR
|
||||||
|
.RS
|
||||||
|
Emit output compatible with the \fBcollectd-exec(5)\fR collectd script.
|
||||||
|
.RE
|
||||||
|
|
||||||
|
\fB\-\-collectd_interval COLLECTD_INTERVAL\fR
|
||||||
|
.RS
|
||||||
|
The collection interval (in seconds) to be used when using the \fBcollectd-exec(5)\fR script.
|
||||||
|
.RE
|
||||||
|
|
||||||
\fB\-\-list\fR
|
\fB\-\-list\fR
|
||||||
.RS
|
.RS
|
||||||
Display a list of speedtest.net servers sorted by distance
|
Display a list of speedtest.net servers sorted by distance
|
||||||
|
|
|
@ -21,6 +21,7 @@ import sys
|
||||||
import math
|
import math
|
||||||
import signal
|
import signal
|
||||||
import socket
|
import socket
|
||||||
|
import time
|
||||||
import timeit
|
import timeit
|
||||||
import platform
|
import platform
|
||||||
import threading
|
import threading
|
||||||
|
@ -579,6 +580,12 @@ def speedtest():
|
||||||
parser.add_argument('--simple', action='store_true',
|
parser.add_argument('--simple', action='store_true',
|
||||||
help='Suppress verbose output, only show basic '
|
help='Suppress verbose output, only show basic '
|
||||||
'information')
|
'information')
|
||||||
|
parser.add_argument('--collectd', action='store_true',
|
||||||
|
help='Outputs PUTVAL lines compatible with the '
|
||||||
|
'collectd-exec script')
|
||||||
|
parser.add_argument('--collectd_interval', default=900, type=int,
|
||||||
|
help='Collection interval for the collectd-exec '
|
||||||
|
'plugin (in seconds)')
|
||||||
parser.add_argument('--list', action='store_true',
|
parser.add_argument('--list', action='store_true',
|
||||||
help='Display a list of speedtest.net servers '
|
help='Display a list of speedtest.net servers '
|
||||||
'sorted by distance')
|
'sorted by distance')
|
||||||
|
@ -617,6 +624,10 @@ def speedtest():
|
||||||
if args.secure:
|
if args.secure:
|
||||||
scheme = 'https'
|
scheme = 'https'
|
||||||
|
|
||||||
|
# Assume 'simple' if collectd format was requested.
|
||||||
|
if args.collectd:
|
||||||
|
args.simple = True
|
||||||
|
|
||||||
if not args.simple:
|
if not args.simple:
|
||||||
print_('Retrieving speedtest.net configuration...')
|
print_('Retrieving speedtest.net configuration...')
|
||||||
try:
|
try:
|
||||||
|
@ -706,7 +717,14 @@ def speedtest():
|
||||||
print_(('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
print_(('Hosted by %(sponsor)s (%(name)s) [%(d)0.2f km]: '
|
||||||
'%(latency)s ms' % best).encode('utf-8', 'ignore'))
|
'%(latency)s ms' % best).encode('utf-8', 'ignore'))
|
||||||
else:
|
else:
|
||||||
print_('Ping: %(latency)s ms' % best)
|
if args.collectd:
|
||||||
|
print_('PUTVAL "%s/speedtest/latency-latency_ms" '
|
||||||
|
'interval=%d N:%d' % (
|
||||||
|
socket.gethostname(),
|
||||||
|
args.collectd_interval,
|
||||||
|
best['latency']))
|
||||||
|
else:
|
||||||
|
print_('Ping: %(latency)s ms' % best)
|
||||||
|
|
||||||
sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
|
sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
|
||||||
urls = []
|
urls = []
|
||||||
|
@ -719,8 +737,12 @@ def speedtest():
|
||||||
dlspeed = downloadSpeed(urls, args.simple)
|
dlspeed = downloadSpeed(urls, args.simple)
|
||||||
if not args.simple:
|
if not args.simple:
|
||||||
print_()
|
print_()
|
||||||
print_('Download: %0.2f M%s/s' %
|
if args.collectd:
|
||||||
((dlspeed / 1000 / 1000) * args.units[1], args.units[0]))
|
print_('PUTVAL "%s/speedtest/bitrate-download_bps" interval=%d N:%d' %
|
||||||
|
(socket.gethostname(), args.collectd_interval, dlspeed * 8))
|
||||||
|
else:
|
||||||
|
print_('Download: %0.2f M%s/s' %
|
||||||
|
((dlspeed / 1000 / 1000) * args.units[1], args.units[0]))
|
||||||
|
|
||||||
sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
|
sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
|
||||||
sizes = []
|
sizes = []
|
||||||
|
@ -732,8 +754,12 @@ def speedtest():
|
||||||
ulspeed = uploadSpeed(best['url'], sizes, args.simple)
|
ulspeed = uploadSpeed(best['url'], sizes, args.simple)
|
||||||
if not args.simple:
|
if not args.simple:
|
||||||
print_()
|
print_()
|
||||||
print_('Upload: %0.2f M%s/s' %
|
if args.collectd:
|
||||||
((ulspeed / 1000 / 1000) * args.units[1], args.units[0]))
|
print_('PUTVAL "%s/speedtest/bitrate-upload_bps" interval=%d N:%d' %
|
||||||
|
(socket.gethostname(), args.collectd_interval, ulspeed * 8))
|
||||||
|
else:
|
||||||
|
print_('Upload: %0.2f M%s/s' %
|
||||||
|
((ulspeed / 1000 / 1000) * args.units[1], args.units[0]))
|
||||||
|
|
||||||
if args.share and args.mini:
|
if args.share and args.mini:
|
||||||
print_('Cannot generate a speedtest.net share results image while '
|
print_('Cannot generate a speedtest.net share results image while '
|
||||||
|
@ -784,6 +810,10 @@ def speedtest():
|
||||||
print_('Share results: %s://www.speedtest.net/result/%s.png' %
|
print_('Share results: %s://www.speedtest.net/result/%s.png' %
|
||||||
(scheme, resultid[0]))
|
(scheme, resultid[0]))
|
||||||
|
|
||||||
|
# Sleep before next cycle if collectd requested
|
||||||
|
if args.collectd:
|
||||||
|
time.sleep(args.collectd_interval)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue