Collection of CheckMK checks (see https://checkmk.com/). All checks and plugins are provided as is. Absolutely no warranty. Send any comments to thl-cmk[at]outlook[dot]com

Skip to content
Snippets Groups Projects
Commit 397b4ab5 authored by thl-cmk's avatar thl-cmk :flag_na:
Browse files

update project

parent e8dcc82a
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -9,14 +9,10 @@ ...@@ -9,14 +9,10 @@
# File : fritzbox_smarthome_power_meter.py (check plugin) # File : fritzbox_smarthome_power_meter.py (check plugin)
# #
# #
# import time
from time import localtime, time as time_now from time import localtime, time as time_now
from typing import Dict from typing import Dict
from os import environ
from pathlib import Path
from json import loads, dumps
from cmk.checkers import plugin_contexts
from cmk.base.plugins.agent_based.agent_based_api.v1 import ( from cmk.base.plugins.agent_based.agent_based_api.v1 import (
Metric, Metric,
Result, Result,
...@@ -27,58 +23,7 @@ from cmk.base.plugins.agent_based.agent_based_api.v1 import ( ...@@ -27,58 +23,7 @@ from cmk.base.plugins.agent_based.agent_based_api.v1 import (
) )
from cmk.base.plugins.agent_based.agent_based_api.v1.type_defs import CheckResult, DiscoveryResult from cmk.base.plugins.agent_based.agent_based_api.v1.type_defs import CheckResult, DiscoveryResult
from cmk.utils.render import physical_precision from cmk.utils.render import physical_precision
from cmk.base.plugins.agent_based.utils.fritzbox_smarthome import AvmSmartHomeDevice from cmk.base.plugins.agent_based.utils.fritzbox_smarthome import AvmSmartHomeDevice, FritzBoxValueStore
OMD_ROOT = environ["OMD_ROOT"]
class FritzBoxValueStore:
"""
Persistent kind of value_store. Located under ~/var/fritzbox_smarthome/{hostname}_{service_description}
"""
def __init__(self):
self._host_name = plugin_contexts.host_name()
self._service_description = plugin_contexts.service_description().replace(' ', '_')
self._file = Path(f'{OMD_ROOT}/var/firtzbox_smarthome/{self._host_name}_{self._service_description}')
self._counters = {}
if self._file.exists():
self._counters = loads(self._file.read_text())
else:
self._counters = {}
def get(self, key: str, default_value: int | str | float | bool | None = None):
"""
Get a value from the value store
Args:
key: the key in the value store
default_value: the default value to return if key not found
Returns:
The value for key from the value store. If key not exists the default_value or None
"""
return self._counters.get(key, default_value)
def set(self, key: str, value: str | int | float | None) -> None:
"""
Set a value in the value stare. Use "None" as value to remove a key.
Args:
key: the key in the value store
value: the value to set
"""
if value is None:
try:
self._counters.pop(key)
except KeyError:
pass
else:
self._counters[key] = value
def save(self) -> None:
"""
Save the value_store as json to file
"""
self._file.parent.mkdir(exist_ok=True, parents=True)
self._file.write_text(dumps(self._counters))
def discovery_fritzbox_smarthome_voltage_single( def discovery_fritzbox_smarthome_voltage_single(
......
...@@ -46,14 +46,24 @@ def check_fritzbox_smarthome_power_socket_single( ...@@ -46,14 +46,24 @@ def check_fritzbox_smarthome_power_socket_single(
return return
def _get_status(status: int): def _get_status(status: int):
_switch_state = { _state = {
0: 'off', 0: ('off', params.get('state', {}).get('off', 0)),
1: 'on', 1: ('on', params.get('state', {}).get('on', 0)),
} }
return _switch_state.get(status, f'unknown ({status})') return _state.get(status, (f'unknown ({status})', 3))
yield Result(state=State.OK, summary=f'State: {_get_status(section.switch.state)}') state_readable, state = _get_status(section.switch.state)
yield Result(state=State.OK, summary=f'Mode: {section.switch.mode}') yield Result(state=State(state), summary=f'State: {state_readable}')
def _get_mode(mode: str):
_mode = {
'manuell': ('manuell', params.get('mode', {}).get('manuell', 0)),
'automatic': ('automatic', params.get('mode', {}).get('automatic', 0)),
}
return _mode.get(mode, (f'unknown ({mode})', 3))
mode_readable, mode = _get_mode(section.switch.mode)
yield Result(state=State(mode), summary=f'Mode: {mode_readable}')
def check_fritzbox_smarthome_power_socket_multiple( def check_fritzbox_smarthome_power_socket_multiple(
...@@ -72,7 +82,7 @@ register.check_plugin( ...@@ -72,7 +82,7 @@ register.check_plugin(
sections=['fritzbox_smarthome'], sections=['fritzbox_smarthome'],
discovery_function=discovery_fritzbox_smarthome_power_socket_single, discovery_function=discovery_fritzbox_smarthome_power_socket_single,
check_function=check_fritzbox_smarthome_power_socket_single, check_function=check_fritzbox_smarthome_power_socket_single,
# check_ruleset_name='fritzbox_smarthome_power_socket', check_ruleset_name='fritzbox_smarthome_power_socket_single',
check_default_parameters={} check_default_parameters={}
) )
...@@ -83,6 +93,6 @@ register.check_plugin( ...@@ -83,6 +93,6 @@ register.check_plugin(
sections=['fritzbox_smarthome'], sections=['fritzbox_smarthome'],
discovery_function=discovery_fritzbox_smarthome_power_socket_multiple, discovery_function=discovery_fritzbox_smarthome_power_socket_multiple,
check_function=check_fritzbox_smarthome_power_socket_multiple, check_function=check_fritzbox_smarthome_power_socket_multiple,
# check_ruleset_name='fritzbox_smarthome_power_socket', check_ruleset_name='fritzbox_smarthome_power_socket_multiple',
check_default_parameters={} check_default_parameters={}
) )
...@@ -65,7 +65,7 @@ def check_fritzbox_smarthome_thermostat_single( ...@@ -65,7 +65,7 @@ def check_fritzbox_smarthome_thermostat_single(
if thermostat.temp_target == 126.5: # == radiator off if thermostat.temp_target == 126.5: # == radiator off
yield Result(state=State.OK, summary=f'Temperature current: {thermostat.temp_current}°C') yield Result(state=State.OK, summary=f'Temperature current: {thermostat.temp_current}°C')
yield Result(state=State.OK, summary=f'Temperature target: radiator off') yield Result(state=State(params.get('state_off', 0)), summary=f'Temperature target: radiator off')
else: else:
deviation = thermostat.temp_current - thermostat.temp_target deviation = thermostat.temp_current - thermostat.temp_target
if deviation == 0: if deviation == 0:
......
...@@ -14,6 +14,11 @@ ...@@ -14,6 +14,11 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing import Any, List, Dict from typing import Any, List, Dict
from os import environ
from pathlib import Path
from json import loads, dumps
from cmk.checkers import plugin_contexts
@dataclass(frozen=True) @dataclass(frozen=True)
...@@ -93,6 +98,57 @@ _AVM_TEMPERATURE = 'temperature' ...@@ -93,6 +98,57 @@ _AVM_TEMPERATURE = 'temperature'
_AVM_SIMPLE_ON_OFF = 'simpleonoff' _AVM_SIMPLE_ON_OFF = 'simpleonoff'
_AVM_NEXT_CHANGE = 'nextchange' _AVM_NEXT_CHANGE = 'nextchange'
_OMD_ROOT = environ["OMD_ROOT"]
class FritzBoxValueStore:
"""
Persistent kind of value_store. Located under ~/var/fritzbox_smarthome/{hostname}_{service_description}
"""
def __init__(self):
self._host_name = plugin_contexts.host_name()
self._service_description = plugin_contexts.service_description().replace(' ', '_')
self._file = Path(f'{_OMD_ROOT}/var/firtzbox_smarthome/{self._host_name}_{self._service_description}')
self._counters = {}
if self._file.exists():
self._counters = loads(self._file.read_text())
else:
self._counters = {}
def get(self, key: str, default_value: int | str | float | bool | None = None):
"""
Get a value from the value store
Args:
key: the key in the value store
default_value: the default value to return if key not found
Returns:
The value for key from the value store. If key not exists the default_value or None
"""
return self._counters.get(key, default_value)
def set(self, key: str, value: str | int | float | None) -> None:
"""
Set a value in the value stare. Use "None" as value to remove a key.
Args:
key: the key in the value store
value: the value to set
"""
if value is None:
try:
self._counters.pop(key)
except KeyError:
pass
else:
self._counters[key] = value
def save(self) -> None:
"""
Save the value_store as json to file
"""
self._file.parent.mkdir(exist_ok=True, parents=True)
self._file.write_text(dumps(self._counters))
def _get_battery_low(device: Dict[str, Any]) -> int | None: def _get_battery_low(device: Dict[str, Any]) -> int | None:
try: try:
......
...@@ -73,6 +73,11 @@ def _parameter_valuespec_fritzbox_smarthome_thermostat(): ...@@ -73,6 +73,11 @@ def _parameter_valuespec_fritzbox_smarthome_thermostat():
Integer(title=_('Warning'), default_value=3, unit=_('°C')), Integer(title=_('Warning'), default_value=3, unit=_('°C')),
Integer(title=_('Critical'), default_value=5, unit=_('°C')), Integer(title=_('Critical'), default_value=5, unit=_('°C')),
])), ])),
('state_off',
MonitoringState(
title=_('Monitoring state if thermostat is off'),
default_value=0,
)),
('state_on_error', ('state_on_error',
MonitoringState( MonitoringState(
title=_('Monitoring state on error'), title=_('Monitoring state on error'),
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# License: GNU General Public License v2
#
# Author: thl-cmk[at]outlook[dot]com
# URL : https://thl-cmk.hopto.org
# Date : 2024-01-09
# File : fritzbox_smarthome_lock.py (WATO check plugin)
#
from cmk.gui.i18n import _
from cmk.gui.valuespec import (
Dictionary,
MonitoringState,
TextInput,
Alternative,
)
from cmk.gui.plugins.wato.utils import (
CheckParameterRulespecWithItem,
CheckParameterRulespecWithoutItem,
RulespecGroupCheckParametersApplications,
rulespec_registry,
)
def _parameter_valuespec_fritzbox_smarthome_power_socket():
return Dictionary(
title=_('Parameter'),
elements=[
('state',
Alternative(
title=_('Power Socket state'),
elements=[
Dictionary(
title=_('Switched On'),
optional_keys=False,
elements=[
('on',
MonitoringState(
title=_('Monitoring state'),
default_value=0,
)),
]),
Dictionary(
title=_('Switched off'),
optional_keys=False,
elements=[
('off',
MonitoringState(
title=_('Monitoring state'),
default_value=0,
))
]),
]
)),
('mode',
Alternative(
title=_('Power Socket switching mode'),
elements=[
Dictionary(
title=_('Manuell'),
optional_keys=False,
elements=[
('manuell',
MonitoringState(
title=_('Monitoring state'),
default_value=0,
)),
]),
Dictionary(
title=_('Automatic'),
optional_keys=False,
elements=[
('automatic',
MonitoringState(
title=_('Monitoring state'),
default_value=0,
))
]),
]
)),
],
)
rulespec_registry.register(
CheckParameterRulespecWithoutItem(
check_group_name="fritzbox_smarthome_power_socket_single",
group=RulespecGroupCheckParametersApplications,
match_type="dict",
parameter_valuespec=_parameter_valuespec_fritzbox_smarthome_power_socket,
title=lambda: _('Fritz!Box Smarthome Power socket')
)
)
rulespec_registry.register(
CheckParameterRulespecWithItem(
check_group_name="fritzbox_smarthome_power_socket_multiple",
group=RulespecGroupCheckParametersApplications,
match_type="dict",
parameter_valuespec=_parameter_valuespec_fritzbox_smarthome_power_socket,
title=lambda: _('Fritz!Box Smarthome Power Socket (with Device-ID)'),
item_spec=lambda: TextInput(title=_('Device-ID')),
)
)
...@@ -44,7 +44,8 @@ ...@@ -44,7 +44,8 @@
'metrics/fritzbox_smarthome.py', 'metrics/fritzbox_smarthome.py',
'wato/check_parameters/temperature_single.py', 'wato/check_parameters/temperature_single.py',
'wato/check_parameters/voltage_single.py', 'wato/check_parameters/voltage_single.py',
'wato/check_parameters/fritzbox_smarthome_lock.py'], 'wato/check_parameters/fritzbox_smarthome_lock.py',
'wato/check_parameters/fritzbox_smarthome_power_coscket.py'],
'lib': ['python3/cmk/special_agents/agent_fritzbox_smarthome.py'], 'lib': ['python3/cmk/special_agents/agent_fritzbox_smarthome.py'],
'web': ['plugins/wato/agent_fritzbox_smarthome.py', 'web': ['plugins/wato/agent_fritzbox_smarthome.py',
'plugins/views/fritzbox_smarthome.py']}, 'plugins/views/fritzbox_smarthome.py']},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment