Compare commits

..

No commits in common. "645bc06a8e7d4236eac98f2ce4d9cbc6c5014332" and "1b8653a6d1ba434da0b16e55be1f2ee72f20fc89" have entirely different histories.

2 changed files with 53 additions and 66 deletions

View File

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