Compare commits
4 Commits
b6d0964d4c
...
b3fd15789a
Author | SHA1 | Date | |
---|---|---|---|
b3fd15789a | |||
8d88430577 | |||
efd40f2a51 | |||
5eb9c52207 |
@ -9,6 +9,10 @@ object CheckCommand "home_assistant_state" {
|
|||||||
value = "$home_assistant_state_url$"
|
value = "$home_assistant_state_url$"
|
||||||
description = "URL for Home Assistant"
|
description = "URL for Home Assistant"
|
||||||
}
|
}
|
||||||
|
"--device-class" = {
|
||||||
|
value = "$home_assistant_state_device_class$"
|
||||||
|
description = "Device class of entities to monitor"
|
||||||
|
}
|
||||||
"--warning" = {
|
"--warning" = {
|
||||||
value = "$home_assistant_state_warning$"
|
value = "$home_assistant_state_warning$"
|
||||||
description = "Return warning if battery percentage is outside RANGE"
|
description = "Return warning if battery percentage is outside RANGE"
|
||||||
@ -17,5 +21,21 @@ object CheckCommand "home_assistant_state" {
|
|||||||
value = "$home_assistant_state_critical$"
|
value = "$home_assistant_state_critical$"
|
||||||
description = "Return critical if battery percentage is outside RANGE"
|
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)"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,6 +5,7 @@ import dataclasses
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import nagiosplugin
|
import nagiosplugin
|
||||||
@ -16,6 +17,11 @@ _log = logging.getLogger("nagiosplugin")
|
|||||||
class Entities(nagiosplugin.Resource):
|
class Entities(nagiosplugin.Resource):
|
||||||
url: str
|
url: str
|
||||||
token: str
|
token: str
|
||||||
|
device_class: str
|
||||||
|
min: float
|
||||||
|
max: float
|
||||||
|
filters: list[str]
|
||||||
|
attribute: Optional[str]
|
||||||
|
|
||||||
def hass_get(self, endpoint: str) -> requests.Response:
|
def hass_get(self, endpoint: str) -> requests.Response:
|
||||||
headers = {
|
headers = {
|
||||||
@ -40,19 +46,33 @@ class Entities(nagiosplugin.Resource):
|
|||||||
def probe(self):
|
def probe(self):
|
||||||
response = self.hass_get("/api/states")
|
response = self.hass_get("/api/states")
|
||||||
for state in response:
|
for state in response:
|
||||||
if state["attributes"].get("device_class") == "battery":
|
if state["attributes"].get("device_class") == self.device_class and all(
|
||||||
|
state["attributes"].get(k) == v for k, v in self.filters
|
||||||
|
):
|
||||||
|
if self.attribute is not None:
|
||||||
|
value = state["attributes"].get(self.attribute, -1)
|
||||||
|
uom = None
|
||||||
|
else:
|
||||||
|
value = float(state["state"]) if state["state"].isnumeric() else -1
|
||||||
|
uom = state["attributes"].get("unit_of_measurement")
|
||||||
|
|
||||||
if state["state"] == "unavailable":
|
if state["state"] == "unavailable":
|
||||||
_log.info(f"{state['entity_id']} unavailable")
|
_log.info(f"{state['entity_id']} unavailable")
|
||||||
yield nagiosplugin.Metric(
|
yield nagiosplugin.Metric(
|
||||||
state["entity_id"],
|
state["entity_id"],
|
||||||
float(state["state"]) if state["state"].isnumeric() else -1,
|
value,
|
||||||
uom=state["attributes"].get("unit_of_measurement"),
|
uom,
|
||||||
min=0,
|
context=self.device_class,
|
||||||
max=100,
|
min=self.min,
|
||||||
context="battery",
|
max=self.max,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def key_value(arg: str):
|
||||||
|
k, _, v = arg.partition("=")
|
||||||
|
return k, v
|
||||||
|
|
||||||
|
|
||||||
@nagiosplugin.guarded
|
@nagiosplugin.guarded
|
||||||
def main():
|
def main():
|
||||||
argp = argparse.ArgumentParser(description=__doc__)
|
argp = argparse.ArgumentParser(description=__doc__)
|
||||||
@ -64,6 +84,13 @@ def main():
|
|||||||
)
|
)
|
||||||
argp.add_argument("-v", "--verbose", action="count", default=0)
|
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",
|
||||||
|
)
|
||||||
argp.add_argument(
|
argp.add_argument(
|
||||||
"-w",
|
"-w",
|
||||||
"--warning",
|
"--warning",
|
||||||
@ -78,12 +105,41 @@ def main():
|
|||||||
required=True,
|
required=True,
|
||||||
help="return critical if battery percentage is outside RANGE",
|
help="return critical if battery percentage is outside RANGE",
|
||||||
)
|
)
|
||||||
|
argp.add_argument(
|
||||||
|
"--min",
|
||||||
|
type=float,
|
||||||
|
help="Min for performance data",
|
||||||
|
)
|
||||||
|
argp.add_argument(
|
||||||
|
"--max",
|
||||||
|
type=float,
|
||||||
|
help="Max for performance data",
|
||||||
|
)
|
||||||
|
argp.add_argument(
|
||||||
|
"-a", "--attribute", type=str, help="Check attribute instead of value"
|
||||||
|
)
|
||||||
|
argp.add_argument(
|
||||||
|
"-f",
|
||||||
|
"--filter",
|
||||||
|
type=key_value,
|
||||||
|
default=[],
|
||||||
|
nargs="*",
|
||||||
|
help="Filter by 'attribute=value'",
|
||||||
|
)
|
||||||
|
|
||||||
args = argp.parse_args()
|
args = argp.parse_args()
|
||||||
|
|
||||||
check = nagiosplugin.Check(
|
check = nagiosplugin.Check(
|
||||||
Entities(args.url, args.token),
|
Entities(
|
||||||
nagiosplugin.ScalarContext("battery", args.warning, args.critical),
|
args.url,
|
||||||
|
args.token,
|
||||||
|
args.device_class,
|
||||||
|
args.min,
|
||||||
|
args.max,
|
||||||
|
args.filter,
|
||||||
|
args.attribute,
|
||||||
|
),
|
||||||
|
nagiosplugin.ScalarContext(args.device_class, args.warning, args.critical),
|
||||||
)
|
)
|
||||||
check.main(args.verbose)
|
check.main(args.verbose)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user