Raspbian Wheezy: AVM FRITZ!Box 7390 mit MRTG überwachen
In der folgenden Anleitung wird beschrieben wie Sie von der AVM FRITZ!Box 7390 die Trafficstatistiken abrufen und die Daten mit MRTG grafisch darstellen können. Die folgende Vorgehensweise setzt voraus, dass Sie MRTG bereits wie in der Anleitung Raspbian Wheezy: MRTG als Daemon installieren beschrieben installiert und konfiguriert haben.
Da die FRITZ!Box das Simple Network Management Protocol (SNMP) nicht unterstützt, müssen die Daten über Universal Plug and Play (UPnP) ausgelesen werden. Hierfür kann das von Michael Tomschitz geschriebene Shell-Skript upnp2mrtg verwendet werden.
Erstellen Sie eine neue Datei für das upnp2mrtg Shell-Skript.
pi@raspberrypi ~ $ sudo vi /usr/local/bin/upnp2mrtg
Fügen Sie anschließend das folgende Shell-Skript in diese Datei ein. Sie können auch den Original-Quellcode von http://www.anetzb.de/upnp2mrtg/upnp2mrtg verwenden.
#!/bin/bash
#
# upnp2mrtg - Monitoring AVM Fritz!Box With MRTG
# This versions have been reported as working: 3030, 5050, 7050, 7141 and 7170
#
# Copyright (C) 2005 Michael Tomschitz <michael.tomschitz@ANetzB.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The latest version of this software can be obtained here:
#
# http://www.ANetzB.de/upnp2mrtg/
#
# $Id: upnp2mrtg,v 1.9 2008/02/09 15:37:57 tomcat Exp $
# default configuration
HOST="fritz.box"
PORT="49000"
NETCAT="netcat"
# if available, read configuration
test -f /etc/upnp2mrtg.cfg && . /etc/upnp2mrtg.cfg
case $NETCAT in
bash) nc="shell_netcat" ;;
netcat) nc="netcat" ;;
nc_q) nc="nc -q 1" ;;
*) nc="nc" ;;
esac
ver_txt="upnp2mrtg, version $Revision: 1.9 $
Copyright (C) 2005-2008 Michael Tomschitz
upnp2mrtg comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions."
help_txt="\
Usage: upnp2mrtg [-a <host>] [-p <port>] [-P] [-d] [-h] [-i] [-t] [-v] [-V]
-a <host> hostname or ip adress of upnp device (default: $HOST)
-p <port> port to connect (default: $PORT)
-P query packets instead of bytes
-d debug mode
-h show help and exit
-i <outfile> get all igd description
-t test connection
-v show upnp2mrtg version and exit
-V be verbose for testing
"
while getopts "a:dhi:p:PtvV" option; do
case $option in
a) HOST="$OPTARG";;
d) set -x ;;
h) echo "$help_txt"; exit 0;;
i) MODE=igd; IGDXML="$OPTARG";;
p) PORT="$OPTARG";;
P) PACKET_MODE=true;;
t) MODE="test";;
v) echo "$ver_txt"; exit 0;;
V) VERBOSE=true;;
?) exit 1;;
esac
done
# functions
request_header () {
echo "POST /igdupnp/control/$4 HTTP/1.0
HOST: $1:$2
CONTENT-LENGTH: $3
CONTENT-TYPE: text/xml; charset=\"utf-8\"
SOAPACTION: \"urn:schemas-upnp-org:service:$5:1#$6\""
}
soap_form () {
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<s:Envelope
xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"
s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
<s:Body>
<u:$1 xmlns:u=\"urn:schemas-upnp-org:service:$2:1\" />
</s:Body>
</s:Envelope>"
}
get_attribute () {
_get_attribute_start_tag () { echo "${2#*<$1>*}"; }
_get_attribute_end_tag () { echo "${2%*</$1>*}"; }
_get_attribute_tag () { _get_attribute_start_tag "$1" "`_get_attribute_end_tag $1 "$2"`" ; }
_get_attribute_num () { echo $#; }
if [ `_get_attribute_num $1` -gt 1 ]; then
get_attribute "${1#* }" "`_get_attribute_tag "${1%% *}" "$2"`"
else
_get_attribute_tag "${1}" "$2"
fi
}
modulo_time () {
echo "$((${1} / ${2})) $((${1} % ${2}))"
}
shell_netcat () {
exec 5<>/dev/tcp/$1/$2; cat >&5; cat <&5;
}
get_response () {
_get_response_rs="`echo "$1" | $nc $HOST $PORT 2>/dev/null`"
_get_response_rv=$?
echo "$_get_response_rs"
if ${VERBOSE:-false}; then
echo
echo "---- REQUEST: ----">&2
echo "$1">&2
echo "---- RESPONSE: ----">&2
echo "$_get_response_rs">&2
echo "----">&2
fi
return $_get_response_rv
}
request_header_http () {
echo "GET $3 HTTP/1.0
"
}
ws_operation () {
request="`soap_form "$1" WANCommonInterfaceConfig`"
post="`request_header $HOST $PORT ${#request} WANCommonIFC1 WANCommonInterfaceConfig "$1"`
$request"
rs="`get_response "$post"`"
if [ $? -eq 0 ]; then
echo "`get_attribute "$2" "$rs"`"
fi
}
case $MODE in
test)
echo "GET /any.xml HTTP/1.0
" | $nc $HOST $PORT >/dev/null
if [ $? -eq 0 ]; then
echo "OK"; exit 0
else
echo "Connection Error"; exit 1
fi;;
igd)
if [ -f "$IGDXML" ];then
echo "ERROR: $IGDXML: File exists.">&2; exit 1
fi
for igd in any igdconnSCPD igddesc igddslSCPD igdicfgSCPD; do
request="`request_header_http $HOST $PORT /$igd.xml`
"
rs="`get_response "$request"`"
if [ "$IGDXML" = "-" ]; then
echo "---- $igd.xml ----
$rs"
else
echo "---- $igd.xml ----
$rs" >> "$IGDXML"
fi
done;;
*)
# get uptime
request="`soap_form GetStatusInfo WANIPConnection`"
post="`request_header $HOST $PORT ${#request} WANIPConn1 WANIPConnection GetStatusInfo`
$request"
# rs="`echo "$post" | $nc $HOST $PORT 2>/dev/null`"
rs="`get_response "$post"`"
if [ $? -eq 0 ]; then
ut=`get_attribute NewUptime "$rs"`
# calculate days + hours, minutes, seconds
s=`modulo_time $ut 60`
m=`modulo_time ${s% *} 60`
h=`modulo_time ${m% *} 24`
fi
# get data in/out
if ${PACKET_MODE:-false}; then
b1="`ws_operation GetTotalPacketsReceived NewTotalPacketsReceived`"
b2="`ws_operation GetTotalPacketsSent NewTotalPacketsSent`"
else
b1="`ws_operation GetAddonInfos NewTotalBytesReceived`"
b2="`ws_operation GetAddonInfos NewTotalBytesSent`"
fi
# output for mrtg
printf "%s\n%s\n%d days %.2d:%.2d:%.2d h (online)\nFRITZ!Box\n" \
"${b1:-UNKNOWN}" "${b2:-UNKNOWN}" "${h% *}" "${h#* }" "${m#* }" "${s#* }"
;;
esac
#
# upnp2mrtg - Monitoring AVM Fritz!Box With MRTG
# This versions have been reported as working: 3030, 5050, 7050, 7141 and 7170
#
# Copyright (C) 2005 Michael Tomschitz <michael.tomschitz@ANetzB.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# The latest version of this software can be obtained here:
#
# http://www.ANetzB.de/upnp2mrtg/
#
# $Id: upnp2mrtg,v 1.9 2008/02/09 15:37:57 tomcat Exp $
# default configuration
HOST="fritz.box"
PORT="49000"
NETCAT="netcat"
# if available, read configuration
test -f /etc/upnp2mrtg.cfg && . /etc/upnp2mrtg.cfg
case $NETCAT in
bash) nc="shell_netcat" ;;
netcat) nc="netcat" ;;
nc_q) nc="nc -q 1" ;;
*) nc="nc" ;;
esac
ver_txt="upnp2mrtg, version $Revision: 1.9 $
Copyright (C) 2005-2008 Michael Tomschitz
upnp2mrtg comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions."
help_txt="\
Usage: upnp2mrtg [-a <host>] [-p <port>] [-P] [-d] [-h] [-i] [-t] [-v] [-V]
-a <host> hostname or ip adress of upnp device (default: $HOST)
-p <port> port to connect (default: $PORT)
-P query packets instead of bytes
-d debug mode
-h show help and exit
-i <outfile> get all igd description
-t test connection
-v show upnp2mrtg version and exit
-V be verbose for testing
"
while getopts "a:dhi:p:PtvV" option; do
case $option in
a) HOST="$OPTARG";;
d) set -x ;;
h) echo "$help_txt"; exit 0;;
i) MODE=igd; IGDXML="$OPTARG";;
p) PORT="$OPTARG";;
P) PACKET_MODE=true;;
t) MODE="test";;
v) echo "$ver_txt"; exit 0;;
V) VERBOSE=true;;
?) exit 1;;
esac
done
# functions
request_header () {
echo "POST /igdupnp/control/$4 HTTP/1.0
HOST: $1:$2
CONTENT-LENGTH: $3
CONTENT-TYPE: text/xml; charset=\"utf-8\"
SOAPACTION: \"urn:schemas-upnp-org:service:$5:1#$6\""
}
soap_form () {
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<s:Envelope
xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"
s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">
<s:Body>
<u:$1 xmlns:u=\"urn:schemas-upnp-org:service:$2:1\" />
</s:Body>
</s:Envelope>"
}
get_attribute () {
_get_attribute_start_tag () { echo "${2#*<$1>*}"; }
_get_attribute_end_tag () { echo "${2%*</$1>*}"; }
_get_attribute_tag () { _get_attribute_start_tag "$1" "`_get_attribute_end_tag $1 "$2"`" ; }
_get_attribute_num () { echo $#; }
if [ `_get_attribute_num $1` -gt 1 ]; then
get_attribute "${1#* }" "`_get_attribute_tag "${1%% *}" "$2"`"
else
_get_attribute_tag "${1}" "$2"
fi
}
modulo_time () {
echo "$((${1} / ${2})) $((${1} % ${2}))"
}
shell_netcat () {
exec 5<>/dev/tcp/$1/$2; cat >&5; cat <&5;
}
get_response () {
_get_response_rs="`echo "$1" | $nc $HOST $PORT 2>/dev/null`"
_get_response_rv=$?
echo "$_get_response_rs"
if ${VERBOSE:-false}; then
echo
echo "---- REQUEST: ----">&2
echo "$1">&2
echo "---- RESPONSE: ----">&2
echo "$_get_response_rs">&2
echo "----">&2
fi
return $_get_response_rv
}
request_header_http () {
echo "GET $3 HTTP/1.0
"
}
ws_operation () {
request="`soap_form "$1" WANCommonInterfaceConfig`"
post="`request_header $HOST $PORT ${#request} WANCommonIFC1 WANCommonInterfaceConfig "$1"`
$request"
rs="`get_response "$post"`"
if [ $? -eq 0 ]; then
echo "`get_attribute "$2" "$rs"`"
fi
}
case $MODE in
test)
echo "GET /any.xml HTTP/1.0
" | $nc $HOST $PORT >/dev/null
if [ $? -eq 0 ]; then
echo "OK"; exit 0
else
echo "Connection Error"; exit 1
fi;;
igd)
if [ -f "$IGDXML" ];then
echo "ERROR: $IGDXML: File exists.">&2; exit 1
fi
for igd in any igdconnSCPD igddesc igddslSCPD igdicfgSCPD; do
request="`request_header_http $HOST $PORT /$igd.xml`
"
rs="`get_response "$request"`"
if [ "$IGDXML" = "-" ]; then
echo "---- $igd.xml ----
$rs"
else
echo "---- $igd.xml ----
$rs" >> "$IGDXML"
fi
done;;
*)
# get uptime
request="`soap_form GetStatusInfo WANIPConnection`"
post="`request_header $HOST $PORT ${#request} WANIPConn1 WANIPConnection GetStatusInfo`
$request"
# rs="`echo "$post" | $nc $HOST $PORT 2>/dev/null`"
rs="`get_response "$post"`"
if [ $? -eq 0 ]; then
ut=`get_attribute NewUptime "$rs"`
# calculate days + hours, minutes, seconds
s=`modulo_time $ut 60`
m=`modulo_time ${s% *} 60`
h=`modulo_time ${m% *} 24`
fi
# get data in/out
if ${PACKET_MODE:-false}; then
b1="`ws_operation GetTotalPacketsReceived NewTotalPacketsReceived`"
b2="`ws_operation GetTotalPacketsSent NewTotalPacketsSent`"
else
b1="`ws_operation GetAddonInfos NewTotalBytesReceived`"
b2="`ws_operation GetAddonInfos NewTotalBytesSent`"
fi
# output for mrtg
printf "%s\n%s\n%d days %.2d:%.2d:%.2d h (online)\nFRITZ!Box\n" \
"${b1:-UNKNOWN}" "${b2:-UNKNOWN}" "${h% *}" "${h#* }" "${m#* }" "${s#* }"
;;
esac
Hinweis: Sollten Sie eine ältere Version von FRITZ!OS einsetzen, müssen Sie im Skript nach der folgenden Zeile suchen und /igdupnp durch /upnp ersetzen.
echo "POST /igdupnp/control/$4 HTTP/1.0
Nachdem Sie das Shell-Skript gespeichert haben, müssen Sie die Zugriffsrechte wie folgt anpassen damit das Skript ausgeführt werden kann.
pi@raspberrypi ~ $ sudo chmod 0755 /usr/local/bin/upnp2mrtg
Indem Sie das Shell-Skript wie folgt aufrufen, können Sie bereits überprüfen ob upnp2mrtg die gewünschten Daten von der FRITZ!Box auslesen kann.
pi@raspberrypi ~ $ upnp2mrtg
226782930
101378138
0 days 15:27:34 h (online)
FRITZ!Box
226782930
101378138
0 days 15:27:34 h (online)
FRITZ!Box
Jetzt muss die MRTG-Konfigurationsdatei noch entsprechend für die FRITZ!Box erweitert werden. Öffnen Sie daher die /etc/mrtg.cfg mit einem Editor.
pi@raspberrypi ~ $ sudo vi /etc/mrtg.cfg
Neben dem Shell-Skript upnp2mrtg findet man unter http://www.anetzb.de/upnp2mrtg/mrtg.cfg auch eine Beispielkonfiguration für MRTG von Michael Tomschitz. Die folgende Konfiguration basiert auf dieser Beispielkonfiguration, wurde jedoch etwas angepasst und erweitert.
Fügen Sie am Ende der bestehenden MRTG-Konfigurationsdatei die folgenden Zeilen ein und speichern die Änderungen ab.
######################################################################
# System: FRITZ!Box 7390
# Description: AVM FRITZ!Box 7390
######################################################################
Target[fritzbox]: `/usr/local/bin/upnp2mrtg`
Title[fritzbox]: Traffic Analysis for FRITZ!Box 7390
# DSL light (384 kbit / 64 kbit)
#MaxBytes1[fritzbox]: 48000
#MaxBytes2[fritzbox]: 8000
# DSL 1024 (1024 kbit / 128 kbit)
#MaxBytes1[fritzbox]: 128000
#MaxBytes2[fritzbox]: 16000
# DSL 2048 (2048 kbit / 192 kbit)
#MaxBytes1[fritzbox]: 256000
#MaxBytes2[fritzbox]: 24000
# DSL 6016 (6016 kbit / 576 kbit)
#MaxBytes1[fritzbox]: 752000
#MaxBytes2[fritzbox]: 72000
# DSL 16000 (16000 kbit / 1024 kbit)
MaxBytes1[fritzbox]: 2000000
MaxBytes2[fritzbox]: 128000
PageTop[fritzbox]: <h1>Traffic Analysis for FRITZ!Box 7390</h1>
<div id="sysdetails">
<table>
<tr>
<td>System:</td>
<td>FRITZ!Box 7390</td>
</tr>
<tr>
<td>IP Address:</td>
<td>192.168.10.2 (fritzbox.home.lan)</td>
</tr>
<tr>
<td>Description:</td>
<td>DSL</td>
</tr>
<tr>
<td>Max Download Speed:</td>
<td>16000 kbit/s</td>
</tr>
<tr>
<td>Max Upload Speed:</td>
<td>1024 kbit/s</td>
</tr>
</table>
</div>
# System: FRITZ!Box 7390
# Description: AVM FRITZ!Box 7390
######################################################################
Target[fritzbox]: `/usr/local/bin/upnp2mrtg`
Title[fritzbox]: Traffic Analysis for FRITZ!Box 7390
# DSL light (384 kbit / 64 kbit)
#MaxBytes1[fritzbox]: 48000
#MaxBytes2[fritzbox]: 8000
# DSL 1024 (1024 kbit / 128 kbit)
#MaxBytes1[fritzbox]: 128000
#MaxBytes2[fritzbox]: 16000
# DSL 2048 (2048 kbit / 192 kbit)
#MaxBytes1[fritzbox]: 256000
#MaxBytes2[fritzbox]: 24000
# DSL 6016 (6016 kbit / 576 kbit)
#MaxBytes1[fritzbox]: 752000
#MaxBytes2[fritzbox]: 72000
# DSL 16000 (16000 kbit / 1024 kbit)
MaxBytes1[fritzbox]: 2000000
MaxBytes2[fritzbox]: 128000
PageTop[fritzbox]: <h1>Traffic Analysis for FRITZ!Box 7390</h1>
<div id="sysdetails">
<table>
<tr>
<td>System:</td>
<td>FRITZ!Box 7390</td>
</tr>
<tr>
<td>IP Address:</td>
<td>192.168.10.2 (fritzbox.home.lan)</td>
</tr>
<tr>
<td>Description:</td>
<td>DSL</td>
</tr>
<tr>
<td>Max Download Speed:</td>
<td>16000 kbit/s</td>
</tr>
<tr>
<td>Max Upload Speed:</td>
<td>1024 kbit/s</td>
</tr>
</table>
</div>
Nachdem Sie die Konfigurationsdatei erweitert haben, müssen Sie die Index-Datei unter /var/www/mrtg aktualisieren.
pi@raspberrypi ~ $ sudo su -c 'indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html'
Starten Sie im Anschluss den MRTG-Daemon neu.
pi@raspberrypi ~ $ sudo /etc/init.d/mrtg restart
MRTG generiert jetzt die dazugehörigen Graphen und die HTML-Seiten welche Sie über das MRTG-Webinterface abrufen können und sammelt regelmäßig die Übertragungsdaten von der FRITZ!Box ein und aktualisiert die Graphen.
Dieser Eintrag wurde am 23.12.2013 erstellt und zuletzt am 25.09.2016 bearbeitet.
Direkter Link zu dieser Seite: http://www.gtkdb.de/index_36_2458.html
[ Zur Startseite ] [ Zur Kategorie ]
© 2004-2021 by Georg Kainzbauer