diff options
Diffstat (limited to 'plugins/check_hpjd.c')
-rw-r--r-- | plugins/check_hpjd.c | 539 |
1 files changed, 242 insertions, 297 deletions
diff --git a/plugins/check_hpjd.c b/plugins/check_hpjd.c index c34bb082..62417fd6 100644 --- a/plugins/check_hpjd.c +++ b/plugins/check_hpjd.c | |||
@@ -1,47 +1,46 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_hpjd plugin | 3 | * Monitoring check_hpjd plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2000-2007 Monitoring Plugins Development Team | 6 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains the check_hpjd plugin | 10 | * This file contains the check_hpjd plugin |
11 | * | 11 | * |
12 | * This plugin tests the STATUS of an HP printer with a JetDirect card. | 12 | * This plugin tests the STATUS of an HP printer with a JetDirect card. |
13 | * Net-SNMP must be installed on the computer running the plugin. | 13 | * Net-SNMP must be installed on the computer running the plugin. |
14 | * | 14 | * |
15 | * | 15 | * |
16 | * This program is free software: you can redistribute it and/or modify | 16 | * This program is free software: you can redistribute it and/or modify |
17 | * it under the terms of the GNU General Public License as published by | 17 | * it under the terms of the GNU General Public License as published by |
18 | * the Free Software Foundation, either version 3 of the License, or | 18 | * the Free Software Foundation, either version 3 of the License, or |
19 | * (at your option) any later version. | 19 | * (at your option) any later version. |
20 | * | 20 | * |
21 | * This program is distributed in the hope that it will be useful, | 21 | * This program is distributed in the hope that it will be useful, |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24 | * GNU General Public License for more details. | 24 | * GNU General Public License for more details. |
25 | * | 25 | * |
26 | * You should have received a copy of the GNU General Public License | 26 | * You should have received a copy of the GNU General Public License |
27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 27 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
28 | * | 28 | * |
29 | * | 29 | * |
30 | *****************************************************************************/ | 30 | *****************************************************************************/ |
31 | 31 | ||
32 | const char *progname = "check_hpjd"; | 32 | const char *progname = "check_hpjd"; |
33 | const char *copyright = "2000-2007"; | 33 | const char *copyright = "2000-2024"; |
34 | const char *email = "devel@monitoring-plugins.org"; | 34 | const char *email = "devel@monitoring-plugins.org"; |
35 | 35 | ||
36 | #include "common.h" | 36 | #include "common.h" |
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 | const char *option_summary = "-H host [-C community]\n"; | ||
45 | 44 | ||
46 | #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" |
47 | #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" |
@@ -56,185 +55,161 @@ const char *option_summary = "-H host [-C community]\n"; | |||
56 | #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19" | 55 | #define HPJD_GD_PAPER_OUTPUT ".1.3.6.1.4.1.11.2.3.9.1.1.2.19" |
57 | #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3" | 56 | #define HPJD_GD_STATUS_DISPLAY ".1.3.6.1.4.1.11.2.3.9.1.1.3" |
58 | 57 | ||
59 | #define ONLINE 0 | 58 | #define ONLINE 0 |
60 | #define OFFLINE 1 | 59 | #define OFFLINE 1 |
61 | |||
62 | int process_arguments (int, char **); | ||
63 | int validate_arguments (void); | ||
64 | void print_help (void); | ||
65 | void print_usage (void); | ||
66 | 60 | ||
67 | char *community = NULL; | 61 | typedef struct { |
68 | char *address = NULL; | 62 | int errorcode; |
69 | unsigned int port = 0; | 63 | check_hpjd_config config; |
70 | int check_paper_out = 1; | 64 | } check_hpjd_config_wrapper; |
65 | static check_hpjd_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
66 | static void print_help(void); | ||
67 | void print_usage(void); | ||
71 | 68 | ||
72 | int | 69 | int main(int argc, char **argv) { |
73 | main (int argc, char **argv) | 70 | setlocale(LC_ALL, ""); |
74 | { | 71 | bindtextdomain(PACKAGE, LOCALEDIR); |
75 | char command_line[1024]; | 72 | textdomain(PACKAGE); |
76 | int result = STATE_UNKNOWN; | ||
77 | int line; | ||
78 | char input_buffer[MAX_INPUT_BUFFER]; | ||
79 | char query_string[512]; | ||
80 | char *errmsg; | ||
81 | char *temp_buffer; | ||
82 | int line_status = ONLINE; | ||
83 | int paper_status = 0; | ||
84 | int intervention_required = 0; | ||
85 | int peripheral_error = 0; | ||
86 | int paper_jam = 0; | ||
87 | int paper_out = 0; | ||
88 | int toner_low = 0; | ||
89 | int page_punt = 0; | ||
90 | int memory_out = 0; | ||
91 | int door_open = 0; | ||
92 | int paper_output = 0; | ||
93 | char display_message[MAX_INPUT_BUFFER]; | ||
94 | 73 | ||
95 | errmsg = malloc(MAX_INPUT_BUFFER); | 74 | /* Parse extra opts if any */ |
75 | argv = np_extra_opts(&argc, argv, progname); | ||
96 | 76 | ||
97 | setlocale (LC_ALL, ""); | 77 | check_hpjd_config_wrapper tmp_config = process_arguments(argc, argv); |
98 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
99 | textdomain (PACKAGE); | ||
100 | 78 | ||
101 | /* Parse extra opts if any */ | 79 | if (tmp_config.errorcode == ERROR) { |
102 | argv=np_extra_opts (&argc, argv, progname); | 80 | usage4(_("Could not parse arguments")); |
81 | } | ||
103 | 82 | ||
104 | if (process_arguments (argc, argv) == ERROR) | 83 | const check_hpjd_config config = tmp_config.config; |
105 | usage4 (_("Could not parse arguments")); | ||
106 | 84 | ||
85 | char query_string[512]; | ||
107 | /* removed ' 2>1' at end of command 10/27/1999 - EG */ | 86 | /* removed ' 2>1' at end of command 10/27/1999 - EG */ |
108 | /* create the query string */ | 87 | /* create the query string */ |
109 | sprintf | 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, |
110 | (query_string, | 89 | HPJD_INTERVENTION_REQUIRED, HPJD_GD_PERIPHERAL_ERROR, HPJD_GD_PAPER_JAM, HPJD_GD_PAPER_OUT, HPJD_GD_TONER_LOW, |
111 | "%s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0 %s.0", | 90 | HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); |
112 | HPJD_LINE_STATUS, | ||
113 | HPJD_PAPER_STATUS, | ||
114 | HPJD_INTERVENTION_REQUIRED, | ||
115 | HPJD_GD_PERIPHERAL_ERROR, | ||
116 | HPJD_GD_PAPER_JAM, | ||
117 | HPJD_GD_PAPER_OUT, | ||
118 | HPJD_GD_TONER_LOW, | ||
119 | HPJD_GD_PAGE_PUNT, | ||
120 | HPJD_GD_MEMORY_OUT, | ||
121 | HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, HPJD_GD_STATUS_DISPLAY); | ||
122 | 91 | ||
123 | /* get the command to run */ | 92 | /* get the command to run */ |
124 | sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", | 93 | char command_line[1024]; |
125 | PATH_TO_SNMPGET, | 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); |
126 | community, | ||
127 | address, | ||
128 | port, | ||
129 | query_string); | ||
130 | 95 | ||
131 | /* run the command */ | 96 | /* run the command */ |
132 | child_process = spopen (command_line); | 97 | child_process = spopen(command_line); |
133 | if (child_process == NULL) { | 98 | if (child_process == NULL) { |
134 | printf (_("Could not open pipe: %s\n"), command_line); | 99 | printf(_("Could not open pipe: %s\n"), command_line); |
135 | return STATE_UNKNOWN; | 100 | return STATE_UNKNOWN; |
136 | } | 101 | } |
137 | 102 | ||
138 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | 103 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); |
139 | if (child_stderr == NULL) { | 104 | if (child_stderr == NULL) { |
140 | printf (_("Could not open stderr for %s\n"), command_line); | 105 | printf(_("Could not open stderr for %s\n"), command_line); |
141 | } | 106 | } |
142 | 107 | ||
143 | result = STATE_OK; | 108 | mp_state_enum result = STATE_OK; |
144 | 109 | ||
145 | line = 0; | 110 | int line_status = ONLINE; |
146 | 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; | ||
147 | 126 | ||
127 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
148 | /* strip the newline character from the end of the input */ | 128 | /* strip the newline character from the end of the input */ |
149 | if (input_buffer[strlen (input_buffer) - 1] == '\n') | 129 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
150 | input_buffer[strlen (input_buffer) - 1] = 0; | 130 | input_buffer[strlen(input_buffer) - 1] = 0; |
131 | } | ||
151 | 132 | ||
152 | line++; | 133 | line++; |
153 | 134 | ||
154 | temp_buffer = strtok (input_buffer, "="); | 135 | char *temp_buffer = strtok(input_buffer, "="); |
155 | temp_buffer = strtok (NULL, "="); | 136 | temp_buffer = strtok(NULL, "="); |
156 | 137 | ||
157 | if (temp_buffer == NULL && line < 13) { | 138 | if (temp_buffer == NULL && line < 13) { |
158 | 139 | result = STATE_UNKNOWN; | |
159 | result = STATE_UNKNOWN; | 140 | strcpy(errmsg, input_buffer); |
160 | strcpy (errmsg, input_buffer); | ||
161 | |||
162 | } else { | 141 | } else { |
163 | |||
164 | switch (line) { | 142 | switch (line) { |
165 | 143 | case 1: /* 1st line should contain the line status */ | |
166 | case 1: /* 1st line should contain the line status */ | 144 | line_status = atoi(temp_buffer); |
167 | line_status = atoi (temp_buffer); | ||
168 | break; | 145 | break; |
169 | case 2: /* 2nd line should contain the paper status */ | 146 | case 2: /* 2nd line should contain the paper status */ |
170 | paper_status = atoi (temp_buffer); | 147 | paper_status = atoi(temp_buffer); |
171 | break; | 148 | break; |
172 | case 3: /* 3rd line should be intervention required */ | 149 | case 3: /* 3rd line should be intervention required */ |
173 | intervention_required = atoi (temp_buffer); | 150 | intervention_required = atoi(temp_buffer); |
174 | break; | 151 | break; |
175 | case 4: /* 4th line should be peripheral error */ | 152 | case 4: /* 4th line should be peripheral error */ |
176 | peripheral_error = atoi (temp_buffer); | 153 | peripheral_error = atoi(temp_buffer); |
177 | break; | 154 | break; |
178 | case 5: /* 5th line should contain the paper jam status */ | 155 | case 5: /* 5th line should contain the paper jam status */ |
179 | paper_jam = atoi (temp_buffer); | 156 | paper_jam = atoi(temp_buffer); |
180 | break; | 157 | break; |
181 | case 6: /* 6th line should contain the paper out status */ | 158 | case 6: /* 6th line should contain the paper out status */ |
182 | paper_out = atoi (temp_buffer); | 159 | paper_out = atoi(temp_buffer); |
183 | break; | 160 | break; |
184 | case 7: /* 7th line should contain the toner low status */ | 161 | case 7: /* 7th line should contain the toner low status */ |
185 | toner_low = atoi (temp_buffer); | 162 | toner_low = atoi(temp_buffer); |
186 | break; | 163 | break; |
187 | case 8: /* did data come too slow for engine */ | 164 | case 8: /* did data come too slow for engine */ |
188 | page_punt = atoi (temp_buffer); | 165 | page_punt = atoi(temp_buffer); |
189 | break; | 166 | break; |
190 | case 9: /* did we run out of memory */ | 167 | case 9: /* did we run out of memory */ |
191 | memory_out = atoi (temp_buffer); | 168 | memory_out = atoi(temp_buffer); |
192 | break; | 169 | break; |
193 | case 10: /* is there a door open */ | 170 | case 10: /* is there a door open */ |
194 | door_open = atoi (temp_buffer); | 171 | door_open = atoi(temp_buffer); |
195 | break; | 172 | break; |
196 | case 11: /* is output tray full */ | 173 | case 11: /* is output tray full */ |
197 | paper_output = atoi (temp_buffer); | 174 | paper_output = atoi(temp_buffer); |
198 | break; | 175 | break; |
199 | case 12: /* display panel message */ | 176 | case 12: /* display panel message */ |
200 | strcpy (display_message, temp_buffer + 1); | 177 | strcpy(display_message, temp_buffer + 1); |
201 | break; | 178 | break; |
202 | default: /* fold multiline message */ | 179 | default: /* fold multiline message */ |
203 | strncat (display_message, input_buffer, | 180 | strncat(display_message, input_buffer, sizeof(display_message) - strlen(display_message) - 1); |
204 | sizeof (display_message) - strlen (display_message) - 1); | ||
205 | } | 181 | } |
206 | |||
207 | } | 182 | } |
208 | 183 | ||
209 | /* break out of the read loop if we encounter an error */ | 184 | /* break out of the read loop if we encounter an error */ |
210 | if (result != STATE_OK) | 185 | if (result != STATE_OK) { |
211 | break; | 186 | break; |
187 | } | ||
212 | } | 188 | } |
213 | 189 | ||
214 | /* WARNING if output found on stderr */ | 190 | /* WARNING if output found on stderr */ |
215 | if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | 191 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { |
216 | result = max_state (result, STATE_WARNING); | 192 | result = max_state(result, STATE_WARNING); |
217 | /* remove CRLF */ | 193 | /* remove CRLF */ |
218 | if (input_buffer[strlen (input_buffer) - 1] == '\n') | 194 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
219 | input_buffer[strlen (input_buffer) - 1] = 0; | 195 | input_buffer[strlen(input_buffer) - 1] = 0; |
220 | sprintf (errmsg, "%s", input_buffer ); | 196 | } |
221 | 197 | sprintf(errmsg, "%s", input_buffer); | |
222 | } | 198 | } |
223 | 199 | ||
224 | /* close stderr */ | 200 | /* close stderr */ |
225 | (void) fclose (child_stderr); | 201 | (void)fclose(child_stderr); |
226 | 202 | ||
227 | /* close the pipe */ | 203 | /* close the pipe */ |
228 | if (spclose (child_process)) | 204 | if (spclose(child_process)) { |
229 | result = max_state (result, STATE_WARNING); | 205 | result = max_state(result, STATE_WARNING); |
206 | } | ||
230 | 207 | ||
231 | /* if there wasn't any output, display an error */ | 208 | /* if there wasn't any output, display an error */ |
232 | if (line == 0) { | 209 | if (line == 0) { |
233 | |||
234 | /* might not be the problem, but most likely is. */ | 210 | /* might not be the problem, but most likely is. */ |
235 | result = STATE_UNKNOWN ; | 211 | result = STATE_UNKNOWN; |
236 | xasprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address ); | 212 | xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, config.address); |
237 | |||
238 | } | 213 | } |
239 | 214 | ||
240 | /* if we had no read errors, check the printer status results... */ | 215 | /* if we had no read errors, check the printer status results... */ |
@@ -242,201 +217,171 @@ main (int argc, char **argv) | |||
242 | 217 | ||
243 | if (paper_jam) { | 218 | if (paper_jam) { |
244 | result = STATE_WARNING; | 219 | result = STATE_WARNING; |
245 | strcpy (errmsg, _("Paper Jam")); | 220 | strcpy(errmsg, _("Paper Jam")); |
246 | } | 221 | } else if (paper_out) { |
247 | else if (paper_out) { | 222 | if (config.check_paper_out) { |
248 | if (check_paper_out) | ||
249 | result = STATE_WARNING; | 223 | result = STATE_WARNING; |
250 | strcpy (errmsg, _("Out of Paper")); | 224 | } |
251 | } | 225 | strcpy(errmsg, _("Out of Paper")); |
252 | else if (line_status == OFFLINE) { | 226 | } else if (line_status == OFFLINE) { |
253 | if (strcmp (errmsg, "POWERSAVE ON") != 0) { | 227 | if (strcmp(errmsg, "POWERSAVE ON") != 0) { |
254 | result = STATE_WARNING; | 228 | result = STATE_WARNING; |
255 | strcpy (errmsg, _("Printer Offline")); | 229 | strcpy(errmsg, _("Printer Offline")); |
256 | } | 230 | } |
257 | } | 231 | } else if (peripheral_error) { |
258 | else if (peripheral_error) { | ||
259 | result = STATE_WARNING; | 232 | result = STATE_WARNING; |
260 | strcpy (errmsg, _("Peripheral Error")); | 233 | strcpy(errmsg, _("Peripheral Error")); |
261 | } | 234 | } else if (intervention_required) { |
262 | else if (intervention_required) { | ||
263 | result = STATE_WARNING; | 235 | result = STATE_WARNING; |
264 | strcpy (errmsg, _("Intervention Required")); | 236 | strcpy(errmsg, _("Intervention Required")); |
265 | } | 237 | } else if (toner_low) { |
266 | else if (toner_low) { | ||
267 | result = STATE_WARNING; | 238 | result = STATE_WARNING; |
268 | strcpy (errmsg, _("Toner Low")); | 239 | strcpy(errmsg, _("Toner Low")); |
269 | } | 240 | } else if (memory_out) { |
270 | else if (memory_out) { | ||
271 | result = STATE_WARNING; | 241 | result = STATE_WARNING; |
272 | strcpy (errmsg, _("Insufficient Memory")); | 242 | strcpy(errmsg, _("Insufficient Memory")); |
273 | } | 243 | } else if (door_open) { |
274 | else if (door_open) { | ||
275 | result = STATE_WARNING; | 244 | result = STATE_WARNING; |
276 | strcpy (errmsg, _("A Door is Open")); | 245 | strcpy(errmsg, _("A Door is Open")); |
277 | } | 246 | } else if (paper_output) { |
278 | else if (paper_output) { | ||
279 | result = STATE_WARNING; | 247 | result = STATE_WARNING; |
280 | strcpy (errmsg, _("Output Tray is Full")); | 248 | strcpy(errmsg, _("Output Tray is Full")); |
281 | } | 249 | } else if (page_punt) { |
282 | else if (page_punt) { | ||
283 | result = STATE_WARNING; | 250 | result = STATE_WARNING; |
284 | strcpy (errmsg, _("Data too Slow for Engine")); | 251 | strcpy(errmsg, _("Data too Slow for Engine")); |
285 | } | 252 | } else if (paper_status) { |
286 | else if (paper_status) { | ||
287 | result = STATE_WARNING; | 253 | result = STATE_WARNING; |
288 | strcpy (errmsg, _("Unknown Paper Error")); | 254 | strcpy(errmsg, _("Unknown Paper Error")); |
289 | } | 255 | } |
290 | } | 256 | } |
291 | 257 | ||
292 | if (result == STATE_OK) | 258 | if (result == STATE_OK) { |
293 | printf (_("Printer ok - (%s)\n"), display_message); | 259 | printf(_("Printer ok - (%s)\n"), display_message); |
294 | 260 | } else if (result == STATE_UNKNOWN) { | |
295 | else if (result == STATE_UNKNOWN) { | 261 | printf("%s\n", errmsg); |
296 | |||
297 | printf ("%s\n", errmsg); | ||
298 | |||
299 | /* if printer could not be reached, escalate to critical */ | 262 | /* if printer could not be reached, escalate to critical */ |
300 | if (strstr (errmsg, "Timeout")) | 263 | if (strstr(errmsg, "Timeout")) { |
301 | result = STATE_CRITICAL; | 264 | result = STATE_CRITICAL; |
265 | } | ||
266 | } else if (result == STATE_WARNING) { | ||
267 | printf("%s (%s)\n", errmsg, display_message); | ||
302 | } | 268 | } |
303 | 269 | ||
304 | else if (result == STATE_WARNING) | 270 | exit(result); |
305 | printf ("%s (%s)\n", errmsg, display_message); | ||
306 | |||
307 | return result; | ||
308 | } | 271 | } |
309 | 272 | ||
310 | |||
311 | /* process command-line arguments */ | 273 | /* process command-line arguments */ |
312 | int | 274 | check_hpjd_config_wrapper process_arguments(int argc, char **argv) { |
313 | process_arguments (int argc, char **argv) | 275 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
314 | { | 276 | {"community", required_argument, 0, 'C'}, |
315 | int c; | 277 | /* {"critical", required_argument,0,'c'}, */ |
316 | 278 | /* {"warning", required_argument,0,'w'}, */ | |
317 | int option = 0; | 279 | {"port", required_argument, 0, 'p'}, |
318 | static struct option longopts[] = { | 280 | {"version", no_argument, 0, 'V'}, |
319 | {"hostname", required_argument, 0, 'H'}, | 281 | {"help", no_argument, 0, 'h'}, |
320 | {"community", required_argument, 0, 'C'}, | 282 | {0, 0, 0, 0}}; |
321 | /* {"critical", required_argument,0,'c'}, */ | 283 | |
322 | /* {"warning", required_argument,0,'w'}, */ | 284 | check_hpjd_config_wrapper result = { |
323 | {"port", required_argument,0,'p'}, | 285 | .errorcode = OK, |
324 | {"version", no_argument, 0, 'V'}, | 286 | .config = check_hpjd_config_init(), |
325 | {"help", no_argument, 0, 'h'}, | ||
326 | {0, 0, 0, 0} | ||
327 | }; | 287 | }; |
328 | 288 | ||
329 | if (argc < 2) | 289 | if (argc < 2) { |
330 | return ERROR; | 290 | result.errorcode = ERROR; |
331 | 291 | return result; | |
292 | } | ||
332 | 293 | ||
333 | while (1) { | 294 | int option = 0; |
334 | 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); | ||
335 | 297 | ||
336 | if (c == -1 || c == EOF || c == 1) | 298 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
337 | break; | 299 | break; |
300 | } | ||
338 | 301 | ||
339 | switch (c) { | 302 | switch (option_index) { |
340 | case 'H': /* hostname */ | 303 | case 'H': /* hostname */ |
341 | if (is_host (optarg)) { | 304 | if (is_host(optarg)) { |
342 | address = strscpy(address, optarg) ; | 305 | result.config.address = strscpy(result.config.address, optarg); |
343 | } | 306 | } else { |
344 | else { | 307 | usage2(_("Invalid hostname/address"), optarg); |
345 | usage2 (_("Invalid hostname/address"), optarg); | ||
346 | } | 308 | } |
347 | break; | 309 | break; |
348 | case 'C': /* community */ | 310 | case 'C': /* community */ |
349 | community = strscpy (community, optarg); | 311 | result.config.community = strscpy(result.config.community, optarg); |
350 | break; | 312 | break; |
351 | case 'p': | 313 | case 'p': |
352 | if (!is_intpos(optarg)) | 314 | if (!is_intpos(optarg)) { |
353 | usage2 (_("Port must be a positive short integer"), optarg); | 315 | usage2(_("Port must be a positive short integer"), optarg); |
354 | else | 316 | } else { |
355 | port = atoi(optarg); | 317 | result.config.port = atoi(optarg); |
318 | } | ||
356 | break; | 319 | break; |
357 | case 'D': /* disable paper out check*/ | 320 | case 'D': /* disable paper out check*/ |
358 | check_paper_out = 0; | 321 | result.config.check_paper_out = false; |
359 | break; | 322 | break; |
360 | case 'V': /* version */ | 323 | case 'V': /* version */ |
361 | print_revision (progname, NP_VERSION); | 324 | print_revision(progname, NP_VERSION); |
362 | exit (STATE_UNKNOWN); | 325 | exit(STATE_UNKNOWN); |
363 | case 'h': /* help */ | 326 | case 'h': /* help */ |
364 | print_help (); | 327 | print_help(); |
365 | exit (STATE_UNKNOWN); | 328 | exit(STATE_UNKNOWN); |
366 | case '?': /* help */ | 329 | case '?': /* help */ |
367 | usage5 (); | 330 | usage5(); |
368 | } | 331 | } |
369 | } | 332 | } |
370 | 333 | ||
371 | c = optind; | 334 | int c = optind; |
372 | if (address == NULL) { | 335 | if (result.config.address == NULL) { |
373 | if (is_host (argv[c])) { | 336 | if (is_host(argv[c])) { |
374 | address = argv[c++]; | 337 | result.config.address = argv[c++]; |
375 | } | 338 | } else { |
376 | else { | 339 | usage2(_("Invalid hostname/address"), argv[c]); |
377 | usage2 (_("Invalid hostname/address"), argv[c]); | ||
378 | } | 340 | } |
379 | } | 341 | } |
380 | 342 | ||
381 | if (community == NULL) { | 343 | if (result.config.community == NULL) { |
382 | if (argv[c] != NULL ) | 344 | if (argv[c] != NULL) { |
383 | community = argv[c]; | 345 | result.config.community = argv[c]; |
384 | else | 346 | } else { |
385 | community = strdup (DEFAULT_COMMUNITY); | 347 | result.config.community = strdup(DEFAULT_COMMUNITY); |
386 | } | 348 | } |
387 | |||
388 | if (port == 0) { | ||
389 | port = atoi(DEFAULT_PORT); | ||
390 | } | 349 | } |
391 | 350 | ||
392 | return validate_arguments (); | 351 | return result; |
393 | } | ||
394 | |||
395 | |||
396 | int | ||
397 | validate_arguments (void) | ||
398 | { | ||
399 | return OK; | ||
400 | } | 352 | } |
401 | 353 | ||
354 | void print_help(void) { | ||
355 | print_revision(progname, NP_VERSION); | ||
402 | 356 | ||
403 | void | 357 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
404 | print_help (void) | 358 | printf(COPYRIGHT, copyright, email); |
405 | { | ||
406 | print_revision (progname, NP_VERSION); | ||
407 | |||
408 | printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); | ||
409 | printf (COPYRIGHT, copyright, email); | ||
410 | 359 | ||
411 | printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card.")); | 360 | printf("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card.")); |
412 | printf ("%s\n", _("Net-snmp must be installed on the computer running the plugin.")); | 361 | printf("%s\n", _("Net-snmp must be installed on the computer running the plugin.")); |
413 | 362 | ||
414 | printf ("\n\n"); | 363 | printf("\n\n"); |
415 | 364 | ||
416 | print_usage (); | 365 | print_usage(); |
417 | 366 | ||
418 | printf (UT_HELP_VRSN); | 367 | printf(UT_HELP_VRSN); |
419 | printf (UT_EXTRA_OPTS); | 368 | printf(UT_EXTRA_OPTS); |
420 | 369 | ||
421 | printf (" %s\n", "-C, --community=STRING"); | 370 | printf(" %s\n", "-C, --community=STRING"); |
422 | printf (" %s", _("The SNMP community name ")); | 371 | printf(" %s", _("The SNMP community name ")); |
423 | printf (_("(default=%s)"), DEFAULT_COMMUNITY); | 372 | printf(_("(default=%s)"), DEFAULT_COMMUNITY); |
424 | printf ("\n"); | 373 | printf("\n"); |
425 | printf (" %s\n", "-p, --port=STRING"); | 374 | printf(" %s\n", "-p, --port=STRING"); |
426 | printf (" %s", _("Specify the port to check ")); | 375 | printf(" %s", _("Specify the port to check ")); |
427 | printf (_("(default=%s)"), DEFAULT_PORT); | 376 | printf(_("(default=%s)"), DEFAULT_PORT); |
428 | printf ("\n"); | 377 | printf("\n"); |
429 | printf (" %s\n", "-D"); | 378 | printf(" %s\n", "-D"); |
430 | printf (" %s", _("Disable paper check ")); | 379 | printf(" %s", _("Disable paper check ")); |
431 | 380 | ||
432 | printf (UT_SUPPORT); | 381 | printf(UT_SUPPORT); |
433 | } | 382 | } |
434 | 383 | ||
435 | 384 | void print_usage(void) { | |
436 | 385 | printf("%s\n", _("Usage:")); | |
437 | void | 386 | printf("%s -H host [-C community] [-p port] [-D]\n", progname); |
438 | print_usage (void) | ||
439 | { | ||
440 | printf ("%s\n", _("Usage:")); | ||
441 | printf ("%s -H host [-C community] [-p port] [-D]\n", progname); | ||
442 | } | 387 | } |