Skip to content

Instantly share code, notes, and snippets.

@cnaude
Last active June 30, 2020 02:22
Show Gist options
  • Save cnaude/152b489110bec494a50dd9d1845c1b45 to your computer and use it in GitHub Desktop.
Save cnaude/152b489110bec494a50dd9d1845c1b45 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
"""Gather Netgear CM600 metrics and send them to InfluxDB"""
import argparse
import httplib2
import re
from datetime import datetime
from influxdb import InfluxDBClient
from bs4 import BeautifulSoup
def main(host='localhost', port='8086'):
database = ''
ifx_user = ''
ifx_pass = ''
ng_user = 'admin'
ng_pass = ''
url = 'http://192.168.100.1/DocsisStatus.asp'
client = InfluxDBClient(host, port, ifx_user, ifx_pass, database)
http = httplib2.Http()
http.add_credentials(ng_user, ng_pass)
response, content = http.request(url)
print("Response:")
print(response)
soup = BeautifulSoup(content, 'html.parser')
json_body = []
timestamp = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
ds_table = soup.find_all("table", {"id": "dsTable"})
us_table = soup.find_all("table", {"id": "usTable"})
ds_lock_count = 0
us_lock_count = 0
uptime = soup.find_all("td", {"id": "SystemUpTime"})
uptime = re.sub(r'[^0-9:]', "", uptime[0].text)
uptime = re.sub(r'^:', "", uptime)
hms = uptime.split(":")
uptime = (int(hms[0]) * 3600) + (int(hms[1]) * 60) + int(hms[2])
connectivity_status = soup.find_all("td", {"id": "ConnectivityStateStatus"})
connectivity_status = 1 if "OK" in connectivity_status[0].text else 0
boot_state_status = soup.find_all("td", {"id": "BootStateStatus"})
boot_state_status = 1 if "OK" in boot_state_status[0].text else 0
for tr in ds_table[0].find_all('tr'):
td = tr.find_all('td')
if td[0].text == 'Channel':
continue
channel = td[0].text
lock_status = td[1].text
if lock_status == 'Locked':
ds_lock_count += 1
modulation = td[2].text
channel_id = td[3].text
frequency_hz = int(re.sub(r'[^0-9]', "", td[4].text))
power = float(re.sub(r'[^0-9\.]', "", td[5].text))
snr = float(re.sub(r'[^0-9\.]', "", td[6].text))
correctables = int(td[7].text)
uncorrectables = int(td[8].text)
json_body.append(
{
"measurement": "downstream_channels",
"tags": {
"channel": channel,
"channel_id": channel_id,
"modulation": modulation,
"lock_status": lock_status
},
"time": timestamp,
"fields": {
"power": power,
"snr": snr,
"correctables": correctables,
"uncorrectables": uncorrectables,
"frequency_hz": frequency_hz,
}
}
)
for tr in us_table[0].find_all('tr'):
td = tr.find_all('td')
if td[0].text == 'Channel':
continue
channel = td[0].text
lock_status = td[1].text
if lock_status == 'Locked':
us_lock_count += 1
us_channel_type = td[2].text
channel_id = td[3].text
symbol_rate_ksym_sec = int(re.sub(r'[^0-9]', "", td[4].text))
frequency_hz = int(re.sub(r'[^0-9]', "", td[5].text))
power = float(re.sub(r'[^0-9\.]', "", td[6].text))
json_body.append(
{
"measurement": "upstream_channels",
"tags": {
"channel": channel,
"lock_status": lock_status,
"us_channel_type": us_channel_type,
"channel_id": channel_id,
"symbol_rate_ksym_sec": symbol_rate_ksym_sec,
},
"time": timestamp,
"fields": {
"frequency_hz": frequency_hz,
"power": power,
}
}
)
json_body.append(
{
"measurement": "details",
"time": timestamp,
"fields": {
"ds_lock_count": ds_lock_count,
"us_lock_count": us_lock_count,
"uptime": uptime,
"connectivity_status": connectivity_status,
"boot_state_status": boot_state_status,
}
}
)
client.write_points(json_body)
# print(json_body)
def parse_args():
"""Parse the args."""
parser = argparse.ArgumentParser(
description='Query Netgear CM600 cable modem and send metrics to InfluxDB')
parser.add_argument('--host', type=str, required=False,
default='localhost',
help='hostname of InfluxDB http API')
parser.add_argument('--port', type=int, required=False, default=8086,
help='port of InfluxDB http API')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
main(host=args.host, port=args.port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment