Add cli args for specifying performance data min/max

This commit is contained in:
Adam Goldsmith 2022-11-26 19:40:57 -05:00
parent 1c183fb6b4
commit 5eb9c52207
2 changed files with 29 additions and 4 deletions

View File

@ -17,5 +17,13 @@ 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"
}
} }
} }

View File

@ -16,6 +16,8 @@ _log = logging.getLogger("nagiosplugin")
class Entities(nagiosplugin.Resource): class Entities(nagiosplugin.Resource):
url: str url: str
token: str token: str
min: float
max: float
def hass_get(self, endpoint: str) -> requests.Response: def hass_get(self, endpoint: str) -> requests.Response:
headers = { headers = {
@ -47,9 +49,9 @@ class Entities(nagiosplugin.Resource):
state["entity_id"], state["entity_id"],
float(state["state"]) if state["state"].isnumeric() else -1, float(state["state"]) if state["state"].isnumeric() else -1,
uom=state["attributes"].get("unit_of_measurement"), uom=state["attributes"].get("unit_of_measurement"),
min=0,
max=100,
context="battery", context="battery",
min=self.min,
max=self.max,
) )
@ -78,12 +80,27 @@ 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",
)
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.min,
args.max,
),
nagiosplugin.ScalarContext(args.device_class, args.warning, args.critical),
) )
check.main(args.verbose) check.main(args.verbose)