P1-Meter.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 requests
  16. API_PATH = "/api/v1/data"
  17. TIMEOUT = 10
  18. def main():
  19. if len(sys.argv) < 2:
  20. print("Usage: script.py IP_or_FQDN")
  21. sys.exit(1)
  22. address = sys.argv[1]
  23. try:
  24. response = requests.get(
  25. f"http://{address}{API_PATH}",
  26. timeout=TIMEOUT,
  27. )
  28. response.raise_for_status()
  29. parsed = response.json()
  30. except requests.exceptions.RequestException as exc:
  31. raise SystemExit(f"API request failed: {exc}")
  32. except ValueError as exc:
  33. raise SystemExit(f"API returned invalid JSON: {exc}")
  34. print("<<<local>>>")
  35. iterate_json(parsed)
  36. # Using For Loop to iterate thru the data offered by the P1-API
  37. # Threshold values are tuned to my own setup, they might need ajustment for others.
  38. def iterate_json(json_obj):
  39. for key, value in json_obj.items():
  40. if isinstance(value, dict):
  41. iterate_json(value)
  42. else:
  43. if key.startswith("active"):
  44. if key.endswith("_w"):
  45. print(f"P {key} Power={value};2100;2200;0;2500 Current Watts:{value}")
  46. if key.endswith("_v"):
  47. print(f"P {key} volt={value};252;253;0;300 Current Volts:{value}")
  48. if key.endswith("_a"):
  49. print(f"P {key} amps={value};8;10;0;20 Current Amps:{value}")
  50. if key.endswith("tariff"):
  51. print(f"P {key} tariff={value};;;; Current Tariff:{value}")
  52. # Additional information is available, but not at current needed
  53. # if key.startswith("external"):
  54. # continue
  55. # else:
  56. # print(f"0 {key} - {value}")
  57. if __name__ == "__main__":
  58. main()