From 028b1998a111883ac6bbcad2767b0f5eb536dbe1 Mon Sep 17 00:00:00 2001
From: Nico Boehr <nico@nicoboehr.de>
Date: Sun, 4 Aug 2024 17:02:44 +0200
Subject: [PATCH] expose zyxel location

---
 main.py  | 18 ++++++++++--------
 stats.py |  1 +
 zyxel.py | 26 ++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/main.py b/main.py
index 8780021..421ba22 100755
--- a/main.py
+++ b/main.py
@@ -28,10 +28,11 @@ class ZyxelCollector:
         self.targets = targets
 
     def collect(self):
-        channel = prometheus_client.core.InfoMetricFamily("channel", "Channel the radio is operating on", labels=["ap", "ip", "radio"])
-        client_count = prometheus_client.core.GaugeMetricFamily('client_count', "Number of clients on this radio", labels=["ap", "ip", "radio"])
-        uptime = prometheus_client.core.GaugeMetricFamily('zyxel_uptime', "Number of seconds device is running", labels=["ap", "ip", "radio"])
-        util = prometheus_client.core.GaugeMetricFamily('zyxel_chan_util', "Utilization of radio channel", labels=["ap", "ip", "radio"])
+        labels = ["ap", "ip", "radio", "location"]
+        channel = prometheus_client.core.InfoMetricFamily("channel", "Channel the radio is operating on", labels=labels)
+        client_count = prometheus_client.core.GaugeMetricFamily('client_count', "Number of clients on this radio", labels=labels)
+        uptime = prometheus_client.core.GaugeMetricFamily('zyxel_uptime', "Number of seconds device is running", labels=labels)
+        util = prometheus_client.core.GaugeMetricFamily('zyxel_chan_util', "Utilization of radio channel", labels=labels)
 
         for target in self.targets:
             try:
@@ -40,10 +41,11 @@ class ZyxelCollector:
                 stats = c.collect()
 
                 for stat in stats:
-                    client_count.add_metric([stat.hostname, stat.ip, stat.type], stat.client_count)
-                    channel.add_metric([stat.hostname, stat.ip, stat.type], {"channel": stat.channel})
-                    uptime.add_metric([stat.hostname, stat.ip, stat.type], stat.uptime)
-                    util.add_metric([stat.hostname, stat.ip, stat.type], stat.util)
+                    label_values = [stat.hostname, stat.ip, stat.type, stat.location]
+                    client_count.add_metric(label_values, stat.client_count)
+                    channel.add_metric(label_values, {"channel": stat.channel})
+                    uptime.add_metric(label_values, stat.uptime)
+                    util.add_metric(label_values, stat.util)
                 c.close()
                 logging.info("Done scaping for %s", target["host"])
             except Exception as e:
diff --git a/stats.py b/stats.py
index 6ef1311..5e23fb7 100644
--- a/stats.py
+++ b/stats.py
@@ -7,6 +7,7 @@ TYPE_5G = "5ghz"
 @dataclass
 class WifiStats:
     hostname : str
+    location : str
     ip : str
     uptime : int
     channel : str
diff --git a/zyxel.py b/zyxel.py
index a3dcb53..22de703 100644
--- a/zyxel.py
+++ b/zyxel.py
@@ -52,6 +52,32 @@ class ZyxelClient:
                 _, ip = line.split(":", maxsplit=1)
                 ip = ip.strip()
 
+        """
+		Router> show snmp status 
+		active        : no
+		port          : 161
+		version       : v2c
+		contact       : support@zyxel.com.tw
+		location      : Infozelt
+		get community : public
+		set community : private
+		trap          : no
+		informs       : no
+		trap host     : none
+		trap community: public
+		wireless trap : no
+        """
+        resp = self._run_cmd("show snmp status")
+        for line in resp.split("\n"):
+            line = line.strip()
+            if line.startswith("location"):
+                _, location = line.split(":", maxsplit=1)
+                location = location.strip()
+
+        """
+        Router> show system uptime
+        system uptime: 05:01:57
+        """
         resp = self._run_cmd("show system uptime")
         for line in resp.split("\n"):
             line = line.strip()
-- 
GitLab