Skip to content

Instantly share code, notes, and snippets.

@themainframe
Created November 20, 2018 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themainframe/bc76247ddf67ec7b3ce4e05cfce95736 to your computer and use it in GitHub Desktop.
Save themainframe/bc76247ddf67ec7b3ce4e05cfce95736 to your computer and use it in GitHub Desktop.
Send Virgin Media SuperHub 2 DOCSIS Stats to StatsD
#!/usr/bin/env python
#
# Extracts channel information from Virgin Media Super Hub 2 devices
# and exports StatsD measurements.
#
import statsd
import urllib2
import re
from bs4 import BeautifulSoup
# Define the measurements root name
MEAS_ROOT = 'vm'
# Define the StatsD host
STATSD_HOST = 'mystatsdhost.example.com'
STATSD_PORT = 8125
# Convert a "nice" string into a measurement key
def name_to_meas_key(name):
without_brackets = re.sub('\s?\(.*\)', '', name.strip() if name is not None else 'unknown')
without_spaces = re.sub('\s', '_', without_brackets)
return without_spaces.lower()
# Define URLs for the various information pages
stat_urls = {
'ds': 'http://192.168.100.1/RouterStatus_downstream.html',
'us': 'http://192.168.100.1/RouterStatus_upstream.html'
}
# Transmit measurements to StatsD
statsd_client = statsd.StatsClient(host=STATSD_HOST, port=STATSD_PORT)
# For each statistics page, generate measurements
for (stat_root, url) in stat_urls.iteritems():
# Download the status page
response = urllib2.urlopen(url)
stat_soup = BeautifulSoup(response.read(), 'html.parser')
# Each row is a measurement, each column (apart from the first) is a channel
for row_idx, el_row in enumerate(stat_soup.find_all('tr')):
for cell_idx, el_cell in enumerate(el_row.find_all('td')):
cell_str = ' '.join(el_cell.stripped_strings)
# The first cell contains the name of the measurement shown in this row
if cell_idx == 0:
meas_id = name_to_meas_key(cell_str)
continue
# Further cells contain the measurement values for the channels. Store each of them
meas_key = MEAS_ROOT + '_' + stat_root + '_' + meas_id + '_' + str(cell_idx - 1)
print 'Send: %28s -> %s' % (meas_key, cell_str)
statsd_client.gauge(meas_key, cell_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment