#!/bin/bash ###################created by Pascal Dokan Zahn ######################################### # # copyright (c) 2016 Pascal Dokan Zahn # 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; # # 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. # # contact the author directly for more information at: pascalza@p-zahn.net ########################################################################################## #Version 1.00 if [ ! "$#" == "5" ]; then echo -e "\nWarning: Wrong command line arguments. \nUsage: ./check_hp_printer \n \nParts are: black, yellow, cyan and magenta nExample: ./check_hp_printer 127.0.0.1 public black 80 95\n" && exit "3" fi strHostname=$1 strCommunity=$2 strpart=$3 strWarning=$4 strCritical=$5 # # Check whether the printer is availlable TEST=$(snmpstatus -v 1 $Hostname -c "$strCommunity" -t 5 -r 0 2>&1) # echo "Test: $TEST"; if [ "$TEST" == "Timeout: No Response from $strHostname" ]; then echo "CRITICAL: SNMP to $strHostname is not available"; exit 2; fi # Black ink usage --------------------------------------------------------------------------------------------------------------------------------------- if [ "$strpart" == "black" ]; then maxink=$(snmpget -v1 -c "$strCommunity" "$strHostname" 1.3.6.1.2.1.43.11.1.1.8.0.1 | awk '{print $4}' | sed 's/.\(.*\)...../\1/') left=$(snmpget -v1 -c "$strCommunity" "$strHostname" 1.3.6.1.2.1.43.11.1.1.9.0.1 | awk '{print $4}' | sed 's/.\(.*\)...../\1/') used=$(echo "scale=0; $maxink-$left" | bc -l) PERC=$(echo "scale=0; $used*100/$maxink" | bc -l) leftPerc=$(echo "scale=0; $left*100/$maxink" | bc -l) OUTPUT="Ink level:"$leftPerc%" left |Used=$PERC%;$strWarning;$strCritical;0;100" if [ $PERC -ge $strCritical ]; then echo "CRITICAL: "$OUTPUT exit 2 elif [ $PERC -ge $strWarning ]; then echo "WARNING: "$OUTPUT exit 1 else echo "OK: "$OUTPUT exit 0 fi # Yellow ink usage---------------------------------------------------------------------------------------------------------------------------------------------- elif [ "$strpart" == "yellow" ]; then maxink=$(snmpget -v1 -c "$strCommunity" "$strHostname" 1.3.6.1.2.1.43.11.1.1.8.0.2 | awk '{print $4}' | sed 's/.\(.*\)...../\1/') left=$(snmpget -v1 -c "$strCommunity" "$strHostname" 1.3.6.1.2.1.43.11.1.1.9.0.2 | awk '{print $4}' | sed 's/.\(.*\)...../\1/') used=$(echo "scale=0; $maxink-$left" | bc -l) PERC=$(echo "scale=0; $used*100/$maxink" | bc -l) leftPerc=$(echo "scale=0; $left*100/$maxink" | bc -l) OUTPUT="Ink level:"$leftPerc%" left |Used=$PERC%;$strWarning;$strCritical;0;100" if [ $PERC -ge $strCritical ]; then echo "CRITICAL: "$OUTPUT exit 2 elif [ $PERC -ge $strWarning ]; then echo "WARNING: "$OUTPUT exit 1 else echo "OK: "$OUTPUT exit 0 fi #Cyan ink usage------------------------------------------------------------------------------------------------------------------------ elif [ "$strpart" == "cyan" ]; then maxink=$(snmpget -v1 -c "$strCommunity" "$strHostname" 1.3.6.1.2.1.43.11.1.1.8.0.3 | awk '{print $4}' | sed 's/.\(.*\)...../\1/') left=$(snmpget -v1 -c "$strCommunity" "$strHostname" 1.3.6.1.2.1.43.11.1.1.9.0.3 | awk '{print $4}' | sed 's/.\(.*\)...../\1/') used=$(echo "scale=0; $maxink-$left" | bc -l) PERC=$(echo "scale=0; $used*100/$maxink" | bc -l) leftPerc=$(echo "scale=0; $left*100/$maxink" | bc -l) OUTPUT="Ink level:"$leftPerc%" left |Used=$PERC%;$strWarning;$strCritical;0;100" if [ $PERC -ge $strCritical ]; then echo "CRITICAL: "$OUTPUT exit 2 elif [ $PERC -ge $strWarning ]; then echo "WARNING: "$OUTPUT exit 1 else echo "OK: "$OUTPUT exit 0 fi #Magenta ink usage--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #There is a bug in the HP MIB tree. You can't triger the maxink or left ink directly. It will show you "No such name error etc.". #Fix: use snmpgetnext and it will find it. Curious but it works this way :-D. elif [ "$strpart" == "magenta" ]; then maxink=$(snmpgetnext v1 -c "$strCommunity" "$strHostname" .1.3.6.1.2.1.43.11.1.1.8.0.3 | awk '{print $4}' | sed 's/.\(.*\)...../\1/') left=$(snmpgetnext -v1 -c "$strCommunity" "$strHostname" .1.3.6.1.2.1.43.11.1.1.9.0.3 | awk '{print $4}' | sed 's/.\(.*\)...../\1/') used=$(echo "scale=0; $maxink-$left" | bc -l) PERC=$(echo "scale=0; $used*100/$maxink" | bc -l) leftPerc=$(echo "scale=0; $left*100/$maxink" | bc -l) OUTPUT="Left Ink:"$leftPerc%" |Used=$PERC%;$strWarning;$strCritical;0;100" if [ $PERC -ge $strCritical ]; then echo "CRITICAL: "$OUTPUT exit 2 elif [ $PERC -ge $strWarning ]; then echo "WARNING: "$OUTPUT exit 1 else echo "OK: "$OUTPUT exit 0 fi # System Uptime---------------------------------------------------------------------------------------------------------------------------------------- elif [ "$strpart" == "systemuptime" ]; then uptime=$(snmpget -v1 -c "$strCommunity" "$strHostname" .1.3.6.1.2.1.1.3.0 | awk '{print $5, $6, $7, $8}') echo $uptime exit 0 #---------------------------------------------------------------------------------------------------------------------------------------------------- else echo -e "\nUnknown Part!" && exit "3" fi exit 0