aboutsummaryrefslogtreecommitdiff
path: root/plugins/check_hpjd.c
blob: 5699570c08e8a064bfb2dd259a0f8df54e3c2536 (plain)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/******************************************************************************
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*****************************************************************************/

#include "common.h"
#include "popen.h"
#include "utils.h"
#include "netutils.h"

#define DEFAULT_COMMUNITY "public"

const char *progname = "check_hpjd";
const char *revision = "$Revision$";
const char *copyright = "2000-2003";
const char *email = "nagiosplug-devel@lists.sourceforge.net";

const char *option_summary = "-H host [-C community]\n";

#define HPJD_LINE_STATUS           ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
#define HPJD_PAPER_STATUS          ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
#define HPJD_INTERVENTION_REQUIRED ".1.3.6.1.4.1.11.2.3.9.1.1.2.3"
#define HPJD_GD_PERIPHERAL_ERROR   ".1.3.6.1.4.1.11.2.3.9.1.1.2.6"
#define HPJD_GD_PAPER_JAM          ".1.3.6.1.4.1.11.2.3.9.1.1.2.8"
#define HPJD_GD_PAPER_OUT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.9"
#define HPJD_GD_TONER_LOW          ".1.3.6.1.4.1.11.2.3.9.1.1.2.10"
#define HPJD_GD_PAGE_PUNT          ".1.3.6.1.4.1.11.2.3.9.1.1.2.11"
#define HPJD_GD_MEMORY_OUT         ".1.3.6.1.4.1.11.2.3.9.1.1.2.12"
#define HPJD_GD_DOOR_OPEN          ".1.3.6.1.4.1.11.2.3.9.1.1.2.17"
#define HPJD_GD_PAPER_OUTPUT       ".1.3.6.1.4.1.11.2.3.9.1.1.2.19"
#define HPJD_GD_STATUS_DISPLAY     ".1.3.6.1.4.1.11.2.3.9.1.1.3"

#define ONLINE		0
#define OFFLINE		1

int process_arguments (int, char **);
int validate_arguments (void);
void print_help (void);
void print_usage (void);

char *community = NULL;
char *address = NULL;

int
main (int argc, char **argv)
{
	char command_line[1024];
	int result;
	int line;
	char input_buffer[MAX_INPUT_BUFFER];
	char query_string[512];
	char *errmsg;
	char *temp_buffer;
	int line_status = ONLINE;
	int paper_status = 0;
	int intervention_required = 0;
	int peripheral_error = 0;
	int paper_jam = 0;
	int paper_out = 0;
	int toner_low = 0;
	int page_punt = 0;
	int memory_out = 0;
	int door_open = 0;
	int paper_output = 0;
	char display_message[MAX_INPUT_BUFFER];

	errmsg = malloc(MAX_INPUT_BUFFER);

	setlocale (LC_ALL, "");
	bindtextdomain (PACKAGE, LOCALEDIR);
	textdomain (PACKAGE);

	if (process_arguments (argc, argv) != OK)
		usage (_("Incorrect arguments supplied\n"));

	/* removed ' 2>1' at end of command 10/27/1999 - EG */
	/* create the query string */
	sprintf
		(query_string,
		 "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0",
		 HPJD_LINE_STATUS,
		 HPJD_PAPER_STATUS,
		 HPJD_INTERVENTION_REQUIRED,
		 HPJD_GD_PERIPHERAL_ERROR,
		 HPJD_GD_PAPER_JAM,
		 HPJD_GD_PAPER_OUT,
		 HPJD_GD_TONER_LOW,
		 HPJD_GD_PAGE_PUNT,
		 HPJD_GD_MEMORY_OUT,
		 HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY);

	/* get the command to run */
	sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s %s", PATH_TO_SNMPGET, community, 
									address, query_string);

	/* run the command */
	child_process = spopen (command_line);
	if (child_process == NULL) {
		printf (_("Could not open pipe: %s\n"), command_line);
		return STATE_UNKNOWN;
	}

	child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
	if (child_stderr == NULL) {
		printf (_("Could not open stderr for %s\n"), command_line);
	}

	result = STATE_OK;

	line = 0;
	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {

		/* strip the newline character from the end of the input */
		if (input_buffer[strlen (input_buffer) - 1] == '\n')
			input_buffer[strlen (input_buffer) - 1] = 0;

		line++;

		temp_buffer = strtok (input_buffer, "=");
		temp_buffer = strtok (NULL, "=");

		if (temp_buffer == NULL) {

				result = STATE_UNKNOWN;
				strcpy (errmsg, input_buffer);

		} else {

			switch (line) {

			case 1:										/* 1st line should contain the line status */
				line_status = atoi (temp_buffer);
				break;
			case 2:										/* 2nd line should contain the paper status */
				paper_status = atoi (temp_buffer);
				break;
			case 3:										/* 3rd line should be intervention required */
				intervention_required = atoi (temp_buffer);
				break;
			case 4:										/* 4th line should be peripheral error */
				peripheral_error = atoi (temp_buffer);
				break;
			case 5:										/* 5th line should contain the paper jam status */
				paper_jam = atoi (temp_buffer);
				break;
			case 6:										/* 6th line should contain the paper out status */
				paper_out = atoi (temp_buffer);
				break;
			case 7:										/* 7th line should contain the toner low status */
				toner_low = atoi (temp_buffer);
				break;
			case 8:										/* did data come too slow for engine */
				page_punt = atoi (temp_buffer);
				break;
			case 9:										/* did we run out of memory */
				memory_out = atoi (temp_buffer);
				break;
			case 10:										/* is there a door open */
				door_open = atoi (temp_buffer);
				break;
			case 11:										/* is output tray full */
				paper_output = atoi (temp_buffer);
				break;
			case 12:										/* display panel message */
				strcpy (display_message, temp_buffer + 1);
				break;
			default:
				break;
			}

		}

		/* break out of the read loop if we encounter an error */
		if (result != STATE_OK)
			break;
	}

	/* WARNING if output found on stderr */
	if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
		result = max_state (result, STATE_WARNING);
		/* remove CRLF */
		if (input_buffer[strlen (input_buffer) - 1] == '\n')
			input_buffer[strlen (input_buffer) - 1] = 0;
		sprintf (errmsg, "%s", input_buffer );

	}
	
	/* close stderr */
	(void) fclose (child_stderr);

	/* close the pipe */
	if (spclose (child_process))
		result = max_state (result, STATE_WARNING);

	/* if there wasn't any output, display an error */
	if (line == 0) {

		/* might not be the problem, but most likely is. */
		result = STATE_UNKNOWN ;
		asprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address );
		 
	}

	/* if we had no read errors, check the printer status results... */
	if (result == STATE_OK) {

		if (paper_jam) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Paper Jam"));
		}
		else if (paper_out) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Out of Paper"));
		}
		else if (line_status == OFFLINE) {
			if (strcmp (errmsg, "POWERSAVE ON") != 0) {
				result = STATE_WARNING;
				strcpy (errmsg, _("Printer Offline"));
			}
		}
		else if (peripheral_error) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Peripheral Error"));
		}
		else if (intervention_required) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Intervention Required"));
		}
		else if (toner_low) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Toner Low"));
		}
		else if (memory_out) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Insufficient Memory"));
		}
		else if (door_open) {
			result = STATE_WARNING;
			strcpy (errmsg, _("A Door is Open"));
		}
		else if (paper_output) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Output Tray is Full"));
		}
		else if (page_punt) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Data too Slow for Engine"));
		}
		else if (paper_status) {
			result = STATE_WARNING;
			strcpy (errmsg, _("Unknown Paper Error"));
		}
	}

	if (result == STATE_OK)
		printf (_("Printer ok - (%s)\n"), display_message);

	else if (result == STATE_UNKNOWN) {

		printf ("%s\n", errmsg);

		/* if printer could not be reached, escalate to critical */
		if (strstr (errmsg, "Timeout"))
			result = STATE_CRITICAL;
	}

	else if (result == STATE_WARNING)
		printf ("%s (%s)\n", errmsg, display_message);

	return result;
}





/* process command-line arguments */
int
process_arguments (int argc, char **argv)
{
	int c;

	int option = 0;
	static struct option longopts[] = {
		{"hostname", required_argument, 0, 'H'},
		{"community", required_argument, 0, 'C'},
/*  		{"critical",       required_argument,0,'c'}, */
/*  		{"warning",        required_argument,0,'w'}, */
/*  		{"port",           required_argument,0,'P'}, */
		{"version", no_argument, 0, 'V'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};

	if (argc < 2)
		return ERROR;

	
	while (1) {
		c = getopt_long (argc, argv, "+hVH:C:", longopts, &option);

		if (c == -1 || c == EOF || c == 1)
			break;

		switch (c) {
		case 'H':									/* hostname */
			if (is_host (optarg)) {
				address = strscpy(address, optarg) ;
			}
			else {
				usage2 (_("Invalid host name"), optarg);
			}
			break;
		case 'C':									/* community */
			community = strscpy (community, optarg);
			break;
		case 'V':									/* version */
			print_revision (progname, revision);
			exit (STATE_OK);
		case 'h':									/* help */
			print_help ();
			exit (STATE_OK);
		case '?':									/* help */
			usage (_("Invalid argument\n"));
		}
	}

	c = optind;
	if (address == NULL) {
		if (is_host (argv[c])) {
			address = argv[c++];
		}
		else {
			usage2 (_("Invalid host name"), argv[c]);
		}
	}
	
	if (community == NULL) {
		if (argv[c] != NULL )
			community = argv[c];
		else
			community = strdup (DEFAULT_COMMUNITY);
	}

	return validate_arguments ();
}





int
validate_arguments (void)
{
	return OK;
}






void
print_help (void)
{
	print_revision (progname, revision);

	printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
	printf (COPYRIGHT, copyright, email);

	printf (_("\
This plugin tests the STATUS of an HP printer with a JetDirect card.\n\
Net-snmp must be installed on the computer running the plugin.\n\n"));

	print_usage ();

	printf (_(UT_HELP_VRSN));

	printf (_("\
 -C, --community=STRING\n\
    The SNMP community name (default=%s)\n"), DEFAULT_COMMUNITY);

	printf (_(UT_SUPPORT));
}




void
print_usage (void)
{
	printf (_("\
Usage: %s -H host [-C community]\n"), progname);
	printf (_(UT_HLP_VRS), progname, progname);
}