1
0
mirror of https://github.com/Tafkas/fritzbox-munin.git synced 2023-10-10 13:36:55 +02:00

Add support for user/password login. New plugin for smarthome temperatures

This commit is contained in:
Bernd Oerding 2018-10-07 13:51:58 +02:00
parent ef6f81b426
commit 9453b9ff6d
9 changed files with 144 additions and 39 deletions

View File

@ -69,6 +69,9 @@ If you are using the scripts on a different Fritz!Box model please let me know b
fritzbox\_wifi\_devices shows you the number of connected wifi clients (requires password) (language dependant, see below).
![http://i.imgur.com/lqvK1b2.png](http://i.imgur.com/lqvK1b2.png)
## fritzbox\_smart\_home\_temperatures
fritzbox\_smart\_home\_temperatures show the temperature of connected smart home devices
## Installation & Configuration
1. Pre-requesites for the fritzbox\_traffic and fritzbox\_uptime plugins are the [fritzconnection](https://pypi.python.org/pypi/fritzconnection) and [requests](https://pypi.python.org/pypi/requests) package. To install it
@ -84,6 +87,7 @@ If you are using the scripts on a different Fritz!Box model please let me know b
[fritzbox_*]
env.fritzbox_ip <ip_address_to_your_fritzbox>
env.fritzbox_user <user> # if you do not specify a user authentification done by password only
env.fritzbox_password <fritzbox_password>
env.traffic_remove_max true # if you do not want the possible max values
host_name fritzbox

View File

@ -9,6 +9,8 @@
[fritzbox_*]
env.fritzbox_ip [ip address of the fritzbox]
env.fritzbox_port [optional port, default: 80]
env.fritzbox_user [optionial, if you configured the FritzBox to use user and password]
env.fritzbox_password [fritzbox password]
This plugin supports the following munin configuration parameters:
@ -28,11 +30,8 @@ pattern = re.compile('Query\s=\s"(\d{1,3})')
def get_cpu_temperature():
"""get the current cpu temperature"""
server = os.environ['fritzbox_ip']
password = os.environ['fritzbox_password']
session_id = fh.get_session_id(server, password)
data = fh.get_page_content(server, session_id, PAGE)
session_id = fh.get_session_id()
data = fh.get_page_content(session_id, PAGE)
m = re.search(pattern, data)
if m:

View File

@ -9,6 +9,8 @@
[fritzbox_*]
env.fritzbox_ip [ip address of the fritzbox]
env.fritzbox_port [optional port, default: 80]
env.fritzbox_user [optionial, if you configured the FritzBox to use user and password]
env.fritzbox_password [fritzbox password]
This plugin supports the following munin configuration parameters:
@ -28,11 +30,8 @@ pattern = re.compile('Query1\s=\s"(\d{1,3})')
def get_cpu_usage():
"""get the current cpu usage"""
server = os.environ['fritzbox_ip']
password = os.environ['fritzbox_password']
session_id = fh.get_session_id(server, password)
data = fh.get_page_content(server, session_id, PAGE)
session_id = fh.get_session_id()
data = fh.get_page_content(session_id, PAGE)
m = re.search(pattern, data)
if m:

View File

@ -9,6 +9,8 @@
[fritzbox_*]
env.fritzbox_ip [ip address of the fritzbox]
env.fritzbox_port [optional port, default: 80]
env.fritzbox_user [optionial, if you configured the FritzBox to use user and password]
env.fritzbox_password [fritzbox password]
This plugin supports the following munin configuration parameters:
@ -22,6 +24,7 @@
import hashlib
import sys
import os
import requests
from lxml import etree
@ -29,17 +32,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():
"""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 port: the port the Fritzbox webserver runs on
:return: the session id
"""
server = os.environ['fritzbox_ip']
if 'fritzbox_port' in os.environ :
port = os.environ['fritzbox_port']
else :
port = 80
if 'fritzbox_user' in os.environ :
user = os.environ['fritzbox_user']
else :
user = ""
password = os.environ['fritzbox_password']
headers = {"Accept": "application/xml",
"Content-Type": "text/plain",
"User-Agent": USER_AGENT}
@ -67,7 +78,10 @@ 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)
if user :
url = 'http://{}:{}/login_sid.lua?username={}&response={}'.format(server, port, user, response_bf)
else :
url = 'http://{}:{}/login_sid.lua?response={}'.format(server, port, response_bf)
try:
r = requests.get(url, headers=headers)
r.raise_for_status()
@ -83,21 +97,27 @@ def get_session_id(server, password, port=80):
return session_id
def get_page_content(server, session_id, page, port=80):
def get_page_content(session_id, page):
"""Fetches a page 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
"""
server = os.environ['fritzbox_ip']
if 'fritzbox_port' in os.environ :
port = os.environ['fritzbox_port']
else :
port = 80
headers = {"Accept": "application/xml",
"Content-Type": "text/plain",
"User-Agent": USER_AGENT}
url = 'http://{}:{}/{}?sid={}'.format(server, port, page, session_id)
if "?" in page :
url = 'http://{}:{}/{}&sid={}'.format(server, port, page, session_id)
else :
url = 'http://{}:{}/{}?sid={}'.format(server, port, page, session_id)
try:
r = requests.get(url, headers=headers)
r.raise_for_status()
@ -105,3 +125,4 @@ def get_page_content(server, session_id, page, port=80):
print(err)
sys.exit(1)
return r.content

View File

@ -9,6 +9,8 @@
[fritzbox_*]
env.fritzbox_ip [ip address of the fritzbox]
env.fritzbox_port [optional port, default: 80]
env.fritzbox_user [optionial, if you configured the FritzBox to use user and password]
env.fritzbox_password [fritzbox password]
This plugin supports the following munin configuration parameters:
@ -29,11 +31,8 @@ USAGE = ['free', 'cache', 'strict']
def get_memory_usage():
"""get the current memory usage"""
server = os.environ['fritzbox_ip']
password = os.environ['fritzbox_password']
session_id = fh.get_session_id(server, password)
data = fh.get_page_content(server, session_id, PAGE)
session_id = fh.get_session_id()
data = fh.get_page_content(session_id, PAGE)
matches = re.finditer(pattern, data)
if matches:
data = zip(USAGE, [m.group(1) for m in matches])

View File

@ -9,6 +9,8 @@
[fritzbox_*]
env.fritzbox_ip [ip address of the fritzbox]
env.fritzbox_port [optional port, default: 80]
env.fritzbox_user [optionial, if you configured the FritzBox to use user and password]
env.fritzbox_password [fritzbox password]
This plugin supports the following munin configuration parameters:
@ -29,11 +31,8 @@ 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']
session_id = fh.get_session_id(server, password)
data = fh.get_page_content(server, session_id, PAGE)
session_id = fh.get_session_id()
data = fh.get_page_content(session_id, PAGE)
matches = re.finditer(pattern, data)
if matches:
data = zip(DEVICES, [m.group(4) for m in matches])

View File

@ -0,0 +1,86 @@
#!/usr/bin/env python
"""
fritzbox_smart_home_temperature - A munin plugin for Linux to monitor AVM Fritzbox SmartHome temperatures
Copyright (C) 2018 Bernd Oerding
Author: Bernd Oerding
Like Munin, this plugin is licensed under the GNU GPL v2 license
http://www.opensource.org/licenses/GPL-2.0
Add the following section to your munin-node's plugin configuration:
[fritzbox_*]
env.fritzbox_ip [ip address of the fritzbox]
env.fritzbox_port [optional port, default: 80]
env.fritzbox_user [optionial, if you configured the FritzBox to use user and password]
env.fritzbox_password [fritzbox password]
This plugin supports the following munin configuration parameters:
#%# family=auto contrib
#%# capabilities=autoconf
"""
import os
import re
import sys
import fritzbox_helper as fh
from lxml import etree
from unidecode import unidecode
PAGE = 'webservices/homeautoswitch.lua?switchcmd=getdevicelistinfos'
def get_smart_home_temperature(debug=False):
"""get the current cpu temperature"""
session_id = fh.get_session_id()
data = fh.get_page_content(session_id, PAGE)
root = etree.fromstring(data)
if debug :
print(etree.tostring(root, pretty_print=True))
for d in root :
id = d.xpath("@id")[0]
present = int(d.xpath("present/text()")[0])
if present :
temp= float(d.xpath("temperature/celsius/text()")[0])/10
print ("t{}.value {}".format(id,temp))
def print_config():
print("graph_title AVM Fritz!Box SmartHome temperature")
print("graph_vlabel degrees Celsius")
print("graph_category sensors")
print("graph_scale no")
server = os.environ['fritzbox_ip']
user = os.environ['fritzbox_user']
password = os.environ['fritzbox_password']
session_id = fh.get_session_id()
data = fh.get_page_content(session_id, PAGE)
root = etree.fromstring(data)
for d in root :
id = d.xpath("@id")[0]
identifier = d.xpath("@identifier")[0]
name = d.xpath("name/text()")[0]
name = unidecode(unicode(name))
pname = d.xpath("@productname")[0]
print ("t{}.label {}".format(id,name))
print ("t{}.type GAUGE".format(id))
print ("t{}.graph LINE".format(id))
print ("t{}.info Temperature [{} - {}]".format(id,pname,identifier))
if os.environ.get('host_name'):
print("host_name " + os.environ['host_name'])
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == 'config':
print_config()
elif len(sys.argv) == 2 and sys.argv[1] == 'autoconf':
print('yes')
elif len(sys.argv) == 2 and sys.argv[1] == 'debug':
get_smart_home_temperature(True)
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_smart_home_temperature()
except:
sys.exit("Couldn't retrieve fritzbox smarthome temperatures")

View File

@ -9,6 +9,8 @@
[fritzbox_*]
env.fritzbox_ip [ip address of the fritzbox]
env.fritzbox_port [optional port, default: 80]
env.fritzbox_user [optionial, if you configured the FritzBox to use user and password]
env.fritzbox_password [fritzbox password]
This plugin supports the following munin configuration parameters:
@ -36,11 +38,8 @@ pattern = re.compile(patternLoc[locale])
def get_uptime():
"""get the current uptime"""
server = os.environ['fritzbox_ip']
password = os.environ['fritzbox_password']
session_id = fh.get_session_id(server, password)
data = fh.get_page_content(server, session_id, PAGE)
session_id = fh.get_session_id()
data = fh.get_page_content(session_id, PAGE)
matches = re.finditer(pattern, data)
if matches:
hours = 0.0

View File

@ -9,6 +9,8 @@
[fritzbox_*]
env.fritzbox_ip [ip address of the fritzbox]
env.fritzbox_port [optional port, default: 80]
env.fritzbox_user [optionial, if you configured the FritzBox to use user and password]
env.fritzbox_password [fritzbox password]
This plugin supports the following munin configuration parameters:
@ -32,11 +34,8 @@ 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['fritzbox_password']
session_id = fh.get_session_id(server, password)
data = fh.get_page_content(server, session_id, PAGE)
session_id = fh.get_session_id()
data = fh.get_page_content(session_id, PAGE)
m = re.search(pattern, data)
if m:
connected_devices = int(m.group(1))