77 lines
2.4 KiB
Plaintext
77 lines
2.4 KiB
Plaintext
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
"""
|
||
|
Author: Ruben Bueno (2024-04-29)
|
||
|
Modified by: Juan Ferrer (2024-09-03)
|
||
|
"""
|
||
|
|
||
|
import sys
|
||
|
import requests
|
||
|
import xml.etree.ElementTree as ET
|
||
|
|
||
|
def papago_check_value(host, name, attribute, warning, critical):
|
||
|
try:
|
||
|
match attribute:
|
||
|
case "temp":
|
||
|
xml_attribute = "val"
|
||
|
unit_name = "Temperature"
|
||
|
unit = "ºC"
|
||
|
case "hd":
|
||
|
xml_attribute = "val2"
|
||
|
unit_name = "Humidity"
|
||
|
unit = "%"
|
||
|
case "cond":
|
||
|
xml_attribute = "val3"
|
||
|
unit_name = "Condensation"
|
||
|
unit = "ºC"
|
||
|
case _:
|
||
|
raise ValueError(f"Unknown passed attribute '{attribute}'.")
|
||
|
|
||
|
response = requests.get(f"http://{host}/fresh.xml")
|
||
|
|
||
|
if response.status_code != 200:
|
||
|
raise ValueError("Failed to get device XML.")
|
||
|
|
||
|
root = ET.fromstring(response.text)
|
||
|
status = root.find(".//status")
|
||
|
|
||
|
if status is None:
|
||
|
raise ValueError("The 'status' element was not found on XML.")
|
||
|
|
||
|
location = status.get('location', 'Unknown')
|
||
|
data = root.find(f".//sns[@name='{name}']")
|
||
|
|
||
|
if data is None:
|
||
|
raise ValueError(f"The name element '{name}' was not found on XML.")
|
||
|
|
||
|
check_value = float(data.get(xml_attribute))
|
||
|
perf_data = f"check_value={check_value} location={location}"
|
||
|
check_msg = f"{unit_name} of {check_value}{unit} in {location} {name}"
|
||
|
|
||
|
if check_value >= critical:
|
||
|
print(f"CRITICAL - {check_msg} exceeds the critical threshold of {critical}{unit} | {perf_data}")
|
||
|
sys.exit(2)
|
||
|
elif check_value >= warning:
|
||
|
print(f"WARNING - {check_msg} exceeds the warning threshold of {warning}{unit} | {perf_data}")
|
||
|
sys.exit(1)
|
||
|
else:
|
||
|
print(f"OK - {check_msg} | {perf_data}")
|
||
|
sys.exit(0)
|
||
|
|
||
|
except Exception as e:
|
||
|
print(f"UNKNOWN - Error: {e}")
|
||
|
sys.exit(3)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
if len(sys.argv) != 6:
|
||
|
print("Usage: check_papago <host> <name> <temp|cond|hd> <warning> <critical>")
|
||
|
sys.exit(3)
|
||
|
|
||
|
host = sys.argv[1]
|
||
|
name = sys.argv[2]
|
||
|
attribute = sys.argv[3]
|
||
|
warning = float(sys.argv[4])
|
||
|
critical = float(sys.argv[5])
|
||
|
papago_check_value(host, name, attribute, warning, critical)
|