#!/bin/bash

# License Information:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

### default variables ###
use_printer=0
use_plattform=0
use_copier=0
use_fax=0
use_scanner=0
overall_state=0
error_present=0
proc_version="v03"

### Nagios Return Codes ###
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3

help(){

cat <<EOF
check_ricoh_printerstate v01
Copyright (c) 2008 Thomas Borger <Thomas.Borger@esg.de>

Use check_ricoh_printerstate to check the device states for a remote
RICOH Printer (example Aficio MP 2500, Aficio MP 3000).

Usage:check_ricoh_printerstate -H <host_address> -c <community>

Options:
          -h             Print detailed help screen
          -V             Print version information
          -C             snmp community string (read only)

EOF
exit 0
}

if [ "$#" -lt 4 ]; then
   echo "insufficient arguments"
   help
fi

while getopts VhH:C: opt; do
   case $opt in
      V)   echo "check_ricoh_printerstate $proc_version"
           exit;;
      H)   host=$OPTARG;;
      C)   community=$OPTARG;;
      h)   help;;
      *)   echo "invalid option"
           exit $UNKNOWN
           ;;
   esac
done

### environment check ###
if ! which snmpwalk >/dev/null 2>&1; then
   echo "UNKNOWN: - could not find snmpwalk on this system"
   exit $UNKNOWN
fi

### get device states from printer ###
printerstates=`snmpwalk -Os -c $community -v 1 $host hrDeviceStatus 2>&1`
if [ "$?" -ne 0 ]; then
   echo "UNKNOWN: $printerstates"
   exit $UNKNOWN
fi

### get all error lines from printer ###
error_description=`snmpwalk -Os -c $community -v 1 $host mib-2.43.18.1.1.8.1 2>&1`
if [ "$?" -ne 0 ]; then
   echo "UNKNOWN: $error_description"
   exit $UNKNOWN
fi

if [ -n "$error_description" ]; then
   error_present=1
   error_description=`echo "$error_description" | tr [:blank:] "#"`
   error_arr=(`echo "$error_description" | tr -s "\n" " "`)

   for var in ${error_arr[*]}; do
   
      ### build the string for verbose output ###
      var=`echo "$var" | tr "#" " "`
      var=${var##*STRING: }
      var=${var%\"}
      var=${var#\"}
      error_line="${error_line}<BR>${var}"
   done
   error_line=${error_line#<BR>}
   
fi

printer=`echo "$printerstates" | grep "Status.1"`
plattform=`echo "$printerstates" | grep "Status.2"`
copier=`echo "$printerstates" | grep "Status.3"`
fax=`echo "$printerstates" | grep "Status.4"`
scanner=`echo "$printerstates" | grep "Status.5"`

### check which devices are in the printer ###
if [ -n "$printer" ]; then
   use_printer=1
   state_printer=${printer##*INTEGER: }
   l=${#state_printer}
   lstr=`expr $l - 2`
   rc_printer=${state_printer:$lstr:1}
   state_printer=${state_printer%(*)}

   if [ "$rc_printer" -ne 2 -o "$rc_printer" -gt "$overall_state" ]; then
      overall_state="$rc_printer"
   fi

fi

if [ -n "$plattform" ]; then
   use_plattform=1
   state_plattform=${plattform##*INTEGER: }
   lstr=`expr $l - 2`
   rc_plattform=${state_plattform:$lstr:1}
   state_plattform=${state_plattform%(*)}
   
   if [ "$rc_plattform" -ne 2 -a "$rc_plattform" -gt "$overall_state" ]; then
      overall_state="$rc_plattform"
   fi
fi

if [ -n "$copier" ]; then
   use_copier=1
   state_copier=${copier##*INTEGER: }
   lstr=`expr $l - 2`
   rc_copier=${state_copier:$lstr:1}
   state_copier=${state_copier%(*)}
   
   if [ "$rc_copier" -ne 2 -a "$rc_copier" -gt "$overall_state" ]; then
      overall_state="$rc_copier"
   fi
fi

if [ -n "$fax" ]; then
   use_fax=1
   state_fax=${fax##*INTEGER: }
   lstr=`expr $l - 2`
   rc_fax=${state_fax:$lstr:1}
   state_fax=${state_fax%(*)}
   
   if [ "$rc_fax" -ne 2 -a "$rc_fax" -gt "$overall_state" ]; then
      overall_state="$rc_fax"
   fi
fi

if [ -n "$scanner" ]; then
   use_scanner=1
   state_scanner=${scanner##*INTEGER: }
   lstr=`expr $l - 2`
   rc_scanner=${state_scanner:$lstr:1}
   state_scanner=${state_scanner%(*)}
   
   if [ "$rc_scanner" -ne 2 -a "$rc_scanner" -gt "$overall_state" ]; then
      overall_state="$rc_scanner"
   fi
fi

### build the output line  ###
if [ "$use_printer" -eq 1 ];then
   output="Printer = $state_printer"
fi
if [ "$use_plattform" -eq 1 ];then
   output="${output} * Plattform = $state_plattform"
fi
if [ "$use_copier" -eq 1 ];then
   output="${output} * Copier = $state_copier"
fi
if [ "$use_fax" -eq 1 ];then
   output="${output} * Fax = $state_fax"
fi
if [ "$use_scanner" -eq 1 ];then
   output="${output} * Scanner = $state_scanner"
fi
###############################

if [ "$error_present" -eq 1 ]; then
   output="${output}<BR>${error_line}"
fi

### evaluate the return code ###
if [ "$overall_state" -eq 0 -o "$overall_state" -eq 2 ]; then
   echo "OK: $output"
   exit $OK
elif [ "$overall_state" -eq 1 -o "$overall_state" -eq 3 -o "$overall_state" -eq 4 ]; then
   echo "WARNING: $output"
   exit $WARNING
elif [ "$overall_state" -eq 5 ]; then
   echo "CRITICAL: $output"
   exit $CRITICAL
else
   echo "UNKNOWN: $output"
   exit $UNKNOWN
fi
