#!/bin/bash
snmpwalk="/usr/bin/snmpwalk"
community=public
port=161
ecode=0
# loop while passing arguments 
while [ "$#" -gt 0 ]
do
        case "$1" in
        -p)
                shift
                port="$1"
                ;;
        -h)
                shift
                hostaddress="$1"
                ;;

        -c)
		shift
                community="$1"
                ;;

        -W)
		shift
                warning="$1"
                ;;

        -C)
		shift
                critical="$1"
                ;;

        -o)
		shift
		options=$1
                ;;

	-s)	
		shift
		status="s"
		;;

	--help)
		mode="help"
		;;

        *)
                echo "Unknown argument '$1'" >&2
                exit 1
                ;;
        esac

        shift
done

if [ "$mode" = "help" ]
 then
  echo "Plug-In for Monitoring Plotters in Nagios"
  echo "Use:"
  echo "   -l   list all available Cartridges"
  echo "   -o   Options ..."
  echo "          il = list available Ink Catridges"
  echo "          i<Nr> = check Catridge Number"
  echo "          ia = check all Catridges"
  echo "          pl = list available Ink Catridges"
  echo "          p<Nr> = check Catridge Number"
  echo "          pa = check all Catridges"
  echo "          hl = list available Printheads"
  echo "          h<Nr> = lheck Printhead Number"
  echo "          ha = lheck all Printhead"
  echo "   -h   Hostaddress"
  echo "   -c   Community String, default: public"
  echo "   -p   Port, default: 161" 
  echo "   -C   Critical"
  echo "   -W   Warning"
  echo "   -s   Druckerstatus"
  exit
fi

if [ -z "$hostaddress" ]
  then
	echo "Hostadresse nicht angegeben!"
exit
fi

# Check if warning and critical is set, when using options 

if [ -n "$options" ]
 then
  if [ -z "$warning" -o -z "$critical" ]
   then
	echo "Set Critical and Warning" 
     exit
  fi
fi
if [ "$options" = "il" ]
  then
   i=1
   for OID in `$snmpwalk -v 2c -c $community $hostaddress:$port  1.3.6.1.2.1.43.11.1.1.9.1 2>/dev/null |cut -d " " -f1 |cut -d "." -f13`
   do 
     names=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.11.1.1.6.1.$OID |cut -d \" -f 2`
     echo "${i}: ${names} " 
   i=`expr $i + 1`
   done
exit

elif [ "$options" = "ia" ]
  then 
  for OID in `$snmpwalk -v 2c -c $community $hostaddress:$port  1.3.6.1.2.1.43.11.1.1.9.1 2>/dev/null |cut -d " " -f1 |cut -d "." -f13`
  do 
   names=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.12.1.1.4.1.$OID |cut -d \" -f 2`
   fuel=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.11.1.1.9.1.$OID |cut -d \: -f 2 |cut -c 2-`
   output="${output} ${names} ${fuel}ml, " 
   if [ "$ecode" != "2" ]
	then
     if [ "$fuel" -le "$critical" ]
	then 
	   ecode=2
     elif [ "$fuel" -le "$warning" ]
       then
	ecode=1
     fi
   fi
  done
elif [[ "$options" = i[0-9] ]]
  then
   ink=`echo $options |cut -c 2-`
   names=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.11.1.1.6.1.$ink |cut -d \" -f 2`
   names2=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.12.1.1.4.1.$ink |cut -d \" -f 2`
   fuel=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.11.1.1.9.1.$ink |cut -d \: -f 2 |cut -c 2-`
   output="${names2} ${names} ${fuel}ml" 
   if [ "$ecode" != "2" ]
	then
     if [ "$fuel" -le "$critical" ]
	then 
	   ecode=2
     elif [ "$fuel" -le "$warning" ]
       then
	ecode=1
     fi
   fi

elif [ "$options" = "pl" ]
  then
   i=1
   for OID in `$snmpwalk -v 2c -c $community $hostaddress:$port  1.3.6.1.2.1.43.8.2.1.13.1 2>/dev/null |cut -d " " -f1 |cut -d "." -f13`
   do 
     names=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.8.2.1.13.1.$OID |cut -d \" -f 2`
   #  fuel=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.8.2.1.4.1.$OID |cut -d \: -f 2 |cut -c 2-`
     echo "${i}: ${names} " 
   i=`expr $i + 1`
   done
exit

elif [ "$options" = "pa" ]
  then 
  for OID in `$snmpwalk -v 2c -c $community $hostaddress:$port  iso.3.6.1.2.1.43.8.2.1.13.1 2>/dev/null |cut -d " " -f1 |cut -d "." -f13`
  do 
   names=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.8.2.1.13.1.$OID |cut -d \" -f 2`
   fuel=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.8.2.1.4.1.$OID |cut -d \: -f 2 |cut -c 2-`
   if [ $fuel != -2 ]
     then
       fuel=`echo "$fuel / 1000000 * 2.54" |bc |cut -d . -f -1`
       output="${output} ${names} ${fuel}m, " 
   else
     output="${output} ${names} Papersize Unknown, " 
   fi
   if [ "$ecode" != "2" ]
	then
     if [ "$fuel" -le "$critical" ]
	then 
	   ecode=2
     elif [ "$fuel" -le "$warning" ]
       then
	ecode=1
     fi
   fi
  done
elif [[ "$options" = p[0-9] ]]
  then
   paper=`echo $options |cut -c 2-`
   names=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.8.2.1.13.1.$paper |cut -d \" -f 2`
   fuel=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.8.2.1.4.1.$paper |cut -d \: -f 2 |cut -c 2-`
   if [ $fuel != -2 ]
     then
       #fuel=$(awk 'BEGIN{print 10000 / 100000 * 2.54}')
       fuel=`echo "$fuel / 1000000 * 2.54" |bc |cut -d . -f -1`
       output="${names} $fuel m" 
   else
     output="${names} Papersize Unknown" 
   fi
   if [ "$ecode" != "2" ]
	then
     if [ "$fuel" -le "$critical" ]
	then 
	   ecode=2
     elif [ "$fuel" -le "$warning" ]
       then
	ecode=1
     fi
   fi
   if [ "$fuel" = -2 ]
     then 
      output="${names} Papersize Unknown"
      ecode=0
   fi
elif [ "$options" = "hl" ]
  then
   i=1
   for OID in `$snmpwalk -v 2c -Oa -c $community $hostaddress:$port 1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.5.3 2>/dev/null |cut -d . -f 18- |cut -d . -f -1 |uniq`
   do 
     names=`$snmpwalk -v 2c -Oa -c $community $hostaddress:$port 1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.5.3.$OID.13.0 |cut -d \" -f 2`
     echo "${i}: ${names} " 
   i=`expr $i + 1`
   done
exit

elif [ "$options" = "ha" ]
  then 
  for OID in `$snmpwalk -v 2c -Oa -c $community $hostaddress:$port 1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.5.3 2>/dev/null |cut -d . -f 18- |cut -d . -f -1 |uniq`
  do 
   names=`$snmpwalk -v 2c -Oa -c $community $hostaddress:$port 1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.5.3.$OID.13.0 |cut -d \" -f 2`
   fuel=`$snmpwalk -v 2c -Oa -c $community $hostaddress:$port 1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.5.3.$OID.19.0 |cut -d \: -f 2- |cut -c 2-`
   fuel=`echo "$fuel / 100" |bc`
   output="${output} ${names} ${fuel}ml, " 
   if [ "$ecode" != "2" ]
	then
     if [ "$fuel" -le "$critical" ]
	then 
	   ecode=2
     elif [ "$fuel" -le "$warning" ]
       then
	ecode=1
     fi
   fi
  done
elif [[ "$options" = h[0-9] ]]
  then
   ink=`echo $options |cut -c 2-`
   names=`$snmpwalk -v 2c -Oa -c $community $hostaddress:$port 1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.5.3.$ink.13.0 |cut -d \" -f 2`
   fuel=`$snmpwalk -v 2c -Oa -c $community $hostaddress:$port 1.3.6.1.4.1.11.2.3.9.4.2.1.4.1.5.3.$ink.19.0 |cut -d \: -f 2- |cut -c 2-`
   fuel=`echo "$fuel / 100" |bc`
   output="${names} $fuel ml" 
   if [ "$ecode" != "2" ]
	then
     if [ "$fuel" -le "$critical" ]
	then 
	   ecode=2
     elif [ "$fuel" -le "$warning" ]
       then
	ecode=1
     fi
   fi
elif [ "$status" = "s" ]
  then 
   for OID in `$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.18.1.1.2.1  |cut -d " " -f -1 |cut -d . -f 13-` 
   do 
    status=`$snmpwalk -v 2c -c $community $hostaddress:$port 1.3.6.1.2.1.43.18.1.1.2.1.$OID |cut -d \: -f 2 |cut -c 2-`
    value=`$snmpwalk -v 2c -Oa -c $community $hostaddress:$port 1.3.6.1.2.1.43.18.1.1.8.1.$OID |cut -d \: -f 2- |cut -c 2-`
    if [ "$status" = "4" ]
      then	
	output="$output $value"
	if [ "$ecode" != "2" ]
          then
	   ecode=1
	fi
    elif [ "$status" = "3" ]
       then
	output="$output $status"
	ecode=2
    elif [ -z "$output" ]
       then
	output="Status Ok"
    fi
   done
else
   echo "Wrong Option"
   exit
fi
echo $output
exit `echo $ecode`

