Automatically generate Icinga2 CheckCommand conf

This commit is contained in:
Adam Goldsmith 2022-11-29 17:54:54 -05:00
parent 1b8653a6d1
commit f886e9afeb
2 changed files with 52 additions and 45 deletions

View File

@ -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"
}
}
}

View File

@ -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",