P1-Meter.py 1.8 KB

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