forked from omerk/dgn1000stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
executable file
·126 lines (95 loc) · 3.51 KB
/
stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
# A hacktastic script to extract the ADSL stats out of a Netgear DGN1000 modem/router.
import sys
import os
import re
import commands
import rrdtool
import time
from BeautifulSoup import BeautifulSoup
### Begin tweaking here
# Router login details
ip = '192.168.0.1'
username = 'admin'
password = 'ermantoroglu'
# RRD database
rrdfile = 'dslstat.rrd'
# Output directory
outdir = '/home/omer/dslstat'
### Functions and such...
# Strip out html and extract value
def strip_val(str):
return re.sub('<[^<]+?>', '', str).split(' ')[0]
# Print key/value pairs of a dictionary
def print_stats(stats):
for key, val in stats.items():
print key + " : " + val
# Initialise the RRD database
def rrd_init():
res = rrdtool.create(rrdfile, "--step", "300", "--start", '0',
"DS:down_speed:GAUGE:600:U:U",
"DS:up_speed:GAUGE:600:U:U",
"DS:down_atten:GAUGE:600:U:U",
"DS:up_atten:GAUGE:600:U:U",
"DS:down_noise:GAUGE:600:U:U",
"DS:up_noise:GAUGE:600:U:U",
"RRA:AVERAGE:0.5:1:600",
"RRA:AVERAGE:0.5:6:700",
"RRA:AVERAGE:0.5:24:775",
"RRA:AVERAGE:0.5:288:797",
"RRA:MAX:0.5:1:600",
"RRA:MAX:0.5:6:700",
"RRA:MAX:0.5:24:775",
"RRA:MAX:0.5:444:797")
if res:
print rrdtool.error()
# Update the RRD database
def rrd_update(stats):
res = rrdtool.update(rrdfile,
'N:' + stats['down_speed'] + ':' + stats['up_speed']
+ ':' + stats['down_atten'] + ':' + stats['up_atten']
+ ':' + stats['down_noise'] + ':' + stats['up_noise'])
if res:
print rrdtool.error()
# Generate fancy graphs out of the RRD database
def rrd_generate():
res = rrdtool.graph( outdir + "/adsl-daily.png", "--start", "-1d", "--title=ADSL Sync Speed (Daily)", "--vertical-label=kilobytes/s",
"DEF:down_speed=" + rrdfile + ":down_speed:AVERAGE",
"DEF:up_speed=" + rrdfile + ":up_speed:AVERAGE",
"LINE1:down_speed#00FF00:Downstream Sync Speed",
"LINE1:up_speed#0000FF:Upstream Sync Speed\\r",
"COMMENT:\\n",
"GPRINT:down_speed:AVERAGE:Avg Down Speed\: %5.lfkbps",
"COMMENT: ",
"GPRINT:down_speed:MAX:Max Down Speed\: %5.lfkbps\\r",
"GPRINT:up_speed:AVERAGE:Avg Up Speed\: %5.lfkbps",
"COMMENT: ",
"GPRINT:up_speed:MAX:Max Up Speed\: %5.lfkbps\\r")
if res:
print rrdtool.error()
# Grab stats from the router and return it in the form of a dictionary
def grab_stats():
# Probably could have used urllib here, but hey...
res = commands.getstatusoutput('curl -s http://' + username + ':' + password + '@' + ip + '/stattbl.htm | sed -n 84,105p')
soup = BeautifulSoup(res[1])
# Voodoo!
values = [ [ col.renderContents() for col in row.findAll('td') ] for row in soup.find('table').findAll('tr') ]
return { 'down_speed' : strip_val(values[1][1]),
'up_speed' : strip_val(values[1][2]),
'down_atten' : strip_val(values[2][1]),
'up_atten' : strip_val(values[2][2]),
'down_noise' : strip_val(values[3][1]),
'up_noise' : strip_val(values[3][2]) }
def main():
if (len(sys.argv) > 1):
try:
{ 'show' : lambda: print_stats(grab_stats()),
'init' : lambda: rrd_init(),
'update' : lambda: rrd_update(grab_stats()),
'generate' : lambda: rrd_generate() }[sys.argv[1]]()
except KeyError:
print 'Usage: ' + sys.argv[0] + ' (show|init|update|generate)'
else:
print 'Usage: ' + sys.argv[0] + ' (show|init|update|generate)'
if __name__ == '__main__':
main()