diff --git a/fritzbox_cpu_temperature.py b/fritzbox_cpu_temperature.py index 4764ad4..d38015a 100755 --- a/fritzbox_cpu_temperature.py +++ b/fritzbox_cpu_temperature.py @@ -15,14 +15,12 @@ #%# family=auto contrib #%# capabilities=autoconf """ - +import json import os -import re import sys import fritzbox_helper as fh -PAGE = '/system/ecostat.lua' -pattern = re.compile('Query\s=\s"(\d{1,3})') +PAGE = 'ecoStat' def get_cpu_temperature(): @@ -32,11 +30,9 @@ def get_cpu_temperature(): password = os.environ['fritzbox_password'] session_id = fh.get_session_id(server, password) - data = fh.get_page_content(server, session_id, PAGE) - - m = re.search(pattern, data) - if m: - print('temp.value %d' % (int(m.group(1)))) + 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(): @@ -61,7 +57,4 @@ if __name__ == '__main__': print('yes') elif len(sys.argv) == 1 or len(sys.argv) == 2 and sys.argv[1] == 'fetch': # Some docs say it'll be called with fetch, some say no arg at all - try: - get_cpu_temperature() - except: - sys.exit("Couldn't retrieve fritzbox cpu temperature") + get_cpu_temperature() diff --git a/fritzbox_cpu_usage.py b/fritzbox_cpu_usage.py index c5c0f80..4f56321 100755 --- a/fritzbox_cpu_usage.py +++ b/fritzbox_cpu_usage.py @@ -15,14 +15,12 @@ #%# family=auto contrib #%# capabilities=autoconf """ - +import json import os -import re import sys import fritzbox_helper as fh -PAGE = '/system/ecostat.lua' -pattern = re.compile('Query1\s=\s"(\d{1,3})') +PAGE = 'ecoStat' def get_cpu_usage(): @@ -32,11 +30,9 @@ def get_cpu_usage(): password = os.environ['fritzbox_password'] session_id = fh.get_session_id(server, password) - data = fh.get_page_content(server, session_id, PAGE) - - m = re.search(pattern, data) - if m: - print('cpu.value %d' % (int(m.group(1)))) + 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(): @@ -51,7 +47,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.environ['host_name']) if __name__ == '__main__': diff --git a/fritzbox_helper.py b/fritzbox_helper.py index b1110bb..7c9fcd3 100755 --- a/fritzbox_helper.py +++ b/fritzbox_helper.py @@ -56,7 +56,7 @@ def get_session_id(server, password, port=80): session_id = root.xpath('//SessionInfo/SID/text()')[0] if session_id == "0000000000000000": challenge = root.xpath('//SessionInfo/Challenge/text()')[0] - challenge_bf = ('{}-{}'.format(challenge, password)).decode('iso-8859-1').encode('utf-16le') + challenge_bf = ('{}-{}'.format(challenge, password)).encode('utf-16le') m = hashlib.md5() m.update(challenge_bf) response_bf = '{}-{}'.format(challenge, m.hexdigest().lower()) @@ -105,3 +105,33 @@ def get_page_content(server, session_id, page, port=80): print(err) sys.exit(1) return r.content + + +def get_xhr_content(server, session_id, page, port=80): + """Fetches the xhr content from the Fritzbox and returns its content + + :param server: the ip address of the Fritzbox + :param session_id: a valid session id + :param page: the page you are regquesting + :param port: the port the Fritzbox webserver runs on + :return: the content of the page + """ + + headers = {"Accept": "application/xml", + "Content-Type": "application/x-www-form-urlencoded", + "User-Agent": USER_AGENT} + + url = 'http://{}:{}/data.lua'.format(server, port) + data = {"xhr": 1, + "sid": session_id, + "lang": "en", + "page": page, + "xhrId": "all", + "no_sidrenew": "" + } + try: + r = requests.post(url, data=data, headers=headers) + except requests.exceptions.HTTPError as err: + print(err) + sys.exit(1) + return r.content diff --git a/fritzbox_memory_usage.py b/fritzbox_memory_usage.py index e1cdb7c..598f13c 100755 --- a/fritzbox_memory_usage.py +++ b/fritzbox_memory_usage.py @@ -15,15 +15,13 @@ #%# family=auto contrib #%# capabilities=autoconf """ - +import json import os -import re import sys import fritzbox_helper as fh -PAGE = '/system/ecostat.lua' -pattern = re.compile('Query[1-3]\s="(\d{1,3})') -USAGE = ['free', 'cache', 'strict'] +PAGE = 'ecoStat' +USAGE = ['strict', 'cache', 'free'] def get_memory_usage(): @@ -33,12 +31,10 @@ def get_memory_usage(): password = os.environ['fritzbox_password'] session_id = fh.get_session_id(server, password) - data = fh.get_page_content(server, session_id, PAGE) - matches = re.finditer(pattern, data) - if matches: - data = zip(USAGE, [m.group(1) for m in matches]) - for d in data: - print('%s.value %s' % (d[0], d[1])) + xhr_data = fh.get_xhr_content(server, session_id, PAGE) + data = json.loads(xhr_data) + for usage in enumerate(USAGE): + print('%s.value %s' % (usage[1], data['data']['ramusage']['series'][usage[0]][-1])) def print_config(): diff --git a/fritzbox_power_consumption.py b/fritzbox_power_consumption.py index 38d09ee..0669aa4 100755 --- a/fritzbox_power_consumption.py +++ b/fritzbox_power_consumption.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# coding=utf-8 """ fritzbox_power_consumption - A munin plugin for Linux to monitor AVM Fritzbox Copyright (C) 2015 Christian Stade-Schuldt @@ -15,15 +16,19 @@ #%# family=auto contrib #%# capabilities=autoconf """ - +import json import os -import re import sys + import fritzbox_helper as fh -PAGE = '/system/energy.lua' -pattern = re.compile('