From f14ecaa4b3e7bbba7cbbf883e1155868c7454892 Mon Sep 17 00:00:00 2001 From: Olivier Mehani Date: Tue, 24 Sep 2019 22:22:36 +1000 Subject: [PATCH] Python3 compat and cleanup Signed-off-by: Olivier Mehani Show exception message on error Signed-off-by: Olivier Mehani (cherry picked from commit 98d41e311c3c04f9ea87fc1e93104be8ef503762) --- fritzbox_connection_uptime.py | 12 ++++++------ fritzbox_cpu_temperature.py | 10 +++++----- fritzbox_cpu_usage.py | 14 +++++++------- fritzbox_memory_usage.py | 14 +++++++------- fritzbox_power_consumption.py | 14 +++++++------- fritzbox_traffic.py | 12 ++++++------ fritzbox_uptime.py | 14 +++++++------- fritzbox_wifi_devices.py | 14 +++++++------- 8 files changed, 52 insertions(+), 52 deletions(-) diff --git a/fritzbox_connection_uptime.py b/fritzbox_connection_uptime.py index 5742610..40af873 100755 --- a/fritzbox_connection_uptime.py +++ b/fritzbox_connection_uptime.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ fritzbox_connection_uptime - A munin plugin for Linux to monitor AVM Fritzbox connection uptime Copyright (C) 2015 Christian Stade-Schuldt @@ -22,9 +22,9 @@ from fritzconnection import FritzConnection def print_values(): try: - conn = FritzConnection(address=os.environ['fritzbox_ip']) + conn = FritzConnection(address=os.getenv('fritzbox_ip')) except Exception as e: - sys.exit("Couldn't get connection uptime") + sys.exit(f"Couldn't get connection uptime: {e}") uptime = conn.call_action('WANIPConnection', 'GetStatusInfo')['NewUptime'] print('uptime.value %.2f' % (int(uptime) / 86400.0)) @@ -39,7 +39,7 @@ def print_config(): print("uptime.label uptime") print("uptime.draw AREA") if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) + print("host_name " + os.getenv('host_name')) if __name__ == "__main__": @@ -50,5 +50,5 @@ if __name__ == "__main__": elif len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'): try: print_values() - except: - sys.exit("Couldn't retrieve fritzbox connection uptime") + except Exception as e: + sys.exit(f"Couldn't retrieve fritzbox connection uptime: {e}") diff --git a/fritzbox_cpu_temperature.py b/fritzbox_cpu_temperature.py index 8b9ffaa..770d4d1 100755 --- a/fritzbox_cpu_temperature.py +++ b/fritzbox_cpu_temperature.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ fritzbox_cpu_temperature - A munin plugin for Linux to monitor AVM Fritzbox Copyright (C) 2015 Christian Stade-Schuldt @@ -27,11 +27,11 @@ PAGE = 'ecoStat' def get_cpu_temperature(): """get the current cpu temperature""" - server = os.environ['fritzbox_ip'] - password = os.environ['FRITZ_PASSWORD'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') if "FRITZ_USERNAME" in os.environ: - fritzuser = os.environ['FRITZ_USERNAME'] + fritzuser = os.getenv('FRITZ_USERNAME') session_id = fh.get_session_id(server, password, fritzuser) else: session_id = fh.get_session_id(server, password) @@ -52,7 +52,7 @@ def print_config(): print("temp.min 0") print("temp.info Fritzbox CPU temperature") if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) + print("host_name " + os.getenv('host_name')) if __name__ == '__main__': diff --git a/fritzbox_cpu_usage.py b/fritzbox_cpu_usage.py index bf4cea1..7797b9d 100755 --- a/fritzbox_cpu_usage.py +++ b/fritzbox_cpu_usage.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ fritzbox_cpu_usage - A munin plugin for Linux to monitor AVM Fritzbox Copyright (C) 2015 Christian Stade-Schuldt @@ -27,11 +27,11 @@ PAGE = 'ecoStat' def get_cpu_usage(): """get the current cpu usage""" - server = os.environ['fritzbox_ip'] - password = os.environ['FRITZ_PASSWORD'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') if "FRITZ_USERNAME" in os.environ: - fritzuser = os.environ['FRITZ_USERNAME'] + fritzuser = os.getenv('FRITZ_USERNAME') session_id = fh.get_session_id(server, password, fritzuser) else: session_id = fh.get_session_id(server, password) @@ -52,7 +52,7 @@ def print_config(): print("cpu.min 0") print("cpu.info Fritzbox CPU usage") if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) + print("host_name " + os.getenv('host_name')) if __name__ == '__main__': @@ -64,5 +64,5 @@ if __name__ == '__main__': # Some docs say it'll be called with fetch, some say no arg at all try: get_cpu_usage() - except: - sys.exit("Couldn't retrieve fritzbox cpu usage") + except Exception as e: + sys.exit(f"Couldn't retrieve fritzbox cpu usage: {e}") diff --git a/fritzbox_memory_usage.py b/fritzbox_memory_usage.py index 8a26729..513d03d 100755 --- a/fritzbox_memory_usage.py +++ b/fritzbox_memory_usage.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ fritzbox_memory_usage - A munin plugin for Linux to monitor AVM Fritzbox Copyright (C) 2015 Christian Stade-Schuldt @@ -28,11 +28,11 @@ USAGE = ['strict', 'cache', 'free'] def get_memory_usage(): """get the current memory usage""" - server = os.environ['fritzbox_ip'] - password = os.environ['FRITZ_PASSWORD'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') if "FRITZ_USERNAME" in os.environ: - fritzuser = os.environ['FRITZ_USERNAME'] + fritzuser = os.getenv('FRITZ_USERNAME') session_id = fh.get_session_id(server, password, fritzuser) else: session_id = fh.get_session_id(server, password) @@ -60,7 +60,7 @@ def print_config(): print("free.type GAUGE") print("free.draw STACK") if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) + print("host_name " + os.getenv('host_name')) if __name__ == '__main__': @@ -72,5 +72,5 @@ if __name__ == '__main__': # Some docs say it'll be called with fetch, some say no arg at all try: get_memory_usage() - except: - sys.exit("Couldn't retrieve fritzbox memory usage") + except Exception as e: + sys.exit(f"Couldn't retrieve fritzbox memory usage: {e}") diff --git a/fritzbox_power_consumption.py b/fritzbox_power_consumption.py index 57b980c..ee18c3d 100755 --- a/fritzbox_power_consumption.py +++ b/fritzbox_power_consumption.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # coding=utf-8 """ fritzbox_power_consumption - A munin plugin for Linux to monitor AVM Fritzbox @@ -30,11 +30,11 @@ DEVICES = ['system', 'cpu', 'wifi', 'dsl', 'ab', 'usb'] def get_power_consumption(): """get the current power consumption usage""" - server = os.environ['fritzbox_ip'] - password = os.environ['FRITZ_PASSWORD'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') if "FRITZ_USERNAME" in os.environ: - fritzuser = os.environ['FRITZ_USERNAME'] + fritzuser = os.getenv('FRITZ_USERNAME') session_id = fh.get_session_id(server, password, fritzuser) else: session_id = fh.get_session_id(server, password) @@ -87,7 +87,7 @@ def print_config(): print("usb.max 100") print("usb.info Fritzbox usb devices power consumption") if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) + print("host_name " + os.getenv('host_name')) if __name__ == '__main__': @@ -99,5 +99,5 @@ if __name__ == '__main__': # Some docs say it'll be called with fetch, some say no arg at all try: get_power_consumption() - except: - sys.exit("Couldn't retrieve fritzbox power consumption") + except Exception as e: + sys.exit(f"Couldn't retrieve fritzbox power consumption: {e}") diff --git a/fritzbox_traffic.py b/fritzbox_traffic.py index 25ccb24..40d0c3a 100755 --- a/fritzbox_traffic.py +++ b/fritzbox_traffic.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ fritzbox_traffic - A munin plugin for Linux to monitor AVM Fritzbox WAN traffic Copyright (C) 2015 Christian Stade-Schuldt @@ -22,9 +22,9 @@ from fritzconnection import FritzConnection def print_values(): try: - conn = FritzConnection(address=os.environ['fritzbox_ip']) + conn = FritzConnection(address=os.getenv('fritzbox_ip')) except Exception as e: - sys.exit("Couldn't get WAN traffic") + sys.exit(f"Couldn't get WAN traffic: {e}") down_traffic = conn.call_action('WANCommonInterfaceConfig', 'GetTotalBytesReceived')['NewTotalBytesReceived'] print('down.value %d' % down_traffic) @@ -72,7 +72,7 @@ def print_config(): print("maxup.draw LINE1") print("maxup.info Maximum speed of the WAN interface.") if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) + print("host_name " + os.getenv('host_name')) if __name__ == "__main__": @@ -83,5 +83,5 @@ if __name__ == "__main__": elif len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == 'fetch'): try: print_values() - except: - sys.exit("Couldn't retrieve fritzbox traffic") + except Exception as e: + sys.exit(f"Couldn't retrieve fritzbox traffic: {e}") diff --git a/fritzbox_uptime.py b/fritzbox_uptime.py index 0246719..384c8e6 100755 --- a/fritzbox_uptime.py +++ b/fritzbox_uptime.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ fritzbox_uptime - A munin plugin for Linux to monitor AVM Fritzbox Copyright (C) 2015 Christian Stade-Schuldt @@ -37,11 +37,11 @@ pattern = re.compile(patternLoc[locale]) def get_uptime(): """get the current uptime""" - server = os.environ['fritzbox_ip'] - password = os.environ['FRITZ_PASSWORD'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') if "FRITZ_USERNAME" in os.environ: - fritzuser = os.environ['FRITZ_USERNAME'] + fritzuser = os.getenv('FRITZ_USERNAME') session_id = fh.get_session_id(server, password, fritzuser) else: session_id = fh.get_session_id(server, password) @@ -72,7 +72,7 @@ def print_config(): print("uptime.label uptime") print("uptime.draw AREA") if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) + print("host_name " + os.getenv('host_name')) if __name__ == '__main__': @@ -84,5 +84,5 @@ if __name__ == '__main__': # Some docs say it'll be called with fetch, some say no arg at all try: get_uptime() - except: - sys.exit("Couldn't retrieve fritzbox uptime") + except Exception as e: + sys.exit(f"Couldn't retrieve fritzbox uptime; {e}") diff --git a/fritzbox_wifi_devices.py b/fritzbox_wifi_devices.py index 935fab9..0c1ff47 100755 --- a/fritzbox_wifi_devices.py +++ b/fritzbox_wifi_devices.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ fritzbox_wifi_devices - A munin plugin for Linux to monitor AVM Fritzbox Copyright (C) 2015 Christian Stade-Schuldt @@ -34,11 +34,11 @@ pattern = re.compile(patternLoc[locale]) def get_connected_wifi_devices(): """gets the numbrer of currently connected wifi devices""" - server = os.environ['fritzbox_ip'] - password = os.environ['FRITZ_PASSWORD'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') if "FRITZ_USERNAME" in os.environ: - fritzuser = os.environ['FRITZ_USERNAME'] + fritzuser = os.getenv('FRITZ_USERNAME') session_id = fh.get_session_id(server, password, fritzuser) else: session_id = fh.get_session_id(server, password) @@ -61,7 +61,7 @@ def print_config(): print('wifi.graph LINE1') print('wifi.info Wifi Connections on 2.4 & 5 Ghz') if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) + print("host_name " + os.getenv('host_name')) if __name__ == '__main__': @@ -73,5 +73,5 @@ if __name__ == '__main__': # Some docs say it'll be called with fetch, some say no arg at all try: get_connected_wifi_devices() - except: - sys.exit("Couldn't retrieve connected fritzbox wifi devices") + except Exception as e: + sys.exit(f"Couldn't retrieve connected fritzbox wifi devices: {e}")