P1-Meter.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. # -*- mode: Python; encoding: utf-8; indent-offset: 4; autowrap: nil -*-
  3. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  4. # License: GNU General Public License v2
  5. # Version 1.0
  6. # Initial release of this check
  7. # in combination with a rule for "individual program call instead of agent" as described on the official docs of CheckMK:
  8. # https://docs.checkmk.com/latest/en/datasource_programs.html
  9. # The (this) code should be placed (as cmk user) in ~/local/bin, and made executable (chmod +x filename)
  10. #
  11. # The commandline to be used in CheckMK is : this_script_name.py $HOSTADDRESS$
  12. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  13. # License: GNU General Public License v2
  14. import sys
  15. import json
  16. import requests
  17. API_PATH = "/api/v1/data"
  18. TIMEOUT = 10
  19. def main():
  20. if len(sys.argv) < 2:
  21. print("Usage: script.py IP_or_FQDN")
  22. sys.exit(1)
  23. address = sys.argv[1]
  24. try:
  25. response = requests.get(
  26. f"http://{address}{API_PATH}",
  27. timeout=TIMEOUT,
  28. )
  29. response.raise_for_status()
  30. parsed = response.json()
  31. except requests.exceptions.RequestException as exc:
  32. raise SystemExit(f"API request failed: {exc}")
  33. except ValueError as exc:
  34. raise SystemExit(f"API returned invalid JSON: {exc}")
  35. print("<<<local>>>")
  36. iterate_json(parsed)
  37. # Using For Loop to iterate thru the data offered by the P1-API
  38. # Threshold values are tuned to my own setup, they might need ajustment for others.
  39. def iterate_json(json_obj):
  40. for key, value in json_obj.items():
  41. if isinstance(value, dict):
  42. iterate_json(value)
  43. else:
  44. if key.startswith("active"):
  45. if key.endswith("_w"):
  46. print(f"P {key} Power={value};2100;2200;2300;0;2500 Current Watts:{value}")
  47. if key.endswith("_v"):
  48. print(f"P {key} volt={value};252;253;260;0;300 Current Volts:{value}")
  49. if key.endswith("_a"):
  50. print(f"P {key} amps={value};8;10;15;0;20 Current Amps:{value}")
  51. if key.endswith("tariff"):
  52. print(f"P {key} tariff={value};;;;; Current Tariff:{value}")
  53. # Additional information is available, but not at current needed
  54. # if key.startswith("external"):
  55. # continue
  56. # else:
  57. # print(f"0 {key} - {value}")
  58. if __name__ == "__main__":
  59. main()