Code

changed Error: by CRITICAL -
[nagiosplug.git] / plugins / check_dns.c
1 /******************************************************************************
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; either version 2 of the License, or
6  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which will not 
18  be picked up by this plugin
19  
20  $Id$
22 ******************************************************************************/
24 #include "common.h"
25 #include "popen.h"
26 #include "utils.h"
27 #include "netutils.h"
29 const char *progname = "check_dns";
30 const char *revision = "$Revision$";
31 const char *copyright = "2000-2004";
32 const char *email = "nagiosplug-devel@lists.sourceforge.net";
34 int process_arguments (int, char **);
35 int validate_arguments (void);
36 int error_scan (char *);
37 void print_help (void);
38 void print_usage (void);
40 #define ADDRESS_LENGTH 256
41 char query_address[ADDRESS_LENGTH] = "";
42 char dns_server[ADDRESS_LENGTH] = "";
43 char ptr_server[ADDRESS_LENGTH] = "";
44 int verbose = FALSE;
45 char expected_address[ADDRESS_LENGTH] = "";
46 int match_expected_address = FALSE;
47 int expect_authority = FALSE;
49 int
50 main (int argc, char **argv)
51 {
52         char *command_line = NULL;
53         char input_buffer[MAX_INPUT_BUFFER];
54         char *output = NULL;
55         char *address = NULL;
56         char *temp_buffer = NULL;
57         int non_authoritative = FALSE;
58         int result = STATE_UNKNOWN;
59         double elapsed_time;
60         long microsec;
61         struct timeval tv;
62         int multi_address;
63         int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
65         setlocale (LC_ALL, "");
66         bindtextdomain (PACKAGE, LOCALEDIR);
67         textdomain (PACKAGE);
69         /* Set signal handling and alarm */
70         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
71                 printf (_("Cannot catch SIGALRM"));
72                 return STATE_UNKNOWN;
73         }
75         if (process_arguments (argc, argv) != OK) {
76                 usage (_("check_dns: could not parse arguments\n"));
77                 return STATE_UNKNOWN;
78         }
80         /* get the command to run */
81         asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
83         alarm (timeout_interval);
84         gettimeofday (&tv, NULL);
86         if (verbose)
87                 printf ("%s\n", command_line);
89         /* run the command */
90         child_process = spopen (command_line);
91         if (child_process == NULL) {
92                 printf (_("Could not open pipe: %s\n"), command_line);
93                 return STATE_UNKNOWN;
94         }
96         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
97         if (child_stderr == NULL)
98                 printf (_("Could not open stderr for %s\n"), command_line);
100         /* scan stdout */
101         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
103                 if (verbose)
104                         printf ("%s", input_buffer);
106                 if (strstr (input_buffer, ".in-addr.arpa")) {
107                         if ((temp_buffer = strstr (input_buffer, "name = ")))
108                                 address = strdup (temp_buffer + 7);
109                         else {
110                                 output = strdup (_("Warning plugin error"));
111                                 result = STATE_WARNING;
112                         }
113                 }
115                 /* the server is responding, we just got the host name... */
116                 if (strstr (input_buffer, "Name:"))
117                         parse_address = TRUE;
118                 else if (parse_address == TRUE && (strstr (input_buffer, "Address:") ||
119                          strstr (input_buffer, "Addresses:"))) {
120                         temp_buffer = index (input_buffer, ':');
121                         temp_buffer++;
123                         /* Strip leading spaces */
124                         for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
125                                 /* NOOP */;
126                         
127                         strip(temp_buffer);
128                         if (temp_buffer==NULL || strlen(temp_buffer)==0) {
129                                 die (STATE_CRITICAL,
130                                      _("DNS CRITICAL - '%s' returned empty host name string\n"),
131                                      NSLOOKUP_COMMAND);
132                         }
134                         if (address == NULL)
135                                 address = strdup (temp_buffer);
136                         else
137                                 asprintf(&address, "%s,%s", address, temp_buffer);
138                 }
140                 else if (strstr (input_buffer, "Non-authoritative answer:")) {
141                         non_authoritative = TRUE;
142                 }
144                 result = error_scan (input_buffer);
145                 if (result != STATE_OK) {
146                         output = strdup (1 + index (input_buffer, ':'));
147                         strip (output);
148                         break;
149                 }
151         }
153         /* scan stderr */
154         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
155                 if (error_scan (input_buffer) != STATE_OK) {
156                         result = max_state (result, error_scan (input_buffer));
157                         output = strdup (1 + index (input_buffer, ':'));
158                         strip (output);
159                 }
160         }
162         /* close stderr */
163         (void) fclose (child_stderr);
165         /* close stdout */
166         if (spclose (child_process)) {
167                 result = max_state (result, STATE_WARNING);
168                 if (!strcmp (output, ""))
169                         output = strdup (_("nslookup returned error status"));
170         }
172         /* If we got here, we should have an address string, 
173                  and we can segfault if we do not */
174         if (address==NULL || strlen(address)==0)
175                 die (STATE_CRITICAL,
176                      _("DNS CRITICAL - '%s' output parsing exited with no address\n"),
177                      NSLOOKUP_COMMAND);
179         /* compare to expected address */
180         if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
181                 result = STATE_CRITICAL;
182                 asprintf(&output, _("expected %s but got %s"), expected_address, address);
183         }
185         /* check if authoritative */
186         if (result == STATE_OK && expect_authority && non_authoritative) {
187                 result = STATE_CRITICAL;
188                 asprintf(&output, _("server %s is not authoritative for %s"), dns_server, query_address);
189         }
191         microsec = deltime (tv);
192         elapsed_time = (double)microsec / 1.0e6;
194         if (result == STATE_OK) {
195                 if (strchr (address, ',') == NULL)
196                         multi_address = FALSE;
197                 else
198                         multi_address = TRUE;
200                 printf ("DNS %s: ", _("OK"));
201                 printf (ngettext("%.3f second response time, ", "%.3f seconds response time, ", elapsed_time), elapsed_time);
202                 printf (_("%s returns %s"), query_address, address);
203                 printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
204         }
205         else if (result == STATE_WARNING)
206                 printf (_("DNS WARNING - %s\n"),
207                         !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
208         else if (result == STATE_CRITICAL)
209                 printf (_("DNS CRITICAL - %s\n"),
210                         !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
211         else
212                 printf (_("DNS UNKNOW - %s\n"),
213                         !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
215         return result;
220 int
221 error_scan (char *input_buffer)
224         /* the DNS lookup timed out */
225         if (strstr (input_buffer, "Note: nslookup is deprecated and may be removed from future releases.") ||
226             strstr (input_buffer, "Consider using the `dig' or `host' programs instead.  Run nslookup with") ||
227             strstr (input_buffer, "the `-sil[ent]' option to prevent this message from appearing."))
228                 return STATE_OK;
230         /* DNS server is not running... */
231         else if (strstr (input_buffer, "No response from server"))
232                 die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
234         /* Host name is valid, but server doesn't have records... */
235         else if (strstr (input_buffer, "No records"))
236                 die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
238         /* Connection was refused */
239         else if (strstr (input_buffer, "Connection refused") ||
240                  strstr (input_buffer, "Refused") ||
241                  (strstr (input_buffer, "** server can't find") &&
242                   strstr (input_buffer, ": REFUSED")))
243                 die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
245         /* Host or domain name does not exist */
246         else if (strstr (input_buffer, "Non-existent") ||
247                  strstr (input_buffer, "** server can't find") ||
248                  strstr (input_buffer,"NXDOMAIN"))
249                 die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
251         /* Network is unreachable */
252         else if (strstr (input_buffer, "Network is unreachable"))
253                 die (STATE_CRITICAL, _("Network is unreachable\n"));
255         /* Internal server failure */
256         else if (strstr (input_buffer, "Server failure"))
257                 die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
259         /* Request error or the DNS lookup timed out */
260         else if (strstr (input_buffer, "Format error") ||
261                  strstr (input_buffer, "Timed out"))
262                 return STATE_WARNING;
264         return STATE_OK;
270 /* process command-line arguments */
271 int
272 process_arguments (int argc, char **argv)
274         int c;
276         int opt_index = 0;
277         static struct option long_opts[] = {
278                 {"help", no_argument, 0, 'h'},
279                 {"version", no_argument, 0, 'V'},
280                 {"verbose", no_argument, 0, 'v'},
281                 {"timeout", required_argument, 0, 't'},
282                 {"hostname", required_argument, 0, 'H'},
283                 {"server", required_argument, 0, 's'},
284                 {"reverse-server", required_argument, 0, 'r'},
285                 {"expected-address", required_argument, 0, 'a'},
286                 {"expect-authority", no_argument, 0, 'A'},
287                 {0, 0, 0, 0}
288         };
290         if (argc < 2)
291                 return ERROR;
293         for (c = 1; c < argc; c++)
294                 if (strcmp ("-to", argv[c]) == 0)
295                         strcpy (argv[c], "-t");
297         while (1) {
298                 c = getopt_long (argc, argv, "hVvAt:H:s:r:a:", long_opts, &opt_index);
300                 if (c == -1 || c == EOF)
301                         break;
303                 switch (c) {
304                 case '?': /* args not parsable */
305                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
306                         print_usage ();
307                         exit (STATE_UNKNOWN);
308                 case 'h': /* help */
309                         print_help ();
310                         exit (STATE_OK);
311                 case 'V': /* version */
312                         print_revision (progname, revision);
313                         exit (STATE_OK);
314                 case 'v': /* version */
315                         verbose = TRUE;
316                         break;
317                 case 't': /* timeout period */
318                         timeout_interval = atoi (optarg);
319                         break;
320                 case 'H': /* hostname */
321                         if (strlen (optarg) >= ADDRESS_LENGTH)
322                                 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
323                         strcpy (query_address, optarg);
324                         break;
325                 case 's': /* server name */
326                         /* TODO: this is_host check is probably unnecessary. */
327                         /* Better to confirm nslookup response matches */
328                         if (is_host (optarg) == FALSE) {
329                                 printf (_("Invalid hostname/address\n\n"));
330                                 print_usage ();
331                                 exit (STATE_UNKNOWN);
332                         }
333                         if (strlen (optarg) >= ADDRESS_LENGTH)
334                                 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
335                         strcpy (dns_server, optarg);
336                         break;
337                 case 'r': /* reverse server name */
338                         /* TODO: Is this is_host necessary? */
339                         if (is_host (optarg) == FALSE) {
340                                 usage2 (_("Invalid hostname/address"), optarg);
341                         }
342                         if (strlen (optarg) >= ADDRESS_LENGTH)
343                                 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
344                         strcpy (ptr_server, optarg);
345                         break;
346                 case 'a': /* expected address */
347                         if (strlen (optarg) >= ADDRESS_LENGTH)
348                                 die (STATE_UNKNOWN, _("Input buffer overflow\n"));
349                         strcpy (expected_address, optarg);
350                         match_expected_address = TRUE;
351                         break;
352                 case 'A': /* expect authority */
353                         expect_authority = TRUE;
354                         break;
355                 }
356         }
358         c = optind;
359         if (strlen(query_address)==0 && c<argc) {
360                 if (strlen(argv[c])>=ADDRESS_LENGTH)
361                         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
362                 strcpy (query_address, argv[c++]);
363         }
365         if (strlen(dns_server)==0 && c<argc) {
366                 /* TODO: See -s option */
367                 if (is_host(argv[c]) == FALSE) {
368                         printf (_("Invalid hostname/address: %s\n\n"), argv[c]);
369                         return ERROR;
370                 }
371                 if (strlen(argv[c]) >= ADDRESS_LENGTH)
372                         die (STATE_UNKNOWN, _("Input buffer overflow\n"));
373                 strcpy (dns_server, argv[c++]);
374         }
376         return validate_arguments ();
381 int
382 validate_arguments ()
384         if (query_address[0] == 0)
385                 return ERROR;
386         else
387                 return OK;
392 void
393 print_help (void)
395         print_revision (progname, revision);
397         printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
398         printf (COPYRIGHT, copyright, email);
400         print_usage ();
402         printf (_(UT_HELP_VRSN));
404         printf (_("\
405 -H, --hostname=HOST\n\
406    The name or address you want to query\n\
407 -s, --server=HOST\n\
408    Optional DNS server you want to use for the lookup\n\
409 -a, --expected-address=IP-ADDRESS\n\
410    Optional IP address you expect the DNS server to return\n\
411 -A, --expect-authority\n\
412    Optionally expect the DNS server to be authoritative for the lookup\n"));
414         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
416         printf (_("\n\
417 This plugin uses the nslookup program to obtain the IP address\n\
418 for the given host/domain query.  A optional DNS server to use may\n\
419 be specified.  If no DNS server is specified, the default server(s)\n\
420 specified in /etc/resolv.conf will be used.\n"));
422         printf (_(UT_SUPPORT));
427 void
428 print_usage (void)
430         printf (_("\
431 Usage: %s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n\
432        %s --help\n\
433        %s --version\n"), progname, progname, progname);