adapt scripts for FritzOS 7.1, scripts are Python 3 only now

This commit is contained in:
Christian Stade-Schuldt 2019-04-09 09:47:49 +02:00
parent ef6f81b426
commit 883c61e498
7 changed files with 99 additions and 71 deletions

View File

@ -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()

View File

@ -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__':

View File

@ -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

View File

@ -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():

View File

@ -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('<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():
@ -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():

View File

@ -15,7 +15,7 @@
#%# family=auto contrib
#%# capabilities=autoconf
"""
import json
import os
import re
import sys
@ -23,13 +23,13 @@ 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])
@ -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():

View File

@ -15,7 +15,7 @@
#%# family=auto contrib
#%# capabilities=autoconf
"""
import json
import os
import re
import sys
@ -23,9 +23,10 @@ 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])
@ -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():