1
0

P1-Meter.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/usr/bin/env python3
  2. # This file should be placed in ~/local/bin/ and used for a monitoring host that has a/the IP of the device
  3. # in combination with a rule for "individual program call instead of agent" as described on the official docs of CheckMK:
  4. # https://docs.checkmk.com/latest/en/datasource_programs.html
  5. #
  6. # The commandline to be used in CheckMK is : this_script_name.py $HOSTADDRESS$
  7. import sys
  8. import json
  9. import requests
  10. address = sys.argv[1]
  11. response = requests.get('http://{}/api/v1/data'.format(address))
  12. parsed = response.json()
  13. # Using For Loop to iterate thru the data offered by the P1-API
  14. # Threshold values are tuned to my own setup, they might need ajustment for others.
  15. def iterate_json(json_obj):
  16. for key, value in json_obj.items():
  17. if isinstance(value, dict):
  18. iterate_json(value)
  19. else:
  20. if key.startswith("active"):
  21. if key.endswith("_w"):
  22. print(f"P {key} watt={value};2100;2200;2300;0;2500 Current Watts:{value}")
  23. if key.endswith("_v"):
  24. print(f"P {key} volt={value};252;253;260;0;300 Current Volts:{value}")
  25. if key.endswith("_a"):
  26. print(f"P {key} amps={value};8;10;15;0;20 Current Amps:{value}")
  27. if key.endswith("tariff"):
  28. print(f"P {key} tariff={value};;;;; Current Tariff:{value}")
  29. # Additional information is available, but not at current needed in my case
  30. # if key.startswith("external"):
  31. # continue
  32. # else:
  33. # print(f"0 {key} - {value}")
  34. print('<<<local>>>')
  35. iterate_json(parsed)