Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zabbix monitoring #2166

Merged
merged 25 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
61eed09
install zabbix client
ndrsnhs Nov 27, 2024
5e80265
add zabbix module
ndrsnhs Nov 27, 2024
82e3ea9
subdata
ndrsnhs Nov 28, 2024
ecc9f99
Merge branch 'openWB:master' into zabbix-monitoring
ndrsnhs Nov 28, 2024
c8e27bb
write to config files
ndrsnhs Nov 28, 2024
4e0ad15
restart zabbix service
ndrsnhs Nov 28, 2024
9992ce9
write to config only on changes, not on startup
ndrsnhs Nov 28, 2024
fcb6ed8
validate values in setdata
ndrsnhs Nov 29, 2024
b5ed414
Monitoring config independent from zabbix
LKuemmel Dec 3, 2024
7923773
change zabbix version
LKuemmel Dec 4, 2024
ee3cf8e
fix install
LKuemmel Dec 4, 2024
9bdb674
use contextmanager for file handling, touch file with sudo
LKuemmel Dec 4, 2024
f73745d
update valid topics
LKuemmel Dec 6, 2024
fe0068b
improve package installation
ndrsnhs Jan 9, 2025
3fc8a25
check payload
ndrsnhs Jan 9, 2025
9a8578f
enable/ disable monitoring
ndrsnhs Jan 14, 2025
dd4d9e6
Merge branch 'zabbix-monitoring' of https://github.com/ndrsnhs/core i…
ndrsnhs Jan 14, 2025
92ee6b6
remove whitespace
ndrsnhs Jan 14, 2025
acedca9
move functionality to monitoring modul
ndrsnhs Jan 15, 2025
c7db4ca
save instance of monitoring in optional
ndrsnhs Jan 15, 2025
d00efcc
Merge branch 'zabbix-monitoring' of https://github.com/ndrsnhs/core i…
ndrsnhs Jan 16, 2025
8c6c7f9
adjust configurable monitoring
ndrsnhs Jan 16, 2025
50eb96a
add return type
ndrsnhs Jan 16, 2025
2e5783b
Merge branch 'openWB:master' into zabbix-monitoring
ndrsnhs Jan 21, 2025
f157e8d
store functions in configurable_monitoring
ndrsnhs Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/control/optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -23,11 +24,20 @@ def __init__(self):
try:
self.data = OptionalData()
self.et_module: ConfigurableElectricityTariff = 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.monitoring_module is not None:
self.monitoring_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

Expand Down
13 changes: 6 additions & 7 deletions packages/helpermodules/subdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
if config["type"] is None:
var.monitoring_stop()
var.monitoring_module = None
else:
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)
var.monitoring_module = mod.create_monitoring(config)
var.monitoring_start()
else:
log.debug("skipping monitoring config on startup")
else:
Expand Down
22 changes: 22 additions & 0 deletions packages/modules/common/configurable_monitoring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logging
from typing import Callable


log = logging.getLogger(__name__)


class ConfigurableMonitoring():
def __init__(self,
start_initializer: Callable[[], None],
stop_initializer: Callable[[], None]) -> None:
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()

def stop_monitoring(self):
self._stop_monitoring()
50 changes: 32 additions & 18 deletions packages/modules/monitoring/zabbix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
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"
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):
Expand All @@ -28,12 +24,30 @@ 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 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")

def stop_monitoring():
os.system("sudo systemctl stop zabbix-agent2")
os.system("sudo systemctl disable zabbix-agent2")
return ConfigurableMonitoring(start_monitoring, stop_monitoring)


device_descriptor = DeviceDescriptor(configuration_factory=Zabbix)
10 changes: 0 additions & 10 deletions runs/install_packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 15 additions & 0 deletions runs/install_zabbix.sh
Original file line number Diff line number Diff line change
@@ -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"