P1-Meter.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #
  10. # The commandline to be used in CheckMK is : this_script_name.py $HOSTADDRESS$
  11. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  12. # License: GNU General Public License v2
  13. import sys
  14. import json
  15. import requests
  16. def main():
  17. args = sys.argv[1:]
  18. if len(args) < 1:
  19. print("Usage: script.py IP or FQDN")
  20. sys.exit(1)
  21. address = args[0]
  22. try:
  23. response = requests.get('http://{}/api/v1/data'.format(address))
  24. except requests.exceptions.RequestException as e:
  25. raise SystemExit(e)
  26. parsed = response.json()
  27. print('<<<local>>>')
  28. iterate_json(parsed)
  29. # Using For Loop to iterate thru the data offered by the P1-API
  30. # Threshold values are tuned to my own setup, they might need ajustment for others.
  31. def iterate_json(json_obj):
  32. for key, value in json_obj.items():
  33. if isinstance(value, dict):
  34. iterate_json(value)
  35. else:
  36. if key.startswith("active"):
  37. if key.endswith("_w"):
  38. print(f"P {key} Power={value};2100;2200;2300;0;2500 Current Watts:{value}")
  39. if key.endswith("_v"):
  40. print(f"P {key} volt={value};252;253;260;0;300 Current Volts:{value}")
  41. if key.endswith("_a"):
  42. print(f"P {key} amps={value};8;10;15;0;20 Current Amps:{value}")
  43. if key.endswith("tariff"):
  44. print(f"P {key} tariff={value};;;;; Current Tariff:{value}")
  45. # Additional information is available, but not at current needed
  46. # if key.startswith("external"):
  47. # continue
  48. # else:
  49. # print(f"0 {key} - {value}")
  50. if __name__ == "__main__":
  51. main()