summaryrefslogtreecommitdiffstats
path: root/plugins/check_hpjd.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_hpjd.c')
-rw-r--r--plugins/check_hpjd.c186
1 files changed, 94 insertions, 92 deletions
diff --git a/plugins/check_hpjd.c b/plugins/check_hpjd.c
index b39bccff..9907abc5 100644
--- a/plugins/check_hpjd.c
+++ b/plugins/check_hpjd.c
@@ -37,9 +37,10 @@ const char *email = "devel@monitoring-plugins.org";
37#include "popen.h" 37#include "popen.h"
38#include "utils.h" 38#include "utils.h"
39#include "netutils.h" 39#include "netutils.h"
40#include "states.h"
41#include "check_hpjd.d/config.h"
40 42
41#define DEFAULT_COMMUNITY "public" 43#define DEFAULT_COMMUNITY "public"
42#define DEFAULT_PORT "161"
43 44
44#define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1" 45#define HPJD_LINE_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.1"
45#define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2" 46#define HPJD_PAPER_STATUS ".1.3.6.1.4.1.11.2.3.9.1.1.2.2"
@@ -57,39 +58,15 @@ const char *email = "devel@monitoring-plugins.org";
57#define ONLINE 0 58#define ONLINE 0
58#define OFFLINE 1 59#define OFFLINE 1
59 60
60static int process_arguments(int /*argc*/, char ** /*argv*/); 61typedef struct {
61static int validate_arguments(void); 62 int errorcode;
63 check_hpjd_config config;
64} check_hpjd_config_wrapper;
65static check_hpjd_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
62static void print_help(void); 66static void print_help(void);
63void print_usage(void); 67void print_usage(void);
64 68
65static char *community = NULL;
66static char *address = NULL;
67static unsigned int port = 0;
68static int check_paper_out = 1;
69
70int main(int argc, char **argv) { 69int main(int argc, char **argv) {
71 char command_line[1024];
72 int result = STATE_UNKNOWN;
73 int line;
74 char input_buffer[MAX_INPUT_BUFFER];
75 char query_string[512];
76 char *errmsg;
77 char *temp_buffer;
78 int line_status = ONLINE;
79 int paper_status = 0;
80 int intervention_required = 0;
81 int peripheral_error = 0;
82 int paper_jam = 0;
83 int paper_out = 0;
84 int toner_low = 0;
85 int page_punt = 0;
86 int memory_out = 0;
87 int door_open = 0;
88 int paper_output = 0;
89 char display_message[MAX_INPUT_BUFFER];
90
91 errmsg = malloc(MAX_INPUT_BUFFER);
92
93 setlocale(LC_ALL, ""); 70 setlocale(LC_ALL, "");
94 bindtextdomain(PACKAGE, LOCALEDIR); 71 bindtextdomain(PACKAGE, LOCALEDIR);
95 textdomain(PACKAGE); 72 textdomain(PACKAGE);
@@ -97,17 +74,27 @@ int main(int argc, char **argv) {
97 /* Parse extra opts if any */ 74 /* Parse extra opts if any */
98 argv = np_extra_opts(&argc, argv, progname); 75 argv = np_extra_opts(&argc, argv, progname);
99 76
100 if (process_arguments(argc, argv) == ERROR) 77 check_hpjd_config_wrapper tmp_config = process_arguments(argc, argv);
78
79 if (tmp_config.errorcode == ERROR) {
101 usage4(_("Could not parse arguments")); 80 usage4(_("Could not parse arguments"));
81 }
82
83 const check_hpjd_config config = tmp_config.config;
102 84
85 char query_string[512];
103 /* removed ' 2>1' at end of command 10/27/1999 - EG */ 86 /* removed ' 2>1' at end of command 10/27/1999 - EG */
104 /* create the query string */ 87 /* create the query string */
105 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, 88 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",
106 HPJD_INTERVENTION_REQUIRED, HPJD_GD_PERIPHERAL_ERROR, HPJD_GD_PAPER_JAM, HPJD_GD_PAPER_OUT, HPJD_GD_TONER_LOW, 89 HPJD_LINE_STATUS, HPJD_PAPER_STATUS, HPJD_INTERVENTION_REQUIRED,
107 HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); 90 HPJD_GD_PERIPHERAL_ERROR, HPJD_GD_PAPER_JAM, HPJD_GD_PAPER_OUT, HPJD_GD_TONER_LOW,
91 HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT,
92 HPJD_GD_STATUS_DISPLAY);
108 93
109 /* get the command to run */ 94 /* get the command to run */
110 sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, community, address, port, query_string); 95 char command_line[1024];
96 sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, config.community,
97 config.address, config.port, query_string);
111 98
112 /* run the command */ 99 /* run the command */
113 child_process = spopen(command_line); 100 child_process = spopen(command_line);
@@ -121,29 +108,41 @@ int main(int argc, char **argv) {
121 printf(_("Could not open stderr for %s\n"), command_line); 108 printf(_("Could not open stderr for %s\n"), command_line);
122 } 109 }
123 110
124 result = STATE_OK; 111 mp_state_enum result = STATE_OK;
125 112
126 line = 0; 113 int line_status = ONLINE;
127 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 114 int paper_status = 0;
115 int intervention_required = 0;
116 int peripheral_error = 0;
117 int paper_jam = 0;
118 int paper_out = 0;
119 int toner_low = 0;
120 int page_punt = 0;
121 int memory_out = 0;
122 int door_open = 0;
123 int paper_output = 0;
124 char display_message[MAX_INPUT_BUFFER];
128 125
126 char input_buffer[MAX_INPUT_BUFFER];
127 char *errmsg = malloc(MAX_INPUT_BUFFER);
128 int line = 0;
129
130 while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
129 /* strip the newline character from the end of the input */ 131 /* strip the newline character from the end of the input */
130 if (input_buffer[strlen(input_buffer) - 1] == '\n') 132 if (input_buffer[strlen(input_buffer) - 1] == '\n') {
131 input_buffer[strlen(input_buffer) - 1] = 0; 133 input_buffer[strlen(input_buffer) - 1] = 0;
134 }
132 135
133 line++; 136 line++;
134 137
135 temp_buffer = strtok(input_buffer, "="); 138 char *temp_buffer = strtok(input_buffer, "=");
136 temp_buffer = strtok(NULL, "="); 139 temp_buffer = strtok(NULL, "=");
137 140
138 if (temp_buffer == NULL && line < 13) { 141 if (temp_buffer == NULL && line < 13) {
139
140 result = STATE_UNKNOWN; 142 result = STATE_UNKNOWN;
141 strcpy(errmsg, input_buffer); 143 strcpy(errmsg, input_buffer);
142
143 } else { 144 } else {
144
145 switch (line) { 145 switch (line) {
146
147 case 1: /* 1st line should contain the line status */ 146 case 1: /* 1st line should contain the line status */
148 line_status = atoi(temp_buffer); 147 line_status = atoi(temp_buffer);
149 break; 148 break;
@@ -181,21 +180,24 @@ int main(int argc, char **argv) {
181 strcpy(display_message, temp_buffer + 1); 180 strcpy(display_message, temp_buffer + 1);
182 break; 181 break;
183 default: /* fold multiline message */ 182 default: /* fold multiline message */
184 strncat(display_message, input_buffer, sizeof(display_message) - strlen(display_message) - 1); 183 strncat(display_message, input_buffer,
184 sizeof(display_message) - strlen(display_message) - 1);
185 } 185 }
186 } 186 }
187 187
188 /* break out of the read loop if we encounter an error */ 188 /* break out of the read loop if we encounter an error */
189 if (result != STATE_OK) 189 if (result != STATE_OK) {
190 break; 190 break;
191 }
191 } 192 }
192 193
193 /* WARNING if output found on stderr */ 194 /* WARNING if output found on stderr */
194 if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { 195 if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
195 result = max_state(result, STATE_WARNING); 196 result = max_state(result, STATE_WARNING);
196 /* remove CRLF */ 197 /* remove CRLF */
197 if (input_buffer[strlen(input_buffer) - 1] == '\n') 198 if (input_buffer[strlen(input_buffer) - 1] == '\n') {
198 input_buffer[strlen(input_buffer) - 1] = 0; 199 input_buffer[strlen(input_buffer) - 1] = 0;
200 }
199 sprintf(errmsg, "%s", input_buffer); 201 sprintf(errmsg, "%s", input_buffer);
200 } 202 }
201 203
@@ -203,15 +205,15 @@ int main(int argc, char **argv) {
203 (void)fclose(child_stderr); 205 (void)fclose(child_stderr);
204 206
205 /* close the pipe */ 207 /* close the pipe */
206 if (spclose(child_process)) 208 if (spclose(child_process)) {
207 result = max_state(result, STATE_WARNING); 209 result = max_state(result, STATE_WARNING);
210 }
208 211
209 /* if there wasn't any output, display an error */ 212 /* if there wasn't any output, display an error */
210 if (line == 0) { 213 if (line == 0) {
211
212 /* might not be the problem, but most likely is. */ 214 /* might not be the problem, but most likely is. */
213 result = STATE_UNKNOWN; 215 result = STATE_UNKNOWN;
214 xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, address); 216 xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, config.address);
215 } 217 }
216 218
217 /* if we had no read errors, check the printer status results... */ 219 /* if we had no read errors, check the printer status results... */
@@ -221,8 +223,9 @@ int main(int argc, char **argv) {
221 result = STATE_WARNING; 223 result = STATE_WARNING;
222 strcpy(errmsg, _("Paper Jam")); 224 strcpy(errmsg, _("Paper Jam"));
223 } else if (paper_out) { 225 } else if (paper_out) {
224 if (check_paper_out) 226 if (config.check_paper_out) {
225 result = STATE_WARNING; 227 result = STATE_WARNING;
228 }
226 strcpy(errmsg, _("Out of Paper")); 229 strcpy(errmsg, _("Out of Paper"));
227 } else if (line_status == OFFLINE) { 230 } else if (line_status == OFFLINE) {
228 if (strcmp(errmsg, "POWERSAVE ON") != 0) { 231 if (strcmp(errmsg, "POWERSAVE ON") != 0) {
@@ -256,29 +259,23 @@ int main(int argc, char **argv) {
256 } 259 }
257 } 260 }
258 261
259 if (result == STATE_OK) 262 if (result == STATE_OK) {
260 printf(_("Printer ok - (%s)\n"), display_message); 263 printf(_("Printer ok - (%s)\n"), display_message);
261 264 } else if (result == STATE_UNKNOWN) {
262 else if (result == STATE_UNKNOWN) {
263
264 printf("%s\n", errmsg); 265 printf("%s\n", errmsg);
265
266 /* if printer could not be reached, escalate to critical */ 266 /* if printer could not be reached, escalate to critical */
267 if (strstr(errmsg, "Timeout")) 267 if (strstr(errmsg, "Timeout")) {
268 result = STATE_CRITICAL; 268 result = STATE_CRITICAL;
269 } 269 }
270 270 } else if (result == STATE_WARNING) {
271 else if (result == STATE_WARNING)
272 printf("%s (%s)\n", errmsg, display_message); 271 printf("%s (%s)\n", errmsg, display_message);
272 }
273 273
274 return result; 274 exit(result);
275} 275}
276 276
277/* process command-line arguments */ 277/* process command-line arguments */
278int process_arguments(int argc, char **argv) { 278check_hpjd_config_wrapper process_arguments(int argc, char **argv) {
279 int c;
280
281 int option = 0;
282 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, 279 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'},
283 {"community", required_argument, 0, 'C'}, 280 {"community", required_argument, 0, 'C'},
284 /* {"critical", required_argument,0,'c'}, */ 281 /* {"critical", required_argument,0,'c'}, */
@@ -288,34 +285,44 @@ int process_arguments(int argc, char **argv) {
288 {"help", no_argument, 0, 'h'}, 285 {"help", no_argument, 0, 'h'},
289 {0, 0, 0, 0}}; 286 {0, 0, 0, 0}};
290 287
291 if (argc < 2) 288 check_hpjd_config_wrapper result = {
292 return ERROR; 289 .errorcode = OK,
290 .config = check_hpjd_config_init(),
291 };
292
293 if (argc < 2) {
294 result.errorcode = ERROR;
295 return result;
296 }
293 297
294 while (1) { 298 int option = 0;
295 c = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option); 299 while (true) {
300 int option_index = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option);
296 301
297 if (c == -1 || c == EOF || c == 1) 302 if (option_index == -1 || option_index == EOF || option_index == 1) {
298 break; 303 break;
304 }
299 305
300 switch (c) { 306 switch (option_index) {
301 case 'H': /* hostname */ 307 case 'H': /* hostname */
302 if (is_host(optarg)) { 308 if (is_host(optarg)) {
303 address = strscpy(address, optarg); 309 result.config.address = strscpy(result.config.address, optarg);
304 } else { 310 } else {
305 usage2(_("Invalid hostname/address"), optarg); 311 usage2(_("Invalid hostname/address"), optarg);
306 } 312 }
307 break; 313 break;
308 case 'C': /* community */ 314 case 'C': /* community */
309 community = strscpy(community, optarg); 315 result.config.community = strscpy(result.config.community, optarg);
310 break; 316 break;
311 case 'p': 317 case 'p':
312 if (!is_intpos(optarg)) 318 if (!is_intpos(optarg)) {
313 usage2(_("Port must be a positive short integer"), optarg); 319 usage2(_("Port must be a positive short integer"), optarg);
314 else 320 } else {
315 port = atoi(optarg); 321 result.config.port = atoi(optarg);
322 }
316 break; 323 break;
317 case 'D': /* disable paper out check*/ 324 case 'D': /* disable paper out check*/
318 check_paper_out = 0; 325 result.config.check_paper_out = false;
319 break; 326 break;
320 case 'V': /* version */ 327 case 'V': /* version */
321 print_revision(progname, NP_VERSION); 328 print_revision(progname, NP_VERSION);
@@ -328,31 +335,26 @@ int process_arguments(int argc, char **argv) {
328 } 335 }
329 } 336 }
330 337
331 c = optind; 338 int c = optind;
332 if (address == NULL) { 339 if (result.config.address == NULL) {
333 if (is_host(argv[c])) { 340 if (is_host(argv[c])) {
334 address = argv[c++]; 341 result.config.address = argv[c++];
335 } else { 342 } else {
336 usage2(_("Invalid hostname/address"), argv[c]); 343 usage2(_("Invalid hostname/address"), argv[c]);
337 } 344 }
338 } 345 }
339 346
340 if (community == NULL) { 347 if (result.config.community == NULL) {
341 if (argv[c] != NULL) 348 if (argv[c] != NULL) {
342 community = argv[c]; 349 result.config.community = argv[c];
343 else 350 } else {
344 community = strdup(DEFAULT_COMMUNITY); 351 result.config.community = strdup(DEFAULT_COMMUNITY);
345 } 352 }
346
347 if (port == 0) {
348 port = atoi(DEFAULT_PORT);
349 } 353 }
350 354
351 return validate_arguments(); 355 return result;
352} 356}
353 357
354int validate_arguments(void) { return OK; }
355
356void print_help(void) { 358void print_help(void) {
357 print_revision(progname, NP_VERSION); 359 print_revision(progname, NP_VERSION);
358 360