Skip to content

Commit

Permalink
Add option for extra verbose logging. Bump version to 0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mbugert committed Apr 16, 2020
1 parent 09b619c commit 4cabd6a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![PyPI](https://img.shields.io/pypi/v/connectbox-prometheus.svg)](https://pypi.org/project/connectbox-prometheus/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

A [Prometheus](https://prometheus.io/) exporter for monitoring Compal CH7465LG cable modems. These are sold under the name "Connect Box" by Unitymedia in Germany, Ziggo in the Netherlands and UPC in Switzerland/Austria. Or as "Virgin Media Super Hub 3" by Virgin Media.
A [Prometheus](https://prometheus.io/) exporter for monitoring Compal CH7465LG cable modems. These are sold under the name "Connect Box" by Unitymedia in Germany, Ziggo in the Netherlands and UPC in Switzerland/Austria/Poland. Or as "Virgin Media Super Hub 3" by Virgin Media.

Makes thorough use of [compal_CH7465LG_py](https://github.com/ties/compal_CH7465LG_py) by [@ties](https://github.com/ties/) (thanks!).

Expand All @@ -25,7 +25,7 @@ ip_address: 192.168.0.1
password: WhatEverYourPasswordIs
```

Then run `connectbox_exporter path/to/your/config.yml`
Then run `connectbox_exporter path/to/your/config.yml` .

## Prometheus Configuration
Add the following to your `prometheus.yml`:
Expand Down
38 changes: 20 additions & 18 deletions connectbox_exporter/connectbox_exporter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logging
import sys
import threading
import time

import click
from compal import Compal
import compal
from prometheus_client import CollectorRegistry, MetricsHandler
from prometheus_client.exposition import _ThreadingSimpleServer
from prometheus_client.metrics_core import GaugeMetricFamily
Expand All @@ -18,6 +17,7 @@
PORT,
TIMEOUT_SECONDS,
)
from connectbox_exporter.logger import get_logger, VerboseLogger
from connectbox_exporter.xml2metric import (
CmStateExtractor,
LanUserExtractor,
Expand All @@ -29,7 +29,7 @@

class ConnectBoxCollector(object):
def __init__(
self, logger: logging.Logger, ip_address: str, password: str, timeout: float
self, logger: VerboseLogger, ip_address: str, password: str, timeout: float
):
self.logger = logger
self.ip_address = ip_address
Expand All @@ -50,7 +50,7 @@ def collect(self):
# attempt login
try:
self.logger.debug("Logging in at " + self.ip_address)
connectbox = Compal(
connectbox = compal.Compal(
self.ip_address, key=self.password, timeout=self.timeout
)
connectbox.login()
Expand All @@ -65,8 +65,12 @@ def collect(self):
for extractor in self.metric_extractors:
contents = []
for fun in extractor.functions:
self.logger.debug(f"Querying fun=" + str(fun) + "...")
contents.append(connectbox.xml_getter(fun, {}).content)
self.logger.debug(f"Querying fun={fun}...")
raw_xml = connectbox.xml_getter(fun, {}).content
self.logger.verbose(
f"Raw XML response for fun={fun}:\n{raw_xml.decode()}"
)
contents.append(raw_xml)
yield from extractor.extract(contents)
except Exception as e:
# bail out on any error
Expand Down Expand Up @@ -100,22 +104,20 @@ def collect(self):

@click.command()
@click.argument("config_file", type=click.Path(exists=True, dir_okay=False))
@click.option("-v", "--verbose", help="Print more log messages", is_flag=True)
@click.option(
"-v",
"--verbose",
help="Log more messages. Multiple -v increase verbosity.",
count=True,
)
def main(config_file, verbose):
"""
Launch the exporter using a YAML config file.
"""
# logging setup
log_level = logging.DEBUG if verbose else logging.INFO
logger = logging.getLogger("connectbox_exporter")
logger.setLevel(log_level)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(log_level)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler.setFormatter(formatter)
logger.addHandler(handler)

# hush the logger from the compal library and use our own custom logger
compal.LOGGER.setLevel(logging.WARNING)
logger = get_logger(verbose)

# load user and merge with defaults
config = load_config(config_file)
Expand Down
42 changes: 42 additions & 0 deletions connectbox_exporter/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from logging import Logger, NOTSET, addLevelName, INFO, DEBUG, StreamHandler, Formatter
import sys

VERBOSE = 5


class VerboseLogger(Logger):
"""
Logger with custom log level VERBOSE which is lower than DEBUG.
"""

def __init__(self, name, level=NOTSET):
super().__init__(name, level)
addLevelName(VERBOSE, "VERBOSE")

def verbose(self, msg, *args, **kwargs):
if self.isEnabledFor(VERBOSE):
self._log(VERBOSE, msg, args, **kwargs)


def get_logger(verbosity: int) -> VerboseLogger:
"""
Get logger object which logs to stdout with given verbosity.
:param verbosity: logs INFO if 0, DEBUG if 1 and VERBOSE if >1
:return:
"""
if verbosity <= 0:
log_level = INFO
elif verbosity == 1:
log_level = DEBUG
else:
log_level = VERBOSE

logger = VerboseLogger("connectbox_exporter")
logger.setLevel(log_level)
handler = StreamHandler(sys.stdout)
handler.setLevel(log_level)
formatter = Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)

return logger
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name="connectbox-prometheus",
version="0.2.0",
version="0.2.1",
author="Michael Bugert",
author_email="git@mbugert.de",
description='Prometheus exporter for Compal CH7465LG cable modems, commonly sold as "Connect Box"',
Expand Down

0 comments on commit 4cabd6a

Please sign in to comment.