diff options
author | Guido Günther <agx@sigxcpu.org> | 2022-12-05 17:20:05 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2022-12-05 17:20:05 +0100 |
commit | 49fcb403ed11b33810d5598f44a952db0ca69af5 (patch) | |
tree | 4a6d3e9832105da831c4c9d7af65f806e7b98ebb | |
parent | 089765b3e631c328240fe5d2812ae33f8ce93e04 (diff) |
Port switches to python3
-rwxr-xr-x | pellematic-switches | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/pellematic-switches b/pellematic-switches index 0b4ab4a..e699e3c 100755 --- a/pellematic-switches +++ b/pellematic-switches @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # vim: set fileencoding=utf-8 : # # Munin plugin to monitor the switches in the pellematic @@ -63,7 +63,7 @@ def canon(name): return re.sub(r"[^a-zA-Z0-9_]", "_", name) def get_name(item): - if item['name'] in items.keys(): + if item['name'] in list(items.keys()): if item['shortText'] != '???': return item['shortText'] else: @@ -72,11 +72,11 @@ def get_name(item): return None def print_config(url): - print """graph_title Pellematic Switches + print("""graph_title Pellematic Switches graph_vlabel On (1) or Off (0) graph_category Heating graph_info This graph shows the values for different on/off switches -""" +""") names = [] draw = "AREA" out = fetch_raw(url) @@ -86,14 +86,14 @@ graph_info This graph shows the values for different on/off switches names.append(name) for name in sorted(names): - print "%s.label %s" % (canon(name), name) - print "%s.type GAUGE" % canon(name) - print "%s.draw %s" % (canon(name), draw) + print("%s.label %s" % (canon(name), name)) + print("%s.type GAUGE" % canon(name)) + print("%s.draw %s" % (canon(name), draw)) if draw == "AREA": draw = "STACK" if 'Malfunction' in name: - print "%s.warning 0.1" % canon(name) - print "%s.critical 1" % canon(name) + print("%s.warning 0.1" % canon(name)) + print("%s.critical 1" % canon(name)) def fetch_raw(url): # Perform authentication and get the cookie @@ -120,20 +120,15 @@ def fetch_raw(url): } # The items to fetch are passed as simple json string - payload = json.dumps(items.keys()) + payload = json.dumps(list(items.keys())) r = requests.post(url, data=payload, params=params, cookies=cookies, headers=headers) - - if hasattr(r.json, '__call__'): - ret = r.json() - else: # in requests 0.12 json isn't a callable - ret = r.json - + ret = r.json() if debug: - print >>sys.stderr, ret + print(ret, file=sys.stderr) return ret @@ -143,34 +138,34 @@ def fetch_values(url): for item in out: name = get_name(item) if name: - print "%s.value %s" % (canon(name), item['value']) + print("%s.value %s" % (canon(name), item['value'])) def main(args): if not url: - print >>sys.stderr, "No url configured" + print("No url configured", file=sys.stderr) return 1 if len(args) > 1: if args[1] in [ 'autoconf', 'detect' ]: try: fetch_raw(url) - print "yes" + print("yes") return 0 except: - print "no" + print("no") return 1 elif args[1] == 'config': try: print_config(url) except Exception as e: raise - print >>sys.stderr, "Failed to fetch config: '%s'" % e + print("Failed to fetch config: '%s'" % e, file=sys.stderr) return 1 return 0 try: fetch_values(url) except Exception as e: - print >>sys.stderr, "Failed to fetch values: '%s'" % e + print("Failed to fetch values: '%s'" % e, file=sys.stderr) raise return 1 return 0 |