Compare commits

...

4 Commits

2 changed files with 66 additions and 53 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 import sys
from urllib.parse import urljoin from urllib.parse import urljoin
from typing import Optional from typing import Optional
from pathlib import Path
import requests import requests
import nagiosplugin import nagiosplugin
@ -83,23 +84,80 @@ def key_value(arg: str):
return k, v 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 @nagiosplugin.guarded
def main(): def main():
argp = argparse.ArgumentParser(description=__doc__) argp = argparse.ArgumentParser(description=__doc__)
argp.add_argument("-v", "--verbose", action="count", default=0)
argp.add_argument("--make-icinga-conf", action=Icinga2ConfAction)
argp.add_argument( argp.add_argument(
"-t", "--token", required=True, type=str, help="API token for Home Assistant" "-t",
"--token",
required=True,
type=str,
help="token for Home Assistant REST API (see https://developers.home-assistant.io/docs/api/rest/)",
) )
argp.add_argument( argp.add_argument(
"-u", "--url", required=True, type=str, help="URL for Home Assistant" "-u", "--url", required=True, type=str, help="URL for Home Assistant"
) )
argp.add_argument("-v", "--verbose", action="count", default=0)
argp.add_argument( argp.add_argument(
"-d", "-d",
"--device-class", "--device-class",
type=str, type=str,
required=True, required=True,
help="Device class of entities to monitor", help="device class of entities to monitor",
) )
argp.add_argument( argp.add_argument(
"-w", "-w",
@ -118,15 +176,15 @@ def main():
argp.add_argument( argp.add_argument(
"--min", "--min",
type=float, type=float,
help="Min for performance data", help="min for performance data",
) )
argp.add_argument( argp.add_argument(
"--max", "--max",
type=float, type=float,
help="Max for performance data", help="max for performance data",
) )
argp.add_argument( argp.add_argument(
"-a", "--attribute", type=str, help="Check attribute instead of value" "-a", "--attribute", type=str, help="check attribute instead of value"
) )
argp.add_argument( argp.add_argument(
"-f", "-f",
@ -134,12 +192,12 @@ def main():
type=key_value, type=key_value,
default=[], default=[],
nargs="*", nargs="*",
help="Filter by 'attribute=value'", help="filter by 'attribute=value' (may be specified multiple times)",
) )
argp.add_argument( argp.add_argument(
"--friendly", "--friendly",
action="store_true", action="store_true",
help="Use friendly name, when available", help="use friendly name, when available",
) )
args = argp.parse_args() args = argp.parse_args()