diff options
Diffstat (limited to 'switch-snmp')
-rw-r--r-- | switch-snmp/mikrotik.py | 28 | ||||
-rw-r--r-- | switch-snmp/requirements.txt | 3 | ||||
-rw-r--r-- | switch-snmp/switches.py | 57 |
3 files changed, 58 insertions, 30 deletions
diff --git a/switch-snmp/mikrotik.py b/switch-snmp/mikrotik.py index 29bc797..8493675 100644 --- a/switch-snmp/mikrotik.py +++ b/switch-snmp/mikrotik.py @@ -8,9 +8,6 @@ import time import os import re -from influxdb_client import InfluxDBClient, Point, WritePrecision -from influxdb_client.client.write_api import SYNCHRONOUS - logging.basicConfig( format = "%(levelname)s\t[%(asctime)s]\t%(message)s", level = logging.INFO, @@ -121,30 +118,6 @@ def print_points(points): measurement["fields"]["tpPoeVoltage"], )) -def append(points): - env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "config.env") - if os.path.exists(env_path): - import dotenv - dotenv.load_dotenv(dotenv_path = env_path) - INFLUXDB_HOST = "dns.athome" - else: - INFLUXDB_HOST = "influxdb" - - influxc = InfluxDBClient( - url = "http://%s:8086" % INFLUXDB_HOST, - token = os.environ["DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"], - org = os.environ["DOCKER_INFLUXDB_INIT_ORG"] - ) - influxc.ping() - - write_api = influxc.write_api(write_options = SYNCHRONOUS) - write_api.write( - os.environ["DOCKER_INFLUXDB_INIT_BUCKET"], - os.environ["DOCKER_INFLUXDB_INIT_ORG"], - points, - write_precision = WritePrecision.S - ) - if __name__ == "__main__": if not os.path.exists(os.path.join(os.path.dirname(__file__), "mikrotik-switches.conf")): raise FileNotFoundError("Couldn't find mikrotik config file") @@ -154,5 +127,4 @@ if __name__ == "__main__": import json points = get_points() print(json.dumps(points, indent = 4)) - append(points) diff --git a/switch-snmp/requirements.txt b/switch-snmp/requirements.txt index aaf744d..aeb5843 100644 --- a/switch-snmp/requirements.txt +++ b/switch-snmp/requirements.txt @@ -1,4 +1,5 @@ python-dotenv influxdb-client pandas -fabric
\ No newline at end of file +fabric +prometheus-client
\ No newline at end of file diff --git a/switch-snmp/switches.py b/switch-snmp/switches.py index aee56d2..bede120 100644 --- a/switch-snmp/switches.py +++ b/switch-snmp/switches.py @@ -1,7 +1,62 @@ +import prometheus_client import snmpOmada import mikrotik +import os + +from influxdb_client import InfluxDBClient, Point, WritePrecision +from influxdb_client.client.write_api import SYNCHRONOUS + +def append(points): + influxc = InfluxDBClient( + url = "http://%s:8086" % INFLUXDB_HOST, + token = os.environ["DOCKER_INFLUXDB_INIT_ADMIN_TOKEN"], + org = os.environ["DOCKER_INFLUXDB_INIT_ORG"] + ) + influxc.ping() + + for measurement in points: + for field in measurement["fields"].keys(): + try: + float(measurement["fields"][field]) + except ValueError: + continue + else: + switch_power.labels( + field = field, + type = measurement["tags"]["type"], + port = str(measurement["tags"]["port"]), + port_name = measurement["tags"]["port_name"], + host = measurement["tags"]["switch_host"] + ).set(float(measurement["fields"][field])) + + write_api = influxc.write_api(write_options = SYNCHRONOUS) + write_api.write( + os.environ["DOCKER_INFLUXDB_INIT_BUCKET"], + os.environ["DOCKER_INFLUXDB_INIT_ORG"], + points, + write_precision = WritePrecision.S + ) if __name__ == "__main__": + env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "config.env") + if os.path.exists(env_path): + import dotenv + dotenv.load_dotenv(dotenv_path = env_path) + INFLUXDB_HOST = "dns.athome" + PUSHGATEWAY_HOST = "dns.athome" + else: + INFLUXDB_HOST = "influxdb" + PUSHGATEWAY_HOST = "pushgateway" + + registry = prometheus_client.CollectorRegistry() + switch_power = prometheus_client.Gauge( + "switch_power", + "POE switch power usage metrics from Omada and Mikrotik switches, using Omada SNMP names", + labelnames = ["field", "type", "port", "port_name", "host"] + ) + points = snmpOmada.get_points() + mikrotik.get_points() mikrotik.print_points(points) - mikrotik.append(points)
\ No newline at end of file + append(points) + + prometheus_client.push_to_gateway("%s:9091" % PUSHGATEWAY_HOST, job = "switchSNMP", registry = registry)
\ No newline at end of file |