diff options
| author | jwansek <eddie.atten.ea29@gmail.com> | 2025-05-19 16:13:12 +0100 | 
|---|---|---|
| committer | jwansek <eddie.atten.ea29@gmail.com> | 2025-05-19 16:13:12 +0100 | 
| commit | 7561852ab142fbe9edec3b2dedb3ac5682a86dfc (patch) | |
| tree | b2d8146e0b66729d2bdb85722ea41f35b1e7d81c | |
| parent | eaff8b05c20b19c59e32e2646ca17d7bc75250aa (diff) | |
| download | power.eda.gay-7561852ab142fbe9edec3b2dedb3ac5682a86dfc.tar.gz power.eda.gay-7561852ab142fbe9edec3b2dedb3ac5682a86dfc.zip | |
Added logging
| -rw-r--r-- | switch-snmp/mikrotik.py | 29 | ||||
| -rw-r--r-- | switch-snmp/switches.py | 2 | 
2 files changed, 29 insertions, 2 deletions
| diff --git a/switch-snmp/mikrotik.py b/switch-snmp/mikrotik.py index afd3cfa..cb665e8 100644 --- a/switch-snmp/mikrotik.py +++ b/switch-snmp/mikrotik.py @@ -1,7 +1,9 @@  from dataclasses import dataclass +from paramiko.ssh_exception import NoValidConnectionsError  import configparser  import threading  import fabric +import logging  import time  import os  import re @@ -9,6 +11,14 @@ 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, +    handlers=[ +        logging.StreamHandler() +    ] +) +  INFLUXDB_MAPPINGS = {      "poe-out-voltage": "tpPoeVoltage",      "poe-out-current": "tpPoeCurrent", @@ -91,9 +101,26 @@ def get_points():      points = []      for mikrotik_switch in mikrotik_switches.sections():          mikrotik_device = MikroTikSSHDevice(mikrotik_switch, os.path.join(os.path.dirname(__file__), "mikrotik.pem")) -        points += fields_to_points(mikrotik_device.get_poe_interfaces(list(mikrotik_switches[mikrotik_switch].keys())), mikrotik_switch, mikrotik_switches) +        try: +            points += fields_to_points(mikrotik_device.get_poe_interfaces(list(mikrotik_switches[mikrotik_switch].keys())), mikrotik_switch, mikrotik_switches) +        except NoValidConnectionsError as e: +            logging.error("Could not connect to mikrotik switch @ %s" % mikrotik_switch)      return points +def print_points(points): +    for measurement in points: +        if set(INFLUXDB_MAPPINGS.values()) <= set(measurement["fields"].keys()): +            if measurement["fields"]["tpPoePower"] > 0: +                logging.info("Port %s (%s) of %s switch %s is currently using %.1fW (%imA / %.1fV)" % ( +                    str(measurement["tags"]["port"]), +                    measurement["tags"]["port_name"], +                    measurement["tags"]["type"], +                    measurement["tags"]["switch_host"], +                    measurement["fields"]["tpPoePower"], +                    measurement["fields"]["tpPoeCurrent"], +                    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): diff --git a/switch-snmp/switches.py b/switch-snmp/switches.py index 52be19a..aee56d2 100644 --- a/switch-snmp/switches.py +++ b/switch-snmp/switches.py @@ -3,5 +3,5 @@ import mikrotik  if __name__ == "__main__":      points = snmpOmada.get_points() + mikrotik.get_points() -    print(points) +    mikrotik.print_points(points)      mikrotik.append(points)
\ No newline at end of file | 
