diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-11 12:04:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-11 12:04:40 +0100 |
commit | b8460603aaa0d707015733ee0d7d40b1949d40b9 (patch) | |
tree | c81e165e03a601c6976a11e25ef7797b791ab438 /plugins/check_hpjd.c | |
parent | 7acfac740975f633ae83883535590dffb2650c88 (diff) | |
parent | abd6295439dac66ca4d5dd280d4a6528cdfe973d (diff) | |
download | monitoring-plugins-b8460603aaa0d707015733ee0d7d40b1949d40b9.tar.gz |
Merge branch 'master' into refactor/check_fping
Diffstat (limited to 'plugins/check_hpjd.c')
-rw-r--r-- | plugins/check_hpjd.c | 174 |
1 files changed, 86 insertions, 88 deletions
diff --git a/plugins/check_hpjd.c b/plugins/check_hpjd.c index b39bccff..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,9 +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")); |
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", HPJD_LINE_STATUS, HPJD_PAPER_STATUS, |
@@ -107,7 +90,8 @@ int main(int argc, char **argv) { | |||
107 | 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); |
108 | 91 | ||
109 | /* get the command to run */ | 92 | /* 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); | 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); | ||
111 | 95 | ||
112 | /* run the command */ | 96 | /* run the command */ |
113 | child_process = spopen(command_line); | 97 | child_process = spopen(command_line); |
@@ -121,29 +105,41 @@ int main(int argc, char **argv) { | |||
121 | printf(_("Could not open stderr for %s\n"), command_line); | 105 | printf(_("Could not open stderr for %s\n"), command_line); |
122 | } | 106 | } |
123 | 107 | ||
124 | result = STATE_OK; | 108 | mp_state_enum result = STATE_OK; |
125 | 109 | ||
126 | line = 0; | 110 | int line_status = ONLINE; |
127 | 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]; | ||
128 | 122 | ||
123 | char input_buffer[MAX_INPUT_BUFFER]; | ||
124 | char *errmsg = malloc(MAX_INPUT_BUFFER); | ||
125 | int line = 0; | ||
126 | |||
127 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
129 | /* strip the newline character from the end of the input */ | 128 | /* strip the newline character from the end of the input */ |
130 | if (input_buffer[strlen(input_buffer) - 1] == '\n') | 129 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
131 | input_buffer[strlen(input_buffer) - 1] = 0; | 130 | input_buffer[strlen(input_buffer) - 1] = 0; |
131 | } | ||
132 | 132 | ||
133 | line++; | 133 | line++; |
134 | 134 | ||
135 | temp_buffer = strtok(input_buffer, "="); | 135 | char *temp_buffer = strtok(input_buffer, "="); |
136 | temp_buffer = strtok(NULL, "="); | 136 | temp_buffer = strtok(NULL, "="); |
137 | 137 | ||
138 | if (temp_buffer == NULL && line < 13) { | 138 | if (temp_buffer == NULL && line < 13) { |
139 | |||
140 | result = STATE_UNKNOWN; | 139 | result = STATE_UNKNOWN; |
141 | strcpy(errmsg, input_buffer); | 140 | strcpy(errmsg, input_buffer); |
142 | |||
143 | } else { | 141 | } else { |
144 | |||
145 | switch (line) { | 142 | switch (line) { |
146 | |||
147 | case 1: /* 1st line should contain the line status */ | 143 | case 1: /* 1st line should contain the line status */ |
148 | line_status = atoi(temp_buffer); | 144 | line_status = atoi(temp_buffer); |
149 | break; | 145 | break; |
@@ -186,16 +182,18 @@ int main(int argc, char **argv) { | |||
186 | } | 182 | } |
187 | 183 | ||
188 | /* break out of the read loop if we encounter an error */ | 184 | /* break out of the read loop if we encounter an error */ |
189 | if (result != STATE_OK) | 185 | if (result != STATE_OK) { |
190 | break; | 186 | break; |
187 | } | ||
191 | } | 188 | } |
192 | 189 | ||
193 | /* WARNING if output found on stderr */ | 190 | /* WARNING if output found on stderr */ |
194 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | 191 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { |
195 | result = max_state(result, STATE_WARNING); | 192 | result = max_state(result, STATE_WARNING); |
196 | /* remove CRLF */ | 193 | /* remove CRLF */ |
197 | if (input_buffer[strlen(input_buffer) - 1] == '\n') | 194 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
198 | input_buffer[strlen(input_buffer) - 1] = 0; | 195 | input_buffer[strlen(input_buffer) - 1] = 0; |
196 | } | ||
199 | sprintf(errmsg, "%s", input_buffer); | 197 | sprintf(errmsg, "%s", input_buffer); |
200 | } | 198 | } |
201 | 199 | ||
@@ -203,15 +201,15 @@ int main(int argc, char **argv) { | |||
203 | (void)fclose(child_stderr); | 201 | (void)fclose(child_stderr); |
204 | 202 | ||
205 | /* close the pipe */ | 203 | /* close the pipe */ |
206 | if (spclose(child_process)) | 204 | if (spclose(child_process)) { |
207 | result = max_state(result, STATE_WARNING); | 205 | result = max_state(result, STATE_WARNING); |
206 | } | ||
208 | 207 | ||
209 | /* if there wasn't any output, display an error */ | 208 | /* if there wasn't any output, display an error */ |
210 | if (line == 0) { | 209 | if (line == 0) { |
211 | |||
212 | /* might not be the problem, but most likely is. */ | 210 | /* might not be the problem, but most likely is. */ |
213 | result = STATE_UNKNOWN; | 211 | result = STATE_UNKNOWN; |
214 | xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, address); | 212 | xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, config.address); |
215 | } | 213 | } |
216 | 214 | ||
217 | /* if we had no read errors, check the printer status results... */ | 215 | /* if we had no read errors, check the printer status results... */ |
@@ -221,8 +219,9 @@ int main(int argc, char **argv) { | |||
221 | result = STATE_WARNING; | 219 | result = STATE_WARNING; |
222 | strcpy(errmsg, _("Paper Jam")); | 220 | strcpy(errmsg, _("Paper Jam")); |
223 | } else if (paper_out) { | 221 | } else if (paper_out) { |
224 | if (check_paper_out) | 222 | if (config.check_paper_out) { |
225 | result = STATE_WARNING; | 223 | result = STATE_WARNING; |
224 | } | ||
226 | strcpy(errmsg, _("Out of Paper")); | 225 | strcpy(errmsg, _("Out of Paper")); |
227 | } else if (line_status == OFFLINE) { | 226 | } else if (line_status == OFFLINE) { |
228 | if (strcmp(errmsg, "POWERSAVE ON") != 0) { | 227 | if (strcmp(errmsg, "POWERSAVE ON") != 0) { |
@@ -256,29 +255,23 @@ int main(int argc, char **argv) { | |||
256 | } | 255 | } |
257 | } | 256 | } |
258 | 257 | ||
259 | if (result == STATE_OK) | 258 | if (result == STATE_OK) { |
260 | printf(_("Printer ok - (%s)\n"), display_message); | 259 | printf(_("Printer ok - (%s)\n"), display_message); |
261 | 260 | } else if (result == STATE_UNKNOWN) { | |
262 | else if (result == STATE_UNKNOWN) { | ||
263 | |||
264 | printf("%s\n", errmsg); | 261 | printf("%s\n", errmsg); |
265 | |||
266 | /* if printer could not be reached, escalate to critical */ | 262 | /* if printer could not be reached, escalate to critical */ |
267 | if (strstr(errmsg, "Timeout")) | 263 | if (strstr(errmsg, "Timeout")) { |
268 | result = STATE_CRITICAL; | 264 | result = STATE_CRITICAL; |
269 | } | 265 | } |
270 | 266 | } else if (result == STATE_WARNING) { | |
271 | else if (result == STATE_WARNING) | ||
272 | printf("%s (%s)\n", errmsg, display_message); | 267 | printf("%s (%s)\n", errmsg, display_message); |
268 | } | ||
273 | 269 | ||
274 | return result; | 270 | exit(result); |
275 | } | 271 | } |
276 | 272 | ||
277 | /* process command-line arguments */ | 273 | /* process command-line arguments */ |
278 | int process_arguments(int argc, char **argv) { | 274 | check_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'}, | 275 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
283 | {"community", required_argument, 0, 'C'}, | 276 | {"community", required_argument, 0, 'C'}, |
284 | /* {"critical", required_argument,0,'c'}, */ | 277 | /* {"critical", required_argument,0,'c'}, */ |
@@ -288,34 +281,44 @@ int process_arguments(int argc, char **argv) { | |||
288 | {"help", no_argument, 0, 'h'}, | 281 | {"help", no_argument, 0, 'h'}, |
289 | {0, 0, 0, 0}}; | 282 | {0, 0, 0, 0}}; |
290 | 283 | ||
291 | if (argc < 2) | 284 | check_hpjd_config_wrapper result = { |
292 | return ERROR; | 285 | .errorcode = OK, |
286 | .config = check_hpjd_config_init(), | ||
287 | }; | ||
288 | |||
289 | if (argc < 2) { | ||
290 | result.errorcode = ERROR; | ||
291 | return result; | ||
292 | } | ||
293 | 293 | ||
294 | while (1) { | 294 | int option = 0; |
295 | 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); | ||
296 | 297 | ||
297 | if (c == -1 || c == EOF || c == 1) | 298 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
298 | break; | 299 | break; |
300 | } | ||
299 | 301 | ||
300 | switch (c) { | 302 | switch (option_index) { |
301 | case 'H': /* hostname */ | 303 | case 'H': /* hostname */ |
302 | if (is_host(optarg)) { | 304 | if (is_host(optarg)) { |
303 | address = strscpy(address, optarg); | 305 | result.config.address = strscpy(result.config.address, optarg); |
304 | } else { | 306 | } else { |
305 | usage2(_("Invalid hostname/address"), optarg); | 307 | usage2(_("Invalid hostname/address"), optarg); |
306 | } | 308 | } |
307 | break; | 309 | break; |
308 | case 'C': /* community */ | 310 | case 'C': /* community */ |
309 | community = strscpy(community, optarg); | 311 | result.config.community = strscpy(result.config.community, optarg); |
310 | break; | 312 | break; |
311 | case 'p': | 313 | case 'p': |
312 | if (!is_intpos(optarg)) | 314 | if (!is_intpos(optarg)) { |
313 | usage2(_("Port must be a positive short integer"), optarg); | 315 | usage2(_("Port must be a positive short integer"), optarg); |
314 | else | 316 | } else { |
315 | port = atoi(optarg); | 317 | result.config.port = atoi(optarg); |
318 | } | ||
316 | break; | 319 | break; |
317 | case 'D': /* disable paper out check*/ | 320 | case 'D': /* disable paper out check*/ |
318 | check_paper_out = 0; | 321 | result.config.check_paper_out = false; |
319 | break; | 322 | break; |
320 | case 'V': /* version */ | 323 | case 'V': /* version */ |
321 | print_revision(progname, NP_VERSION); | 324 | print_revision(progname, NP_VERSION); |
@@ -328,31 +331,26 @@ int process_arguments(int argc, char **argv) { | |||
328 | } | 331 | } |
329 | } | 332 | } |
330 | 333 | ||
331 | c = optind; | 334 | int c = optind; |
332 | if (address == NULL) { | 335 | if (result.config.address == NULL) { |
333 | if (is_host(argv[c])) { | 336 | if (is_host(argv[c])) { |
334 | address = argv[c++]; | 337 | result.config.address = argv[c++]; |
335 | } else { | 338 | } else { |
336 | usage2(_("Invalid hostname/address"), argv[c]); | 339 | usage2(_("Invalid hostname/address"), argv[c]); |
337 | } | 340 | } |
338 | } | 341 | } |
339 | 342 | ||
340 | if (community == NULL) { | 343 | if (result.config.community == NULL) { |
341 | if (argv[c] != NULL) | 344 | if (argv[c] != NULL) { |
342 | community = argv[c]; | 345 | result.config.community = argv[c]; |
343 | else | 346 | } else { |
344 | community = strdup(DEFAULT_COMMUNITY); | 347 | result.config.community = strdup(DEFAULT_COMMUNITY); |
345 | } | 348 | } |
346 | |||
347 | if (port == 0) { | ||
348 | port = atoi(DEFAULT_PORT); | ||
349 | } | 349 | } |
350 | 350 | ||
351 | return validate_arguments(); | 351 | return result; |
352 | } | 352 | } |
353 | 353 | ||
354 | int validate_arguments(void) { return OK; } | ||
355 | |||
356 | void print_help(void) { | 354 | void print_help(void) { |
357 | print_revision(progname, NP_VERSION); | 355 | print_revision(progname, NP_VERSION); |
358 | 356 | ||