P1-Meter.py 1.9 KB

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