Skip to content

Commit a0fb6d4

Browse files
ndrsnhsLKuemmel
andauthored
Zabbix monitoring (#2166)
* install zabbix client * add zabbix module * subdata * write to config files * restart zabbix service * write to config only on changes, not on startup * validate values in setdata * Monitoring config independent from zabbix * change zabbix version * fix install * use contextmanager for file handling, touch file with sudo * update valid topics * improve package installation * check payload * enable/ disable monitoring * remove whitespace * move functionality to monitoring modul * save instance of monitoring in optional * adjust configurable monitoring * add return type * store functions in configurable_monitoring --------- Co-authored-by: LKuemmel <lena.kuemmel@openwb.de>
1 parent 47c9346 commit a0fb6d4

6 files changed

Lines changed: 85 additions & 35 deletions

File tree

packages/control/optional.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from helpermodules.timecheck import create_unix_timestamp_current_full_hour
1515
from helpermodules.utils import thread_handler
1616
from modules.common.configurable_tariff import ConfigurableElectricityTariff
17+
from modules.common.configurable_monitoring import ConfigurableMonitoring
1718

1819
log = logging.getLogger(__name__)
1920

@@ -23,11 +24,20 @@ def __init__(self):
2324
try:
2425
self.data = OptionalData()
2526
self.et_module: ConfigurableElectricityTariff = None
27+
self.monitoring_module: ConfigurableMonitoring = None
2628
self.data.dc_charging = hardware_configuration.get_hardware_configuration_setting("dc_charging")
2729
Pub().pub("openWB/optional/dc_charging", self.data.dc_charging)
2830
except Exception:
2931
log.exception("Fehler im Optional-Modul")
3032

33+
def monitoring_start(self):
34+
if self.monitoring_module is not None:
35+
self.monitoring_module.start_monitoring()
36+
37+
def monitoring_stop(self):
38+
if self.mon_module is not None:
39+
self.mon_module.stop_monitoring()
40+
3141
def et_provider_available(self) -> bool:
3242
return self.et_module is not None and self.data.et.get.fault_state != 2
3343

packages/helpermodules/subdata.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -689,15 +689,14 @@ def process_optional_topic(self, var: optional.Optional, msg: mqtt.MQTTMessage):
689689
# do not reconfigure monitoring if topic is received on startup
690690
if self.event_subdata_initialized.is_set():
691691
config = decode_payload(msg.payload)
692-
if config["type"] is not None:
692+
if config["type"] is None:
693+
var.monitoring_stop()
694+
var.monitoring_module = None
695+
else:
693696
mod = importlib.import_module(f".monitoring.{config['type']}.api", "modules")
694697
config = dataclass_from_dict(mod.device_descriptor.configuration_factory, config)
695-
mod.create_config(config)
696-
run_command(["sudo", "systemctl", "restart", "zabbix-agent2"], process_exception=True)
697-
run_command(["sudo", "systemctl", "enable", "zabbix-agent2"], process_exception=True)
698-
else:
699-
run_command(["sudo", "systemctl", "stop", "zabbix-agent2"], process_exception=True)
700-
run_command(["sudo", "systemctl", "disable", "zabbix-agent2"], process_exception=True)
698+
var.monitoring_module = mod.create_monitoring(config)
699+
var.monitoring_start()
701700
else:
702701
log.debug("skipping monitoring config on startup")
703702
else:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import logging
2+
from typing import Callable
3+
4+
5+
log = logging.getLogger(__name__)
6+
7+
8+
class ConfigurableMonitoring():
9+
def __init__(self,
10+
start_initializer: Callable[[], None],
11+
stop_initializer: Callable[[], None]) -> None:
12+
try:
13+
self._start_monitoring = start_initializer
14+
self._stop_monitoring = stop_initializer
15+
except Exception:
16+
log.exception("Fehler im Monitoring Modul")
17+
18+
def start_monitoring(self):
19+
self._start_monitoring()
20+
21+
def stop_monitoring(self):
22+
self._stop_monitoring()

packages/modules/monitoring/zabbix/api.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@
22
import os
33
from modules.common.abstract_device import DeviceDescriptor
44
from modules.monitoring.zabbix.config import Zabbix
5+
from modules.common.configurable_monitoring import ConfigurableMonitoring
56

67

78
KEY_PATH = "/etc/zabbix/encrypt.psk"
89
CONFIG_PATH = "/etc/zabbix/zabbix_agent2.conf"
910

1011

11-
def set_value(path, key, value):
12-
with open(path, "r+") as file:
13-
lines = file.readlines()
14-
for i, line in enumerate(lines):
15-
if line.startswith(key):
16-
lines[i] = f"{key}={value}\n"
17-
break
18-
else:
19-
lines.append(f"{key}={value}\n")
20-
file.seek(0)
21-
file.writelines(lines)
22-
file.truncate()
12+
def set_value(lines, key, value):
13+
for i, line in enumerate(lines):
14+
if line.startswith(key):
15+
lines[i] = f"{key}={value}\n"
16+
break
17+
else:
18+
lines.append(f"{key}={value}\n")
2319

2420

2521
def create_config(config: Zabbix):
@@ -28,12 +24,30 @@ def create_config(config: Zabbix):
2824
os.system(f"sudo chmod 666 {CONFIG_PATH}")
2925
with open(KEY_PATH, "w") as key_file:
3026
key_file.write(config.configuration.psk_key)
31-
set_value(CONFIG_PATH, "ServerActive", config.configuration.destination_host)
32-
set_value(CONFIG_PATH, "Hostname", config.configuration.hostname)
33-
set_value(CONFIG_PATH, "TLSConnect", "psk")
34-
set_value(CONFIG_PATH, "TLSAccept", "psk")
35-
set_value(CONFIG_PATH, "TLSPSKFile", KEY_PATH)
36-
set_value(CONFIG_PATH, "TLSPSKIdentity", config.configuration.psk_identifier)
27+
with open(CONFIG_PATH, "r+") as config_file:
28+
lines = config_file.readlines()
29+
set_value(lines, "ServerActive", config.configuration.destination_host)
30+
set_value(lines, "Hostname", config.configuration.hostname)
31+
set_value(lines, "TLSConnect", "psk")
32+
set_value(lines, "TLSAccept", "psk")
33+
set_value(lines, "TLSPSKFile", KEY_PATH)
34+
set_value(lines, "TLSPSKIdentity", config.configuration.psk_identifier)
35+
config_file.seek(0)
36+
config_file.writelines(lines)
37+
config_file.truncate()
38+
39+
40+
def create_monitoring(config: Zabbix):
41+
def start_monitoring():
42+
os.system("sudo ./runs/install_zabbix.sh")
43+
create_config(config)
44+
os.system("sudo systemctl restart zabbix-agent2")
45+
os.system("sudo systemctl enable zabbix-agent2")
46+
47+
def stop_monitoring():
48+
os.system("sudo systemctl stop zabbix-agent2")
49+
os.system("sudo systemctl disable zabbix-agent2")
50+
return ConfigurableMonitoring(start_monitoring, stop_monitoring)
3751

3852

3953
device_descriptor = DeviceDescriptor(configuration_factory=Zabbix)

runs/install_packages.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,4 @@ echo "install required packages with 'apt-get'..."
1010
python3-pip \
1111
xserver-xorg x11-xserver-utils openbox-lxde-session lightdm lightdm-autologin-greeter accountsservice \
1212
chromium chromium-l10n
13-
if [ -z "$(dpkg -l | grep zabbix-agent2)" ]
14-
then
15-
echo "install zabbix"
16-
wget -P /tmp/ https://repo.zabbix.com/zabbix/6.2/raspbian/pool/main/z/zabbix-release/zabbix-release_6.2-3%2Bdebian11_all.deb
17-
sudo dpkg -i /tmp/zabbix-release_6.2-3+debian11_all.deb
18-
echo "download"
19-
sudo apt-get -q update
20-
sudo apt install zabbix-agent2 zabbix-agent2-plugin-*
21-
sudo rm /tmp/zabbix-release_6.2-3+debian11_all.deb
22-
fi
2313
echo "done"

runs/install_zabbix.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
echo "check if zabbix agent is already installed..."
3+
if [ -z "$(dpkg -l | grep zabbix-agent2)" ]
4+
then
5+
echo "start download..."
6+
wget -P /tmp/ https://repo.zabbix.com/zabbix/6.2/raspbian/pool/main/z/zabbix-release/zabbix-release_6.2-3%2Bdebian11_all.deb
7+
sudo dpkg -i /tmp/zabbix-release_6.2-3+debian11_all.deb
8+
echo "install zabbix."
9+
sudo apt-get -q update
10+
sudo apt install zabbix-agent2 zabbix-agent2-plugin-*
11+
sudo rm /tmp/zabbix-release_6.2-3+debian11_all.deb
12+
else
13+
echo "nothing to do."
14+
fi
15+
echo "done"

0 commit comments

Comments
 (0)