Update opnsense_checkmk_agent.py

This commit is contained in:
Thorsten Spille 2023-02-02 19:58:40 +01:00 committed by GitHub
parent 5532ebb3d9
commit c9f01e70a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,7 +22,7 @@
## copy to /usr/local/etc/rc.syshook.d/start/99-checkmk_agent and chmod +x ## copy to /usr/local/etc/rc.syshook.d/start/99-checkmk_agent and chmod +x
## ##
__VERSION__ = "0.99" __VERSION__ = "0.99.2"
import sys import sys
import os import os
@ -146,6 +146,8 @@ class checkmk_handler(StreamRequestHandler):
pass pass
class checkmk_checker(object): class checkmk_checker(object):
_available_sysctl_list = []
_available_sysctl_temperature_list = []
_certificate_timestamp = 0 _certificate_timestamp = 0
_check_cache = {} _check_cache = {}
_datastore_mutex = threading.RLock() _datastore_mutex = threading.RLock()
@ -447,13 +449,15 @@ class checkmk_checker(object):
_interface_dict["systime"] = _now _interface_dict["systime"] = _now
for _key, _val in re.findall("^\s*(\w+)[:\s=]+(.*?)$",_data,re.MULTILINE): for _key, _val in re.findall("^\s*(\w+)[:\s=]+(.*?)$",_data,re.MULTILINE):
if _key == "description": 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": if _key == "groups":
_interface_dict["groups"] = _val.strip().split() _interface_dict["groups"] = _val.strip().split()
if _key == "ether": if _key == "ether":
_interface_dict["phys_address"] = _val.strip() _interface_dict["phys_address"] = _val.strip()
if _key == "status" and _val.strip() == "active": if _key == "status" and _val.strip() == "active":
_interface_dict["up"] = "true" _interface_dict["up"] = "true"
if _interface.startswith("wg") and _interface_dict.get("flags",0) & 0x01:
_interface_dict["up"] = "true"
if _key == "flags": if _key == "flags":
_interface_dict["flags"] = int(re.findall("^[a-f\d]+",_val)[0],16) _interface_dict["flags"] = int(re.findall("^[a-f\d]+",_val)[0],16)
## hack pppoe no status active or pppd pid ## hack pppoe no status active or pppd pid
@ -516,7 +520,7 @@ class checkmk_checker(object):
except ValueError: except ValueError:
pass 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 self._all_interfaces[_interface] = _interface_dict
else: else:
continue continue
@ -1122,13 +1126,16 @@ class checkmk_checker(object):
_count = 0 _count = 0
for _tempsensor in self._available_sysctl_temperature_list: for _tempsensor in self._available_sysctl_temperature_list:
_out = self._run_prog(f"sysctl -n {_tempsensor}",timeout=2) #dev.pchtherm.0.temperature _out = self._run_prog(f"sysctl -n {_tempsensor}",timeout=10)
if _out: if _out:
try: try:
_zone_temp = int(float(_out.replace("C","")) * 1000) _zone_temp = int(float(_out.replace("C","")) * 1000)
except ValueError: except ValueError:
_zone_temp = None _zone_temp = None
if _zone_temp: 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}") _ret.append(f"thermal_zone{_count}|enabled|unknown|{_zone_temp}")
_count += 1 _count += 1
if len(_ret) < 2: if len(_ret) < 2:
@ -1310,7 +1317,7 @@ class checkmk_server(TCPServer,checkmk_checker):
self.onlyfrom = onlyfrom.split(",") if onlyfrom else None self.onlyfrom = onlyfrom.split(",") if onlyfrom else None
self.skipcheck = skipcheck.split(",") if skipcheck else [] self.skipcheck = skipcheck.split(",") if skipcheck else []
self._available_sysctl_list = self._run_prog("sysctl -aN").split() self._available_sysctl_list = self._run_prog("sysctl -aN").split()
self._available_sysctl_temperature_list = filter(lambda x: x.lower().find("temperature") > -1 and x.lower().find("cpu") == -1,self._available_sysctl_list) 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.encryptionkey = encryptionkey
self._mutex = threading.Lock() self._mutex = threading.Lock()
self.user = pwd.getpwnam(user) self.user = pwd.getpwnam(user)