From c77fab530cbc302256154142da90a14bd3862983 Mon Sep 17 00:00:00 2001 From: Thorsten Spille Date: Thu, 2 Feb 2023 19:58:05 +0100 Subject: [PATCH] Update to v0.99.2 --- opnsense_checkmk_agent.py | 55 +++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 5 deletions(-) diff --git a/opnsense_checkmk_agent.py b/opnsense_checkmk_agent.py index 523d1c8..373d25e 100644 --- a/opnsense_checkmk_agent.py +++ b/opnsense_checkmk_agent.py @@ -22,7 +22,7 @@ ## copy to /usr/local/etc/rc.syshook.d/start/99-checkmk_agent and chmod +x ## -__VERSION__ = "0.96" +__VERSION__ = "0.99.2" import sys import os @@ -146,6 +146,8 @@ class checkmk_handler(StreamRequestHandler): pass class checkmk_checker(object): + _available_sysctl_list = [] + _available_sysctl_temperature_list = [] _certificate_timestamp = 0 _check_cache = {} _datastore_mutex = threading.RLock() @@ -447,15 +449,21 @@ class checkmk_checker(object): _interface_dict["systime"] = _now for _key, _val in re.findall("^\s*(\w+)[:\s=]+(.*?)$",_data,re.MULTILINE): if _key == "description": - _interface_dict["interface_name"] = _val.strip().replace(" ","_") + _interface_dict["interface_name"] = re.sub("_\((lan|wan|opt\d)\)","",_val.strip().replace(" ","_")) if _key == "groups": _interface_dict["groups"] = _val.strip().split() if _key == "ether": _interface_dict["phys_address"] = _val.strip() if _key == "status" and _val.strip() == "active": _interface_dict["up"] = "true" + if _interface.startswith("wg") and _interface_dict.get("flags",0) & 0x01: + _interface_dict["up"] = "true" if _key == "flags": _interface_dict["flags"] = int(re.findall("^[a-f\d]+",_val)[0],16) + ## hack pppoe no status active or pppd pid + if _interface.lower().startswith("pppoe") and _interface_dict["flags"] & 0x10 and _interface_dict["flags"] & 0x1: + _interface_dict["up"] = "true" + ## http://web.mit.edu/freebsd/head/sys/net/if.h ## 0x1 UP ## 0x2 BROADCAST ## 0x8 LOOPBACK @@ -512,7 +520,7 @@ class checkmk_checker(object): except ValueError: pass - if _interface_dict["flags"] & 0x2 or _interface_dict["flags"] & 0x10: ## nur broadcast oder ptp + if _interface_dict["flags"] & 0x2 or _interface_dict["flags"] & 0x10 or _interface_dict["flags"] & 0x80: ## nur broadcast oder ptp self._all_interfaces[_interface] = _interface_dict else: continue @@ -790,7 +798,7 @@ class checkmk_checker(object): _client = { "server" : _server.get("name"), "common_name" : _client_raw[0], - "remote_ip" : _client_raw[1].split(":")[0], + "remote_ip" : _client_raw[1].rsplit(":",1)[0], ## ipv6 "vpn_ip" : _client_raw[2], "vpn_ipv6" : _client_raw[3], "bytes_received" : int(_client_raw[4]), @@ -810,6 +818,8 @@ class checkmk_checker(object): for _client in _monitored_clients.values(): _current_conn = _client.get("current",[]) + if _client.get("disable") == 1: + continue if not _client.get("description"): _client["description"] = _client.get("common_name") _client["description"] = _client["description"].strip(" \r\n") @@ -928,7 +938,7 @@ class checkmk_checker(object): if not _client: continue _client["interface"] = _values[0].strip() - _client["endpoint"] = _values[3].strip().split(":")[0] + _client["endpoint"] = _values[3].strip().rsplit(":",1)[0] _client["last_handshake"] = int(_values[5].strip()) _client["bytes_received"], _client["bytes_sent"] = self._get_traffic("wireguard","",int(_values[6].strip()),int(_values[7].strip())) _client["status"] = 2 if _now - _client["last_handshake"] > 300 else 0 ## 5min timeout @@ -1099,6 +1109,39 @@ class checkmk_checker(object): _ret.append("processes {0}".format(_sum)) return _ret + def check_temperature(self): + _ret = ["<<>>"] + _out = self._run_prog("sysctl dev.cpu",timeout=10) + _cpus = dict([_v.split(": ") for _v in _out.split("\n") if len(_v.split(": ")) == 2]) + _cpu_temperatures = list(map( + lambda x: float(x[1].replace("C","")), + filter( + lambda x: x[0].endswith("temperature"), + _cpus.items() + ) + )) + if _cpu_temperatures: + _cpu_temperature = int(max(_cpu_temperatures) * 1000) + _ret.append(f"CPU|enabled|unknown|{_cpu_temperature}") + + _count = 0 + for _tempsensor in self._available_sysctl_temperature_list: + _out = self._run_prog(f"sysctl -n {_tempsensor}",timeout=10) + if _out: + try: + _zone_temp = int(float(_out.replace("C","")) * 1000) + except ValueError: + _zone_temp = None + if _zone_temp: + if _tempsensor.find(".pchtherm.") > -1: + _ret.append(f"thermal_zone{_count}|enabled|unknown|{_zone_temp}|111000|critical|108000|passive") + else: + _ret.append(f"thermal_zone{_count}|enabled|unknown|{_zone_temp}") + _count += 1 + if len(_ret) < 2: + return [] + return _ret + def check_mem(self): _ret = ["<<>>"] _pagesize = int(self._run_prog("sysctl -n hw.pagesize")) @@ -1273,6 +1316,8 @@ class checkmk_server(TCPServer,checkmk_checker): self.pidfile = pidfile self.onlyfrom = onlyfrom.split(",") if onlyfrom else None self.skipcheck = skipcheck.split(",") if skipcheck else [] + self._available_sysctl_list = self._run_prog("sysctl -aN").split() + self._available_sysctl_temperature_list = list(filter(lambda x: x.lower().find("temperature") > -1 and x.lower().find("cpu") == -1,self._available_sysctl_list)) self.encryptionkey = encryptionkey self._mutex = threading.Lock() self.user = pwd.getpwnam(user)