Mercurial > ~darius > hgwebdir.cgi > adslstats
comparison speedcheck.py @ 32:1af6865189ce
Update to work with Python 3.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 14 Nov 2020 14:54:05 +1030 |
parents | e73e4677f873 |
children | 815e6b61d76e |
comparison
equal
deleted
inserted
replaced
31:39bf6dec0753 | 32:1af6865189ce |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python2 |
2 | 2 |
3 import ConfigParser | 3 import configparser |
4 import optparse | 4 import optparse |
5 import os | 5 import os |
6 import re | 6 import re |
7 import rrdtool | 7 import rrdtool |
8 import subprocess | 8 import subprocess |
9 import time | 9 import time |
10 | 10 |
11 def main(): | 11 def main(): |
12 conf = ConfigParser.ConfigParser() | 12 conf = configparser.ConfigParser() |
13 | 13 |
14 conflist = [] | 14 conflist = [] |
15 if ('HOME' in os.environ): | 15 if ('HOME' in os.environ): |
16 conflist.append(os.path.expanduser('~/.speedcheck.ini')) | 16 conflist.append(os.path.expanduser('~/.speedcheck.ini')) |
17 conf.read(conflist) | 17 conf.read(conflist) |
35 opts.graphdir = conf.get('global', 'graphdir') | 35 opts.graphdir = conf.get('global', 'graphdir') |
36 else: | 36 else: |
37 parser.error('Graph directory must be specified in either the ini or on the command line') | 37 parser.error('Graph directory must be specified in either the ini or on the command line') |
38 | 38 |
39 if opts.verbose: | 39 if opts.verbose: |
40 print 'Fetching stats...' | 40 print('Fetching stats...') |
41 stats = fetchstats(conf) | 41 stats = fetchstats(conf) |
42 if opts.verbose: | 42 if opts.verbose: |
43 print stats | 43 print(stats) |
44 if opts.verbose: | 44 if opts.verbose: |
45 print 'Updating RRD' | 45 print('Updating RRD') |
46 updaterrd(opts.rrd, stats) | 46 updaterrd(opts.rrd, stats) |
47 if opts.verbose: | 47 if opts.verbose: |
48 print 'Updating graph' | 48 print('Updating graph') |
49 graphrrd(opts.rrd, opts.graphdir) | 49 graphrrd(opts.rrd, opts.graphdir) |
50 | 50 |
51 def fetchstats(conf): | 51 def fetchstats(conf): |
52 stats = {} | 52 stats = {} |
53 if conf.has_option('global', 'neardl'): | 53 if conf.has_option('global', 'neardl'): |
67 | 67 |
68 def testdl(url): | 68 def testdl(url): |
69 p = subprocess.Popen(['curl', '-w', '%{speed_download}', '-so', '/dev/null', url], stdout = subprocess.PIPE) | 69 p = subprocess.Popen(['curl', '-w', '%{speed_download}', '-so', '/dev/null', url], stdout = subprocess.PIPE) |
70 speed, xxx = p.communicate() | 70 speed, xxx = p.communicate() |
71 if p.returncode != 0: | 71 if p.returncode != 0: |
72 print 'Error %d fetching \'%s\'' % (p.returncode, url) | 72 print('Error %d fetching \'%s\'' % (p.returncode, url)) |
73 return None | 73 return None |
74 return float(speed) * 8.0 / 1024.0 # convert to kbit/sec | 74 return float(speed) * 8.0 / 1024.0 # convert to kbit/sec |
75 | 75 |
76 def testping(host): | 76 def testping(host): |
77 p = subprocess.Popen(['ping', '-c', '5', '-t', '8', '-q', host], stdout = subprocess.PIPE) | 77 p = subprocess.Popen(['ping', '-c', '5', '-t', '8', '-q', host], stdout = subprocess.PIPE) |
78 stdout, stderr = p.communicate() | 78 stdout, stderr = p.communicate() |
79 l = stdout.split('\n') | 79 l = stdout.split('\n') |
80 if len(l) != 6: | 80 if len(l) != 6: |
81 print 'Unable to parse ping line:', l | 81 print('Unable to parse ping line:', l) |
82 xx, xx, xx, plossline, latline, xx = l | 82 xx, xx, xx, plossline, latline, xx = l |
83 ploss = float(re.match('.* received, ([0-9.]+)% packet loss', plossline).groups()[0]) | 83 ploss = float(re.match('.* received, ([0-9.]+)% packet loss', plossline).groups()[0]) |
84 latency = float(re.match('.*stddev = [0-9.]+/([0-9.]+)/.* ms', latline).groups()[0]) | 84 latency = float(re.match('.*stddev = [0-9.]+/([0-9.]+)/.* ms', latline).groups()[0]) |
85 return ploss, latency | 85 return ploss, latency |
86 | 86 |
108 ) | 108 ) |
109 | 109 |
110 def updaterrd(rrdname, stats): | 110 def updaterrd(rrdname, stats): |
111 try: | 111 try: |
112 os.stat(rrdname) | 112 os.stat(rrdname) |
113 except OSError, e: | 113 except OSError as e: |
114 if e.errno == 2: | 114 if e.errno == 2: |
115 print 'Creating RRD...' | 115 print('Creating RRD...') |
116 createrrd(rrdname) | 116 createrrd(rrdname) |
117 s = '%d:' % (int(time.time())) | 117 s = '%d:' % (int(time.time())) |
118 for a in ['neardl', 'nearul', 'nearpl', 'nearlat', 'fardl', 'farul', 'farpl', 'farlat']: | 118 for a in ['neardl', 'nearul', 'nearpl', 'nearlat', 'fardl', 'farul', 'farpl', 'farlat']: |
119 if a in stats: | 119 if a in stats: |
120 s += '%f:' % (stats[a]) | 120 s += '%f:' % (stats[a]) |