#!/bin/sh
#
# Plugin to monitor APC environmental units (temperature/humidity)
#
# Usage: Copy or link into /etc/munin/plugins/
#
# Parameters:
#
# 	config   (required)
# 	autoconf (optional - used by munin-config)
#
# Config variables:
#
#       units     - DNS names of environmental units
#       oid       - OID Prefix for humidity probes
#       community - Community to use to access the APC unit
#

#
# Configuration for temperature or humidity probes 
#

type=`echo $0 | sed -e 's/.*_\(.*\)/\1/'`
if [ "${type}" = temperature ] ; then
  TOID="enterprises.318.1.1.10.3.13.1.1.3"
  NAME="temperature"
  LABEL="Celsius Degrees"
  LETTER="t"
else
  if [ "${type}" = humidity ] ; then
   TOID="enterprises.318.1.1.10.3.13.1.1.6"
   NAME="humidity"
   LABEL="Humidity %"
   LETTER="h"
  else 
    exit 1
  fi
fi

UNITS=""
COMMUNITY="public"
SNMPGET=`which snmpget`

if [ "$units" ]; then UNITS=$units ; fi
if [ "$oid" ]; then TOID=$oid ; fi
if [ "$community" ]; then COMMUNITY=$community ; fi

SNMPOPTS="-Ov -Oq -v1 -c ${COMMUNITY}"

if [ "$1" = "autoconf" ]; then
  if [ -z "${UNITS}" -o -z "${SNMPGET}" ] ; then echo "no" ; exit 1 ; fi
  for m in ${UNITS} ; do
    if ping -c1 -q $m  >/dev/null 2>&1 ; then continue ; fi
    echo "no" ; exit 1
  done
  echo "yes" ; exit 0
fi

if [ "$1" = "config" ]; then
  echo "graph_title Environmental units (${NAME} probes)"
  echo "graph_vlabel ${LABEL}"
  for m in ${UNITS} ; do
    mm=`echo ${m} | tr '-' '_'`
    echo "${mm}_${LETTER}1.label $m ${NAME} #1"
    echo "${mm}_${LETTER}2.label $m ${NAME} #2"
  done
  exit 0
fi

for m in ${UNITS} ; do
  v1=`${SNMPGET} ${SNMPOPTS} $m ${TOID}.1`
  v2=`${SNMPGET} ${SNMPOPTS} $m ${TOID}.2`
  mm=`echo ${m} | tr '-' '_'`
  echo "${mm}_${LETTER}1.value $v1"
  echo "${mm}_${LETTER}2.value $v2"
done
