diff options
Diffstat (limited to 'plugins/check_hpjd.c')
| -rw-r--r-- | plugins/check_hpjd.c | 543 |
1 files changed, 246 insertions, 297 deletions
diff --git a/plugins/check_hpjd.c b/plugins/check_hpjd.c index c34bb082..9907abc5 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,165 @@ 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", |
| 110 | (query_string, | 89 | HPJD_LINE_STATUS, HPJD_PAPER_STATUS, HPJD_INTERVENTION_REQUIRED, |
| 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_PERIPHERAL_ERROR, HPJD_GD_PAPER_JAM, HPJD_GD_PAPER_OUT, HPJD_GD_TONER_LOW, |
| 112 | HPJD_LINE_STATUS, | 91 | HPJD_GD_PAGE_PUNT, HPJD_GD_MEMORY_OUT, HPJD_GD_DOOR_OPEN, HPJD_GD_PAPER_OUTPUT, |
| 113 | HPJD_PAPER_STATUS, | 92 | HPJD_GD_STATUS_DISPLAY); |
| 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 | 93 | ||
| 123 | /* get the command to run */ | 94 | /* get the command to run */ |
| 124 | sprintf (command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", | 95 | char command_line[1024]; |
| 125 | PATH_TO_SNMPGET, | 96 | sprintf(command_line, "%s -OQa -m : -v 1 -c %s %s:%u %s", PATH_TO_SNMPGET, config.community, |
| 126 | community, | 97 | config.address, config.port, query_string); |
| 127 | address, | ||
| 128 | port, | ||
| 129 | query_string); | ||
| 130 | 98 | ||
| 131 | /* run the command */ | 99 | /* run the command */ |
| 132 | child_process = spopen (command_line); | 100 | child_process = spopen(command_line); |
| 133 | if (child_process == NULL) { | 101 | if (child_process == NULL) { |
| 134 | printf (_("Could not open pipe: %s\n"), command_line); | 102 | printf(_("Could not open pipe: %s\n"), command_line); |
| 135 | return STATE_UNKNOWN; | 103 | return STATE_UNKNOWN; |
| 136 | } | 104 | } |
| 137 | 105 | ||
| 138 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | 106 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); |
| 139 | if (child_stderr == NULL) { | 107 | if (child_stderr == NULL) { |
| 140 | printf (_("Could not open stderr for %s\n"), command_line); | 108 | printf(_("Could not open stderr for %s\n"), command_line); |
| 141 | } | 109 | } |
| 142 | 110 | ||
| 143 | result = STATE_OK; | 111 | mp_state_enum result = STATE_OK; |
| 144 | 112 | ||
| 145 | line = 0; | 113 | int line_status = ONLINE; |
| 146 | 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]; | ||
| 125 | |||
| 126 | char input_buffer[MAX_INPUT_BUFFER]; | ||
| 127 | char *errmsg = malloc(MAX_INPUT_BUFFER); | ||
| 128 | int line = 0; | ||
| 147 | 129 | ||
| 130 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 148 | /* strip the newline character from the end of the input */ | 131 | /* strip the newline character from the end of the input */ |
| 149 | if (input_buffer[strlen (input_buffer) - 1] == '\n') | 132 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
| 150 | input_buffer[strlen (input_buffer) - 1] = 0; | 133 | input_buffer[strlen(input_buffer) - 1] = 0; |
| 134 | } | ||
| 151 | 135 | ||
| 152 | line++; | 136 | line++; |
| 153 | 137 | ||
| 154 | temp_buffer = strtok (input_buffer, "="); | 138 | char *temp_buffer = strtok(input_buffer, "="); |
| 155 | temp_buffer = strtok (NULL, "="); | 139 | temp_buffer = strtok(NULL, "="); |
| 156 | 140 | ||
| 157 | if (temp_buffer == NULL && line < 13) { | 141 | if (temp_buffer == NULL && line < 13) { |
| 158 | 142 | result = STATE_UNKNOWN; | |
| 159 | result = STATE_UNKNOWN; | 143 | strcpy(errmsg, input_buffer); |
| 160 | strcpy (errmsg, input_buffer); | ||
| 161 | |||
| 162 | } else { | 144 | } else { |
| 163 | |||
| 164 | switch (line) { | 145 | switch (line) { |
| 165 | 146 | case 1: /* 1st line should contain the line status */ | |
| 166 | case 1: /* 1st line should contain the line status */ | 147 | line_status = atoi(temp_buffer); |
| 167 | line_status = atoi (temp_buffer); | ||
| 168 | break; | 148 | break; |
| 169 | case 2: /* 2nd line should contain the paper status */ | 149 | case 2: /* 2nd line should contain the paper status */ |
| 170 | paper_status = atoi (temp_buffer); | 150 | paper_status = atoi(temp_buffer); |
| 171 | break; | 151 | break; |
| 172 | case 3: /* 3rd line should be intervention required */ | 152 | case 3: /* 3rd line should be intervention required */ |
| 173 | intervention_required = atoi (temp_buffer); | 153 | intervention_required = atoi(temp_buffer); |
| 174 | break; | 154 | break; |
| 175 | case 4: /* 4th line should be peripheral error */ | 155 | case 4: /* 4th line should be peripheral error */ |
| 176 | peripheral_error = atoi (temp_buffer); | 156 | peripheral_error = atoi(temp_buffer); |
| 177 | break; | 157 | break; |
| 178 | case 5: /* 5th line should contain the paper jam status */ | 158 | case 5: /* 5th line should contain the paper jam status */ |
| 179 | paper_jam = atoi (temp_buffer); | 159 | paper_jam = atoi(temp_buffer); |
| 180 | break; | 160 | break; |
| 181 | case 6: /* 6th line should contain the paper out status */ | 161 | case 6: /* 6th line should contain the paper out status */ |
| 182 | paper_out = atoi (temp_buffer); | 162 | paper_out = atoi(temp_buffer); |
| 183 | break; | 163 | break; |
| 184 | case 7: /* 7th line should contain the toner low status */ | 164 | case 7: /* 7th line should contain the toner low status */ |
| 185 | toner_low = atoi (temp_buffer); | 165 | toner_low = atoi(temp_buffer); |
| 186 | break; | 166 | break; |
| 187 | case 8: /* did data come too slow for engine */ | 167 | case 8: /* did data come too slow for engine */ |
| 188 | page_punt = atoi (temp_buffer); | 168 | page_punt = atoi(temp_buffer); |
| 189 | break; | 169 | break; |
| 190 | case 9: /* did we run out of memory */ | 170 | case 9: /* did we run out of memory */ |
| 191 | memory_out = atoi (temp_buffer); | 171 | memory_out = atoi(temp_buffer); |
| 192 | break; | 172 | break; |
| 193 | case 10: /* is there a door open */ | 173 | case 10: /* is there a door open */ |
| 194 | door_open = atoi (temp_buffer); | 174 | door_open = atoi(temp_buffer); |
| 195 | break; | 175 | break; |
| 196 | case 11: /* is output tray full */ | 176 | case 11: /* is output tray full */ |
| 197 | paper_output = atoi (temp_buffer); | 177 | paper_output = atoi(temp_buffer); |
| 198 | break; | 178 | break; |
| 199 | case 12: /* display panel message */ | 179 | case 12: /* display panel message */ |
| 200 | strcpy (display_message, temp_buffer + 1); | 180 | strcpy(display_message, temp_buffer + 1); |
| 201 | break; | 181 | break; |
| 202 | default: /* fold multiline message */ | 182 | default: /* fold multiline message */ |
| 203 | strncat (display_message, input_buffer, | 183 | strncat(display_message, input_buffer, |
| 204 | sizeof (display_message) - strlen (display_message) - 1); | 184 | sizeof(display_message) - strlen(display_message) - 1); |
| 205 | } | 185 | } |
| 206 | |||
| 207 | } | 186 | } |
| 208 | 187 | ||
| 209 | /* break out of the read loop if we encounter an error */ | 188 | /* break out of the read loop if we encounter an error */ |
| 210 | if (result != STATE_OK) | 189 | if (result != STATE_OK) { |
| 211 | break; | 190 | break; |
| 191 | } | ||
| 212 | } | 192 | } |
| 213 | 193 | ||
| 214 | /* WARNING if output found on stderr */ | 194 | /* WARNING if output found on stderr */ |
| 215 | if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | 195 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { |
| 216 | result = max_state (result, STATE_WARNING); | 196 | result = max_state(result, STATE_WARNING); |
| 217 | /* remove CRLF */ | 197 | /* remove CRLF */ |
| 218 | if (input_buffer[strlen (input_buffer) - 1] == '\n') | 198 | if (input_buffer[strlen(input_buffer) - 1] == '\n') { |
| 219 | input_buffer[strlen (input_buffer) - 1] = 0; | 199 | input_buffer[strlen(input_buffer) - 1] = 0; |
| 220 | sprintf (errmsg, "%s", input_buffer ); | 200 | } |
| 221 | 201 | sprintf(errmsg, "%s", input_buffer); | |
| 222 | } | 202 | } |
| 223 | 203 | ||
| 224 | /* close stderr */ | 204 | /* close stderr */ |
| 225 | (void) fclose (child_stderr); | 205 | (void)fclose(child_stderr); |
| 226 | 206 | ||
| 227 | /* close the pipe */ | 207 | /* close the pipe */ |
| 228 | if (spclose (child_process)) | 208 | if (spclose(child_process)) { |
| 229 | result = max_state (result, STATE_WARNING); | 209 | result = max_state(result, STATE_WARNING); |
| 210 | } | ||
| 230 | 211 | ||
| 231 | /* if there wasn't any output, display an error */ | 212 | /* if there wasn't any output, display an error */ |
| 232 | if (line == 0) { | 213 | if (line == 0) { |
| 233 | |||
| 234 | /* might not be the problem, but most likely is. */ | 214 | /* might not be the problem, but most likely is. */ |
| 235 | result = STATE_UNKNOWN ; | 215 | result = STATE_UNKNOWN; |
| 236 | xasprintf (&errmsg, "%s : Timeout from host %s\n", errmsg, address ); | 216 | xasprintf(&errmsg, "%s : Timeout from host %s\n", errmsg, config.address); |
| 237 | |||
| 238 | } | 217 | } |
| 239 | 218 | ||
| 240 | /* if we had no read errors, check the printer status results... */ | 219 | /* if we had no read errors, check the printer status results... */ |
| @@ -242,201 +221,171 @@ main (int argc, char **argv) | |||
| 242 | 221 | ||
| 243 | if (paper_jam) { | 222 | if (paper_jam) { |
| 244 | result = STATE_WARNING; | 223 | result = STATE_WARNING; |
| 245 | strcpy (errmsg, _("Paper Jam")); | 224 | strcpy(errmsg, _("Paper Jam")); |
| 246 | } | 225 | } else if (paper_out) { |
| 247 | else if (paper_out) { | 226 | if (config.check_paper_out) { |
| 248 | if (check_paper_out) | ||
| 249 | result = STATE_WARNING; | 227 | result = STATE_WARNING; |
| 250 | strcpy (errmsg, _("Out of Paper")); | 228 | } |
| 251 | } | 229 | strcpy(errmsg, _("Out of Paper")); |
| 252 | else if (line_status == OFFLINE) { | 230 | } else if (line_status == OFFLINE) { |
| 253 | if (strcmp (errmsg, "POWERSAVE ON") != 0) { | 231 | if (strcmp(errmsg, "POWERSAVE ON") != 0) { |
| 254 | result = STATE_WARNING; | 232 | result = STATE_WARNING; |
| 255 | strcpy (errmsg, _("Printer Offline")); | 233 | strcpy(errmsg, _("Printer Offline")); |
| 256 | } | 234 | } |
| 257 | } | 235 | } else if (peripheral_error) { |
| 258 | else if (peripheral_error) { | ||
| 259 | result = STATE_WARNING; | 236 | result = STATE_WARNING; |
| 260 | strcpy (errmsg, _("Peripheral Error")); | 237 | strcpy(errmsg, _("Peripheral Error")); |
| 261 | } | 238 | } else if (intervention_required) { |
| 262 | else if (intervention_required) { | ||
| 263 | result = STATE_WARNING; | 239 | result = STATE_WARNING; |
| 264 | strcpy (errmsg, _("Intervention Required")); | 240 | strcpy(errmsg, _("Intervention Required")); |
| 265 | } | 241 | } else if (toner_low) { |
| 266 | else if (toner_low) { | ||
| 267 | result = STATE_WARNING; | 242 | result = STATE_WARNING; |
| 268 | strcpy (errmsg, _("Toner Low")); | 243 | strcpy(errmsg, _("Toner Low")); |
| 269 | } | 244 | } else if (memory_out) { |
| 270 | else if (memory_out) { | ||
| 271 | result = STATE_WARNING; | 245 | result = STATE_WARNING; |
| 272 | strcpy (errmsg, _("Insufficient Memory")); | 246 | strcpy(errmsg, _("Insufficient Memory")); |
| 273 | } | 247 | } else if (door_open) { |
| 274 | else if (door_open) { | ||
| 275 | result = STATE_WARNING; | 248 | result = STATE_WARNING; |
| 276 | strcpy (errmsg, _("A Door is Open")); | 249 | strcpy(errmsg, _("A Door is Open")); |
| 277 | } | 250 | } else if (paper_output) { |
| 278 | else if (paper_output) { | ||
| 279 | result = STATE_WARNING; | 251 | result = STATE_WARNING; |
| 280 | strcpy (errmsg, _("Output Tray is Full")); | 252 | strcpy(errmsg, _("Output Tray is Full")); |
| 281 | } | 253 | } else if (page_punt) { |
| 282 | else if (page_punt) { | ||
| 283 | result = STATE_WARNING; | 254 | result = STATE_WARNING; |
| 284 | strcpy (errmsg, _("Data too Slow for Engine")); | 255 | strcpy(errmsg, _("Data too Slow for Engine")); |
| 285 | } | 256 | } else if (paper_status) { |
| 286 | else if (paper_status) { | ||
| 287 | result = STATE_WARNING; | 257 | result = STATE_WARNING; |
| 288 | strcpy (errmsg, _("Unknown Paper Error")); | 258 | strcpy(errmsg, _("Unknown Paper Error")); |
| 289 | } | 259 | } |
| 290 | } | 260 | } |
| 291 | 261 | ||
| 292 | if (result == STATE_OK) | 262 | if (result == STATE_OK) { |
| 293 | printf (_("Printer ok - (%s)\n"), display_message); | 263 | printf(_("Printer ok - (%s)\n"), display_message); |
| 294 | 264 | } else if (result == STATE_UNKNOWN) { | |
| 295 | else if (result == STATE_UNKNOWN) { | 265 | printf("%s\n", errmsg); |
| 296 | |||
| 297 | printf ("%s\n", errmsg); | ||
| 298 | |||
| 299 | /* if printer could not be reached, escalate to critical */ | 266 | /* if printer could not be reached, escalate to critical */ |
| 300 | if (strstr (errmsg, "Timeout")) | 267 | if (strstr(errmsg, "Timeout")) { |
| 301 | result = STATE_CRITICAL; | 268 | result = STATE_CRITICAL; |
| 269 | } | ||
| 270 | } else if (result == STATE_WARNING) { | ||
| 271 | printf("%s (%s)\n", errmsg, display_message); | ||
| 302 | } | 272 | } |
| 303 | 273 | ||
| 304 | else if (result == STATE_WARNING) | 274 | exit(result); |
| 305 | printf ("%s (%s)\n", errmsg, display_message); | ||
| 306 | |||
| 307 | return result; | ||
| 308 | } | 275 | } |
| 309 | 276 | ||
| 310 | |||
| 311 | /* process command-line arguments */ | 277 | /* process command-line arguments */ |
| 312 | int | 278 | check_hpjd_config_wrapper process_arguments(int argc, char **argv) { |
| 313 | process_arguments (int argc, char **argv) | 279 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 314 | { | 280 | {"community", required_argument, 0, 'C'}, |
| 315 | int c; | 281 | /* {"critical", required_argument,0,'c'}, */ |
| 316 | 282 | /* {"warning", required_argument,0,'w'}, */ | |
| 317 | int option = 0; | 283 | {"port", required_argument, 0, 'p'}, |
| 318 | static struct option longopts[] = { | 284 | {"version", no_argument, 0, 'V'}, |
| 319 | {"hostname", required_argument, 0, 'H'}, | 285 | {"help", no_argument, 0, 'h'}, |
| 320 | {"community", required_argument, 0, 'C'}, | 286 | {0, 0, 0, 0}}; |
| 321 | /* {"critical", required_argument,0,'c'}, */ | 287 | |
| 322 | /* {"warning", required_argument,0,'w'}, */ | 288 | check_hpjd_config_wrapper result = { |
| 323 | {"port", required_argument,0,'p'}, | 289 | .errorcode = OK, |
| 324 | {"version", no_argument, 0, 'V'}, | 290 | .config = check_hpjd_config_init(), |
| 325 | {"help", no_argument, 0, 'h'}, | ||
| 326 | {0, 0, 0, 0} | ||
| 327 | }; | 291 | }; |
| 328 | 292 | ||
| 329 | if (argc < 2) | 293 | if (argc < 2) { |
| 330 | return ERROR; | 294 | result.errorcode = ERROR; |
| 331 | 295 | return result; | |
| 296 | } | ||
| 332 | 297 | ||
| 333 | while (1) { | 298 | int option = 0; |
| 334 | 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); | ||
| 335 | 301 | ||
| 336 | if (c == -1 || c == EOF || c == 1) | 302 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
| 337 | break; | 303 | break; |
| 304 | } | ||
| 338 | 305 | ||
| 339 | switch (c) { | 306 | switch (option_index) { |
| 340 | case 'H': /* hostname */ | 307 | case 'H': /* hostname */ |
| 341 | if (is_host (optarg)) { | 308 | if (is_host(optarg)) { |
| 342 | address = strscpy(address, optarg) ; | 309 | result.config.address = strscpy(result.config.address, optarg); |
| 343 | } | 310 | } else { |
| 344 | else { | 311 | usage2(_("Invalid hostname/address"), optarg); |
| 345 | usage2 (_("Invalid hostname/address"), optarg); | ||
| 346 | } | 312 | } |
| 347 | break; | 313 | break; |
| 348 | case 'C': /* community */ | 314 | case 'C': /* community */ |
| 349 | community = strscpy (community, optarg); | 315 | result.config.community = strscpy(result.config.community, optarg); |
| 350 | break; | 316 | break; |
| 351 | case 'p': | 317 | case 'p': |
| 352 | if (!is_intpos(optarg)) | 318 | if (!is_intpos(optarg)) { |
| 353 | usage2 (_("Port must be a positive short integer"), optarg); | 319 | usage2(_("Port must be a positive short integer"), optarg); |
| 354 | else | 320 | } else { |
| 355 | port = atoi(optarg); | 321 | result.config.port = atoi(optarg); |
| 322 | } | ||
| 356 | break; | 323 | break; |
| 357 | case 'D': /* disable paper out check*/ | 324 | case 'D': /* disable paper out check*/ |
| 358 | check_paper_out = 0; | 325 | result.config.check_paper_out = false; |
| 359 | break; | 326 | break; |
| 360 | case 'V': /* version */ | 327 | case 'V': /* version */ |
| 361 | print_revision (progname, NP_VERSION); | 328 | print_revision(progname, NP_VERSION); |
| 362 | exit (STATE_UNKNOWN); | 329 | exit(STATE_UNKNOWN); |
| 363 | case 'h': /* help */ | 330 | case 'h': /* help */ |
| 364 | print_help (); | 331 | print_help(); |
| 365 | exit (STATE_UNKNOWN); | 332 | exit(STATE_UNKNOWN); |
| 366 | case '?': /* help */ | 333 | case '?': /* help */ |
| 367 | usage5 (); | 334 | usage5(); |
| 368 | } | 335 | } |
| 369 | } | 336 | } |
| 370 | 337 | ||
| 371 | c = optind; | 338 | int c = optind; |
| 372 | if (address == NULL) { | 339 | if (result.config.address == NULL) { |
| 373 | if (is_host (argv[c])) { | 340 | if (is_host(argv[c])) { |
| 374 | address = argv[c++]; | 341 | result.config.address = argv[c++]; |
| 375 | } | 342 | } else { |
| 376 | else { | 343 | usage2(_("Invalid hostname/address"), argv[c]); |
| 377 | usage2 (_("Invalid hostname/address"), argv[c]); | ||
| 378 | } | 344 | } |
| 379 | } | 345 | } |
| 380 | 346 | ||
| 381 | if (community == NULL) { | 347 | if (result.config.community == NULL) { |
| 382 | if (argv[c] != NULL ) | 348 | if (argv[c] != NULL) { |
| 383 | community = argv[c]; | 349 | result.config.community = argv[c]; |
| 384 | else | 350 | } else { |
| 385 | community = strdup (DEFAULT_COMMUNITY); | 351 | result.config.community = strdup(DEFAULT_COMMUNITY); |
| 386 | } | 352 | } |
| 387 | |||
| 388 | if (port == 0) { | ||
| 389 | port = atoi(DEFAULT_PORT); | ||
| 390 | } | 353 | } |
| 391 | 354 | ||
| 392 | return validate_arguments (); | 355 | return result; |
| 393 | } | ||
| 394 | |||
| 395 | |||
| 396 | int | ||
| 397 | validate_arguments (void) | ||
| 398 | { | ||
| 399 | return OK; | ||
| 400 | } | 356 | } |
| 401 | 357 | ||
| 358 | void print_help(void) { | ||
| 359 | print_revision(progname, NP_VERSION); | ||
| 402 | 360 | ||
| 403 | void | 361 | printf("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"); |
| 404 | print_help (void) | 362 | 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 | 363 | ||
| 411 | printf ("%s\n", _("This plugin tests the STATUS of an HP printer with a JetDirect card.")); | 364 | 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.")); | 365 | printf("%s\n", _("Net-snmp must be installed on the computer running the plugin.")); |
| 413 | 366 | ||
| 414 | printf ("\n\n"); | 367 | printf("\n\n"); |
| 415 | 368 | ||
| 416 | print_usage (); | 369 | print_usage(); |
| 417 | 370 | ||
| 418 | printf (UT_HELP_VRSN); | 371 | printf(UT_HELP_VRSN); |
| 419 | printf (UT_EXTRA_OPTS); | 372 | printf(UT_EXTRA_OPTS); |
| 420 | 373 | ||
| 421 | printf (" %s\n", "-C, --community=STRING"); | 374 | printf(" %s\n", "-C, --community=STRING"); |
| 422 | printf (" %s", _("The SNMP community name ")); | 375 | printf(" %s", _("The SNMP community name ")); |
| 423 | printf (_("(default=%s)"), DEFAULT_COMMUNITY); | 376 | printf(_("(default=%s)"), DEFAULT_COMMUNITY); |
| 424 | printf ("\n"); | 377 | printf("\n"); |
| 425 | printf (" %s\n", "-p, --port=STRING"); | 378 | printf(" %s\n", "-p, --port=STRING"); |
| 426 | printf (" %s", _("Specify the port to check ")); | 379 | printf(" %s", _("Specify the port to check ")); |
| 427 | printf (_("(default=%s)"), DEFAULT_PORT); | 380 | printf(_("(default=%s)"), DEFAULT_PORT); |
| 428 | printf ("\n"); | 381 | printf("\n"); |
| 429 | printf (" %s\n", "-D"); | 382 | printf(" %s\n", "-D"); |
| 430 | printf (" %s", _("Disable paper check ")); | 383 | printf(" %s", _("Disable paper check ")); |
| 431 | 384 | ||
| 432 | printf (UT_SUPPORT); | 385 | printf(UT_SUPPORT); |
| 433 | } | 386 | } |
| 434 | 387 | ||
| 435 | 388 | void print_usage(void) { | |
| 436 | 389 | printf("%s\n", _("Usage:")); | |
| 437 | void | 390 | 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 | } | 391 | } |
