Skip to content

Commit

Permalink
adapt scripts for FritzOS 7.1, scripts are Python 3 only now
Browse files Browse the repository at this point in the history
  • Loading branch information
Tafkas committed Apr 9, 2019
1 parent ef6f81b commit 883c61e
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 71 deletions.
19 changes: 6 additions & 13 deletions fritzbox_cpu_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -61,7 +57,4 @@ def print_config():
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()
16 changes: 6 additions & 10 deletions fritzbox_cpu_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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__':
Expand Down
32 changes: 31 additions & 1 deletion fritzbox_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
18 changes: 7 additions & 11 deletions fritzbox_memory_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand Down
28 changes: 17 additions & 11 deletions fritzbox_power_consumption.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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('<td>(.+?)"bar\s(act|fillonly)"(.+?)\s(\d{1,3})\s%')
DEVICES = ['system', 'cpu', 'wifi', 'dsl', 'ab', 'usb']
PAGE = 'energy'
DEVICES = {'FRITZ!Box Gesamtsystem': 'system',
'FRITZ!Box Hauptprozessor': 'cpu',
'WLAN': 'wifi',
'DSL': 'dsl',
'analoge FON-Anschlüsse': 'ab',
'USB-Geräte': 'usb'}


def get_power_consumption():
Expand All @@ -33,12 +38,13 @@ def get_power_consumption():
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(DEVICES, [m.group(4) 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 d in data['data']['drain']:
try:
print('%s.value %s' % (DEVICES[d['name']], d['actPerc']))
except KeyError as e:
pass


def print_config():
Expand Down
37 changes: 20 additions & 17 deletions fritzbox_uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
#%# family=auto contrib
#%# capabilities=autoconf
"""

import json
import os
import re
import sys

import fritzbox_helper as fh

locale = os.environ.get('locale', 'de')
patternLoc = {"de": "(\d+)\s(Tag|Stunden|Minuten)",
"en": "(\d+)\s(days|hours|minutes)"}
patternLoc = {"de": r"(\d+)\s(Tag|Stunden|Minuten)",
"en": r"(\d+)\s(days|hours|minutes)"}
dayLoc = {"de": "Tag", "en": "days"}
hourLoc = {"de": "Stunden", "en": "hours"}
minutesLoc = {"de": "Minuten", "en": "minutes"}

PAGE = '/system/energy.lua'
PAGE = 'energy'
pattern = re.compile(patternLoc[locale])


Expand All @@ -40,19 +40,22 @@ def get_uptime():
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:
hours = 0.0
for m in matches:
if m.group(2) == dayLoc[locale]:
hours += 24 * int(m.group(1))
if m.group(2) == hourLoc[locale]:
hours += int(m.group(1))
if m.group(2) == minutesLoc[locale]:
hours += int(m.group(1)) / 60.0
uptime = hours / 24
print "uptime.value %.2f" % uptime
xhr_data = fh.get_xhr_content(server, session_id, PAGE)
data = json.loads(xhr_data)
for d in data['data']['drain']:
if 'aktiv' in d['statuses']:
matches = re.finditer(pattern, d['statuses'])
if matches:
hours = 0.0
for m in matches:
if m.group(2) == dayLoc[locale]:
hours += 24 * int(m.group(1))
if m.group(2) == hourLoc[locale]:
hours += int(m.group(1))
if m.group(2) == minutesLoc[locale]:
hours += int(m.group(1)) / 60.0
uptime = hours / 24
print("uptime.value %.2f" % uptime)


def print_config():
Expand Down
20 changes: 12 additions & 8 deletions fritzbox_wifi_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
#%# family=auto contrib
#%# capabilities=autoconf
"""

import json
import os
import re
import sys

import fritzbox_helper as fh

locale = os.environ.get('locale', 'de')
patternLoc = {"de": "(\d+) WLAN", "en": "(\d+) wireless LAN"}
patternLoc = {"de": r"(\d+) WLAN",
"en": r"(\d+) wireless LAN"}

PAGE = '/system/energy.lua'
PAGE = 'energy'
pattern = re.compile(patternLoc[locale])


Expand All @@ -36,11 +37,14 @@ def get_connected_wifi_devices():
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:
connected_devices = int(m.group(1))
print('wifi.value %d' % connected_devices)
xhr_data = fh.get_xhr_content(server, session_id, PAGE)
data = json.loads(xhr_data)
for d in data['data']['drain']:
if d['name'] == 'WLAN':
m = re.search(pattern, d['statuses'][-1])
if m:
connected_devices = int(m.group(1))
print('wifi.value %d' % connected_devices)


def print_config():
Expand Down

0 comments on commit 883c61e

Please sign in to comment.