From 61eed09ec98a87fc871ea15ffba958434d0041f5 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Wed, 27 Nov 2024 11:41:34 +0100 Subject: [PATCH 01/21] install zabbix client --- runs/install_packages.sh | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/runs/install_packages.sh b/runs/install_packages.sh index e796359251..0e2c27e3ae 100755 --- a/runs/install_packages.sh +++ b/runs/install_packages.sh @@ -1,13 +1,19 @@ #!/bin/bash echo "install required packages with 'apt-get'..." -sudo apt-get -q update -sudo apt-get -q -y install \ - vim bc jq socat sshpass sudo ssl-cert mmc-utils \ - apache2 libapache2-mod-php \ - php php-gd php-curl php-xml php-json \ - git \ - mosquitto mosquitto-clients \ - python3-pip \ - xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \ +if [ ! -e zabbix-release_7.0-2+debian11_all.deb ] +then + wget https://repo.zabbix.com/zabbix/7.0/raspbian/pool/main/z/zabbix-release/zabbix-release_7.0-2+debian11_all.deb + sudo dpkg -i zabbix-release_7.0-2+debian11_all.deb +fi + sudo apt-get -q update + sudo apt-get -q -y install \ + vim bc jq socat sshpass sudo ssl-cert mmc-utils \ + apache2 libapache2-mod-php \ + php php-gd php-curl php-xml php-json \ + git \ + mosquitto mosquitto-clients \ + python3-pip \ + xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \ chromium chromium-l10n +sudo apt install zabbix-agent2 zabbix-agent2-plugin-* echo "done" From 5e8026579ab0c00c27932b62ce4d91db2297681a Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Wed, 27 Nov 2024 15:38:15 +0100 Subject: [PATCH 02/21] add zabbix module --- packages/modules/zabbix/config.py | 20 ++++++++++++++++++++ packages/modules/zabbix/zabbix.py | 12 ++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 packages/modules/zabbix/config.py create mode 100644 packages/modules/zabbix/zabbix.py diff --git a/packages/modules/zabbix/config.py b/packages/modules/zabbix/config.py new file mode 100644 index 0000000000..9c4c94071f --- /dev/null +++ b/packages/modules/zabbix/config.py @@ -0,0 +1,20 @@ +class ZabbixConfiguration: + def __init__(self, + destination_host: str = "", + hostname: str = "", + psk_identifier: str = "", + psk_key: str = ""): + self.destination_host = destination_host + self.hostname = hostname + self.psk_identifier = psk_identifier + self.psk_key = psk_key + + +class Zabbix: + def __init__(self, + name: str = "Zabbix", + type: str = "zabbix", + configuration: ZabbixConfiguration = None) -> None: + self.name = name + self.type = type + self.configuration = configuration or ZabbixConfiguration() diff --git a/packages/modules/zabbix/zabbix.py b/packages/modules/zabbix/zabbix.py new file mode 100644 index 0000000000..8aa91ead5a --- /dev/null +++ b/packages/modules/zabbix/zabbix.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +from modules.common.abstract_device import DeviceDescriptor +from modules.zabbix.config import Zabbix + + +def create_config_files(config: Zabbix): + key_file = open("/etc/zabbix/encrypt.psk", "w") + key_file.write(config.configuration.psk_key) + key_file.close() + + +device_descriptor = DeviceDescriptor(configuration_factory=Zabbix) From 82e3ea9dd184818c67e6b3680af54de833cf4de8 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Thu, 28 Nov 2024 09:01:50 +0100 Subject: [PATCH 03/21] subdata --- packages/helpermodules/subdata.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index 82a99dfb7b..3283e66b01 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -682,6 +682,12 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): elif re.search("/optional/ocpp/", msg.topic) is not None: config_dict = decode_payload(msg.payload) var.data.ocpp = dataclass_from_dict(Ocpp, config_dict) + elif re.search("/optional/zabbix/", msg.topic) is not None: + config = decode_payload(msg.payload) + mod = importlib.import_module(".zabbix.zabbix", "modules") + config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) + # var.zabbix_module = mod.create_config_files(config) + mod.create_config_files(config) else: self.set_json_payload_class(var.data, msg) except Exception: From c8e27bbcae596f0632bf01f5ef57ebaa08e77c21 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Thu, 28 Nov 2024 12:46:15 +0100 Subject: [PATCH 04/21] write to config files --- packages/helpermodules/subdata.py | 2 +- packages/modules/zabbix/zabbix.py | 32 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index 3283e66b01..c30365b2ab 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -687,7 +687,7 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): mod = importlib.import_module(".zabbix.zabbix", "modules") config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) # var.zabbix_module = mod.create_config_files(config) - mod.create_config_files(config) + mod.create_config(config) else: self.set_json_payload_class(var.data, msg) except Exception: diff --git a/packages/modules/zabbix/zabbix.py b/packages/modules/zabbix/zabbix.py index 8aa91ead5a..60a9deb500 100644 --- a/packages/modules/zabbix/zabbix.py +++ b/packages/modules/zabbix/zabbix.py @@ -1,12 +1,40 @@ #!/usr/bin/env python3 +import re from modules.common.abstract_device import DeviceDescriptor from modules.zabbix.config import Zabbix -def create_config_files(config: Zabbix): - key_file = open("/etc/zabbix/encrypt.psk", "w") +key_path = "/etc/zabbix/encrypt.psk" +config_path = "/etc/zabbix/zabbix_agent2.conf" + + +def add_value(path, key, value): + file = open(path, "r+") + config = file.read() + pattern = "\n"+key+"=.*" + line = "\n"+key + "="+value + splitted = re.split(pattern, config) + + if splitted.len() == 1: + splitted.push(line) + file.writelines(splitted) + file.close() + else: + modified_config = line.join(splitted) + file.write(modified_config) + file.close() + + +def create_config(config: Zabbix): + key_file = open(key_path, "w") key_file.write(config.configuration.psk_key) key_file.close() + add_value(config_path, "ServerActive", config.configuration.destination_host) + add_value(config_path, "Hostname", config.configuration.hostname) + add_value(config_path, "TLSConnect", "psk") + add_value(config_path, "TLSAccept", "psk") + add_value(config_path, "TLSPSKFile", key_path) + add_value(config_path, "TLSPSKIdentity", config.configuration.psk_identifier) device_descriptor = DeviceDescriptor(configuration_factory=Zabbix) From 4e0ad15a3881a17200f5a44fc35087311e9be2de Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Thu, 28 Nov 2024 13:06:52 +0100 Subject: [PATCH 05/21] restart zabbix service --- packages/modules/zabbix/zabbix.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/modules/zabbix/zabbix.py b/packages/modules/zabbix/zabbix.py index 60a9deb500..b9a256d597 100644 --- a/packages/modules/zabbix/zabbix.py +++ b/packages/modules/zabbix/zabbix.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import re +from helpermodules.utils.run_command import run_command from modules.common.abstract_device import DeviceDescriptor from modules.zabbix.config import Zabbix @@ -35,6 +36,8 @@ def create_config(config: Zabbix): add_value(config_path, "TLSAccept", "psk") add_value(config_path, "TLSPSKFile", key_path) add_value(config_path, "TLSPSKIdentity", config.configuration.psk_identifier) + # restart service + run_command(["sudo systemctl restart zabbix-agent2"], process_exception=True) device_descriptor = DeviceDescriptor(configuration_factory=Zabbix) From 9992ce9fb1089e523dee270e9f1325c919f16a2a Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Thu, 28 Nov 2024 14:27:47 +0100 Subject: [PATCH 06/21] write to config only on changes, not on startup --- packages/helpermodules/subdata.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index c30365b2ab..61078af0f0 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -683,11 +683,15 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): config_dict = decode_payload(msg.payload) var.data.ocpp = dataclass_from_dict(Ocpp, config_dict) elif re.search("/optional/zabbix/", msg.topic) is not None: - config = decode_payload(msg.payload) - mod = importlib.import_module(".zabbix.zabbix", "modules") - config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) - # var.zabbix_module = mod.create_config_files(config) - mod.create_config(config) + # do not reconfigure zabbix if topic is received on startup + if self.event_subdata_initialized.is_set(): + config = decode_payload(msg.payload) + mod = importlib.import_module(".zabbix.zabbix", "modules") + config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) + # var.zabbix_module = mod.create_config_files(config) + mod.create_config(config) + else: + log.debug("skipping zabbix message on startup") else: self.set_json_payload_class(var.data, msg) except Exception: From fcb6ed888b447f007c86b608315da3819401dabb Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Fri, 29 Nov 2024 07:47:45 +0100 Subject: [PATCH 07/21] validate values in setdata --- packages/helpermodules/setdata.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/helpermodules/setdata.py b/packages/helpermodules/setdata.py index ee9831b709..462f006450 100644 --- a/packages/helpermodules/setdata.py +++ b/packages/helpermodules/setdata.py @@ -864,6 +864,14 @@ def process_optional_topic(self, msg: mqtt.MQTTMessage): elif ("openWB/set/optional/et/provider" in msg.topic or "openWB/set/optional/ocpp/config" in msg.topic): self._validate_value(msg, "json") + elif "openWB/set/optional/zabbix/destination_host" in msg.topic: + self._validate_value(msg, str) + elif "openWB/set/optional/zabbix/hostname" in msg.topic: + self._validate_value(msg, str) + elif "openWB/set/optional/zabbix/psk_identifier" in msg.topic: + self._validate_value(msg, str) + elif "openWB/set/optional/zabbix/psk_key" in msg.topic: + self._validate_value(msg, str) elif "openWB/set/optional/rfid/active" in msg.topic: self._validate_value(msg, bool) elif "openWB/set/optional/int_display/rotation" in msg.topic: From b5ed414a3aa187229353df092b56db73b675fdbf Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Tue, 3 Dec 2024 16:13:35 +0100 Subject: [PATCH 08/21] Monitoring config independent from zabbix --- packages/helpermodules/setdata.py | 10 ++---- packages/helpermodules/subdata.py | 9 +++-- packages/helpermodules/update_config.py | 2 ++ packages/modules/configuration.py | 35 +++++++++++++++++++ .../zabbix.py => monitoring/zabbix/api.py} | 2 +- .../modules/{ => monitoring}/zabbix/config.py | 13 ++++--- 6 files changed, 52 insertions(+), 19 deletions(-) rename packages/modules/{zabbix/zabbix.py => monitoring/zabbix/api.py} (96%) rename packages/modules/{ => monitoring}/zabbix/config.py (62%) diff --git a/packages/helpermodules/setdata.py b/packages/helpermodules/setdata.py index 462f006450..0636643b3d 100644 --- a/packages/helpermodules/setdata.py +++ b/packages/helpermodules/setdata.py @@ -864,14 +864,8 @@ def process_optional_topic(self, msg: mqtt.MQTTMessage): elif ("openWB/set/optional/et/provider" in msg.topic or "openWB/set/optional/ocpp/config" in msg.topic): self._validate_value(msg, "json") - elif "openWB/set/optional/zabbix/destination_host" in msg.topic: - self._validate_value(msg, str) - elif "openWB/set/optional/zabbix/hostname" in msg.topic: - self._validate_value(msg, str) - elif "openWB/set/optional/zabbix/psk_identifier" in msg.topic: - self._validate_value(msg, str) - elif "openWB/set/optional/zabbix/psk_key" in msg.topic: - self._validate_value(msg, str) + elif "openWB/set/optional/monitoring" in msg.topic: + self._validate_value(msg, "json") elif "openWB/set/optional/rfid/active" in msg.topic: self._validate_value(msg, bool) elif "openWB/set/optional/int_display/rotation" in msg.topic: diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index 61078af0f0..8bd6e7edbf 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -682,16 +682,15 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): elif re.search("/optional/ocpp/", msg.topic) is not None: config_dict = decode_payload(msg.payload) var.data.ocpp = dataclass_from_dict(Ocpp, config_dict) - elif re.search("/optional/zabbix/", msg.topic) is not None: - # do not reconfigure zabbix if topic is received on startup + elif re.search("/optional/monitoring/", msg.topic) is not None: + # do not reconfigure monitoring if topic is received on startup if self.event_subdata_initialized.is_set(): config = decode_payload(msg.payload) - mod = importlib.import_module(".zabbix.zabbix", "modules") + mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) - # var.zabbix_module = mod.create_config_files(config) mod.create_config(config) else: - log.debug("skipping zabbix message on startup") + log.debug("skipping monitoring config on startup") else: self.set_json_payload_class(var.data, msg) except Exception: diff --git a/packages/helpermodules/update_config.py b/packages/helpermodules/update_config.py index 7e2b99b84d..bed50a6ff9 100644 --- a/packages/helpermodules/update_config.py +++ b/packages/helpermodules/update_config.py @@ -260,6 +260,7 @@ class UpdateConfig: "^openWB/optional/int_display/theme$", "^openWB/optional/int_display/only_local_charge_points", "^openWB/optional/led/active$", + "^openWB/optional/monitoring/config$", "^openWB/optional/rfid/active$", "^openWB/optional/ocpp/config$", @@ -512,6 +513,7 @@ class UpdateConfig: ("openWB/optional/int_display/theme", dataclass_utils.asdict(CardsDisplayTheme())), ("openWB/optional/int_display/only_local_charge_points", False), ("openWB/optional/led/active", False), + ("openWB/optional/monitoring/config", NO_MODULE), ("openWB/optional/ocpp/config", dataclass_utils.asdict(Ocpp())), ("openWB/optional/rfid/active", False), ("openWB/system/backup_cloud/config", NO_MODULE), diff --git a/packages/modules/configuration.py b/packages/modules/configuration.py index b44d832e4f..2a123d32e2 100644 --- a/packages/modules/configuration.py +++ b/packages/modules/configuration.py @@ -19,6 +19,7 @@ def pub_configurable(): _pub_configurable_devices_components() _pub_configurable_chargepoints() _pub_configurable_ripple_control_receivers() + _pub_configurable_monitoring() def _pub_configurable_backup_clouds() -> None: @@ -336,5 +337,39 @@ def _pub_configurable_ripple_control_receivers() -> None: log.exception("Fehler im configuration-Modul") +def _pub_configurable_monitoring() -> None: + try: + monitoring = [] + path_list = Path(_get_packages_path()/"modules"/"monitoring").glob('**/config.py') + for path in path_list: + try: + if path.name.endswith("_test.py"): + # Tests überspringen + continue + dev_defaults = importlib.import_module( + f".monitoring.{path.parts[-2]}.api", "modules").device_descriptor.configuration_factory() + monitoring.append({ + "value": dev_defaults.type, + "text": dev_defaults.name, + "defaults": dataclass_utils.asdict(dev_defaults) + }) + except Exception: + log.exception("Fehler im configuration-Modul") + monitoring = sorted(monitoring, key=lambda d: d['text'].upper()) + # "leeren" Eintrag an erster Stelle einfügen + monitoring.insert(0, + { + "value": None, + "text": "- kein Monitoring -", + "defaults": { + "type": None, + "configuration": {} + } + }) + Pub().pub("openWB/set/system/configurable/monitoring", monitoring) + except Exception: + log.exception("Fehler im configuration-Modul") + + def _get_packages_path() -> Path: return Path(__file__).resolve().parents[2]/"packages" diff --git a/packages/modules/zabbix/zabbix.py b/packages/modules/monitoring/zabbix/api.py similarity index 96% rename from packages/modules/zabbix/zabbix.py rename to packages/modules/monitoring/zabbix/api.py index b9a256d597..dfd607d928 100644 --- a/packages/modules/zabbix/zabbix.py +++ b/packages/modules/monitoring/zabbix/api.py @@ -2,7 +2,7 @@ import re from helpermodules.utils.run_command import run_command from modules.common.abstract_device import DeviceDescriptor -from modules.zabbix.config import Zabbix +from modules.monitoring.zabbix.config import Zabbix key_path = "/etc/zabbix/encrypt.psk" diff --git a/packages/modules/zabbix/config.py b/packages/modules/monitoring/zabbix/config.py similarity index 62% rename from packages/modules/zabbix/config.py rename to packages/modules/monitoring/zabbix/config.py index 9c4c94071f..ddc4723222 100644 --- a/packages/modules/zabbix/config.py +++ b/packages/modules/monitoring/zabbix/config.py @@ -1,9 +1,12 @@ +from typing import Optional + + class ZabbixConfiguration: def __init__(self, - destination_host: str = "", - hostname: str = "", - psk_identifier: str = "", - psk_key: str = ""): + destination_host: Optional[str] = None, + hostname: Optional[str] = None, + psk_identifier: Optional[str] = None, + psk_key: Optional[str] = None): self.destination_host = destination_host self.hostname = hostname self.psk_identifier = psk_identifier @@ -12,7 +15,7 @@ def __init__(self, class Zabbix: def __init__(self, - name: str = "Zabbix", + name: str = "openWB (Zabbix)", type: str = "zabbix", configuration: ZabbixConfiguration = None) -> None: self.name = name From 792377378d9e193efd4d7b2976e97278b43e0b4d Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Wed, 4 Dec 2024 09:42:33 +0100 Subject: [PATCH 09/21] change zabbix version --- runs/install_packages.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/runs/install_packages.sh b/runs/install_packages.sh index 0e2c27e3ae..452375c3b9 100755 --- a/runs/install_packages.sh +++ b/runs/install_packages.sh @@ -1,9 +1,9 @@ #!/bin/bash echo "install required packages with 'apt-get'..." -if [ ! -e zabbix-release_7.0-2+debian11_all.deb ] +if [ ! -e zabbix-release_6.2-3+debian11_all.deb ] then - wget https://repo.zabbix.com/zabbix/7.0/raspbian/pool/main/z/zabbix-release/zabbix-release_7.0-2+debian11_all.deb - sudo dpkg -i zabbix-release_7.0-2+debian11_all.deb + wget https://repo.zabbix.com/zabbix/6.2/raspbian/pool/main/z/zabbix-release/zabbix-release_6.2-3%2Bdebian11_all.deb + sudo dpkg -i zabbix-release_6.2-3+debian11_all.deb fi sudo apt-get -q update sudo apt-get -q -y install \ @@ -14,6 +14,5 @@ fi mosquitto mosquitto-clients \ python3-pip \ xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \ - chromium chromium-l10n -sudo apt install zabbix-agent2 zabbix-agent2-plugin-* + chromium chromium-l10n zabbix-agent2 zabbix-agent2-plugin-* echo "done" From ee3cf8ed1a9e5bb7ace36182a8ad7f49aef4ed4f Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Wed, 4 Dec 2024 10:00:16 +0100 Subject: [PATCH 10/21] fix install --- runs/install_packages.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/runs/install_packages.sh b/runs/install_packages.sh index 452375c3b9..85d974b644 100755 --- a/runs/install_packages.sh +++ b/runs/install_packages.sh @@ -4,6 +4,7 @@ if [ ! -e zabbix-release_6.2-3+debian11_all.deb ] then wget https://repo.zabbix.com/zabbix/6.2/raspbian/pool/main/z/zabbix-release/zabbix-release_6.2-3%2Bdebian11_all.deb sudo dpkg -i zabbix-release_6.2-3+debian11_all.deb + echo "download" fi sudo apt-get -q update sudo apt-get -q -y install \ @@ -14,5 +15,6 @@ fi mosquitto mosquitto-clients \ python3-pip \ xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \ - chromium chromium-l10n zabbix-agent2 zabbix-agent2-plugin-* + chromium chromium-l10n +sudo apt install zabbix-agent2 zabbix-agent2-plugin-* echo "done" From 9bdb6742fe0f2b7e83c6b1579dc59f1b4668f975 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Wed, 4 Dec 2024 11:35:14 +0100 Subject: [PATCH 11/21] use contextmanager for file handling, touch file with sudo --- packages/modules/monitoring/zabbix/api.py | 56 +++++++++++------------ 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/packages/modules/monitoring/zabbix/api.py b/packages/modules/monitoring/zabbix/api.py index dfd607d928..83600a0205 100644 --- a/packages/modules/monitoring/zabbix/api.py +++ b/packages/modules/monitoring/zabbix/api.py @@ -1,43 +1,41 @@ #!/usr/bin/env python3 -import re +import os from helpermodules.utils.run_command import run_command from modules.common.abstract_device import DeviceDescriptor from modules.monitoring.zabbix.config import Zabbix -key_path = "/etc/zabbix/encrypt.psk" -config_path = "/etc/zabbix/zabbix_agent2.conf" +KEY_PATH = "/etc/zabbix/encrypt.psk" +CONFIG_PATH = "/etc/zabbix/zabbix_agent2.conf" -def add_value(path, key, value): - file = open(path, "r+") - config = file.read() - pattern = "\n"+key+"=.*" - line = "\n"+key + "="+value - splitted = re.split(pattern, config) - - if splitted.len() == 1: - splitted.push(line) - file.writelines(splitted) - file.close() - else: - modified_config = line.join(splitted) - file.write(modified_config) - file.close() +def set_value(path, key, value): + with open(path, "r+") as file: + lines = file.readlines() + for i, line in enumerate(lines): + if line.startswith(key): + lines[i] = f"{key}={value}\n" + break + else: + lines.append(f"{key}={value}\n") + file.seek(0) + file.writelines(lines) + file.truncate() def create_config(config: Zabbix): - key_file = open(key_path, "w") - key_file.write(config.configuration.psk_key) - key_file.close() - add_value(config_path, "ServerActive", config.configuration.destination_host) - add_value(config_path, "Hostname", config.configuration.hostname) - add_value(config_path, "TLSConnect", "psk") - add_value(config_path, "TLSAccept", "psk") - add_value(config_path, "TLSPSKFile", key_path) - add_value(config_path, "TLSPSKIdentity", config.configuration.psk_identifier) - # restart service - run_command(["sudo systemctl restart zabbix-agent2"], process_exception=True) + os.system(f"sudo touch {KEY_PATH}") + os.system(f"sudo chmod 666 {KEY_PATH}") + os.system(f"sudo chmod 666 {CONFIG_PATH}") + with open(KEY_PATH, "w") as key_file: + key_file.write(config.configuration.psk_key) + set_value(CONFIG_PATH, "ServerActive", config.configuration.destination_host) + set_value(CONFIG_PATH, "Hostname", config.configuration.hostname) + set_value(CONFIG_PATH, "TLSConnect", "psk") + set_value(CONFIG_PATH, "TLSAccept", "psk") + set_value(CONFIG_PATH, "TLSPSKFile", KEY_PATH) + set_value(CONFIG_PATH, "TLSPSKIdentity", config.configuration.psk_identifier) + run_command(["sudo", "systemctl", "restart", "zabbix-agent2"], process_exception=True) device_descriptor = DeviceDescriptor(configuration_factory=Zabbix) From f73745d73ddbd6a9b0eaf2236923fec093b09385 Mon Sep 17 00:00:00 2001 From: LKuemmel Date: Fri, 6 Dec 2024 10:01:13 +0100 Subject: [PATCH 12/21] update valid topics --- packages/helpermodules/update_config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/helpermodules/update_config.py b/packages/helpermodules/update_config.py index bed50a6ff9..765b864234 100644 --- a/packages/helpermodules/update_config.py +++ b/packages/helpermodules/update_config.py @@ -406,6 +406,7 @@ class UpdateConfig: "^openWB/system/configurable/devices_components$", "^openWB/system/configurable/electricity_tariffs$", "^openWB/system/configurable/display_themes$", + "^openWB/system/configurable/monitoring$", "^openWB/system/configurable/ripple_control_receivers$", "^openWB/system/configurable/soc_modules$", "^openWB/system/configurable/web_themes$", From fe0068baec46a3f8b9319d83fefcee42eff15f69 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Thu, 9 Jan 2025 09:59:42 +0100 Subject: [PATCH 13/21] improve package installation --- runs/install_packages.sh | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/runs/install_packages.sh b/runs/install_packages.sh index 85d974b644..77aaae81ac 100755 --- a/runs/install_packages.sh +++ b/runs/install_packages.sh @@ -1,11 +1,5 @@ #!/bin/bash echo "install required packages with 'apt-get'..." -if [ ! -e zabbix-release_6.2-3+debian11_all.deb ] -then - wget https://repo.zabbix.com/zabbix/6.2/raspbian/pool/main/z/zabbix-release/zabbix-release_6.2-3%2Bdebian11_all.deb - sudo dpkg -i zabbix-release_6.2-3+debian11_all.deb - echo "download" -fi sudo apt-get -q update sudo apt-get -q -y install \ vim bc jq socat sshpass sudo ssl-cert mmc-utils \ @@ -16,5 +10,14 @@ fi python3-pip \ xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \ chromium chromium-l10n -sudo apt install zabbix-agent2 zabbix-agent2-plugin-* +if [ -z "$(dpkg -l | grep zabbix-agent2)" ] +then + echo "install zabbix" + wget -P /tmp/ https://repo.zabbix.com/zabbix/6.2/raspbian/pool/main/z/zabbix-release/zabbix-release_6.2-3%2Bdebian11_all.deb + sudo dpkg -i /tmp/zabbix-release_6.2-3+debian11_all.deb + echo "download" + sudo apt-get -q update + sudo apt install zabbix-agent2 zabbix-agent2-plugin-* + sudo rm /tmp/zabbix-release_6.2-3+debian11_all.deb +fi echo "done" From 3fc8a252a5be492f67a59820dcd7657a6c9580e3 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Thu, 9 Jan 2025 10:10:31 +0100 Subject: [PATCH 14/21] check payload --- packages/helpermodules/subdata.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index 8bd6e7edbf..a91e935072 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -686,9 +686,10 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): # do not reconfigure monitoring if topic is received on startup if self.event_subdata_initialized.is_set(): config = decode_payload(msg.payload) - mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") - config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) - mod.create_config(config) + if config["type"] is not None: + mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") + config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) + mod.create_config(config) else: log.debug("skipping monitoring config on startup") else: From 9a8578f46b263e7a1956d4aca1e8504530873e8a Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Tue, 14 Jan 2025 08:42:47 +0100 Subject: [PATCH 15/21] enable/ disable monitoring --- packages/helpermodules/subdata.py | 5 +++++ packages/modules/monitoring/zabbix/api.py | 2 -- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index a91e935072..ea5f40fe88 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -690,6 +690,11 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) mod.create_config(config) + run_command(["sudo", "systemctl", "restart", "zabbix-agent2"], process_exception=True) + run_command(["sudo", "systemctl", "enable", "zabbix-agent2"], process_exception=True) + else: + run_command(["sudo", "systemctl", "stop", "zabbix-agent2"], process_exception=True) + run_command(["sudo", "systemctl", "disable", "zabbix-agent2"], process_exception=True) else: log.debug("skipping monitoring config on startup") else: diff --git a/packages/modules/monitoring/zabbix/api.py b/packages/modules/monitoring/zabbix/api.py index 83600a0205..782a648dc6 100644 --- a/packages/modules/monitoring/zabbix/api.py +++ b/packages/modules/monitoring/zabbix/api.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 import os -from helpermodules.utils.run_command import run_command from modules.common.abstract_device import DeviceDescriptor from modules.monitoring.zabbix.config import Zabbix @@ -35,7 +34,6 @@ def create_config(config: Zabbix): set_value(CONFIG_PATH, "TLSAccept", "psk") set_value(CONFIG_PATH, "TLSPSKFile", KEY_PATH) set_value(CONFIG_PATH, "TLSPSKIdentity", config.configuration.psk_identifier) - run_command(["sudo", "systemctl", "restart", "zabbix-agent2"], process_exception=True) device_descriptor = DeviceDescriptor(configuration_factory=Zabbix) From 92ee6b62ea1062059e17270d85c6359616b1eee7 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Tue, 14 Jan 2025 08:53:20 +0100 Subject: [PATCH 16/21] remove whitespace --- runs/install_packages.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runs/install_packages.sh b/runs/install_packages.sh index 77aaae81ac..3025dcf69f 100755 --- a/runs/install_packages.sh +++ b/runs/install_packages.sh @@ -8,7 +8,7 @@ echo "install required packages with 'apt-get'..." git \ mosquitto mosquitto-clients \ python3-pip \ - xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \ + xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \ chromium chromium-l10n if [ -z "$(dpkg -l | grep zabbix-agent2)" ] then From acedca91efced91300ecc88723437637fe82d3a2 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Wed, 15 Jan 2025 13:10:36 +0100 Subject: [PATCH 17/21] move functionality to monitoring modul --- packages/helpermodules/subdata.py | 15 ++++--- packages/modules/monitoring/zabbix/api.py | 48 ++++++++++++++--------- runs/install_packages.sh | 10 ----- runs/install_zabbix.sh | 15 +++++++ 4 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 runs/install_zabbix.sh diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index ea244b61b9..a4d91fb105 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -689,15 +689,14 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): # do not reconfigure monitoring if topic is received on startup if self.event_subdata_initialized.is_set(): config = decode_payload(msg.payload) - if config["type"] is not None: - mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") - config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) - mod.create_config(config) - run_command(["sudo", "systemctl", "restart", "zabbix-agent2"], process_exception=True) - run_command(["sudo", "systemctl", "enable", "zabbix-agent2"], process_exception=True) + if config["type"] is None: + mod = importlib.import_module(".monitoring.zabbix.api", "modules") + mod.disable() else: - run_command(["sudo", "systemctl", "stop", "zabbix-agent2"], process_exception=True) - run_command(["sudo", "systemctl", "disable", "zabbix-agent2"], process_exception=True) + if config["type"] == "zabbix": + mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") + config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) + mod.enable(config) else: log.debug("skipping monitoring config on startup") else: diff --git a/packages/modules/monitoring/zabbix/api.py b/packages/modules/monitoring/zabbix/api.py index 782a648dc6..6e124e21ff 100644 --- a/packages/modules/monitoring/zabbix/api.py +++ b/packages/modules/monitoring/zabbix/api.py @@ -8,18 +8,13 @@ CONFIG_PATH = "/etc/zabbix/zabbix_agent2.conf" -def set_value(path, key, value): - with open(path, "r+") as file: - lines = file.readlines() - for i, line in enumerate(lines): - if line.startswith(key): - lines[i] = f"{key}={value}\n" - break - else: - lines.append(f"{key}={value}\n") - file.seek(0) - file.writelines(lines) - file.truncate() +def set_value(lines, key, value): + for i, line in enumerate(lines): + if line.startswith(key): + lines[i] = f"{key}={value}\n" + break + else: + lines.append(f"{key}={value}\n") def create_config(config: Zabbix): @@ -28,12 +23,29 @@ def create_config(config: Zabbix): os.system(f"sudo chmod 666 {CONFIG_PATH}") with open(KEY_PATH, "w") as key_file: key_file.write(config.configuration.psk_key) - set_value(CONFIG_PATH, "ServerActive", config.configuration.destination_host) - set_value(CONFIG_PATH, "Hostname", config.configuration.hostname) - set_value(CONFIG_PATH, "TLSConnect", "psk") - set_value(CONFIG_PATH, "TLSAccept", "psk") - set_value(CONFIG_PATH, "TLSPSKFile", KEY_PATH) - set_value(CONFIG_PATH, "TLSPSKIdentity", config.configuration.psk_identifier) + with open(CONFIG_PATH, "r+") as config_file: + lines = config_file.readlines() + set_value(lines, "ServerActive", config.configuration.destination_host) + set_value(lines, "Hostname", config.configuration.hostname) + set_value(lines, "TLSConnect", "psk") + set_value(lines, "TLSAccept", "psk") + set_value(lines, "TLSPSKFile", KEY_PATH) + set_value(lines, "TLSPSKIdentity", config.configuration.psk_identifier) + config_file.seek(0) + config_file.writelines(lines) + config_file.truncate() + + +def enable(config: Zabbix): + os.system("sudo ./runs/install_zabbix.sh") + create_config(config) + os.system("sudo systemctl restart zabbix-agent2") + os.system("sudo systemctl enable zabbix-agent2") + + +def disable(): + os.system("sudo systemctl stop zabbix-agent2") + os.system("sudo systemctl disable zabbix-agent2") device_descriptor = DeviceDescriptor(configuration_factory=Zabbix) diff --git a/runs/install_packages.sh b/runs/install_packages.sh index 3025dcf69f..2273bf1abb 100755 --- a/runs/install_packages.sh +++ b/runs/install_packages.sh @@ -10,14 +10,4 @@ echo "install required packages with 'apt-get'..." python3-pip \ xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \ chromium chromium-l10n -if [ -z "$(dpkg -l | grep zabbix-agent2)" ] -then - echo "install zabbix" - wget -P /tmp/ https://repo.zabbix.com/zabbix/6.2/raspbian/pool/main/z/zabbix-release/zabbix-release_6.2-3%2Bdebian11_all.deb - sudo dpkg -i /tmp/zabbix-release_6.2-3+debian11_all.deb - echo "download" - sudo apt-get -q update - sudo apt install zabbix-agent2 zabbix-agent2-plugin-* - sudo rm /tmp/zabbix-release_6.2-3+debian11_all.deb -fi echo "done" diff --git a/runs/install_zabbix.sh b/runs/install_zabbix.sh new file mode 100644 index 0000000000..4c9df924ca --- /dev/null +++ b/runs/install_zabbix.sh @@ -0,0 +1,15 @@ +#!/bin/bash +echo "check if zabbix agent is already installed..." +if [ -z "$(dpkg -l | grep zabbix-agent2)" ] +then + echo "start download..." + wget -P /tmp/ https://repo.zabbix.com/zabbix/6.2/raspbian/pool/main/z/zabbix-release/zabbix-release_6.2-3%2Bdebian11_all.deb + sudo dpkg -i /tmp/zabbix-release_6.2-3+debian11_all.deb + echo "install zabbix." + sudo apt-get -q update + sudo apt install zabbix-agent2 zabbix-agent2-plugin-* + sudo rm /tmp/zabbix-release_6.2-3+debian11_all.deb +else + echo "nothing to do." +fi +echo "done" \ No newline at end of file From c7db4ca9fa484d91c4a560b32ed2df3e77f014c5 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Wed, 15 Jan 2025 14:11:33 +0100 Subject: [PATCH 18/21] save instance of monitoring in optional --- packages/control/optional.py | 10 ++++++++ packages/helpermodules/subdata.py | 9 ++++--- .../modules/common/configurable_monitoring.py | 21 ++++++++++++++++ packages/modules/monitoring/zabbix/api.py | 24 +++++++++++-------- 4 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 packages/modules/common/configurable_monitoring.py diff --git a/packages/control/optional.py b/packages/control/optional.py index 938f082595..d19545678c 100644 --- a/packages/control/optional.py +++ b/packages/control/optional.py @@ -14,6 +14,7 @@ from helpermodules.timecheck import create_unix_timestamp_current_full_hour from helpermodules.utils import thread_handler from modules.common.configurable_tariff import ConfigurableElectricityTariff +from modules.common.configurable_monitoring import ConfigurableMonitoring log = logging.getLogger(__name__) @@ -23,11 +24,20 @@ def __init__(self): try: self.data = OptionalData() self.et_module: ConfigurableElectricityTariff = None + self.mon_module: ConfigurableMonitoring = None self.data.dc_charging = hardware_configuration.get_hardware_configuration_setting("dc_charging") Pub().pub("openWB/optional/dc_charging", self.data.dc_charging) except Exception: log.exception("Fehler im Optional-Modul") + def monitoring_start(self): + if self.mon_module is not None: + self.mon_module.start_monitoring() + + def monitoring_stop(self): + if self.mon_module is not None: + self.mon_module.stop_monitoring() + def et_provider_available(self) -> bool: return self.et_module is not None and self.data.et.get.fault_state != 2 diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index a4d91fb105..f765ab0025 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -28,6 +28,7 @@ from helpermodules.pub import Pub from dataclass_utils import dataclass_from_dict from modules.common.abstract_vehicle import CalculatedSocState, GeneralVehicleConfig +from modules.common.configurable_monitoring import ConfigurableMonitoring from modules.common.configurable_backup_cloud import ConfigurableBackupCloud from modules.common.configurable_ripple_control_receiver import ConfigurableRcr from modules.common.configurable_tariff import ConfigurableElectricityTariff @@ -690,13 +691,15 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): if self.event_subdata_initialized.is_set(): config = decode_payload(msg.payload) if config["type"] is None: - mod = importlib.import_module(".monitoring.zabbix.api", "modules") - mod.disable() + var.monitoring_stop() + var.mon_module = None else: if config["type"] == "zabbix": mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) - mod.enable(config) + var.mon_module = ConfigurableMonitoring(mod.create_start_monitoring(config), + mod.create_stop_monitoring) + var.monitoring_start() else: log.debug("skipping monitoring config on startup") else: diff --git a/packages/modules/common/configurable_monitoring.py b/packages/modules/common/configurable_monitoring.py new file mode 100644 index 0000000000..f17e8ff634 --- /dev/null +++ b/packages/modules/common/configurable_monitoring.py @@ -0,0 +1,21 @@ +from typing import Callable + +from modules.common.component_context import SingleComponentUpdateContext +from modules.common.component_type import ComponentType +from modules.common.fault_state import ComponentInfo, FaultState + + +class ConfigurableMonitoring(): + def __init__(self, + start_initializer: Callable[[]], + stop_initializer: Callable[[]]) -> None: + self.fault_state = FaultState(ComponentInfo(None, self.config.name, ComponentType.ELECTRICITY_TARIFF.value)) + with SingleComponentUpdateContext(self.fault_state): + self._start_monitoring = start_initializer() + self._stop_monitoring = stop_initializer() + + def start_monitoring(self): + self._start_monitoring() + + def stop_monitoring(self): + self._stop_monitoring() diff --git a/packages/modules/monitoring/zabbix/api.py b/packages/modules/monitoring/zabbix/api.py index 6e124e21ff..6fcb164620 100644 --- a/packages/modules/monitoring/zabbix/api.py +++ b/packages/modules/monitoring/zabbix/api.py @@ -36,16 +36,20 @@ def create_config(config: Zabbix): config_file.truncate() -def enable(config: Zabbix): - os.system("sudo ./runs/install_zabbix.sh") - create_config(config) - os.system("sudo systemctl restart zabbix-agent2") - os.system("sudo systemctl enable zabbix-agent2") - - -def disable(): - os.system("sudo systemctl stop zabbix-agent2") - os.system("sudo systemctl disable zabbix-agent2") +def create_start_monitoring(config: Zabbix): + def start_monitoring(): + os.system("sudo ./runs/install_zabbix.sh") + create_config(config) + os.system("sudo systemctl restart zabbix-agent2") + os.system("sudo systemctl enable zabbix-agent2") + return start_monitoring + + +def create_stop_monitoring(): + def stop_monitoring(): + os.system("sudo systemctl stop zabbix-agent2") + os.system("sudo systemctl disable zabbix-agent2") + return stop_monitoring device_descriptor = DeviceDescriptor(configuration_factory=Zabbix) From 8c6c7f97eb1106914db30bace672f5bcc248bfab Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Thu, 16 Jan 2025 15:40:26 +0100 Subject: [PATCH 19/21] adjust configurable monitoring --- packages/control/optional.py | 6 +++--- packages/helpermodules/subdata.py | 13 +++++-------- packages/modules/common/configurable_monitoring.py | 11 ++++++----- packages/modules/monitoring/zabbix/api.py | 8 +++----- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/control/optional.py b/packages/control/optional.py index d19545678c..dea75d3df8 100644 --- a/packages/control/optional.py +++ b/packages/control/optional.py @@ -24,15 +24,15 @@ def __init__(self): try: self.data = OptionalData() self.et_module: ConfigurableElectricityTariff = None - self.mon_module: ConfigurableMonitoring = None + self.monitoring_module: ConfigurableMonitoring = None self.data.dc_charging = hardware_configuration.get_hardware_configuration_setting("dc_charging") Pub().pub("openWB/optional/dc_charging", self.data.dc_charging) except Exception: log.exception("Fehler im Optional-Modul") def monitoring_start(self): - if self.mon_module is not None: - self.mon_module.start_monitoring() + if self.monitoring_module is not None: + self.monitoring_module.start_monitoring() def monitoring_stop(self): if self.mon_module is not None: diff --git a/packages/helpermodules/subdata.py b/packages/helpermodules/subdata.py index f765ab0025..f834771322 100644 --- a/packages/helpermodules/subdata.py +++ b/packages/helpermodules/subdata.py @@ -28,7 +28,6 @@ from helpermodules.pub import Pub from dataclass_utils import dataclass_from_dict from modules.common.abstract_vehicle import CalculatedSocState, GeneralVehicleConfig -from modules.common.configurable_monitoring import ConfigurableMonitoring from modules.common.configurable_backup_cloud import ConfigurableBackupCloud from modules.common.configurable_ripple_control_receiver import ConfigurableRcr from modules.common.configurable_tariff import ConfigurableElectricityTariff @@ -692,14 +691,12 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage): config = decode_payload(msg.payload) if config["type"] is None: var.monitoring_stop() - var.mon_module = None + var.monitoring_module = None else: - if config["type"] == "zabbix": - mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") - config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) - var.mon_module = ConfigurableMonitoring(mod.create_start_monitoring(config), - mod.create_stop_monitoring) - var.monitoring_start() + mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules") + config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config) + var.monitoring_module = mod.create_monitoring(config) + var.monitoring_start() else: log.debug("skipping monitoring config on startup") else: diff --git a/packages/modules/common/configurable_monitoring.py b/packages/modules/common/configurable_monitoring.py index f17e8ff634..cdec66357d 100644 --- a/packages/modules/common/configurable_monitoring.py +++ b/packages/modules/common/configurable_monitoring.py @@ -1,18 +1,19 @@ +import logging from typing import Callable -from modules.common.component_context import SingleComponentUpdateContext -from modules.common.component_type import ComponentType -from modules.common.fault_state import ComponentInfo, FaultState + +log = logging.getLogger(__name__) class ConfigurableMonitoring(): def __init__(self, start_initializer: Callable[[]], stop_initializer: Callable[[]]) -> None: - self.fault_state = FaultState(ComponentInfo(None, self.config.name, ComponentType.ELECTRICITY_TARIFF.value)) - with SingleComponentUpdateContext(self.fault_state): + try: self._start_monitoring = start_initializer() self._stop_monitoring = stop_initializer() + except Exception: + log.exception("Fehler im Monitoring Modul") def start_monitoring(self): self._start_monitoring() diff --git a/packages/modules/monitoring/zabbix/api.py b/packages/modules/monitoring/zabbix/api.py index 6fcb164620..f0e51d05de 100644 --- a/packages/modules/monitoring/zabbix/api.py +++ b/packages/modules/monitoring/zabbix/api.py @@ -2,6 +2,7 @@ import os from modules.common.abstract_device import DeviceDescriptor from modules.monitoring.zabbix.config import Zabbix +from modules.common.configurable_monitoring import ConfigurableMonitoring KEY_PATH = "/etc/zabbix/encrypt.psk" @@ -36,20 +37,17 @@ def create_config(config: Zabbix): config_file.truncate() -def create_start_monitoring(config: Zabbix): +def create_monitoring(config: Zabbix): def start_monitoring(): os.system("sudo ./runs/install_zabbix.sh") create_config(config) os.system("sudo systemctl restart zabbix-agent2") os.system("sudo systemctl enable zabbix-agent2") - return start_monitoring - -def create_stop_monitoring(): def stop_monitoring(): os.system("sudo systemctl stop zabbix-agent2") os.system("sudo systemctl disable zabbix-agent2") - return stop_monitoring + return ConfigurableMonitoring(start_monitoring, stop_monitoring) device_descriptor = DeviceDescriptor(configuration_factory=Zabbix) From 50eb96ae4157f4e38dbab5e9056708cb80f74b10 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Thu, 16 Jan 2025 15:46:50 +0100 Subject: [PATCH 20/21] add return type --- packages/modules/common/configurable_monitoring.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/modules/common/configurable_monitoring.py b/packages/modules/common/configurable_monitoring.py index cdec66357d..b47e52afdb 100644 --- a/packages/modules/common/configurable_monitoring.py +++ b/packages/modules/common/configurable_monitoring.py @@ -7,8 +7,8 @@ class ConfigurableMonitoring(): def __init__(self, - start_initializer: Callable[[]], - stop_initializer: Callable[[]]) -> None: + start_initializer: Callable[[], None], + stop_initializer: Callable[[], None]) -> None: try: self._start_monitoring = start_initializer() self._stop_monitoring = stop_initializer() From f157e8d75a094625139346863ae0cf3f46d00e07 Mon Sep 17 00:00:00 2001 From: ndrsnhs Date: Wed, 22 Jan 2025 15:22:52 +0100 Subject: [PATCH 21/21] store functions in configurable_monitoring --- packages/modules/common/configurable_monitoring.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/modules/common/configurable_monitoring.py b/packages/modules/common/configurable_monitoring.py index b47e52afdb..27b7b18889 100644 --- a/packages/modules/common/configurable_monitoring.py +++ b/packages/modules/common/configurable_monitoring.py @@ -10,8 +10,8 @@ def __init__(self, start_initializer: Callable[[], None], stop_initializer: Callable[[], None]) -> None: try: - self._start_monitoring = start_initializer() - self._stop_monitoring = stop_initializer() + self._start_monitoring = start_initializer + self._stop_monitoring = stop_initializer except Exception: log.exception("Fehler im Monitoring Modul")