Browse Source

Update 'datasources/P1-Meter.py'

Michael Honkoop 3 months ago
parent
commit
35053155ce
1 changed files with 19 additions and 8 deletions
  1. 19 8
      datasources/P1-Meter.py

+ 19 - 8
datasources/P1-Meter.py

@@ -1,6 +1,5 @@
 #!/usr/bin/env python3
 
-# This file should be placed in ~/local/bin/ and used for a monitoring host that has a/the IP of the device
 # 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
 #
@@ -10,9 +9,21 @@ import sys
 import json
 import requests
 
-address = sys.argv[1]
-response = requests.get('http://{}/api/v1/data'.format(address))
-parsed = response.json()
+def main():
+    args = sys.argv[1:]
+    if len(args) < 1:
+        print("Usage: script.py IP or FQDN")
+        sys.exit(1)
+
+    address = args[0]
+    try:
+        response = requests.get('http://{}/api/v1/data'.format(address))
+    except requests.exceptions.RequestException as e:
+        raise SystemExit(e)
+
+    parsed = response.json()
+    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.
@@ -24,7 +35,7 @@ def iterate_json(json_obj):
         else:
             if key.startswith("active"):
                 if key.endswith("_w"):
-                    print(f"P {key} watt={value};2100;2200;2300;0;2500 Current Watts:{value}")
+                    print(f"P {key} Power={value};2100;2200;2300;0;2500 Current Watts:{value}")
                 if key.endswith("_v"):
                     print(f"P {key} volt={value};252;253;260;0;300 Current Volts:{value}")
                 if key.endswith("_a"):
@@ -32,11 +43,11 @@ def iterate_json(json_obj):
                 if key.endswith("tariff"):
                     print(f"P {key} tariff={value};;;;; Current Tariff:{value}")
 
-# Additional information is available, but not at current needed in my case
+# Additional information is available, but not at current needed
 #            if key.startswith("external"):
 #                continue
 #            else:
 #                print(f"0 {key} - {value}")
 
-print('<<<local>>>')
-iterate_json(parsed)
+if __name__ == "__main__":
+    main()