From f886e9afebd6462414966157c8c89ec0934667c4 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 29 Nov 2022 17:54:54 -0500 Subject: [PATCH] Automatically generate Icinga2 CheckCommand conf --- check_home_assistant_state.conf | 45 ---------------------------- check_home_assistant_state.py | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 45 deletions(-) delete mode 100644 check_home_assistant_state.conf diff --git a/check_home_assistant_state.conf b/check_home_assistant_state.conf deleted file mode 100644 index 959b4af..0000000 --- a/check_home_assistant_state.conf +++ /dev/null @@ -1,45 +0,0 @@ -object CheckCommand "home_assistant_state" { - command = [ PluginContribDir + "/check_home_assistant_state/check_home_assistant_state.py" ] - arguments = { - "--token" = { - value = "$home_assistant_state_token$" - description = "API token for Home Assistant" - } - "--url" = { - value = "$home_assistant_state_url$" - description = "URL for Home Assistant" - } - "--device-class" = { - value = "$home_assistant_state_device_class$" - description = "Device class of entities to monitor" - } - "--warning" = { - value = "$home_assistant_state_warning$" - description = "Return warning if battery percentage is outside RANGE" - } - "--critical" = { - value = "$home_assistant_state_critical$" - description = "Return critical if battery percentage is outside RANGE" - } - "--min" = { - value = "$home_assistant_state_min$" - description = "Min for performance data" - } - "--max" = { - value = "$home_assistant_state_max$" - description = "Max for performance data" - } - "--attribute" = { - value = "$home_assistant_state_attribute$" - description = "Check attribute instead of value" - } - "--filter" = { - value = "$home_assistant_state_filter$" - description = "Filter by 'attribute=value' (can be an array)" - } - "--friendly" = { - set_if = "$home_assistant_state_friendly$" - description = "Use friendly name, when available" - } - } -} diff --git a/check_home_assistant_state.py b/check_home_assistant_state.py index 51aa492..1a11a02 100755 --- a/check_home_assistant_state.py +++ b/check_home_assistant_state.py @@ -6,6 +6,7 @@ import logging import sys from urllib.parse import urljoin from typing import Optional +from pathlib import Path import requests import nagiosplugin @@ -83,6 +84,56 @@ def key_value(arg: str): return k, v +class Icinga2ConfAction(argparse.Action): + def __init__( + self, + option_strings, + dest=argparse.SUPPRESS, + default=argparse.SUPPRESS, + help="Generate conf file for icinga2 CheckCommand", + ): + super().__init__( + option_strings=option_strings, + dest=dest, + default=default, + nargs=0, + help=help, + ) + + def __call__( + self, parser: argparse.ArgumentParser, namespace, values, option_string=None + ): + filename = Path(__file__).absolute() + command_name = filename.stem.removeprefix("check_") + # FIXME: assumes that script will be in a subdirectory of PluginContribDir + command_path = filename.relative_to(filename.parents[1]) + + print(f'object CheckCommand "{command_name}" {{') + print(f' command = [ PluginContribDir + "/{command_path}" ]') + print(" arguments = {") + for action in parser._actions: + if action.dest in ["verbose", "help", self.dest]: + continue + + arg_str = action.option_strings[-1] + icinga2_var = arg_str.lstrip("-").replace("-", "_") + print(f' "{arg_str}" = {{') + + if action.required: + print(" required = true") + + if isinstance(action, argparse._StoreConstAction): + print(f' set_if = "${command_name}_{icinga2_var}$"') + else: + print(f' value = "${command_name}_{icinga2_var}$"') + + print(f' description = "{action.help}"') + print(" }") + + print(" }\n}") + parser.exit() + + @nagiosplugin.guarded def main(): argp = argparse.ArgumentParser(description=__doc__) @@ -93,6 +144,7 @@ def main(): "-u", "--url", required=True, type=str, help="URL for Home Assistant" ) argp.add_argument("-v", "--verbose", action="count", default=0) + argp.add_argument("--make-icinga-conf", action=Icinga2ConfAction) argp.add_argument( "-d",