Add cli arg for filtering by arbitrary attributes
This commit is contained in:
parent
3ddb67f753
commit
5538de57bd
@ -19,6 +19,7 @@ class Entities(nagiosplugin.Resource):
|
|||||||
device_class: str
|
device_class: str
|
||||||
min: float
|
min: float
|
||||||
max: float
|
max: float
|
||||||
|
filters: list[str]
|
||||||
|
|
||||||
def hass_get(self, endpoint: str) -> requests.Response:
|
def hass_get(self, endpoint: str) -> requests.Response:
|
||||||
headers = {
|
headers = {
|
||||||
@ -43,7 +44,9 @@ 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") == 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":
|
if state["state"] == "unavailable":
|
||||||
_log.info(f"{state['entity_id']} unavailable")
|
_log.info(f"{state['entity_id']} unavailable")
|
||||||
yield nagiosplugin.Metric(
|
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
|
@nagiosplugin.guarded
|
||||||
def main():
|
def main():
|
||||||
argp = argparse.ArgumentParser(description=__doc__)
|
argp = argparse.ArgumentParser(description=__doc__)
|
||||||
@ -98,6 +106,14 @@ def main():
|
|||||||
type=float,
|
type=float,
|
||||||
help="Max for performance data",
|
help="Max for performance data",
|
||||||
)
|
)
|
||||||
|
argp.add_argument(
|
||||||
|
"-f",
|
||||||
|
"--filter",
|
||||||
|
type=key_value,
|
||||||
|
default=[],
|
||||||
|
nargs="*",
|
||||||
|
help="Filter by 'attribute=value'",
|
||||||
|
)
|
||||||
|
|
||||||
args = argp.parse_args()
|
args = argp.parse_args()
|
||||||
|
|
||||||
@ -108,6 +124,7 @@ def main():
|
|||||||
args.device_class,
|
args.device_class,
|
||||||
args.min,
|
args.min,
|
||||||
args.max,
|
args.max,
|
||||||
|
args.filter,
|
||||||
),
|
),
|
||||||
nagiosplugin.ScalarContext(args.device_class, args.warning, args.critical),
|
nagiosplugin.ScalarContext(args.device_class, args.warning, args.critical),
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user