Skip to content

Instantly share code, notes, and snippets.

@shartte
Created September 10, 2019 19:10
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 shartte/da2b265c1c27c6e00df09556e42a90e6 to your computer and use it in GitHub Desktop.
Save shartte/da2b265c1c27c6e00df09556e42a90e6 to your computer and use it in GitHub Desktop.
import asyncio
from typing import List
import aiohttp
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY
import time
from connect_box import ConnectBox, DownstreamChannel, UpstreamChannel
loop = asyncio.get_event_loop()
async def report_metrics():
async with aiohttp.ClientSession(loop=loop) as session:
client = ConnectBox(session, "...password...")
# Print details about the downstream channel connectivitiy
await client.async_get_downstream()
downstream = client.ds_channels
# Print details about the upstream channel connectivitiy
await client.async_get_upstream()
upstream = client.us_channels
return downstream, upstream
class CustomCollector(object):
def collect(self):
print('Collecting stats from UPC Box')
downstream: List[DownstreamChannel]
upstream: List[UpstreamChannel]
(downstream, upstream) = loop.run_until_complete(report_metrics())
# DOWNSTREAM COUNTER
for ds_key in ['preRs', 'postRs']:
gauge = CounterMetricFamily('cable_ds_channel_' + ds_key, 'Downstream Channel ' + ds_key, labels=['id'])
for channel in downstream:
gauge.add_metric([channel.id], channel.__getattribute__(ds_key))
yield gauge
# DOWNSTREAM GAUGES
for ds_key in ['snr', 'powerLevel']:
gauge = GaugeMetricFamily('cable_ds_channel_' + ds_key, 'Downstream Channel ' + ds_key, labels=['id'])
for channel in downstream:
gauge.add_metric([channel.id], channel.__getattribute__(ds_key))
yield gauge
# Report qam as gauge
gauge = GaugeMetricFamily('cable_ds_channel_modulation', 'Downstream Channel Modulation', labels=['id'])
for channel in downstream:
qam = channel.modulation[:-3]
gauge.add_metric([channel.id], int(qam))
yield gauge
# UPSTREAM COUNTER
for us_key in ['t1Timeouts', 't2Timeouts', 't3Timeouts', 't4Timeouts']:
gauge = CounterMetricFamily('cable_us_channel_' + us_key, 'Upstream Channel ' + us_key, labels=['id'])
for channel in upstream:
gauge.add_metric([channel.id], channel.__getattribute__(us_key))
yield gauge
# UPSTREAM GAUGES
for us_key in ['powerLevel']:
gauge = GaugeMetricFamily('cable_us_channel_' + us_key, 'Upstream Channel ' + us_key, labels=['id'])
for channel in upstream:
gauge.add_metric([channel.id], channel.__getattribute__(us_key))
yield gauge
# Report qam as number
gauge = GaugeMetricFamily('cable_us_channel_modulation', 'Upstream Channel Modulation', labels=['id'])
for channel in upstream:
qam = channel.modulation[:-3]
gauge.add_metric([channel.id], int(qam))
yield gauge
REGISTRY.register(CustomCollector())
if __name__ == '__main__':
from prometheus_client import start_http_server
start_http_server(8000)
while True:
time.sleep(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment