diff options
| -rw-r--r-- | plugins/check_real.c | 108 |
1 files changed, 65 insertions, 43 deletions
diff --git a/plugins/check_real.c b/plugins/check_real.c index 369a88b1..3737f69d 100644 --- a/plugins/check_real.c +++ b/plugins/check_real.c | |||
| @@ -66,8 +66,9 @@ int main(int argc, char **argv) { | |||
| 66 | /* Parse extra opts if any */ | 66 | /* Parse extra opts if any */ |
| 67 | argv = np_extra_opts(&argc, argv, progname); | 67 | argv = np_extra_opts(&argc, argv, progname); |
| 68 | 68 | ||
| 69 | if (process_arguments(argc, argv) == ERROR) | 69 | if (process_arguments(argc, argv) == ERROR) { |
| 70 | usage4(_("Could not parse arguments")); | 70 | usage4(_("Could not parse arguments")); |
| 71 | } | ||
| 71 | 72 | ||
| 72 | /* initialize alarm signal handling */ | 73 | /* initialize alarm signal handling */ |
| 73 | signal(SIGALRM, socket_timeout_alarm_handler); | 74 | signal(SIGALRM, socket_timeout_alarm_handler); |
| @@ -78,8 +79,9 @@ int main(int argc, char **argv) { | |||
| 78 | 79 | ||
| 79 | /* try to connect to the host at the given port number */ | 80 | /* try to connect to the host at the given port number */ |
| 80 | int socket; | 81 | int socket; |
| 81 | if (my_tcp_connect(server_address, server_port, &socket) != STATE_OK) | 82 | if (my_tcp_connect(server_address, server_port, &socket) != STATE_OK) { |
| 82 | die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), server_address, server_port); | 83 | die(STATE_CRITICAL, _("Unable to connect to %s on port %d\n"), server_address, server_port); |
| 84 | } | ||
| 83 | 85 | ||
| 84 | /* Part I - Server Check */ | 86 | /* Part I - Server Check */ |
| 85 | 87 | ||
| @@ -100,16 +102,18 @@ int main(int argc, char **argv) { | |||
| 100 | result = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); | 102 | result = recv(socket, buffer, MAX_INPUT_BUFFER - 1, 0); |
| 101 | 103 | ||
| 102 | /* return a CRITICAL status if we couldn't read any data */ | 104 | /* return a CRITICAL status if we couldn't read any data */ |
| 103 | if (result == -1) | 105 | if (result == -1) { |
| 104 | die(STATE_CRITICAL, _("No data received from %s\n"), host_name); | 106 | die(STATE_CRITICAL, _("No data received from %s\n"), host_name); |
| 107 | } | ||
| 105 | 108 | ||
| 106 | char *status_line = NULL; | 109 | char *status_line = NULL; |
| 107 | /* make sure we find the response we are looking for */ | 110 | /* make sure we find the response we are looking for */ |
| 108 | if (!strstr(buffer, server_expect)) { | 111 | if (!strstr(buffer, server_expect)) { |
| 109 | if (server_port == PORT) | 112 | if (server_port == PORT) { |
| 110 | printf("%s\n", _("Invalid REAL response received from host")); | 113 | printf("%s\n", _("Invalid REAL response received from host")); |
| 111 | else | 114 | } else { |
| 112 | printf(_("Invalid REAL response received from host on port %d\n"), server_port); | 115 | printf(_("Invalid REAL response received from host on port %d\n"), server_port); |
| 116 | } | ||
| 113 | } else { | 117 | } else { |
| 114 | /* else we got the REAL string, so check the return code */ | 118 | /* else we got the REAL string, so check the return code */ |
| 115 | 119 | ||
| @@ -119,33 +123,37 @@ int main(int argc, char **argv) { | |||
| 119 | 123 | ||
| 120 | status_line = (char *)strtok(buffer, "\n"); | 124 | status_line = (char *)strtok(buffer, "\n"); |
| 121 | 125 | ||
| 122 | if (strstr(status_line, "200")) | 126 | if (strstr(status_line, "200")) { |
| 123 | result = STATE_OK; | 127 | result = STATE_OK; |
| 128 | } | ||
| 124 | 129 | ||
| 125 | /* client errors result in a warning state */ | 130 | /* client errors result in a warning state */ |
| 126 | else if (strstr(status_line, "400")) | 131 | else if (strstr(status_line, "400")) { |
| 127 | result = STATE_WARNING; | 132 | result = STATE_WARNING; |
| 128 | else if (strstr(status_line, "401")) | 133 | } else if (strstr(status_line, "401")) { |
| 129 | result = STATE_WARNING; | 134 | result = STATE_WARNING; |
| 130 | else if (strstr(status_line, "402")) | 135 | } else if (strstr(status_line, "402")) { |
| 131 | result = STATE_WARNING; | 136 | result = STATE_WARNING; |
| 132 | else if (strstr(status_line, "403")) | 137 | } else if (strstr(status_line, "403")) { |
| 133 | result = STATE_WARNING; | 138 | result = STATE_WARNING; |
| 134 | else if (strstr(status_line, "404")) | 139 | } else if (strstr(status_line, "404")) { |
| 135 | result = STATE_WARNING; | 140 | result = STATE_WARNING; |
| 141 | } | ||
| 136 | 142 | ||
| 137 | /* server errors result in a critical state */ | 143 | /* server errors result in a critical state */ |
| 138 | else if (strstr(status_line, "500")) | 144 | else if (strstr(status_line, "500")) { |
| 139 | result = STATE_CRITICAL; | 145 | result = STATE_CRITICAL; |
| 140 | else if (strstr(status_line, "501")) | 146 | } else if (strstr(status_line, "501")) { |
| 141 | result = STATE_CRITICAL; | 147 | result = STATE_CRITICAL; |
| 142 | else if (strstr(status_line, "502")) | 148 | } else if (strstr(status_line, "502")) { |
| 143 | result = STATE_CRITICAL; | 149 | result = STATE_CRITICAL; |
| 144 | else if (strstr(status_line, "503")) | 150 | } else if (strstr(status_line, "503")) { |
| 145 | result = STATE_CRITICAL; | 151 | result = STATE_CRITICAL; |
| 152 | } | ||
| 146 | 153 | ||
| 147 | else | 154 | else { |
| 148 | result = STATE_UNKNOWN; | 155 | result = STATE_UNKNOWN; |
| 156 | } | ||
| 149 | } | 157 | } |
| 150 | 158 | ||
| 151 | /* Part II - Check stream exists and is ok */ | 159 | /* Part II - Check stream exists and is ok */ |
| @@ -176,10 +184,11 @@ int main(int argc, char **argv) { | |||
| 176 | } else { | 184 | } else { |
| 177 | /* make sure we find the response we are looking for */ | 185 | /* make sure we find the response we are looking for */ |
| 178 | if (!strstr(buffer, server_expect)) { | 186 | if (!strstr(buffer, server_expect)) { |
| 179 | if (server_port == PORT) | 187 | if (server_port == PORT) { |
| 180 | printf("%s\n", _("Invalid REAL response received from host")); | 188 | printf("%s\n", _("Invalid REAL response received from host")); |
| 181 | else | 189 | } else { |
| 182 | printf(_("Invalid REAL response received from host on port %d\n"), server_port); | 190 | printf(_("Invalid REAL response received from host on port %d\n"), server_port); |
| 191 | } | ||
| 183 | } else { | 192 | } else { |
| 184 | 193 | ||
| 185 | /* else we got the REAL string, so check the return code */ | 194 | /* else we got the REAL string, so check the return code */ |
| @@ -190,33 +199,37 @@ int main(int argc, char **argv) { | |||
| 190 | 199 | ||
| 191 | status_line = (char *)strtok(buffer, "\n"); | 200 | status_line = (char *)strtok(buffer, "\n"); |
| 192 | 201 | ||
| 193 | if (strstr(status_line, "200")) | 202 | if (strstr(status_line, "200")) { |
| 194 | result = STATE_OK; | 203 | result = STATE_OK; |
| 204 | } | ||
| 195 | 205 | ||
| 196 | /* client errors result in a warning state */ | 206 | /* client errors result in a warning state */ |
| 197 | else if (strstr(status_line, "400")) | 207 | else if (strstr(status_line, "400")) { |
| 198 | result = STATE_WARNING; | 208 | result = STATE_WARNING; |
| 199 | else if (strstr(status_line, "401")) | 209 | } else if (strstr(status_line, "401")) { |
| 200 | result = STATE_WARNING; | 210 | result = STATE_WARNING; |
| 201 | else if (strstr(status_line, "402")) | 211 | } else if (strstr(status_line, "402")) { |
| 202 | result = STATE_WARNING; | 212 | result = STATE_WARNING; |
| 203 | else if (strstr(status_line, "403")) | 213 | } else if (strstr(status_line, "403")) { |
| 204 | result = STATE_WARNING; | 214 | result = STATE_WARNING; |
| 205 | else if (strstr(status_line, "404")) | 215 | } else if (strstr(status_line, "404")) { |
| 206 | result = STATE_WARNING; | 216 | result = STATE_WARNING; |
| 217 | } | ||
| 207 | 218 | ||
| 208 | /* server errors result in a critical state */ | 219 | /* server errors result in a critical state */ |
| 209 | else if (strstr(status_line, "500")) | 220 | else if (strstr(status_line, "500")) { |
| 210 | result = STATE_CRITICAL; | 221 | result = STATE_CRITICAL; |
| 211 | else if (strstr(status_line, "501")) | 222 | } else if (strstr(status_line, "501")) { |
| 212 | result = STATE_CRITICAL; | 223 | result = STATE_CRITICAL; |
| 213 | else if (strstr(status_line, "502")) | 224 | } else if (strstr(status_line, "502")) { |
| 214 | result = STATE_CRITICAL; | 225 | result = STATE_CRITICAL; |
| 215 | else if (strstr(status_line, "503")) | 226 | } else if (strstr(status_line, "503")) { |
| 216 | result = STATE_CRITICAL; | 227 | result = STATE_CRITICAL; |
| 228 | } | ||
| 217 | 229 | ||
| 218 | else | 230 | else { |
| 219 | result = STATE_UNKNOWN; | 231 | result = STATE_UNKNOWN; |
| 232 | } | ||
| 220 | } | 233 | } |
| 221 | } | 234 | } |
| 222 | } | 235 | } |
| @@ -224,15 +237,17 @@ int main(int argc, char **argv) { | |||
| 224 | /* Return results */ | 237 | /* Return results */ |
| 225 | if (result == STATE_OK) { | 238 | if (result == STATE_OK) { |
| 226 | 239 | ||
| 227 | if (check_critical_time && (end_time - start_time) > critical_time) | 240 | if (check_critical_time && (end_time - start_time) > critical_time) { |
| 228 | result = STATE_CRITICAL; | 241 | result = STATE_CRITICAL; |
| 229 | else if (check_warning_time && (end_time - start_time) > warning_time) | 242 | } else if (check_warning_time && (end_time - start_time) > warning_time) { |
| 230 | result = STATE_WARNING; | 243 | result = STATE_WARNING; |
| 244 | } | ||
| 231 | 245 | ||
| 232 | /* Put some HTML in here to create a dynamic link */ | 246 | /* Put some HTML in here to create a dynamic link */ |
| 233 | printf(_("REAL %s - %d second response time\n"), state_text(result), (int)(end_time - start_time)); | 247 | printf(_("REAL %s - %d second response time\n"), state_text(result), (int)(end_time - start_time)); |
| 234 | } else | 248 | } else { |
| 235 | printf("%s\n", status_line); | 249 | printf("%s\n", status_line); |
| 250 | } | ||
| 236 | 251 | ||
| 237 | /* close the connection */ | 252 | /* close the connection */ |
| 238 | close(socket); | 253 | close(socket); |
| @@ -252,16 +267,18 @@ int process_arguments(int argc, char **argv) { | |||
| 252 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, | 267 | {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, |
| 253 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; | 268 | {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; |
| 254 | 269 | ||
| 255 | if (argc < 2) | 270 | if (argc < 2) { |
| 256 | return ERROR; | 271 | return ERROR; |
| 272 | } | ||
| 257 | 273 | ||
| 258 | for (int i = 1; i < argc; i++) { | 274 | for (int i = 1; i < argc; i++) { |
| 259 | if (strcmp("-to", argv[i]) == 0) | 275 | if (strcmp("-to", argv[i]) == 0) { |
| 260 | strcpy(argv[i], "-t"); | 276 | strcpy(argv[i], "-t"); |
| 261 | else if (strcmp("-wt", argv[i]) == 0) | 277 | } else if (strcmp("-wt", argv[i]) == 0) { |
| 262 | strcpy(argv[i], "-w"); | 278 | strcpy(argv[i], "-w"); |
| 263 | else if (strcmp("-ct", argv[i]) == 0) | 279 | } else if (strcmp("-ct", argv[i]) == 0) { |
| 264 | strcpy(argv[i], "-c"); | 280 | strcpy(argv[i], "-c"); |
| 281 | } | ||
| 265 | } | 282 | } |
| 266 | 283 | ||
| 267 | int option_char; | 284 | int option_char; |
| @@ -269,18 +286,20 @@ int process_arguments(int argc, char **argv) { | |||
| 269 | int option = 0; | 286 | int option = 0; |
| 270 | option_char = getopt_long(argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option); | 287 | option_char = getopt_long(argc, argv, "+hvVI:H:e:u:p:w:c:t:", longopts, &option); |
| 271 | 288 | ||
| 272 | if (option_char == -1 || option_char == EOF) | 289 | if (option_char == -1 || option_char == EOF) { |
| 273 | break; | 290 | break; |
| 291 | } | ||
| 274 | 292 | ||
| 275 | switch (option_char) { | 293 | switch (option_char) { |
| 276 | case 'I': /* hostname */ | 294 | case 'I': /* hostname */ |
| 277 | case 'H': /* hostname */ | 295 | case 'H': /* hostname */ |
| 278 | if (server_address) | 296 | if (server_address) { |
| 279 | break; | 297 | break; |
| 280 | else if (is_host(optarg)) | 298 | } else if (is_host(optarg)) { |
| 281 | server_address = optarg; | 299 | server_address = optarg; |
| 282 | else | 300 | } else { |
| 283 | usage2(_("Invalid hostname/address"), optarg); | 301 | usage2(_("Invalid hostname/address"), optarg); |
| 302 | } | ||
| 284 | break; | 303 | break; |
| 285 | case 'e': /* string to expect in response header */ | 304 | case 'e': /* string to expect in response header */ |
| 286 | server_expect = optarg; | 305 | server_expect = optarg; |
| @@ -341,14 +360,17 @@ int process_arguments(int argc, char **argv) { | |||
| 341 | } | 360 | } |
| 342 | } | 361 | } |
| 343 | 362 | ||
| 344 | if (server_address == NULL) | 363 | if (server_address == NULL) { |
| 345 | usage4(_("You must provide a server to check")); | 364 | usage4(_("You must provide a server to check")); |
| 365 | } | ||
| 346 | 366 | ||
| 347 | if (host_name == NULL) | 367 | if (host_name == NULL) { |
| 348 | host_name = strdup(server_address); | 368 | host_name = strdup(server_address); |
| 369 | } | ||
| 349 | 370 | ||
| 350 | if (server_expect == NULL) | 371 | if (server_expect == NULL) { |
| 351 | server_expect = strdup(EXPECT); | 372 | server_expect = strdup(EXPECT); |
| 373 | } | ||
| 352 | 374 | ||
| 353 | return OK; | 375 | return OK; |
| 354 | } | 376 | } |
