#!/bin/bash
#  Liest die Übertragungszähler der WAN Schnittstelle der Fritzbox aus und 
#  gibt die Daten auf stdout aus, zum Beispiel für Zabbix Monitoring
#  Die Zahl ist der jeweilige Zählerstand der Schnittstelle in Bytes
#  Die Zahlen steigen monoton und Zabbix muss die Auswertung mit dem 
#  Präprozessor »Änderung/Sekunde« verarbeiten, die Einheit ist dann bytes/s
#
case "$1" in
    down)
        curl -s "http://192.168.178.1:49000/igdupnp/control/WANCommonIFC1" \
	-H "Content-Type: text/xml; charset="utf-8"" \
        -H "SoapAction:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetAddonInfos" \
        -d "@/etc/zabbix/bin/linkspeed.xml" | grep -E "TotalBytesReceived64" | \
        sed -e "s/<NewX_AVM_DE_TotalBytesReceived64>//"  -e 's/<\/\?[^>]\+>//'
        ;;
    up)
        curl -s "http://192.168.178.1:49000/igdupnp/control/WANCommonIFC1" \
	-H "Content-Type: text/xml; charset="utf-8"" \
	-H "SoapAction:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetAddonInfos" \
	-d "@/etc/zabbix/bin/linkspeed.xml" | grep -E "TotalBytesSent64" | \
	sed -e "s/<NewX_AVM_DE_TotalBytesSent64>//" -e 's/<\/\?[^>]\+>//'
	;;
    *)
	curl -s "http://192.168.178.1:49000/igdupnp/control/WANCommonIFC1" \
	-H "Content-Type: text/xml; charset="utf-8"" \
	-H "SoapAction:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1#GetAddonInfos" \
	-d "@/etc/zabbix/bin/linkspeed.xml" | grep -E "TotalBytesSent64|TotalBytesReceived64" | \
        sed -e "s/<NewX_AVM_DE_TotalBytesReceived64>/BytesReceived: /" \
	-e "s/<NewX_AVM_DE_TotalBytesSent64>/BytesSent: /" -e 's/<\/\?[^>]\+>//'
	# -e 's/<\/\?[^>]\+>//g'
    ;;
esac

