Add cli arg for filtering by arbitrary attributes
This commit is contained in:
parent
efd40f2a51
commit
8d88430577
@ -29,5 +29,9 @@ object CheckCommand "home_assistant_state" {
|
||||
value = "$home_assistant_state_max$"
|
||||
description = "Max for performance data"
|
||||
}
|
||||
"--filter" = {
|
||||
value = "$home_assistant_state_filter$"
|
||||
description = "Filter by 'attribute=value' (can be an array)"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ class Entities(nagiosplugin.Resource):
|
||||
device_class: str
|
||||
min: float
|
||||
max: float
|
||||
filters: list[str]
|
||||
|
||||
def hass_get(self, endpoint: str) -> requests.Response:
|
||||
headers = {
|
||||
@ -43,7 +44,9 @@ class Entities(nagiosplugin.Resource):
|
||||
def probe(self):
|
||||
response = self.hass_get("/api/states")
|
||||
for state in response:
|
||||
if state["attributes"].get("device_class") == self.device_class:
|
||||
if state["attributes"].get("device_class") == self.device_class and all(
|
||||
state["attributes"].get(k) == v for k, v in self.filters
|
||||
):
|
||||
if state["state"] == "unavailable":
|
||||
_log.info(f"{state['entity_id']} unavailable")
|
||||
yield nagiosplugin.Metric(
|
||||
@ -56,6 +59,11 @@ class Entities(nagiosplugin.Resource):
|
||||
)
|
||||
|
||||
|
||||
def key_value(arg: str):
|
||||
k, _, v = arg.partition("=")
|
||||
return k, v
|
||||
|
||||
|
||||
@nagiosplugin.guarded
|
||||
def main():
|
||||
argp = argparse.ArgumentParser(description=__doc__)
|
||||
@ -98,6 +106,14 @@ def main():
|
||||
type=float,
|
||||
help="Max for performance data",
|
||||
)
|
||||
argp.add_argument(
|
||||
"-f",
|
||||
"--filter",
|
||||
type=key_value,
|
||||
default=[],
|
||||
nargs="*",
|
||||
help="Filter by 'attribute=value'",
|
||||
)
|
||||
|
||||
args = argp.parse_args()
|
||||
|
||||
@ -108,6 +124,7 @@ def main():
|
||||
args.device_class,
|
||||
args.min,
|
||||
args.max,
|
||||
args.filter,
|
||||
),
|
||||
nagiosplugin.ScalarContext(args.device_class, args.warning, args.critical),
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user