| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #!/usr/bin/env python3
- # -*- mode: Python; encoding: utf-8; indent-offset: 4; autowrap: nil -*-
- # (c) Michael Honkoop <mhonkoop@comsolve.nl>
- # License: GNU General Public License v2
- # Version 1.0
- # Initial release of this check
- # in combination with a rule for "individual program call instead of agent" as described on the official docs of CheckMK:
- # https://docs.checkmk.com/latest/en/datasource_programs.html
- # The (this) code should be placed (as cmk user) in ~/local/bin, and made executable (chmod +x filename)
- #
- # The commandline to be used in CheckMK is : this_script_name.py $HOSTADDRESS$
- # (c) Michael Honkoop <mhonkoop@comsolve.nl>
- # License: GNU General Public License v2
- import sys
- import requests
- API_PATH = "/api/v1/data"
- TIMEOUT = 10
- def main():
- if len(sys.argv) < 2:
- print("Usage: script.py IP_or_FQDN")
- sys.exit(1)
- address = sys.argv[1]
- try:
- response = requests.get(
- f"http://{address}{API_PATH}",
- timeout=TIMEOUT,
- )
- response.raise_for_status()
- parsed = response.json()
- except requests.exceptions.RequestException as exc:
- raise SystemExit(f"API request failed: {exc}")
- except ValueError as exc:
- raise SystemExit(f"API returned invalid JSON: {exc}")
- print("<<<local>>>")
- iterate_json(parsed)
- # Using For Loop to iterate thru the data offered by the P1-API
- # Threshold values are tuned to my own setup, they might need ajustment for others.
- def iterate_json(json_obj):
- for key, value in json_obj.items():
- if isinstance(value, dict):
- iterate_json(value)
- else:
- if key.startswith("active"):
- if key.endswith("_w"):
- print(f"P {key} Power={value};2100;2200;0;2500 Current Watts:{value}")
- if key.endswith("_v"):
- print(f"P {key} volt={value};252;253;0;300 Current Volts:{value}")
- if key.endswith("_a"):
- print(f"P {key} amps={value};8;10;0;20 Current Amps:{value}")
- if key.endswith("tariff"):
- print(f"P {key} tariff={value};;;; Current Tariff:{value}")
- # Additional information is available, but not at current needed
- # if key.startswith("external"):
- # continue
- # else:
- # print(f"0 {key} - {value}")
- if __name__ == "__main__":
- main()
|