-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_snmp_printer
executable file
·312 lines (268 loc) · 8.67 KB
/
check_snmp_printer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
#
# Program : check_snmp_printer.sh
# :
# Author : Jason Rivers <[email protected]>
# :
# Purpose : Nagios plugin to return the Remaining pages on a Lexmark C530dn Toner (may work with other Lexmark printers)
# :
# Parameters : --help
# : --version
# :
# Returns : Standard Nagios status_* codes as defined in utils.sh
# :
# Notes : Added support for Canon, also changed the MANUFACTURER MIB call, modifications for Mat with issues importing MIBs into SNMP
#============:==============================================================
#
# Shamelessly stolen from other Nagios plugins.
#
#set -x
PROGNAME=`basename $0`
PROGPATH=`echo $0 | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: 1.3.0.1 $' | sed -e 's/[^0-9.]//g'`
. $PROGPATH/utils.sh
print_usage() {
echo "Usage: $PROGNAME --help"
echo "Usage: $PROGNAME --version"
}
print_help() {
print_revision $PROGNAME $REVISION
echo ""
print_usage
echo ""
echo "Printer Toner Status Check."
echo ""
echo "Usage:"
echo "print_snmp_printer [opts]"
echo ""
echo "Options:"
echo " -h|--help Show help"
echo " -V|--version Show version information"
echo " -H Remote Hostname"
echo " -C Check type"
echo " BLACK Black Toner"
echo " CYAN Cyan Toner"
echo " MAGENTA Magenta Toner"
echo " YELLOW Yellow Toner"
echo " MAINTENANCE Maintenance Kit"
echo " STATUS Printer Status"
echo " -w Warning number"
echo " -c Critical number"
exit 0
# support
}
# No command line arguments are required for this script. We accept only 2,
# --help and --version. If more than 1 is passed in then we have an error
# condition.
#if [ $# -le 4 ]; then
# print_usage
# exit $STATE_UNKNOWN
#fi
#
# If we have arguments, process them.
#
exitstatus=$STATE_WARNING #default
### Defaults
ISCOLOUR=false ## Default
CRITICALNUMBER=10
WARNINGNUMBER=20
CHECK="BLACK" ## Black Toner by default
while test -n "$1"; do
case "$1" in
--help)
print_help
exit $STATE_OK
;;
-h)
print_help
exit $STATE_OK
;;
--version)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-V)
print_revision $PROGNAME $REVISION
exit $STATE_OK
;;
-H)
REMOTEHOST=$2;
shift;
;;
-C) CHECK=$2;
shift;
;;
-c)
CRITICALNUMBER=$2
shift;
;;
-w)
WARNINGNUMBER=$2;
shift;
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
if [ "$ITEM" = "" ]; then
ITEM="TONER"
fi
#### Find out if we can use MIBS or ISO.
SNMPRES=`snmpget -m ALL -v 2c -c public $REMOTEHOST iso.3.6.1.2.1.43.8.2.1.14.1.1 2>/dev/null`
USEISO=`echo $SNMPRES | grep -i "^iso" | head -n 1`
USEPMIB=`echo $SNMPRES | grep -i "^Printer-MIB" | head -n 1`
if [ "$USEPMIB" ]; then
SNMPSUPPYSTRING="Printer-MIB::prtMarkerSuppliesDescription.1."
SNMPNAMECODE="Printer-MIB::prtMarkerSuppliesDescription.1."
SNMPTOTALPAGES="Printer-MIB::prtMarkerSuppliesMaxCapacity.1."
SNMPREMAININGPAGES="Printer-MIB::prtMarkerSuppliesLevel.1."
SNMPSTATUS="Printer-MIB::prtConsoleDescription.1.2"
SNMPMANU="Printer-MIB::prtInputVendorName.1.1"
elif [ "$USEISO" ]; then
SNMPSTATUS1="iso.3.6.1.2.1.43.16.5.1.2.1.1"
SNMPSTATUS2="iso.3.6.1.2.1.43.16.5.1.2.1.2"
SNMPSTATUS3="iso.3.6.1.2.1.43.16.5.1.2.1.3"
SNMPSTATUS4="iso.3.6.1.2.1.43.16.5.1.2.1.4"
SNMPSUPPLYSTRING="iso.3.6.1.2.1.43.11.1.1.6.1."
SNMPNAMECODE="iso.3.6.1.2.1.43.11.1.1.6.1."
SNMPTOTALPAGES="iso.3.6.1.2.1.43.11.1.1.8.1."
SNMPREMAININGPAGES="iso.3.6.1.2.1.43.11.1.1.9.1."
SNMPSTATUS="iso.3.6.1.2.1.43.17.6.1.5.1.1"
SNMPMANU="iso.3.6.1.2.1.43.8.2.1.14.1.1"
else
SNMPSTATUS1="mib-2.43.16.5.1.2.1.1"
SNMPSTATUS2="mib-2.43.16.5.1.2.1.2"
SNMPSTATUS3="mib-2.43.16.5.1.2.1.3"
SNMPSTATUS4="mib-2.43.16.5.1.2.1.4"
SNMPSUPPLYSTRING="mib-2.43.11.1.1.6.1."
SNMPNAMECODE="mib-2.43.11.1.1.6.1."
SNMPTOTALPAGES="mib-2.43.11.1.1.8.1."
SNMPREMAININGPAGES="mib-2.43.11.1.1.9.1."
SNMPSTATUS="mib-2.43.17.6.1.5.1.1"
SNMPMANU="mib-2.43.8.2.1.14.1.1"
fi
## Get the Manufacturer
MANU=`snmpget -m ALL -v 2c -c public $REMOTEHOST ${SNMPMANU} 2>/dev/null | sed 's/.*\ =\ //;s/STRING: //;s/"//g'`
if [ "$MANU" = "No Such Object available on this agent at this OID" ]; then
MANU=`snmpget -m ALL -v 2c -c public $REMOTEHOST iso.3.6.1.4.1.1347.40.35.1.1.2.1 2>/dev/null | sed "s/.*\ =\ //;s/STRING: //;s/\"//g" | awk '{print $1}'`
fi
STR2=`snmpget -m ALL -v 2c -c public $REMOTEHOST ${SNMPSUPPYSTRING}2 2>/dev/null | sed "s/.*\ \"//;s/\"//"`
if [ "`echo $STR2 | grep -i cyan`" ] || [ "`echo $STR2 | grep -i magenta`" ]; then
ISCOLOUR=true
fi
# Work out if we're using CMYK (Sane) or BCMY (damn you)!
## There's probably a better way to work this out by querying the printer
if [ "$MANU" = "Canon" ] || [ "$MANU" = "Hewlett-Packard" ] || [ "$MANU" = "Kyocera" ]; then
#Damn you Canon / HP
BLACK=1
if [ "$ISCOLOUR" = "true" ]; then
CYAN=2
MAGENTA=3
YELLOW=4
MAINTKIT=5
else
MAINTKIT=2
fi
else #CMYK
CYAN=1
MAGENTA=2
YELLOW=3
BLACK=4
MAINTKIT=5
if [ "$MANU" = "Samsung Electronics" ] || [ "$MANU" = "Xerox" ]; then
## Samsung & Xerox show a percentage of ink remaining, not how many pages - Others probably do, too - and I can probably work this out by querying the printer...
PERC="TRUE"
if [ $CRITICALNUMBER -ge 100 ]; then
CRITICALNUMBER=10
fi
if [ $WARNINGNUMBER -ge 100 ]; then
WARNINGNUMBER=5
fi
fi
fi
## find out if we have a colour printer
if snmpget -m ALL -v 2c -c public $REMOTEHOST ${SNMPNAMECODE}${CYAN} 2>/dev/null| grep -i "cyan" >/dev/null 2>&1 ; then
COLOURP=true
if [ "$CHECK" = "CYAN" ]; then
CHECKCOLOUR=$CYAN
elif [ "$CHECK" = "MAGENTA" ]; then
CHECKCOLOUR=$MAGENTA
elif [ "$CHECK" = "YELLOW" ]; then
CHECKCOLOUR=$YELLOW
elif [ "$CHECK" = "BLACK" ]; then
CHECKCOLOUR=$BLACK
elif [ "$CHECK" = "STATUS" ]; then
CHECKCOLOUR=0
elif [ "$CHECK" = "MAINTENANCE" ]; then
CHECKCOLOUR=$MAINTKIT
fi
else
COLOURP=false
if [ "$CHECK" = "CYAN" ]; then
echo "NO CYAN TONER"
exit 2
elif [ "$CHECK" = "MAGENTA" ]; then
echo "NO MAGENTA TONER"
exit 2
elif [ "$CHECK" = "YELLOW" ]; then
echo "NO YELLOW TONER"
exit 2
elif [ "$CHECK" = "BLACK" ]; then
CHECKCOLOUR=$BLACK
elif [ "$CHECK" = "STATUS" ]; then
CHECKCOLOUR=0
elif [ "$CHECK" = "BLACKDRUM" ]; then
CHECKCOLOUR=$BLACKDRUM
elif [ "$CHECK" = "MAINTENANCE" ]; then
CHECKCOLOUR=$MAINTKIT
fi
fi
if [ $CHECKCOLOUR -eq 0 ]; then
PRINTERSTATUS=`snmpget -m ALL -v 2c -c public $REMOTEHOST ${SNMPSTATUS1} 2>/dev/null| sed "s/.*STRING: \(.*\)/\1/;s/\"//g"`
if echo $PRINTERSTATUS | grep "Load Tray" ; then
PAPERTYPE=`snmpget -m ALL -v 2c -c public $REMOTEHOST ${SNMPSTATUS2} 2>/dev/null| sed "s/.*STRING: \(.*\)/\1/;s/\"//g"`
MSG="Out of paper, ${PRINTERSTATUS} ${PAPERTYPE}"
exitstatus=$STATE_CRITICAL
elif echo $PRINTERSTATUS | grep -i "jam" ; then
MSG="Printer Status: CRITICAL:- Paper Jam"
exitstatus=$STATE_CRITICAL
else
MSG="Printer Status: OK"
exitstatus=$STATE_OK
fi
else
TONERNAME=`snmpget -m ALL -v 2c -c public $REMOTEHOST ${SNMPNAMECODE}${CHECKCOLOUR} 2>/dev/null| sed "s/.*STRING: \(.*\)/\1/;s/\"//g"`
TOTPAGES=`snmpget -m ALL -v 2c -c public $REMOTEHOST ${SNMPTOTALPAGES}${CHECKCOLOUR} 2>/dev/null| sed "s/.*INTEGER: \(.*\)/\1/;s/\"//g"`
REMPAGES=`snmpget -m ALL -v 2c -c public $REMOTEHOST ${SNMPREMAININGPAGES}${CHECKCOLOUR} 2>/dev/null| sed "s/.*INTEGER: \(.*\)/\1/;s/\"//g"`
if [ $TOTPAGES = 100 ]; then
PERC="TRUE"
fi
if [ $REMPAGES -le 0 ]; then
REMPAGES=0
fi
if [ $REMPAGES -le $CRITICALNUMBER ]; then
exitstatus=$STATE_CRITICAL
exitmsg="CRITICAL:"
TONERMSG="low"
elif [ $REMPAGES -ge $WARNINGNUMBER ]; then
exitstatus=$STATE_OK
exitmsg="OK:"
TONERMSG="OK"
else
exitstatus=$STATE_WARNING
exitmsg="WARNING:"
TONERMSG="low"
fi
if [ "$PERC" = "TRUE" ]; then
MSG="${exitmsg} ${REMPAGES}% remaining on ${TONERNAME}. Status: ${TONERNAME} ${TONERMSG}|${TONERNAME}=${REMPAGES};${WARNINGNUMBER};${CRITICALNUMBER};0;100"
else
MSG="${exitmsg} ${REMPAGES} of ${TOTPAGES} pages remaining on ${TONERNAME}. Status: ${TONERNAME} ${TONERMSG}|${TONERNAME}=${REMPAGES};${WARNINGNUMBER};${CRITICALNUMBER};;"
fi
fi
## Show the message and exit with the Nagios exit code
echo $MSG
exit $exitstatus