diff options
Diffstat (limited to 'plugins/check_dns.c')
| -rw-r--r-- | plugins/check_dns.c | 415 |
1 files changed, 415 insertions, 0 deletions
diff --git a/plugins/check_dns.c b/plugins/check_dns.c new file mode 100644 index 00000000..eaff4372 --- /dev/null +++ b/plugins/check_dns.c | |||
| @@ -0,0 +1,415 @@ | |||
| 1 | /****************************************************************************** | ||
| 2 | * | ||
| 3 | * CHECK_DNS.C | ||
| 4 | * | ||
| 5 | * Program: DNS plugin for Nagios | ||
| 6 | * License: GPL | ||
| 7 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) | ||
| 8 | * | ||
| 9 | * Last Modified: $Date$ | ||
| 10 | * | ||
| 11 | * Notes: | ||
| 12 | * - Safe popen added by Karl DeBisschop 9-11-99 | ||
| 13 | * | ||
| 14 | * Command line: CHECK_DNS <query_address> [dns_server] | ||
| 15 | * | ||
| 16 | * Description: | ||
| 17 | * | ||
| 18 | * This program will use the nslookup program to obtain the IP address | ||
| 19 | * for a given host name. A optional DNS server may be specified. If | ||
| 20 | * no DNS server is specified, the default server(s) for the system | ||
| 21 | * are used. | ||
| 22 | * | ||
| 23 | * Return Values: | ||
| 24 | * OK The DNS query was successful (host IP address was returned). | ||
| 25 | * WARNING The DNS server responded, but could not fulfill the request. | ||
| 26 | * CRITICAL The DNS server is not responding or encountered an error. | ||
| 27 | * | ||
| 28 | * License Information: | ||
| 29 | * | ||
| 30 | * This program is free software; you can redistribute it and/or modify | ||
| 31 | * it under the terms of the GNU General Public License as published by | ||
| 32 | * the Free Software Foundation; either version 2 of the License, or | ||
| 33 | * (at your option) any later version. | ||
| 34 | * | ||
| 35 | * This program is distributed in the hope that it will be useful, | ||
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 38 | * GNU General Public License for more details. | ||
| 39 | * | ||
| 40 | * You should have received a copy of the GNU General Public License | ||
| 41 | * along with this program; if not, write to the Free Software | ||
| 42 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 43 | * | ||
| 44 | *****************************************************************************/ | ||
| 45 | |||
| 46 | #include "common.h" | ||
| 47 | #include "popen.h" | ||
| 48 | #include "utils.h" | ||
| 49 | |||
| 50 | int process_arguments (int, char **); | ||
| 51 | int call_getopt (int, char **); | ||
| 52 | int validate_arguments (void); | ||
| 53 | void print_usage (char *); | ||
| 54 | void print_help (char *); | ||
| 55 | int error_scan (char *); | ||
| 56 | |||
| 57 | #define ADDRESS_LENGTH 256 | ||
| 58 | char query_address[ADDRESS_LENGTH] = ""; | ||
| 59 | char dns_server[ADDRESS_LENGTH] = ""; | ||
| 60 | char ptr_server[ADDRESS_LENGTH] = ""; | ||
| 61 | int verbose = FALSE; | ||
| 62 | |||
| 63 | int | ||
| 64 | main (int argc, char **argv) | ||
| 65 | { | ||
| 66 | char *command_line = NULL; | ||
| 67 | char input_buffer[MAX_INPUT_BUFFER]; | ||
| 68 | char *output = NULL; | ||
| 69 | char *address = NULL; | ||
| 70 | char *temp_buffer = NULL; | ||
| 71 | int result = STATE_UNKNOWN; | ||
| 72 | |||
| 73 | /* Set signal handling and alarm */ | ||
| 74 | if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) { | ||
| 75 | printf ("Cannot catch SIGALRM"); | ||
| 76 | return STATE_UNKNOWN; | ||
| 77 | } | ||
| 78 | |||
| 79 | if (process_arguments (argc, argv) != OK) { | ||
| 80 | print_usage (my_basename (argv[0])); | ||
| 81 | return STATE_UNKNOWN; | ||
| 82 | } | ||
| 83 | |||
| 84 | /* get the command to run */ | ||
| 85 | command_line = ssprintf (command_line, "%s %s %s", NSLOOKUP_COMMAND, | ||
| 86 | query_address, dns_server); | ||
| 87 | |||
| 88 | alarm (timeout_interval); | ||
| 89 | time (&start_time); | ||
| 90 | |||
| 91 | if (verbose) | ||
| 92 | printf ("%s\n", command_line); | ||
| 93 | /* run the command */ | ||
| 94 | child_process = spopen (command_line); | ||
| 95 | if (child_process == NULL) { | ||
| 96 | printf ("Could not open pipe: %s\n", command_line); | ||
| 97 | return STATE_UNKNOWN; | ||
| 98 | } | ||
| 99 | |||
| 100 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | ||
| 101 | if (child_stderr == NULL) | ||
| 102 | printf ("Could not open stderr for %s\n", command_line); | ||
| 103 | |||
| 104 | /* scan stdout */ | ||
| 105 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | ||
| 106 | |||
| 107 | if (verbose) | ||
| 108 | printf ("%s\n", input_buffer); | ||
| 109 | |||
| 110 | if (strstr (input_buffer, ".in-addr.arpa")) { | ||
| 111 | if ((temp_buffer = strstr (input_buffer, "name = "))) | ||
| 112 | address = strscpy (address, temp_buffer + 7); | ||
| 113 | else { | ||
| 114 | output = strscpy (output, "Unknown error (plugin)"); | ||
| 115 | result = STATE_WARNING; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | /* the server is responding, we just got the host name... */ | ||
| 120 | if (strstr (input_buffer, "Name:")) { | ||
| 121 | |||
| 122 | /* get the host address */ | ||
| 123 | if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) | ||
| 124 | break; | ||
| 125 | |||
| 126 | if (verbose) | ||
| 127 | printf ("%s\n", input_buffer); | ||
| 128 | |||
| 129 | if ((temp_buffer = index (input_buffer, ':'))) { | ||
| 130 | address = strscpy (address, temp_buffer + 2); | ||
| 131 | strip (address); | ||
| 132 | result = STATE_OK; | ||
| 133 | } | ||
| 134 | else { | ||
| 135 | output = strscpy (output, "Unknown error (plugin)"); | ||
| 136 | result = STATE_WARNING; | ||
| 137 | } | ||
| 138 | |||
| 139 | break; | ||
| 140 | } | ||
| 141 | |||
| 142 | result = error_scan (input_buffer); | ||
| 143 | if (result != STATE_OK) { | ||
| 144 | output = strscpy (output, 1 + index (input_buffer, ':')); | ||
| 145 | break; | ||
| 146 | } | ||
| 147 | |||
| 148 | } | ||
| 149 | |||
| 150 | /* scan stderr */ | ||
| 151 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { | ||
| 152 | if (error_scan (input_buffer) != STATE_OK) { | ||
| 153 | result = max (result, error_scan (input_buffer)); | ||
| 154 | output = strscpy (output, 1 + index (input_buffer, ':')); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | /* close stderr */ | ||
| 159 | (void) fclose (child_stderr); | ||
| 160 | |||
| 161 | /* close stdout */ | ||
| 162 | if (spclose (child_process)) { | ||
| 163 | result = max (result, STATE_WARNING); | ||
| 164 | if (!strcmp (output, "")) | ||
| 165 | output = strscpy (output, "nslookup returned error status"); | ||
| 166 | } | ||
| 167 | |||
| 168 | (void) time (&end_time); | ||
| 169 | |||
| 170 | if (result == STATE_OK) | ||
| 171 | printf ("DNS ok - %d seconds response time, Address(es) is/are %s\n", | ||
| 172 | (int) (end_time - start_time), address); | ||
| 173 | else if (result == STATE_WARNING) | ||
| 174 | printf ("DNS WARNING - %s\n", | ||
| 175 | !strcmp (output, "") ? " Probably a non-existent host/domain" : output); | ||
| 176 | else if (result == STATE_CRITICAL) | ||
| 177 | printf ("DNS CRITICAL - %s\n", | ||
| 178 | !strcmp (output, "") ? " Probably a non-existent host/domain" : output); | ||
| 179 | else | ||
| 180 | printf ("DNS problem - %s\n", | ||
| 181 | !strcmp (output, "") ? " Probably a non-existent host/domain" : output); | ||
| 182 | |||
| 183 | return result; | ||
| 184 | } | ||
| 185 | |||
| 186 | int | ||
| 187 | error_scan (char *input_buffer) | ||
| 188 | { | ||
| 189 | |||
| 190 | /* the DNS lookup timed out */ | ||
| 191 | if (strstr (input_buffer, | ||
| 192 | "Note: nslookup is deprecated and may be removed from future releases.") | ||
| 193 | || strstr (input_buffer, | ||
| 194 | "Consider using the `dig' or `host' programs instead. Run nslookup with") | ||
| 195 | || strstr (input_buffer, | ||
| 196 | "the `-sil[ent]' option to prevent this message from appearing.")) | ||
| 197 | return STATE_OK; | ||
| 198 | |||
| 199 | /* the DNS lookup timed out */ | ||
| 200 | else if (strstr (input_buffer, "Timed out")) | ||
| 201 | return STATE_WARNING; | ||
| 202 | |||
| 203 | /* DNS server is not running... */ | ||
| 204 | else if (strstr (input_buffer, "No response from server")) | ||
| 205 | return STATE_CRITICAL; | ||
| 206 | |||
| 207 | /* Host name is valid, but server doesn't have records... */ | ||
| 208 | else if (strstr (input_buffer, "No records")) | ||
| 209 | return STATE_WARNING; | ||
| 210 | |||
| 211 | /* Host or domain name does not exist */ | ||
| 212 | else if (strstr (input_buffer, "Non-existent")) | ||
| 213 | return STATE_CRITICAL; | ||
| 214 | else if (strstr (input_buffer, "** server can't find")) | ||
| 215 | return STATE_CRITICAL; | ||
| 216 | else if(strstr(input_buffer,"NXDOMAIN")) /* 9.x */ | ||
| 217 | return STATE_CRITICAL; | ||
| 218 | |||
| 219 | /* Connection was refused */ | ||
| 220 | else if (strstr (input_buffer, "Connection refused")) | ||
| 221 | return STATE_CRITICAL; | ||
| 222 | |||
| 223 | /* Network is unreachable */ | ||
| 224 | else if (strstr (input_buffer, "Network is unreachable")) | ||
| 225 | return STATE_CRITICAL; | ||
| 226 | |||
| 227 | /* Internal server failure */ | ||
| 228 | else if (strstr (input_buffer, "Server failure")) | ||
| 229 | return STATE_CRITICAL; | ||
| 230 | |||
| 231 | /* DNS server refused to service request */ | ||
| 232 | else if (strstr (input_buffer, "Refused")) | ||
| 233 | return STATE_CRITICAL; | ||
| 234 | |||
| 235 | /* Request error */ | ||
| 236 | else if (strstr (input_buffer, "Format error")) | ||
| 237 | return STATE_WARNING; | ||
| 238 | |||
| 239 | else | ||
| 240 | return STATE_OK; | ||
| 241 | |||
| 242 | } | ||
| 243 | |||
| 244 | /* process command-line arguments */ | ||
| 245 | int | ||
| 246 | process_arguments (int argc, char **argv) | ||
| 247 | { | ||
| 248 | int c; | ||
| 249 | |||
| 250 | if (argc < 2) | ||
| 251 | return ERROR; | ||
| 252 | |||
| 253 | for (c = 1; c < argc; c++) | ||
| 254 | if (strcmp ("-to", argv[c]) == 0) | ||
| 255 | strcpy (argv[c], "-t"); | ||
| 256 | |||
| 257 | c = 0; | ||
| 258 | while (c += (call_getopt (argc - c, &argv[c]))) { | ||
| 259 | if (argc <= c) | ||
| 260 | break; | ||
| 261 | if (query_address[0] == 0) { | ||
| 262 | if (is_host (argv[c]) == FALSE) { | ||
| 263 | printf ("Invalid name/address: %s\n\n", argv[c]); | ||
| 264 | return ERROR; | ||
| 265 | } | ||
| 266 | if (strlen (argv[c]) >= ADDRESS_LENGTH) | ||
| 267 | terminate (STATE_UNKNOWN, "Input buffer overflow\n"); | ||
| 268 | strcpy (query_address, argv[c]); | ||
| 269 | } | ||
| 270 | else if (dns_server[0] == 0) { | ||
| 271 | if (is_host (argv[c]) == FALSE) { | ||
| 272 | printf ("Invalid name/address: %s\n\n", argv[c]); | ||
| 273 | return ERROR; | ||
| 274 | } | ||
| 275 | if (strlen (argv[c]) >= ADDRESS_LENGTH) | ||
| 276 | terminate (STATE_UNKNOWN, "Input buffer overflow\n"); | ||
| 277 | strcpy (dns_server, argv[c]); | ||
| 278 | } | ||
| 279 | } | ||
| 280 | |||
| 281 | return validate_arguments (); | ||
| 282 | |||
| 283 | } | ||
| 284 | |||
| 285 | int | ||
| 286 | call_getopt (int argc, char **argv) | ||
| 287 | { | ||
| 288 | int c, i = 1; | ||
| 289 | |||
| 290 | #ifdef HAVE_GETOPT_H | ||
| 291 | int opt_index = 0; | ||
| 292 | static struct option long_opts[] = { | ||
| 293 | {"help", no_argument, 0, 'h'}, | ||
| 294 | {"version", no_argument, 0, 'V'}, | ||
| 295 | {"verbose", no_argument, 0, 'v'}, | ||
| 296 | {"timeout", required_argument, 0, 't'}, | ||
| 297 | {"hostname", required_argument, 0, 'H'}, | ||
| 298 | {"server", required_argument, 0, 's'}, | ||
| 299 | {"reverse-server", required_argument, 0, 'r'}, | ||
| 300 | {0, 0, 0, 0} | ||
| 301 | }; | ||
| 302 | #endif | ||
| 303 | |||
| 304 | |||
| 305 | while (1) { | ||
| 306 | #ifdef HAVE_GETOPT_H | ||
| 307 | c = getopt_long (argc, argv, "+?hVvt:H:s:r:", long_opts, &opt_index); | ||
| 308 | #else | ||
| 309 | c = getopt (argc, argv, "+?hVvt:H:s:r:"); | ||
| 310 | #endif | ||
| 311 | |||
| 312 | if (c == -1 || c == EOF) | ||
| 313 | break; | ||
| 314 | |||
| 315 | i++; | ||
| 316 | switch (c) { | ||
| 317 | case 't': | ||
| 318 | case 'H': | ||
| 319 | case 's': | ||
| 320 | case 'r': | ||
| 321 | i++; | ||
| 322 | } | ||
| 323 | |||
| 324 | switch (c) { | ||
| 325 | case '?': /* args not parsable */ | ||
| 326 | printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); | ||
| 327 | print_usage (my_basename (argv[0])); | ||
| 328 | exit (STATE_UNKNOWN); | ||
| 329 | case 'h': /* help */ | ||
| 330 | print_help (my_basename (argv[0])); | ||
| 331 | exit (STATE_OK); | ||
| 332 | case 'V': /* version */ | ||
| 333 | print_revision (my_basename (argv[0]), "$Revision$"); | ||
| 334 | exit (STATE_OK); | ||
| 335 | case 'v': /* version */ | ||
| 336 | verbose = TRUE; | ||
| 337 | break; | ||
| 338 | case 't': /* timeout period */ | ||
| 339 | timeout_interval = atoi (optarg); | ||
| 340 | break; | ||
| 341 | case 'H': /* hostname */ | ||
| 342 | if (is_host (optarg) == FALSE) { | ||
| 343 | printf ("Invalid host name/address\n\n"); | ||
| 344 | print_usage (my_basename (argv[0])); | ||
| 345 | exit (STATE_UNKNOWN); | ||
| 346 | } | ||
| 347 | if (strlen (optarg) >= ADDRESS_LENGTH) | ||
| 348 | terminate (STATE_UNKNOWN, "Input buffer overflow\n"); | ||
| 349 | strcpy (query_address, optarg); | ||
| 350 | break; | ||
| 351 | case 's': /* server name */ | ||
| 352 | if (is_host (optarg) == FALSE) { | ||
| 353 | printf ("Invalid server name/address\n\n"); | ||
| 354 | print_usage (my_basename (argv[0])); | ||
| 355 | exit (STATE_UNKNOWN); | ||
| 356 | } | ||
| 357 | if (strlen (optarg) >= ADDRESS_LENGTH) | ||
| 358 | terminate (STATE_UNKNOWN, "Input buffer overflow\n"); | ||
| 359 | strcpy (dns_server, optarg); | ||
| 360 | break; | ||
| 361 | case 'r': /* reverse server name */ | ||
| 362 | if (is_host (optarg) == FALSE) { | ||
| 363 | printf ("Invalid host name/address\n\n"); | ||
| 364 | print_usage (my_basename (argv[0])); | ||
| 365 | exit (STATE_UNKNOWN); | ||
| 366 | } | ||
| 367 | if (strlen (optarg) >= ADDRESS_LENGTH) | ||
| 368 | terminate (STATE_UNKNOWN, "Input buffer overflow\n"); | ||
| 369 | strcpy (ptr_server, optarg); | ||
| 370 | break; | ||
| 371 | } | ||
| 372 | } | ||
| 373 | return i; | ||
| 374 | } | ||
| 375 | |||
| 376 | int | ||
| 377 | validate_arguments () | ||
| 378 | { | ||
| 379 | if (query_address[0] == 0) | ||
| 380 | return ERROR; | ||
| 381 | else | ||
| 382 | return OK; | ||
| 383 | } | ||
| 384 | |||
| 385 | void | ||
| 386 | print_usage (char *cmd) | ||
| 387 | { | ||
| 388 | printf ("Usage: %s -H host [-s server] [-t timeout]\n" " %s --help\n" | ||
| 389 | " %s --version\n", cmd, cmd, cmd); | ||
| 390 | } | ||
| 391 | |||
| 392 | void | ||
| 393 | print_help (char *cmd) | ||
| 394 | { | ||
| 395 | print_revision (cmd, "$Revision$"); | ||
| 396 | printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"); | ||
| 397 | print_usage (cmd); | ||
| 398 | printf ("\n"); | ||
| 399 | printf | ||
| 400 | ("-H, --hostname=HOST\n" | ||
| 401 | " The name or address you want to query\n" | ||
| 402 | "-s, --server=HOST\n" | ||
| 403 | " Optional DNS server you want to use for the lookup\n" | ||
| 404 | "-t, --timeout=INTEGER\n" | ||
| 405 | " Seconds before connection times out (default: %d)\n" | ||
| 406 | "-h, --help\n" | ||
| 407 | " Print detailed help\n" | ||
| 408 | "-V, --version\n" | ||
| 409 | " Print version numbers and license information\n" | ||
| 410 | "\n" | ||
| 411 | "This plugin uses the nslookup program to obtain the IP address\n" | ||
| 412 | "for the given host/domain query. A optional DNS server to use may\n" | ||
| 413 | "be specified. If no DNS server is specified, the default server(s)\n" | ||
| 414 | "specified in /etc/resolv.conf will be used.\n", DEFAULT_SOCKET_TIMEOUT); | ||
| 415 | } | ||
