#! /bin/sh
# Parse box information
# (c) 2013 - GPLv2 - Steve Schnepp <steve.schnepp@pwkf.org>
#
# Configuration is done via ENV vars, here is the default :
#
# [dartybox]
# env.IP 192.168.1.254


. $MUNIN_LIBDIR/plugins/plugin.sh

is_multigraph

# fail on error
set -e

IP=${IP:-"192.168.1.254"}

if [ "$1" = "config" ]
then
	echo multigraph dbox_adsl_bw
	echo graph_title DartyBox Adsl Bandwidth
	echo graph_category network
	echo up.label UpStream
	echo down.label DownStream
	echo multigraph dbox_adsl_att
	echo graph_title DartyBox Adsl Attenuation
	echo graph_category network
	echo up.label UpStream
	echo down.label DownStream
	echo multigraph dbox_adsl_snr
	echo graph_title DartyBox Adsl SignalNoise Ratio
	echo graph_category network
	echo up.label UpStream
	echo down.label DownStream
	echo multigraph dbox_adsl_pkt
	echo graph_title DartyBox Adsl Packets
	echo graph_category network
	echo up.label UpStream
	echo down.label DownStream
	echo up_c.label UpStream "(Correctable)"
	echo down_c.label DownStream "(Correctable)"
	echo up_u.label UpStream "(Uncorrectable)"
	echo down_u.label DownStream "(Uncorrectable)"
	echo up.type DERIVE
	echo down.type DERIVE
	echo up_c.type DERIVE
	echo down_c.type DERIVE
	echo up_u.type DERIVE
	echo down_u.type DERIVE
	echo up.min 0
	echo down.min 0
	echo up_c.min 0
	echo down_c.min 0
	echo up_u.min 0
	echo down_u.min 0
	echo multigraph dbox_adsl_uptime
	echo graph_title DartyBox Adsl Uptime
	echo graph_category network
	echo graph_vlabel days
	echo uptime.label Uptime

	exit 0
fi

TMPFILE=$(mktemp)
trap 'rm -f $TMPFILE' EXIT

wget -qO "$TMPFILE" http://$IP/adslstats.html

[ "$MUNIN_DEBUG" = 1 ] && cat "$TMPFILE"

get() {
	PATTERN=$1
	gawk "match(\$0, /var $PATTERN\s+=\s+\"([^\"]+)\"/, a) { print a[1] }" $TMPFILE
}

getDays() {
	# Convert "20 jours 2 heures 2 mn" in a number of days
	echo "$@" | gawk "match(\$0, /([0-9.]+) jours ([0-9.]+) heures ([0-9.]+) mn/, a) { print a[1] + a[2]/24 + a[3]/1440 }"
}

echo multigraph dbox_adsl_bw
echo up.value $(get UpStream)
echo down.value $(get DownStream)
echo multigraph dbox_adsl_att
echo up.value $(get AttNear)
echo down.value $(get AttFar)
echo multigraph dbox_adsl_snr
echo up.value $(get SNRNear)
echo down.value $(get SNRFar)
echo multigraph dbox_adsl_pkt
echo up.value $(get RSWORDSNear)
echo down.value $(get RSWORDSFar)
echo up_c.value $(get RSCORRERRORNear)
echo down_c.value $(get RSCORRERRORFar)
echo up_u.value $(get RSUNCORRERRORNear)
echo down_u.value $(get RSUNCORRERRORFar)
echo multigraph dbox_adsl_uptime
echo uptime.value $(getDays $(get AdslUpTime))

exit 0

:<<'EOF'
var ADSLLineStatus = "Etabli";
var ADSLMode    = "G992_1_A  (G.DMT)   ";
var UpStream    = "704";
var DownStream  = "7616";
var AttNear     = "33.5";
var AttFar      = "16.5";
var SNRNear     = "11.0";
var SNRFar      = "15.0";
var HECCountNear= "4836";
var HECCountFar= "6";
var AdslVer1    = "0x81ef5379";
var AdslVer2    = "0x6397bde2";
var CmvVer1         = "0x6f249e71";
var CmvVer2         = "0xa703362e";
var ES15CntNear = "0";
var ES15CntFar  = "0";
var CRCErrorsNear = "641";
var CRCErrorsFar = "7";
var ES1CNTNear  = "8";
var ES1CNTFar   = "0";

var ESTOTCNTFar = "5";
var ESTOTCNTNear = "452";
var RSWORDSNear = "1802451823";
var RSWORDSFar  = "2881437753";
var RSCORRERRORNear  = "57293";
var RSCORRERRORFar  = "301";
var RSUNCORRERRORNear  = "641";
var RSUNCORRERRORFar  = "7";
var RSRatioNear  = "0";
var RSRatioFar  = "0";
var AdslUpTime = "20 jours 2 heures 2 mn";
EOF
