#!/bin/sh
# bb-hpljc.sh
#
# BIG BROTHER - HP Color LaserJet External Script
# Copyright (c) 2002 by Pascal Fuckerieder
#
# 2002-08-12 Some fixes by (Thanx to Andreas Schulz)
#

HOSTIP=$1
FULLNAME=$2

BBPROG=bb-hplj.sh; export BBPROG

#
# SNMPGET shows us where to find snmpget
#
SNMPGET="/usr/bin/snmpget"
SNMPWALK="/usr/bin/snmpwalk"

if test "$BBHOME" = ""
then
        echo "BBHOME is not set... exiting"
        exit 1
fi

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
        . $BBHOME/etc/bbdef.sh          # INCLUDE STANDARD DEFINITIONS
fi

#
# SELECT SOME LEVELS... GREEN IS THE DEFAULT...
#
CONSUM_WARN="10"		# GO YELLOW AT THIS LEVEL
CONSUM_PANIC="5"		# GO RED AND PAGE AT THIS LEVEL

LINE=""
COLOR="green"

#
# GRAB THE MODEL AND THE CONSUMABLES FROM THE PRINTER VIA SNMP
#
MODEL=`$SNMPGET -v 1 -c public $HOSTIP host.hrDevice.hrDeviceTable.hrDeviceEntry.hrDeviceDescr.1 2>/dev/null | cut -d" " -f3-`
LOCATION=`$SNMPGET -v 1 -c public $HOSTIP system.sysLocation.0 | cut -d " " -f3-`
COUNTER=`$SNMPGET -v 1 -c public $HOSTIP 43.10.2.1.4.1.1 | cut -d " " -f 4`
MESSAGES=`$SNMPWALK -v 1 -c public $HOSTIP 43.18.1.1.8 | cut -d " " -f3- | tr -d "\""`

BLACK_UNITNAME=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.6.1.1 2>/dev/null | cut -d " " -f3- | tr -d "\""`
BLACK_FULL=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.8.1.1 2>/dev/null | cut -d " " -f3- | tr -d "\""`
BLACK_STATUS=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.9.1.1 2>/dev/null | cut -d " " -f3- | tr -d "\""`
BLACK_LEVEL=`echo $BLACK_STATUS*100/$BLACK_FULL | bc 2>/dev/null`

CYAN_UNITNAME=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.6.1.2 2>/dev/null | cut -d " " -f3- | tr -d "\""`
CYAN_FULL=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.8.1.2 2>/dev/null | cut -d " " -f3- | tr -d "\""`
CYAN_STATUS=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.9.1.2 2>/dev/null | cut -d " " -f3- | tr -d "\""`
CYAN_LEVEL=`echo $CYAN_STATUS*100/$CYAN_FULL | bc 2>/dev/null`

MAGENTA_UNITNAME=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.6.1.3 2>/dev/null | cut -d " " -f3- | tr -d "\""`
MAGENTA_FULL=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.8.1.3 2>/dev/null | cut -d " " -f3- | tr -d "\""`
MAGENTA_STATUS=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.9.1.3 2>/dev/null | cut -d " " -f3- | tr -d "\""`
MAGENTA_LEVEL=`echo $MAGENTA_STATUS*100/$MAGENTA_FULL | bc 2>/dev/null`

YELLOW_UNITNAME=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.6.1.4 2>/dev/null | cut -d " " -f3- | tr -d "\""`
YELLOW_FULL=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.8.1.4 2>/dev/null | cut -d " " -f3- | tr -d "\""`
YELLOW_STATUS=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.9.1.4 2>/dev/null | cut -d " " -f3- | tr -d "\""`
YELLOW_LEVEL=`echo $YELLOW_STATUS*100/$YELLOW_FULL | bc 2>/dev/null`

DRUM_UNITNAME=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.6.1.5 2>/dev/null | cut -d " " -f3- | tr -d "\""`
DRUM_FULL=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.8.1.5 2>/dev/null | cut -d " " -f3- | tr -d "\""`
DRUM_STATUS=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.9.1.5 2>/dev/null | cut -d " " -f3- | tr -d "\""`
DRUM_LEVEL=`echo $DRUM_STATUS*100/$DRUM_FULL | bc 2>/dev/null`

TRANSFER_UNITNAME=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.6.1.6 2>/dev/null | cut -d " " -f3- | tr -d "\""`
TRANSFER_FULL=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.8.1.6 2>/dev/null | cut -d " " -f3- | tr -d "\""`
TRANSFER_STATUS=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.9.1.6 2>/dev/null | cut -d " " -f3- | tr -d "\""`
TRANSFER_LEVEL=`echo $TRANSFER_STATUS*100/$TRANSFER_FULL | bc 2>/dev/null`

FUSER_UNITNAME=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.6.1.7 2>/dev/null | cut -d " " -f3- | tr -d "\""`
FUSER_FULL=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.8.1.7 2>/dev/null | cut -d " " -f3- | tr -d "\""`
FUSER_STATUS=`$SNMPGET -v 1 -c public $HOSTIP 43.11.1.1.9.1.7 2>/dev/null | cut -d " " -f3- | tr -d "\""`
FUSER_LEVEL=`echo $FUSER_STATUS*100/$FUSER_FULL | bc 2>/dev/null`

#
# CHECK BLACK TONER
#
if [ "$BLACK_LEVEL" = "" ]
then
        COLOR="black"
        LINE="&black No status available"
else
	if [ "$BLACK_LEVEL" -le "$CONSUM_PANIC" ]
	then
		COLOR="red"
		LINE="$LINE&red $BLACK_UNITNAME of $MODEL in $LOCATION is at panic level ($BLACK_LEVEL%)
"
	else
		if [ "$BLACK_LEVEL" -le "$CONSUM_WARN" ]
		then
			COLOR="yellow"
			LINE="$LINE&yellow $BLACK_UNITNAME of $MODEL in $LOCATION is at warning level ($BLACK_LEVEL%)
"
		fi
	fi
fi

#
# CHECK CYAN TONER
#
if [ "$CYAN_LEVEL" = "" ]
then
        COLOR="black"
        LINE="&black No status available"
else
	if [ "$CYAN_LEVEL" -le "$CONSUM_PANIC" ]
	then
		COLOR="red"
		LINE="$LINE&red $CYAN_UNITNAME of $MODEL in $LOCATION is at panic level ($CYAN_LEVEL%)
"
	else
		if [ "$CYAN_LEVEL" -le "$CONSUM_WARN" ]
		then
			COLOR="yellow"
			LINE="$LINE&yellow $CYAN_UNITNAME of $MODEL in $LOCATION is at warning level ($CYAN_LEVEL%)
"
		fi
	fi
fi

#
# CHECK MAGENTA TONER
#
if [ "$MAGENTA_LEVEL" = "" ]
then
        COLOR="black"
        LINE="&black No status available"
else
	if [ "$MAGENTA_LEVEL" -le "$CONSUM_PANIC" ]
	then
		COLOR="red"
		LINE="$LINE&red $MAGENTA_UNITNAME of $MODEL in $LOCATION is at panic level ($MAGENTA_LEVEL%)
"
	else
		if [ "$MAGENTA_LEVEL" -le "$CONSUM_WARN" ]
		then
			COLOR="yellow"
			LINE="$LINE&yellow $MAGENTA_UNITNAME of $MODEL in $LOCATION is at warning level ($MAGENTA_LEVEL%)
"
		fi
	fi
fi

#
# CHECK YELLOW TONER
#
if [ "$YELLOW_LEVEL" = "" ]
then
        COLOR="black"
        LINE="&black No status available"
else
	if [ "$YELLOW_LEVEL" -le "$CONSUM_PANIC" ]
	then
		COLOR="red"
		LINE="$LINE&red $YELLOW_UNITNAME of $MODEL in $LOCATION is at panic level ($YELLOW_LEVEL%)
"
	else
		if [ "$YELLOW_LEVEL" -le "$CONSUM_WARN" ]
		then
			COLOR="yellow"
			LINE="$LINE&yellow $YELLOW_UNITNAME of $MODEL in $LOCATION is at warning level ($YELLOW_LEVEL%)
"
		fi
	fi
fi

#
# CHECK DRUM KIT
#
if [ "$DRUM_LEVEL" = "" ]
then
        COLOR="black"
        LINE="&black No status available"
else
	if [ "$DRUM_LEVEL" -le "$CONSUM_PANIC" ]
	then
		COLOR="red"
		LINE="$LINE&red $DRUM_UNITNAME of $MODEL in $LOCATION is at panic level ($DRUM_LEVEL%)
"
	else
		if [ "$DRUM_LEVEL" -le "$CONSUM_WARN" ]
		then
			COLOR="yellow"
			LINE="$LINE&yellow $DRUM_UNITNAME of $MODEL in $LOCATION is at warning level ($DRUM_LEVEL%)
"
		fi
	fi
fi

#
# CHECK TRANSFER KIT
#
if [ "$TRANSFER_LEVEL" = "" ]
then
        COLOR="black"
        LINE="&black No status available"
else
	if [ "$TRANSFER_LEVEL" -le "$CONSUM_PANIC" ]
	then
		COLOR="red"
		LINE="$LINE&red $TRANSFER_UNITNAME of $MODEL in $LOCATION is at panic level ($TRANSFER_LEVEL%)
"
	else
		if [ "$TRANSFER_LEVEL" -le "$CONSUM_WARN" ]
		then
			COLOR="yellow"
			LINE="$LINE&yellow $TRANSFER_UNITNAME of $MODEL in $LOCATION is at warning level ($TRANSFER_LEVEL%)
"
		fi
	fi
fi

#
# CHECK FUSER KIT
#
if [ "$FUSER_LEVEL" = "" ]
then
        COLOR="black"
        LINE="&black No status available"
else
	if [ "$FUSER_LEVEL" -le "$CONSUM_PANIC" ]
	then
		COLOR="red"
		LINE="$LINE&red $FUSER_UNITNAME of $MODEL in $LOCATION is at panic level ($FUSER_LEVEL%)
"
	else
		if [ "$FUSER_LEVEL" -le "$CONSUM_WARN" ]
		then
			COLOR="yellow"
			LINE="$LINE&yellow $FUSER_UNITNAME of $MODEL in $LOCATION is at warning level ($FUSER_LEVEL%)
"
		fi
	fi
fi

#
# CREATE OUTPUT FOR STATUS PAGE
#
if [ "$COLOR" = "green" ]
then
	LINE="&green All items of $MODEL are OK
"
fi
MSG_BLACK=`printf "%-50.50s %3.0f%%\n" "$BLACK_UNITNAME" $BLACK_LEVEL`
MSG_CYAN=`printf "%-50.50s %3.0f%%\n" "$CYAN_UNITNAME" $CYAN_LEVEL`
MSG_MAGENTA=`printf "%-50.50s %3.0f%%\n" "$MAGENTA_UNITNAME" $MAGENTA_LEVEL`
MSG_YELLOW=`printf "%-50.50s %3.0f%%\n" "$YELLOW_UNITNAME" $YELLOW_LEVEL`
MSG_DRUM=`printf "%-50.50s %3.0f%%\n" "$DRUM_UNITNAME" $DRUM_LEVEL`
MSG_TRANSFER=`printf "%-50.50s %3.0f%%\n" "$TRANSFER_UNITNAME" $TRANSFER_LEVEL`
MSG_FUSER=`printf "%-50.50s %3.0f%%\n" "$FUSER_UNITNAME" $FUSER_LEVEL`
MSG_COUNTER=`printf "%-48.48s %6.0f\n" "Page Counter" "$COUNTER"`

LINE="
$LINE
$MSG_BLACK
$MSG_CYAN
$MSG_MAGENTA
$MSG_YELLOW
$MSG_DRUM
$MSG_TRANSFER
$MSG_FUSER
$MSG_COUNTER

Messages:
$MESSAGES
"

$BB $BBDISP "status ${MACHINE}.toner $COLOR `date` $LINE"
