diff --git a/README.md b/README.md index dc0fcfc..d569ea9 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,8 @@ If you are using the scripts on a different Fritz!Box model please let me know b [fritzbox_*] env.fritzbox_ip - env.fritzbox_password + env.FRITZ_USERNAME + env.FRITZ_PASSWORD env.traffic_remove_max true # if you do not want the possible max values host_name fritzbox diff --git a/fritzbox_connection_uptime.py b/fritzbox_connection_uptime.py index 5742610..c80f145 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,15 +22,19 @@ 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)) def print_config(): + if os.environ.get('host_name'): + print("host_name " + os.getenv('host_name')) + print("graph_title Connection Uptime") + else: print("graph_title AVM Fritz!Box Connection Uptime") print("graph_args --base 1000 -l 0") print('graph_vlabel uptime in days') @@ -38,8 +42,6 @@ def print_config(): print("graph_category network") print("uptime.label uptime") print("uptime.draw AREA") - if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) if __name__ == "__main__": @@ -50,5 +52,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 d38015a..0c42fcb 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 @@ -9,7 +9,8 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] - env.fritzbox_password [fritzbox password] + env.FRITZ_PASSWORD [fritzbox password] + env.FRITZ_USERNAME [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -26,17 +27,25 @@ PAGE = 'ecoStat' def get_cpu_temperature(): """get the current cpu temperature""" - server = os.environ['fritzbox_ip'] - password = os.environ['fritzbox_password'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') - session_id = fh.get_session_id(server, password) + if "FRITZ_USERNAME" in os.environ: + fritzuser = os.getenv('FRITZ_USERNAME') + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) xhr_data = fh.get_xhr_content(server, session_id, PAGE) data = json.loads(xhr_data) print('temp.value %d' % (int(data['data']['cputemp']['series'][0][-1]))) def print_config(): - print("graph_title AVM Fritz!Box CPU temperature") + if os.environ.get('host_name'): + print("host_name " + os.getenv('host_name')) + print("graph_title Temperatures") + else: + print("graph_title AVM Fritz!Box CPU temperature") print("graph_vlabel degrees Celsius") print("graph_category sensors") print("graph_order tmp") @@ -46,8 +55,6 @@ def print_config(): print("temp.graph LINE1") print("temp.min 0") print("temp.info Fritzbox CPU temperature") - if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) if __name__ == '__main__': diff --git a/fritzbox_cpu_usage.py b/fritzbox_cpu_usage.py index 4f56321..c331c75 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 @@ -9,7 +9,8 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] - env.fritzbox_password [fritzbox password] + env.FRITZ_PASSWORD [fritzbox password] + env.FRITZ_USERNAME [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -26,17 +27,25 @@ PAGE = 'ecoStat' def get_cpu_usage(): """get the current cpu usage""" - server = os.environ['fritzbox_ip'] - password = os.environ['fritzbox_password'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') - session_id = fh.get_session_id(server, password) + if "FRITZ_USERNAME" in os.environ: + fritzuser = os.getenv('FRITZ_USERNAME') + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) xhr_data = fh.get_xhr_content(server, session_id, PAGE) data = json.loads(xhr_data) print('cpu.value %d' % (int(data['data']['cpuutil']['series'][0][-1]))) def print_config(): - print("graph_title AVM Fritz!Box CPU usage") + if os.environ.get('host_name'): + print("host_name " + os.getenv('host_name')) + print("graph_title CPU usage") + else : + print("graph_title AVM Fritz!Box CPU usage") print("graph_vlabel %") print("graph_category system") print("graph_order cpu") @@ -46,8 +55,6 @@ def print_config(): print("cpu.graph AREA") print("cpu.min 0") print("cpu.info Fritzbox CPU usage") - if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) if __name__ == '__main__': @@ -59,5 +66,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_helper.py b/fritzbox_helper.py index 7c9fcd3..c5432f5 100755 --- a/fritzbox_helper.py +++ b/fritzbox_helper.py @@ -9,7 +9,8 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] - env.fritzbox_password [fritzbox password] + env.FRITZ_PASSWORD [fritzbox password] + env.FRITZ_USERNAME [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -29,22 +30,25 @@ from lxml import etree USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0" -def get_session_id(server, password, port=80): +def get_session_id(server, password, username=None, port=80): """Obtains the session id after login into the Fritzbox. See https://avm.de/fileadmin/user_upload/Global/Service/Schnittstellen/AVM_Technical_Note_-_Session_ID.pdf for deteils (in German). :param server: the ip address of the Fritzbox :param password: the password to log into the Fritzbox webinterface + :param username: (optional) the username with which to log into the Fritzbox webinterface :param port: the port the Fritzbox webserver runs on :return: the session id """ + userpar = '' if username is None else '?username={}'.format(username) + headers = {"Accept": "application/xml", "Content-Type": "text/plain", "User-Agent": USER_AGENT} - url = 'http://{}:{}/login_sid.lua'.format(server, port) + url = 'http://{}:{}/login_sid.lua{}'.format(server, port, userpar) try: r = requests.get(url, headers=headers) r.raise_for_status() @@ -67,7 +71,7 @@ def get_session_id(server, password, port=80): "Content-Type": "application/x-www-form-urlencoded", "User-Agent": USER_AGENT} - url = 'http://{}:{}/login_sid.lua?&response={}'.format(server, port, response_bf) + url = 'http://{}:{}/login_sid.lua?{}&response={}'.format(server, port, userpar[1:], response_bf) try: r = requests.get(url, headers=headers) r.raise_for_status() diff --git a/fritzbox_memory_usage.py b/fritzbox_memory_usage.py index a73d13b..14d0218 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 @@ -9,7 +9,8 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] - env.fritzbox_password [fritzbox password] + env.FRITZ_PASSWORD [fritzbox password] + env.FRITZ_USERNAME [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -27,10 +28,14 @@ USAGE = ['strict', 'cache', 'free'] def get_memory_usage(): """get the current memory usage""" - server = os.environ['fritzbox_ip'] - password = os.environ['fritzbox_password'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') - session_id = fh.get_session_id(server, password) + if "FRITZ_USERNAME" in os.environ: + fritzuser = os.getenv('FRITZ_USERNAME') + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) xhr_data = fh.get_xhr_content(server, session_id, PAGE) data = json.loads(xhr_data) for i, usage in enumerate(USAGE): @@ -38,7 +43,11 @@ def get_memory_usage(): def print_config(): - print("graph_title AVM Fritz!Box Memory") + if os.environ.get('host_name'): + print("host_name " + os.getenv('host_name')) + print("graph_title Memory usage in percent") + else: + print("graph_title AVM Fritz!Box Memory") print("graph_vlabel %") print("graph_args --base 1000 -r --lower-limit 0 --upper-limit 100") print("graph_category system") @@ -54,8 +63,6 @@ def print_config(): print("free.label free") print("free.type GAUGE") print("free.draw STACK") - if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) if __name__ == '__main__': @@ -67,5 +74,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 9d71a76..8f0e660 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 @@ -10,7 +10,8 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] - env.fritzbox_password [fritzbox password] + env.FRITZ_PASSWORD [fritzbox password] + env.FRITZ_USERNAME [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -29,10 +30,14 @@ 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['fritzbox_password'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') - session_id = fh.get_session_id(server, password) + if "FRITZ_USERNAME" in os.environ: + fritzuser = os.getenv('FRITZ_USERNAME') + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) xhr_data = fh.get_xhr_content(server, session_id, PAGE) data = json.loads(xhr_data) devices = data['data']['drain'] @@ -41,9 +46,13 @@ def get_power_consumption(): def print_config(): - print("graph_title AVM Fritz!Box Power Consumption") + if os.environ.get('host_name'): + print("host_name " + os.getenv('host_name')) + print("graph_title Power consumption") + else: + print("graph_title AVM Fritz!Box Power Consumption") print("graph_vlabel %") - print("graph_category system") + print("graph_category sensors") print("graph_order system cpu wifi dsl ab usb") print("system.label system") print("system.type GAUGE") @@ -81,8 +90,6 @@ def print_config(): print("usb.min 0") 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']) if __name__ == '__main__': @@ -94,5 +101,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..9636be0 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) @@ -43,7 +43,11 @@ def print_values(): def print_config(): - print("graph_title AVM Fritz!Box WAN traffic") + if os.environ.get('host_name'): + print("host_name " + os.getenv('host_name')) + print("graph_title Uplink traffic") + else: + print("graph_title AVM Fritz!Box WAN traffic") print("graph_args --base 1000") print("graph_vlabel bits in (-) / out (+) per \${graph_period}") print("graph_category network") @@ -56,7 +60,7 @@ def print_config(): print("down.max 1000000000") print("up.label bps") print("up.type DERIVE") - print("up.draw AREA") + print("up.draw LINE") print("up.cdef up,8,*") print("up.min 0") print("up.max 1000000000") @@ -71,8 +75,6 @@ def print_config(): print("maxup.negative maxdown") 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']) if __name__ == "__main__": @@ -83,5 +85,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 649c0d0..bb04621 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 @@ -9,7 +9,8 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] - env.fritzbox_password [fritzbox password] + env.FRITZ_PASSWORD [fritzbox password] + env.FRITZ_USERNAME [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -36,10 +37,14 @@ pattern = re.compile(patternLoc[locale]) def get_uptime(): """get the current uptime""" - server = os.environ['fritzbox_ip'] - password = os.environ['fritzbox_password'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') - session_id = fh.get_session_id(server, password) + if "FRITZ_USERNAME" in os.environ: + fritzuser = os.getenv('FRITZ_USERNAME') + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) xhr_data = fh.get_xhr_content(server, session_id, PAGE) data = json.loads(xhr_data) for d in data['data']['drain']: @@ -59,15 +64,17 @@ def get_uptime(): def print_config(): - print("graph_title AVM Fritz!Box Uptime") + if os.environ.get('host_name'): + print("host_name " + os.getenv('host_name')) + print("graph_title Uptime") + else: + print("graph_title AVM Fritz!Box Uptime") print("graph_args --base 1000 -l 0") print('graph_vlabel uptime in days') print("graph_scale no'") print("graph_category system") print("uptime.label uptime") print("uptime.draw AREA") - if os.environ.get('host_name'): - print("host_name " + os.environ['host_name']) if __name__ == '__main__': @@ -79,5 +86,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 bc8e24c..674579b 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 @@ -9,7 +9,8 @@ [fritzbox_*] env.fritzbox_ip [ip address of the fritzbox] - env.fritzbox_password [fritzbox password] + env.FRITZ_PASSWORD [fritzbox password] + env.FRITZ_USERNAME [optional: fritzbox username] This plugin supports the following munin configuration parameters: #%# family=auto contrib @@ -31,12 +32,16 @@ pattern = re.compile(patternLoc[locale]) def get_connected_wifi_devices(): - """gets the numbrer of currently connected wifi devices""" + """gets the numbrer of currently connected wi-fi devices""" - server = os.environ['fritzbox_ip'] - password = os.environ['fritzbox_password'] + server = os.getenv('fritzbox_ip') + password = os.getenv('FRITZ_PASSWORD') - session_id = fh.get_session_id(server, password) + if "FRITZ_USERNAME" in os.environ: + fritzuser = os.getenv('FRITZ_USERNAME') + session_id = fh.get_session_id(server, password, fritzuser) + else: + session_id = fh.get_session_id(server, password) xhr_data = fh.get_xhr_content(server, session_id, PAGE) data = json.loads(xhr_data) m = re.search(pattern, data['data']['drain'][2]['statuses'][-1]) @@ -46,7 +51,11 @@ def get_connected_wifi_devices(): def print_config(): - print('graph_title AVM Fritz!Box Connected Wifi Devices') + if os.environ.get('host_name'): + print("host_name " + os.getenv('host_name')) + print('graph_title Connected Wi-Fi Devices') + else: + print('graph_title AVM Fritz!Box Connected Wi-Fi Devices') print('graph_vlabel Number of devices') print('graph_args --base 1000') print('graph_category network') @@ -55,8 +64,6 @@ def print_config(): print('wifi.type GAUGE') 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']) if __name__ == '__main__': @@ -68,5 +75,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}")