diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 11:07:26 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 11:07:26 +0100 |
commit | a2ce9e962d52ebaffc275379c82ce29229a1fba8 (patch) | |
tree | d8524a6786ab459787f62b66b5e94fb59e7bb32c /plugins/check_hpjd.c | |
parent | e001fe5b3e50461b79b7b5ea3180cd11507fd615 (diff) | |
download | monitoring-plugins-a2ce9e962d52ebaffc275379c82ce29229a1fba8.tar.gz |
Refactor check_hpjd
Diffstat (limited to 'plugins/check_hpjd.c')
-rw-r--r-- | plugins/check_hpjd.c | 141 |
1 files changed, 63 insertions, 78 deletions
diff --git a/plugins/check_hpjd.c b/plugins/check_hpjd.c index 78f55b7f..62417fd6 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 | ||
60 | static int process_arguments(int /*argc*/, char ** /*argv*/); | 61 | typedef struct { |
61 | static int validate_arguments(void); | 62 | int errorcode; |
63 | check_hpjd_config config; | ||
64 | } check_hpjd_config_wrapper; | ||
65 | static check_hpjd_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
62 | static void print_help(void); | 66 | static void print_help(void); |
63 | void print_usage(void); | 67 | void print_usage(void); |
64 | 68 | ||
65 | static char *community = NULL; | ||
66 | static char *address = NULL; | ||
67 | static unsigned int port = 0; | ||
68 | static int check_paper_out = 1; | ||
69 | |||
70 | int main(int argc, char **argv) { | 69 | int 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,10 +74,15 @@ 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")); |
102 | } | 81 | } |
103 | 82 | ||
83 | const check_hpjd_config config = tmp_config.config; | ||
84 | |||
85 | char query_string[512]; | ||
104 | /* removed ' 2>1' at end of command 10/27/1999 - EG */ | 86 | /* removed ' 2>1' at end of command 10/27/1999 - EG */ |
105 | /* create the query string */ | 87 | /* create the query string */ |
106 | 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", HPJD_LINE_STATUS, HPJD_PAPER_STATUS, |
@@ -108,7 +90,8 @@ int main(int argc, char **argv) { | |||
108 | HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); | 90 | HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); |
109 | 91 | ||
110 | /* get the command to run */ | 92 | /* get the command to run */ |
111 | sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, community, address, port, query_string); | 93 | char command_line[1024]; |
94 | sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, config.community, config.address, config.port, query_string); | ||
112 | 95 | ||
113 | /* run the command */ | 96 | /* run the command */ |
114 | child_process = spopen(command_line); | 97 | child_process = spopen(command_line); |
@@ -122,11 +105,26 @@ int main(int argc, char **argv) { | |||
122 | printf(_("Could not open stderr for %s\n"), command_line); | 105 | printf(_("Could not open stderr for %s\n"), command_line); |
123 | } | 106 | } |
124 | 107 | ||
125 | result = STATE_OK; | 108 | mp_state_enum result = STATE_OK; |
126 | 109 | ||
127 | line = 0; | 110 | int line_status = ONLINE; |
128 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 111 | int paper_status = 0; |
112 | int intervention_required = 0; | ||
113 | int peripheral_error = 0; | ||
114 | int paper_jam = 0; | ||
115 | int paper_out = 0; | ||
116 | int toner_low = 0; | ||
117 | int page_punt = 0; | ||
118 | int memory_out = 0; | ||
119 | int door_open = 0; | ||
120 | int paper_output = 0; | ||
121 | char display_message[MAX_INPUT_BUFFER]; | ||
122 | |||
123 | char input_buffer[MAX_INPUT_BUFFER]; | ||
124 | char *errmsg = malloc(MAX_INPUT_BUFFER); | ||
125 | int line = 0; | ||
129 | 126 | ||
127 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
130 | /* strip the newline character from the end of the input */ | 128 | /* strip the newline character from the end of the input */ |
131 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { | 129 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
132 | input_buffer[strlen(input_buffer) - 1] = 0; | 130 | input_buffer[strlen(input_buffer) - 1] = 0; |
@@ -134,18 +132,14 @@ int main(int argc, char **argv) { | |||
134 | 132 | ||
135 | line++; | 133 | line++; |
136 | 134 | ||
137 | temp_buffer = strtok(input_buffer, "="); | 135 | char *temp_buffer = strtok(input_buffer, "="); |
138 | temp_buffer = strtok(NULL, "="); | 136 | temp_buffer = strtok(NULL, "="); |
139 | 137 | ||
140 | if (temp_buffer == NULL && line < 13) { | 138 | if (temp_buffer == NULL && line < 13) { |
141 | |||
142 | result = STATE_UNKNOWN; | 139 | result = STATE_UNKNOWN; |
143 | strcpy(errmsg, input_buffer); | 140 | strcpy(errmsg, input_buffer); |
144 | |||
145 | } else { | 141 | } else { |
146 | |||
147 | switch (line) { | 142 | switch (line) { |
148 | |||
149 | case 1: /* 1st line should contain the line status */ | 143 | case 1: /* 1st line should contain the line status */ |
150 | line_status = atoi(temp_buffer); | 144 | line_status = atoi(temp_buffer); |
151 | break; | 145 | break; |
@@ -213,10 +207,9 @@ int main(int argc, char **argv) { | |||
213 | 207 | ||
214 | /* if there wasn't any output, display an error */ | 208 | /* if there wasn't any output, display an error */ |
215 | if (line == 0) { | 209 | if (line == 0) { |
216 | |||
217 | /* might not be the problem, but most likely is. */ | 210 | /* might not be the problem, but most likely is. */ |
218 | result = STATE_UNKNOWN; | 211 | result = STATE_UNKNOWN; |
219 | xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, address); | 212 | xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, config.address); |
220 | } | 213 | } |
221 | 214 | ||
222 | /* if we had no read errors, check the printer status results... */ | 215 | /* if we had no read errors, check the printer status results... */ |
@@ -226,7 +219,7 @@ int main(int argc, char **argv) { | |||
226 | result = STATE_WARNING; | 219 | result = STATE_WARNING; |
227 | strcpy(errmsg, _("Paper Jam")); | 220 | strcpy(errmsg, _("Paper Jam")); |
228 | } else if (paper_out) { | 221 | } else if (paper_out) { |
229 | if (check_paper_out) { | 222 | if (config.check_paper_out) { |
230 | result = STATE_WARNING; | 223 | result = STATE_WARNING; |
231 | } | 224 | } |
232 | strcpy(errmsg, _("Out of Paper")); | 225 | strcpy(errmsg, _("Out of Paper")); |
@@ -264,30 +257,21 @@ int main(int argc, char **argv) { | |||
264 | 257 | ||
265 | if (result == STATE_OK) { | 258 | if (result == STATE_OK) { |
266 | printf(_("Printer ok - (%s)\n"), display_message); | 259 | printf(_("Printer ok - (%s)\n"), display_message); |
267 | } | 260 | } else if (result == STATE_UNKNOWN) { |
268 | |||
269 | else if (result == STATE_UNKNOWN) { | ||
270 | |||
271 | printf("%s\n", errmsg); | 261 | printf("%s\n", errmsg); |
272 | |||
273 | /* if printer could not be reached, escalate to critical */ | 262 | /* if printer could not be reached, escalate to critical */ |
274 | if (strstr(errmsg, "Timeout")) { | 263 | if (strstr(errmsg, "Timeout")) { |
275 | result = STATE_CRITICAL; | 264 | result = STATE_CRITICAL; |
276 | } | 265 | } |
277 | } | 266 | } else if (result == STATE_WARNING) { |
278 | |||
279 | else if (result == STATE_WARNING) { | ||
280 | printf("%s (%s)\n", errmsg, display_message); | 267 | printf("%s (%s)\n", errmsg, display_message); |
281 | } | 268 | } |
282 | 269 | ||
283 | return result; | 270 | exit(result); |
284 | } | 271 | } |
285 | 272 | ||
286 | /* process command-line arguments */ | 273 | /* process command-line arguments */ |
287 | int process_arguments(int argc, char **argv) { | 274 | check_hpjd_config_wrapper process_arguments(int argc, char **argv) { |
288 | int c; | ||
289 | |||
290 | int option = 0; | ||
291 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 275 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
292 | {"community", required_argument, 0, 'C'}, | 276 | {"community", required_argument, 0, 'C'}, |
293 | /* {"critical", required_argument,0,'c'}, */ | 277 | /* {"critical", required_argument,0,'c'}, */ |
@@ -297,37 +281,44 @@ int process_arguments(int argc, char **argv) { | |||
297 | {"help", no_argument, 0, 'h'}, | 281 | {"help", no_argument, 0, 'h'}, |
298 | {0, 0, 0, 0}}; | 282 | {0, 0, 0, 0}}; |
299 | 283 | ||
284 | check_hpjd_config_wrapper result = { | ||
285 | .errorcode = OK, | ||
286 | .config = check_hpjd_config_init(), | ||
287 | }; | ||
288 | |||
300 | if (argc < 2) { | 289 | if (argc < 2) { |
301 | return ERROR; | 290 | result.errorcode = ERROR; |
291 | return result; | ||
302 | } | 292 | } |
303 | 293 | ||
304 | while (1) { | 294 | int option = 0; |
305 | c = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option); | 295 | while (true) { |
296 | int option_index = getopt_long(argc, argv, "+hVH:C:p:D", longopts, &option); | ||
306 | 297 | ||
307 | if (c == -1 || c == EOF || c == 1) { | 298 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
308 | break; | 299 | break; |
309 | } | 300 | } |
310 | 301 | ||
311 | switch (c) { | 302 | switch (option_index) { |
312 | case 'H': /* hostname */ | 303 | case 'H': /* hostname */ |
313 | if (is_host(optarg)) { | 304 | if (is_host(optarg)) { |
314 | address = strscpy(address, optarg); | 305 | result.config.address = strscpy(result.config.address, optarg); |
315 | } else { | 306 | } else { |
316 | usage2(_("Invalid hostname/address"), optarg); | 307 | usage2(_("Invalid hostname/address"), optarg); |
317 | } | 308 | } |
318 | break; | 309 | break; |
319 | case 'C': /* community */ | 310 | case 'C': /* community */ |
320 | community = strscpy(community, optarg); | 311 | result.config.community = strscpy(result.config.community, optarg); |
321 | break; | 312 | break; |
322 | case 'p': | 313 | case 'p': |
323 | if (!is_intpos(optarg)) { | 314 | if (!is_intpos(optarg)) { |
324 | usage2(_("Port must be a positive short integer"), optarg); | 315 | usage2(_("Port must be a positive short integer"), optarg); |
325 | } else { | 316 | } else { |
326 | port = atoi(optarg); | 317 | result.config.port = atoi(optarg); |
327 | } | 318 | } |
328 | break; | 319 | break; |
329 | case 'D': /* disable paper out check*/ | 320 | case 'D': /* disable paper out check*/ |
330 | check_paper_out = 0; | 321 | result.config.check_paper_out = false; |
331 | break; | 322 | break; |
332 | case 'V': /* version */ | 323 | case 'V': /* version */ |
333 | print_revision(progname, NP_VERSION); | 324 | print_revision(progname, NP_VERSION); |
@@ -340,32 +331,26 @@ int process_arguments(int argc, char **argv) { | |||
340 | } | 331 | } |
341 | } | 332 | } |
342 | 333 | ||
343 | c = optind; | 334 | int c = optind; |
344 | if (address == NULL) { | 335 | if (result.config.address == NULL) { |
345 | if (is_host(argv[c])) { | 336 | if (is_host(argv[c])) { |
346 | address = argv[c++]; | 337 | result.config.address = argv[c++]; |
347 | } else { | 338 | } else { |
348 | usage2(_("Invalid hostname/address"), argv[c]); | 339 | usage2(_("Invalid hostname/address"), argv[c]); |
349 | } | 340 | } |
350 | } | 341 | } |
351 | 342 | ||
352 | if (community == NULL) { | 343 | if (result.config.community == NULL) { |
353 | if (argv[c] != NULL) { | 344 | if (argv[c] != NULL) { |
354 | community = argv[c]; | 345 | result.config.community = argv[c]; |
355 | } else { | 346 | } else { |
356 | community = strdup(DEFAULT_COMMUNITY); | 347 | result.config.community = strdup(DEFAULT_COMMUNITY); |
357 | } | 348 | } |
358 | } | 349 | } |
359 | 350 | ||
360 | if (port == 0) { | 351 | return result; |
361 | port = atoi(DEFAULT_PORT); | ||
362 | } | ||
363 | |||
364 | return validate_arguments(); | ||
365 | } | 352 | } |
366 | 353 | ||
367 | int validate_arguments(void) { return OK; } | ||
368 | |||
369 | void print_help(void) { | 354 | void print_help(void) { |
370 | print_revision(progname, NP_VERSION); | 355 | print_revision(progname, NP_VERSION); |
371 | 356 | ||