#!/bin/bash
# -*- sh -*-

: << =cut

=head1 NAME

epc3010_ - munin-plugin to monitor Upstream/Downstream Power Levels and Signal to Noise Ratio on Cisco EPC3010 EuroDocsis 3.0 Data Modem

=head1 CONFIGURATION

This plugin does not require configuration.
Create two links called epc3010_downstream and epc3010_upstream in order to use this script.

=head1 AUTHOR

Finn Andersen - xiphias256@gmail.com

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

 #%# family=contrib
 #%# capabilities=autoconf

=cut

#
###############################################
#
# HISTORY
# v1.0   : Initial release
#  1.0.1 : More secure handling of tempfiles
#
###############################################
#

# Decide if you want upstream og downstream info
DIRECTION=${0##*epc3010_}

# Check if argument is autoconfig or config
case "$1" in

autoconfig)
  # Does your network have a Cisco EPC3010?
  curl -s http://192.168.100.1/Docsis_system.asp | grep -q "Cisco EPC3010"
  if [ $? -eq 0 ]; then
    echo "yes"
    exit 0
  else
    echo "Can't find Cisco EPC3010 modem in your network"
    exit 1
  fi
  ;;
config)
  if [ $DIRECTION == "downstream" ]; then
    printf "graph_title Cisco EPC3010 Downstream measurements\n";
    printf "graph_args -l 0 --base 1000\n"
    printf "graph_category sensors\n"
    printf "graph_vlabel Small number is Power Level, large number is SNR\n"
    printf "graph_info Downstream Signal to Noise Ratio (dB) and Power Level (dBmV) from Cisco EPC3010 modem. Info is scraped from statuspage on the modem (http://192.168.100.1)\n"
    printf "channel_1_pwr.label Channel 1 (dBmV)\n"
    printf "channel_1_pwr.type GAUGE\n"
    printf "channel_2_pwr.label Channel 2 (dBmV)\n"
    printf "channel_2_pwr.type GAUGE\n"
    printf "channel_3_pwr.label Channel 3 (dBmV)\n"
    printf "channel_3_pwr.type GAUGE\n"
    printf "channel_4_pwr.label Channel 4 (dBmV)\n"
    printf "channel_4_pwr.type GAUGE\n"
    printf "channel_5_pwr.label Channel 5 (dBmV)\n"
    printf "channel_5_pwr.type GAUGE\n"
    printf "channel_6_pwr.label Channel 6 (dBmV)\n"
    printf "channel_6_pwr.type GAUGE\n"
    printf "channel_7_pwr.label Channel 7 (dBmV)\n"
    printf "channel_7_pwr.type GAUGE\n"
    printf "channel_8_pwr.label Channel 8 (dBmV)\n"
    printf "channel_8_pwr.type GAUGE\n"
    printf "channel_1_snr.label Channel 1 (dB)\n"
    printf "channel_1_snr.type GAUGE\n"
    printf "channel_2_snr.label Channel 2 (dB)\n"
    printf "channel_2_snr.type GAUGE\n"
    printf "channel_3_snr.label Channel 3 (dB)\n"
    printf "channel_3_snr.type GAUGE\n"
    printf "channel_4_snr.label Channel 4 (dB)\n"
    printf "channel_4_snr.type GAUGE\n"
    printf "channel_5_snr.label Channel 5 (dB)\n"
    printf "channel_5_snr.type GAUGE\n"
    printf "channel_6_snr.label Channel 6 (dB)\n"
    printf "channel_6_snr.type GAUGE\n"
    printf "channel_7_snr.label Channel 7 (dB)\n"
    printf "channel_7_snr.type GAUGE\n"
    printf "channel_8_snr.label Channel 8 (dB)\n"
    printf "channel_8_snr.type GAUGE\n"
    exit 0
  fi
  if [ "$DIRECTION" == "upstream" ]; then
    printf "graph_title Cisco EPC3010 Upstream measurements\n"
    printf "graph_args -l 30 --base 1000\n"
    printf "graph_category sensors\n"
    printf "graph_vlabel Power level in dBmV\n"
    printf "graph_info Upstream Power Levels from Cisco EPC3010 modem,scraped from its statuspage (http://192.168.100.1)\n"
    printf "channel_1_pwr.label Channel 1 (dBmV)\n"
    printf "channel_1_pwr.type GAUGE\n"
    printf "channel_2_pwr.label Channel 2 (dBmV)\n"
    printf "channel_2_pwr.type GAUGE\n"
    printf "channel_3_pwr.label Channel 3 (dBmV)\n"
    printf "channel_3_pwr.type GAUGE\n"
    printf "channel_4_pwr.label Channel 4 (dBmV)\n"
    printf "channel_4_pwr.type GAUGE\n"
    exit 0
  fi
  ;;
esac


FILENAME=$(mktemp -q) || exit 1
trap 'rm -f "$FILENAME"' EXIT

# Get statuspage from the modem
curl -s -o "$FILENAME" http://192.168.100.1/Docsis_system.asp


# Bash arrays starts on index 0, we 0-pad the first index. It makes it easier later on..
SNR_ARRAY=( 0 $(awk -F'[<>]' '/ ch_snr/{print $3}' "$FILENAME") )
PWR_ARRAY=( 0 $(awk -F'[<>]' '/ ch_pwr/{print $3}' "$FILENAME") )
UP_PWR_ARRAY=( 0 $(awk -F'[<>]' '/ up_pwr/{print $3}' "$FILENAME") )


#
# Downstream info - Modem outputs both Power Levels and Signal to Noise Ratio
#

if [ "$DIRECTION" == "downstream" ]; then
  total=${#PWR_ARRAY[*]}
  for (( index=1; index<$(( $total )); index++ ))
  do
    printf "channel_%d_pwr.value %s\n" $index ${PWR_ARRAY[$index]}
  done

  total=${#SNR_ARRAY[*]}
  for (( index=1; index<$(( $total )); index++ ))
  do
      printf "channel_%d_snr.value %s\n" $index ${SNR_ARRAY[$index]}
  done
  exit 0
fi


#
# Upstream info - Modem outputs Power Levels
#

if [ "$DIRECTION" == "upstream" ]; then
  total=${#UP_PWR_ARRAY[*]}
  for (( index=1; index<$(( $total )); index++ ))
  do
    printf "channel_%d_pwr.value %s\n" $index ${UP_PWR_ARRAY[$index]}
  done
  exit 0
fi
