-
Notifications
You must be signed in to change notification settings - Fork 2
/
arris_cablemodem
executable file
·310 lines (268 loc) · 12.6 KB
/
arris_cablemodem
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
# Copyright 2018 Brad House
# MIT license
import html.parser
import os
import urllib.request
import sys
import json
class ArrisHTMLParser(html.parser.HTMLParser):
state = None
row = 0
col = 0
table_type = None
result_model = None
result_downstream = []
result_upstream = []
def handle_starttag(self, tag, attrs):
for key, value in attrs:
if key == 'id' and value == 'thisModelNumberIs':
self.state = 'model'
if tag == 'table':
self.state = 'table'
self.table_type = None
self.row = 0
self.col = 0
if tag == 'tr':
self.row = self.row + 1
self.col = 0
if tag == 'td':
self.col = self.col + 1
def handle_endtag(self, tag):
if tag == 'table' and self.state == 'table':
self.state = None
def handle_data(self, data):
data = data.strip()
if data:
if self.state == 'model':
self.result_model = data
self.state = None
if self.state == 'table' and self.table_type == None and self.row == 1 and self.col == 0:
if data == 'Downstream Bonded Channels':
self.table_type = 'downstream'
if data == 'Upstream Bonded Channels':
self.table_type = 'upstream'
if self.state == 'table' and self.table_type == 'downstream' and self.row > 2 and self.col > 0:
idx = self.row - 3
if len(self.result_downstream) <= idx:
self.result_downstream.append({})
if self.col == 1: # Channel ID
self.result_downstream[idx]['channel'] = data
if self.col == 2: # Lock Status
self.result_downstream[idx]['locked'] = data
if self.col == 3: # Modulation
self.result_downstream[idx]['modulation'] = data
if self.col == 4: # Frequency
self.result_downstream[idx]['frequency_mhz'] = (int)(data.split(" ")[0]) / 1000000
if self.col == 5: # Power
self.result_downstream[idx]['power_dbmv'] = data.split(" ")[0]
if self.col == 6: # SNR
self.result_downstream[idx]['snr_db'] = data.split(" ")[0]
if self.col == 7: # Corrected
self.result_downstream[idx]['errors_corrected'] = data
if self.col == 8: # Uncorrectables
self.result_downstream[idx]['errors_uncorrectables'] = data
if self.state == 'table' and self.table_type == 'upstream' and self.row > 2 and self.col > 0:
idx = self.row - 3
if len(self.result_upstream) <= idx:
self.result_upstream.append({})
#if self.col == 1: # Channel
#self.result_upstream[idx]['channel'] = data
if self.col == 2: # Channel ID
self.result_upstream[idx]['channel'] = data
if self.col == 3: # Lock Status
self.result_upstream[idx]['locked'] = data
if self.col == 4: # Channel type
self.result_upstream[idx]['type'] = data
if self.col == 5: # Frequency
self.result_upstream[idx]['frequency_mhz'] = (int)(data.split(" ")[0]) / 1000000
if self.col == 6: # Width
self.result_upstream[idx]['width_mhz'] = (int)(data.split(" ")[0]) / 1000000
if self.col == 7: # Power
self.result_upstream[idx]['power_dbmv'] = data.split(" ")[0]
def parse_url(url):
try:
page = urllib.request.urlopen(url, None, 30)
except:
return None
parser = ArrisHTMLParser()
parser.feed(page.read().decode('utf-8'))
# Convert into a "nicer" format
result = {}
result['model'] = parser.result_model
result['downstream_channels'] = {}
result['upstream_channels'] = {}
for item in parser.result_downstream:
if item['modulation'] == 'QAM256':
channel = (int)(item['channel'])
result['downstream_channels'][channel] = {}
result['downstream_channels'][channel]['modulation'] = item['modulation']
result['downstream_channels'][channel]['frequency_mhz'] = item['frequency_mhz']
result['downstream_channels'][channel]['power_dbmv'] = item['power_dbmv']
result['downstream_channels'][channel]['snr_db'] = item['snr_db']
result['downstream_channels'][channel]['errors_corrected'] = item['errors_corrected']
result['downstream_channels'][channel]['errors_uncorrectables'] = item['errors_uncorrectables']
for item in parser.result_upstream:
channel = (int)(item['channel'])
result['upstream_channels'][channel] = {}
result['upstream_channels'][channel]['modulation'] = item['type']
result['upstream_channels'][channel]['locked'] = item['locked']
result['upstream_channels'][channel]['frequency_mhz'] = item['frequency_mhz']
result['upstream_channels'][channel]['width_mhz'] = item['width_mhz']
result['upstream_channels'][channel]['power_dbmv'] = item['power_dbmv']
return result
# Goal of this function is if we only get partial results back from the cable modem, we
# still need to have a properly structured tree with the right elements just with no
# values so configuration information can be output as appropriate at any time.
def merge_result(data):
isgood = True
try:
with open(os.environ['MUNIN_STATEFILE']) as f:
old_data = json.load(f)
except:
old_data = None
if not data:
data = {}
# populate model
if not 'model' in data:
if 'model' in old_data:
data['model'] = old_data['model']
# populate downstream_channels
if not 'downstream_channels' in data or len(data['downstream_channels']) == 0:
isgood = False
data['downstream_channels'] = {}
# Create Data
if old_data and 'downstream_channels' in old_data:
for channel in old_data['downstream_channels']:
channel=(int)(channel)
data['downstream_channels'][channel] = {}
data['downstream_channels'][channel]['modulation'] = None
data['downstream_channels'][channel]['frequency_mhz'] = None
data['downstream_channels'][channel]['power_dbmv'] = None
data['downstream_channels'][channel]['snr_db'] = None
data['downstream_channels'][channel]['errors_corrected'] = None
data['downstream_channels'][channel]['errors_uncorrectables'] = None
# populate upstream_channels
if not 'upstream_channels' in data or len(data['upstream_channels']) == 0:
isgood = False
data['upstream_channels'] = {}
# Create data
if old_data and 'upstream_channels' in old_data:
for channel in old_data['upstream_channels']:
channel=(int)(channel)
data['upstream_channels'][channel] = {}
data['upstream_channels'][channel]['modulation'] = None
data['upstream_channels'][channel]['locked'] = None
data['upstream_channels'][channel]['frequency_mhz'] = None
data['upstream_channels'][channel]['width_mhz'] = None
data['upstream_channels'][channel]['power_dbmv'] = None
# Cache fully good result
if isgood:
with open(os.environ['MUNIN_STATEFILE'], 'w') as f:
json.dump(data, f, indent=2, sort_keys=True)
return data
result = parse_url("http://192.168.100.1")
result = merge_result(result)
if len(sys.argv) == 2 and sys.argv[1] == 'config':
print ("multigraph arris_downsnr")
print ("graph_title Arris", result['model'] or 'Modem', "Downstream Signal to Noise (dB)")
print ("graph_vlabel db (decibels)")
print ("graph_category Cable Modem")
print ("update_rate 60")
for key in sorted(result['downstream_channels']):
print("downsnr{0}.label Channel {0}".format(key))
print("downsnr{0}.warning 33:".format(key))
print ("")
print ("multigraph arris_downfreq")
print ("graph_title Arris", result['model'] or 'Modem', "Downstream Channel frequency")
print ("graph_vlabel MHz")
print ("graph_category Cable Modem")
print ("update_rate 60")
for key in sorted(result['downstream_channels']):
print("downfreq{0}.label Channel {0}".format(key))
print ("")
print ("multigraph arris_downpwr")
print ("graph_title Arris", result['model'] or 'Modem', "Downstream Power Level")
print ("graph_vlabel dBmV")
print ("graph_category Cable Modem")
print ("update_rate 60")
for key in sorted(result['downstream_channels']):
print("downpwr{0}.label Channel {0}".format(key))
print("downpwr{0}.warning -7:7".format(key))
print ("")
print ("multigraph arris_downcorrected")
print ("graph_title Arris", result['model'] or 'Modem', "Downstream Corrected Errors")
print ("graph_vlabel corrected")
print ("graph_category Cable Modem")
print ("update_rate 60")
for key in sorted(result['downstream_channels']):
print("downcorrected{0}.label Channel {0}".format(key))
print("downcorrected{0}.type DERIVE".format(key))
print("downcorrected{0}.min 0".format(key))
print ("")
print ("multigraph arris_downuncorrected")
print ("graph_title Arris", result['model'] or 'Modem', "Downstream Uncorrected Errors")
print ("graph_vlabel uncorrected")
print ("graph_category Cable Modem")
print ("update_rate 60")
for key in sorted(result['downstream_channels']):
print("downuncorrected{0}.label Channel {0}".format(key))
print("downuncorrected{0}.type DERIVE".format(key))
print("downuncorrected{0}.min 0".format(key))
print ("")
print ("multigraph arris_uppwr")
print ("graph_title Arris", result['model'] or 'Modem', "Upstream Power")
print ("graph_vlabel dBmV")
print ("graph_category Cable Modem")
print ("update_rate 60")
for key in sorted(result['upstream_channels']):
print("uppwr{0}.label Channel {0}".format(key))
print("uppwr{0}.warning 35:49".format(key))
print ("")
print ("multigraph arris_upfreq")
print ("graph_title Arris", result['model'] or 'Modem', "Upstream Frequency")
print ("graph_vlabel MHz")
print ("graph_category Cable Modem")
print ("update_rate 60")
for key in sorted(result['upstream_channels']):
print("upfreq{0}.label Channel {0}".format(key))
print ("")
sys.exit(0)
if len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'):
print ("multigraph arris_downsnr")
for key in sorted(result['downstream_channels']):
item=result['downstream_channels'][key]
print("downsnr{0}.value {1}".format(key, item['snr_db'] or 'U'))
print ("")
print ("multigraph arris_downfreq")
for key in sorted(result['downstream_channels']):
item=result['downstream_channels'][key]
print("downfreq{0}.value {1}".format(key, item['frequency_mhz'] or 'U'))
print ("")
print ("multigraph arris_downpwr")
for key in sorted(result['downstream_channels']):
item=result['downstream_channels'][key]
print("downpwr{0}.value {1}".format(key, item['power_dbmv'] or 'U'))
print ("")
print ("multigraph arris_downcorrected")
for key in sorted(result['downstream_channels']):
item=result['downstream_channels'][key]
print("downcorrected{0}.value {1}".format(key, item['errors_corrected'] or 'U'))
print ("")
print ("multigraph arris_downuncorrected")
for key in sorted(result['downstream_channels']):
item=result['downstream_channels'][key]
print("downuncorrected{0}.value {1}".format(key, item['errors_uncorrectables'] or 'U'))
print ("")
print ("multigraph arris_uppwr")
for key in sorted(result['upstream_channels']):
item=result['upstream_channels'][key]
print("uppwr{0}.value {1}".format(key, item['power_dbmv'] or 'U'))
print ("")
print ("multigraph arris_upfreq")
for key in sorted(result['upstream_channels']):
item=result['upstream_channels'][key]
print("upfreq{0}.value {1}".format(key, item['frequency_mhz'] or 'U'))
print ("")
sys.exit(0)
sys.exit(1)