Code

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