lunes, 7 de mayo de 2012

Construcción de Plugin Nagios/Centreon para HP Printer

Hace unas horas termine un script en bash para monitorear los consumibles de unas impresoras HP, la idea de agregarlas a centreon es para darnos conocer en buen momento cuando uno de los consumibles se están por terminar así como un histórico (usando las graficas) de cuanto se esta consumiendo. A posteriori, este script se tendría que poder usar en cualquier impresora HP con SNMP activado, los modelos de impresoras que tenemos son las siguientes:
  • HP LaserJet 4700 Color
  • HP LaserJet 4600 Color
  • HP LaserJet 4250
Requerimientos del script:
  • awk
  • snmpwalk
  • A nivel de red, acceso al segmento de las impresoras.
  • bash
  • etc.
#!/bin/bash
# Martin Barriga 2012

# Default options
COMMUNITY="public"
HOSTNAME="127.0.0.1"
CONSUMMABLE="black"
WARNING=20
CRITICAL=10
SNMPVERSION=2c
SHEETCOUNT="0"
sheetcount=""

#used MIBs
MAINMIB="mib-2.43.11.1.1.6.1"
SHEETMIB="enterprises.11.2.3.9.4.2.1.4.1.10.5.1.1"

# Plugin return codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

# Option processing
function phelp {
  echo -e "Usage: $0 -H  -C  -v  -o  -w  -c   [ -l | -s ] | -h "
  echo -e "  -H IP Addres HP Printer"
  echo -e "  -C SNMP Community. Default 'public'"
  echo -e "  -v SNMP Version. Default '2c'"
  echo -e "  -o Color Tonner: black, cyan, magenta, yellow, transfer, fuser. Default 'black'"
  echo -e "  -w Warning Value. Default '80'"
  echo -e "  -c Critical Value. Default '90'"
  echo -e "  -l List consumible support."
  echo -e "  -s Print with sheet count available."
  echo -e "  -h Help, this message.\n"
}

function list {
        allinfo=`snmpwalk -c $COMMUNITY -v $SNMPVERSION $HOSTNAME $MAINMIB`;
        echo "$allinfo" | ( while read line; do
                echo $line | cut -d "\"" -f2;
        done )
}

function withsheetcount {
        index=`echo "$1" | cut -d "." -f8`;
        count=`snmpwalk -c $COMMUNITY -v $SNMPVERSION $HOSTNAME $SHEETMIB.$index | cut -d " " -f4`;
        sheetcount=", Sheet Available: $count";
}

while getopts H:C:v:o:w:c:slh OPT
do
  case $OPT in
    H) HOSTNAME="$OPTARG" ;;
    C) COMMUNITY="$OPTARG" ;;
    v) SNMPVERSION="$OPTARG" ;;
    o) CONSUMMABLE="$OPTARG" ;;
    w) WARNING=$OPTARG ;;
    c) CRITICAL=$OPTARG ;;
    s) SHEETCOUNT="1" ;;
    l)
       list
       exit $STATE_UNKNOWN
    ;;
    h)
       phelp
       exit $STATE_UNKNOWN
    ;;
    #V)
    #  print_version
    #  exit $STATE_UNKNOWN
    #  ;;
   esac
done

info=`snmpwalk -c $COMMUNITY -v $SNMPVERSION $HOSTNAME $MAINMIB | grep -i $CONSUMMABLE`
if [ "$info" != "" ]; then
        name=`echo "$info" | cut -d "\"" -f 2`;
        mib=`echo "$info" | cut -d " " -f 1 | cut -d ":" -f 3`;
        newmib=`echo "$info" | cut -d " " -f 1 | cut -d ":" -f 3 | awk -F. -v OFS=. '{$6=8}1'`;
        size=`snmpwalk -c $COMMUNITY -v $SNMPVERSION $HOSTNAME $newmib | cut -d " " -f4`;
        newmib=`echo "$info" | cut -d " " -f 1 | cut -d ":" -f 3 | awk -F. -v OFS=. '{$6=9}1'`;
        value=`snmpwalk -c $COMMUNITY -v $SNMPVERSION $HOSTNAME $newmib | cut -d " " -f4`;
        let porcent=($value*100)/$size;

        #If sheet count is required
        if [ "$SHEETCOUNT" = "1" ]; then
                withsheetcount $mib
        fi

        if [ "$porcent" -le "$CRITICAL" ]; then
                echo "$name: Status: $value/$size $porcent% available [ CRITICAL ] | size=$size used=$value";
                exit $STATE_CRITICAL;
        else
                if [ "$porcent" -le "$WARNING" ]; then
                        echo "$name: Status: $value/$size $porcent% available [ WARNING ] | size=$size used=$value";
                        exit $STATE_WARNING;
                else
                        echo "$name: Status: $value/$size $porcent% available [ OK ] | size=$size used=$value";
                        exit $STATE_OK;
                fi
        fi
else
        echo "No se encuentra consumible $CONSUMMABLE en $HOSTNAME [ UNKNOW ]"
        exit $STATE_UNKNOWN
fi

No hay comentarios: