-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_printer.php
executable file
·385 lines (347 loc) · 11.1 KB
/
check_printer.php
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/php
<?php
// oids from http://www.oidview.com/mibs/0/Printer-MIB.html
// input types from https://tools.ietf.org/html/rfc1759#page-36
// supply types from https://tools.ietf.org/html/rfc1759#page-60
// supply classes from https://tools.ietf.org/html/rfc1759#page-60
// supply capacities from https://tools.ietf.org/html/rfc1759#page-61
$options = getopt(
"t:m:s:h:w:c:v:",
array(
"ignoreSheetFeedManual"
)
);
// make type/vendor all lowercase
$options["t"] = strtolower($options["t"]);
$oids = array(
"kyocera" => array(
"system" => array(
"totalpages" => "1.3.6.1.2.1.43.10.2.1.4.1.1"
),
"supply" => array(
"index" => "1.3.6.1.2.1.43.11.1.1.2.1.",
"desc" => "1.3.6.1.2.1.43.11.1.1.6.1.",
"type" => "1.3.6.1.2.1.43.11.1.1.5.1.",
"class" => "1.3.6.1.2.1.43.11.1.1.4.1.",
"capacity" => "1.3.6.1.2.1.43.11.1.1.8.1.",
"level" => "1.3.6.1.2.1.43.11.1.1.9.1."
),
"input" => array(
"type" => "1.3.6.1.2.1.43.8.2.1.2.1.",
"capacity" => "1.3.6.1.2.1.43.8.2.1.9.1.",
"level" => "1.3.6.1.2.1.43.8.2.1.10.1.",
"status" => "1.3.6.1.2.1.43.8.2.1.11.1.",
)
)
);
$inputstatuses = array(
0 => "default - available and idle",
6 => "paper motion - available and busy",
9 => "paper out in this tray"
);
$inputtypes = array(
1 => "other",
2 => "unknown",
3 => "sheetFeedAutoRemovableTray",
4 => "sheetFeedAutoNonRemovableTray",
5 => "sheetFeedManual",
6 => "continuousRoll",
7 => "continuousFanFold",
8 => "sheetFeedPull"
);
// "Indicates whether this supply entity represents a supply
// container that is consumed or a receptacle that is filled."
$supplyclasses = array(
1 => "other",
3 => "supplyThatIsConsumed",
4 => "receptacleThatIsFilled"
);
$supplytypes = array(
1 => "other",
2 => "unknown",
3 => "toner",
4 => "wasteToner",
5 => "ink",
6 => "inkCartridge",
7 => "inkRibbon",
8 => "wasteInk",
9 => "opc",
10 => "developer",
11 => "fuserOil",
12 => "solidWax",
13 => "ribbonWax",
14 => "wasteWax",
15 => "fuser",
16 => "coronaWire",
17 => "fuserOilWick",
18 => "cleanerUnit",
19 => "fuserCleaningPad",
20 => "transferUnit",
21 => "tonerCartridge",
22 => "fuserOiler",
23 => "water",
24 => "wasteWater",
25 => "glueWaterAdditive",
26 => "wastePaper",
27 => "bindingSupply",
28 => "bandingSupply",
29 => "stitchingWire",
30 => "shrinkWrap",
31 => "paperWrap",
32 => "staples",
33 => "inserts",
34 => "covers"
);
$supplycapacities = array(
"-1" => "other",
"-2" => "unknown"
);
$supplylevels = array(
"-1" => "other",
"-2" => "unknown",
"-3" => "ok"
);
switch($options["m"]) {
case "supplies":
checkSupplies();
break;
case "inputs":
checkInputs();
break;
case "pages":
checkPageCounter();
break;
default:
echo "usage: check_printer.php [options].\n";
echo "options:\n";
echo "-t Type\n";
echo "-m Mode\n";
echo "-s SNMP Community\n";
echo "-h Host\n";
echo "-w Warnlimit\n";
echo "-c Critlimit\n";
echo "-v SNMP Version\n";
break;
}
function checkPageCounter() {
// set up variables
global $options;
global $oids;
$warn = $options["w"];
$crit = $options["c"];
$type = $options["t"];
// get counter data
$pages["total"] = snmp($oids[$type]["system"]["totalpages"]);
echo "Lifetime total printed pages: ".$pages["total"]."\n";
// set exit code
if($pages["total"] >= $crit) {
// more pages than $crit processed
exitCode("CRITICAL");
} else if($pages["total"] >= $warn) {
// less pages than $crit but more than $warn processed
exitCode("WARNING");
} else if(($pages < $warn) && ($pages >= 0) && (is_numeric($pages))){
// less pages than $warn but no negative amounts processed an $pages is a number
exitCode("OK");
} else {
// negative amount of pages processed or $pages is not numeric
exitCode("UNKNOWN");
}
}
function checkInputs() {
// set up variables
global $options;
global $oids;
global $inputstatuses;
global $inputtypes;
$warn = $options["w"];
$crit = $options["c"];
$type = $options["t"];
$critcount = 0;
$warncount = 0;
$okcount = 0;
// iterate through inputs
$i = 1;
while(snmp($oids[$type]["input"]["type"].$i) != "No Such Instance currently exists at this OID") {
// get input data
$input["type"] = snmp($oids[$type]["input"]["type"].$i);
$input["capacity"] = snmp($oids[$type]["input"]["capacity"].$i);
$input["level"] = snmp($oids[$type]["input"]["level"].$i);
$input["status"] = snmp($oids[$type]["input"]["status"].$i);
// set short variables
$intype = $inputtypes[$input["type"]];
// calculate input usage
$pctlevel = ($input["level"]/$input["capacity"])*100;
if(isset($inputstatuses[$input["status"]])) {
$statustext = $inputstatuses[$input["status"]];
} else {
$statustext = $input["status"];
}
// output info
echo $intype.": ".$pctlevel."% full (".$input["level"]." of ".$input["capacity"]."); status: ".$statustext."\n";
// sheetFeedManual is empty almost all of the time on almost every device
// so --ignoreSheetFeedManual is an option to ignore that in the returned status
// set counters
if(($pctlevel <= $crit) && ($intype != "sheetFeedManual")) {
// less than $crit percent of input medium left
if(($intype == "sheetFeedManual") && ($options["ignoreSheetFeedManual"])) {
$okcount++;
} else {
$critcount++;
}
} else if(($pctlevel <= $warn) && ($intype != "sheetFeedManual")) {
// more than $crit but less than $warn percent of input medium left
if(($intype == "sheetFeedManual") && ($options["ignoreSheetFeedManual"])) {
$okcount++;
} else {
$critcount++;
}
} else if(($pctlevel > $warn) && ($pctlevel <= 100)) {
// more than $warn percent of input medium left (but not more than 100%)
$okcount++;
}
$i++;
}
// set exit code
if($critcount > 0) {
// at least one input critical
exitCode("CRITICAL");
} else if($warncount > 0) {
// no input critical but at least one had a warning
exitCode("WARNING");
} else if($okcount == $i){
// every input ok
exitCode("OK");
} else {
// no criticals, no warnings but also not everything ok
exitCode("UNKNOWN");
}
}
function checkSupplies() {
// set up variables
global $options;
global $oids;
global $supplytypes;
$warn = $options["w"];
$crit = $options["c"];
$type = $options["t"];
$critcount = 0;
$warncount = 0;
$okcount = 0;
// iterate through supplies
$i = 1;
while(snmp($oids[$type]["supply"]["index"].$i) != "No Such Instance currently exists at this OID") {
// get supply data
$supply["desc"] = snmp($oids[$type]["supply"]["desc"].$i);
$supply["class"] = snmp($oids[$type]["supply"]["class"].$i);
$supply["type"] = snmp($oids[$type]["supply"]["type"].$i);
$supply["capacity"] = snmp($oids[$type]["supply"]["capacity"].$i);
$supply["level"] = snmp($oids[$type]["supply"]["level"].$i);
// intercept negative capacity
if($supply["capacity"] < 0) {
// -1=other, -2=unknown
// -> do nothing - so $okcount wont be full and status will be unknown
}
// intercept negative level
if($supply["level"] < 0) {
// -1=other, -2=unknown, -3=ok
switch($supply["level"]) {
case "-3":
echo $supply["desc"]." (".$supplytypes[$supply["type"]]."): OK\n";
$okcount++;
break;
case "-1":
case "-2":
default:
// do nothing - so $okcount wont be full and status will be unknown
break;
}
}
// intercept unknown supply
if($supply["capacity"] == -2 and $supply["level"] == -2) {
echo $supplytypes[$supply["type"]]." with unknown capacity and unknown level\n";
} else {
// if capacity and level both are not negative...
if(($supply["capacity"] >= 0) && ($supply["level"] >= 0)) {
// calculate the percentage of supply left
$pctlevel = ($supply["level"]/$supply["capacity"])*100;
// output info according to supply class
switch($supply["class"]) {
case 1:
// other
echo $supply["desc"]." (".$supplytypes[$supply["type"]]."): ".$pctlevel."%\n";
case 3:
// 3 = supplyThatIsConsumed
echo $supply["desc"]." (".$supplytypes[$supply["type"]]."): ".$pctlevel."% left\n";
break;
case 4:
// 4 = receptacleThatIsFilled
// for receptacles the level is the remaining amount
// -> so invert it to show the used amount
$pctlevel = 100-$pctlevel;
echo $supply["desc"]." (".$supplytypes[$supply["type"]]."): ".$pctlevel."% full\n";
}
}
if(isset($pctlevel)) {
// set counters
if($pctlevel <= $crit) {
// less than $critical percent of this supply left
$critcount++;
} else if($pctlevel <= $warn) {
// more than $critical but less than $warning percent of this supply left
$warncount++;
} else if(($pctlevel > $warn) && ($pctlevel <= 100)) {
// more than $warning percent of this supply left (but not more than 100%)
$okcount++;
}
}
}
$i++;
}
// set exit code
if($critcount > 0) {
// at least one supply was critical
exitCode("CRITICAL");
} else if($warncount > 0) {
// no supply was critical but at least one had a warning
exitCode("WARNING");
} else if($okcount == $i){
// every supply was ok
exitCode("OK");
} else {
// no critical, no warning but also not everything ok
exitCode("UNKNOWN");
}
}
function snmp($oid) {
// get host, snmp community and snmp version
global $options;
$host = $options["h"];
$community = $options["s"];
$version = $options["v"];
// prepare and execute snmp query and
// remove leading and trailing whitespaces, quotation marks, line feeds etc.
// option "-O vq" returns only value (and unit)
$cmd = "snmpget -v".$version." -c ".$community." -O vq ".$host." ".$oid;
$result = trim(shell_exec($cmd), " \n\r\t\v\0\"");
// and return query result
return $result;
}
function exitCode($status) {
switch($status) {
case "OK":
exit(0);
break;
case "WARNING":
exit(1);
break;
case "CRITICAL":
exit(2);
break;
case "UNKNOWN":
default:
exit(3);
break;
}
}
?>