1
0

P1-Meter.py 2.0 KB

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