Code

ba41ebda361eab74d6836b1ff332ed0a2e637bf6
[nagiosplug.git] / plugins / check_dig.c
1 /******************************************************************************
2  *
3  * Program: SNMP plugin for Nagios
4  * License: GPL
5  *
6  * License Information:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *****************************************************************************/
23 #include "config.h"
24 #include "common.h"
25 #include "utils.h"
26 #include "popen.h"
28 #define PROGNAME "check_dig"
29 #define REVISION "$Revision$"
30 #define COPYRIGHT "2000-2002"
31 #define AUTHOR "Karl DeBisschop"
32 #define EMAIL "karl@debisschop.net"
33 #define SUMMARY "Test the DNS service on the specified host using dig\n"
35 int process_arguments (int, char **);
36 int validate_arguments (void);
37 void print_help (void);
38 void print_usage (void);
40 char *query_address = NULL;
41 char *dns_server = NULL;
42 int verbose = FALSE;
44 int
45 main (int argc, char **argv)
46 {
47         char input_buffer[MAX_INPUT_BUFFER];
48         char *command_line = NULL;
49         char *output = NULL;
50         int result = STATE_UNKNOWN;
52         /* Set signal handling and alarm */
53         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
54                 usage ("Cannot catch SIGALRM\n");
56         if (process_arguments (argc, argv) != OK)
57                 usage ("Could not parse arguments\n");
59         /* get the command to run */
60         asprintf (&command_line, "%s @%s %s", PATH_TO_DIG, dns_server, query_address);
62         alarm (timeout_interval);
63         time (&start_time);
65         if (verbose)
66                 printf ("%s\n", command_line);
67         /* run the command */
68         child_process = spopen (command_line);
69         if (child_process == NULL) {
70                 printf ("Could not open pipe: %s\n", command_line);
71                 return STATE_UNKNOWN;
72         }
74         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
75         if (child_stderr == NULL)
76                 printf ("Could not open stderr for %s\n", command_line);
78         output = strscpy (output, "");
80         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
82                 /* the server is responding, we just got the host name... */
83                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
85                         /* get the host address */
86                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
87                                 break;
89                         if (strpbrk (input_buffer, "\r\n"))
90                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
92                         if (strstr (input_buffer, query_address) == input_buffer) {
93                                 output = strscpy (output, input_buffer);
94                                 result = STATE_OK;
95                         }
96                         else {
97                                 strcpy (output, "Server not found in ANSWER SECTION");
98                                 result = STATE_WARNING;
99                         }
101                         continue;
102                 }
104         }
106         if (result != STATE_OK) {
107                 strcpy (output, "No ANSWER SECTION found");
108         }
110         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
111                 /* If we get anything on STDERR, at least set warning */
112                 result = max_state (result, STATE_WARNING);
113                 printf ("%s", input_buffer);
114                 if (!strcmp (output, ""))
115                         strcpy (output, 1 + index (input_buffer, ':'));
116         }
118         (void) fclose (child_stderr);
120         /* close the pipe */
121         if (spclose (child_process)) {
122                 result = max_state (result, STATE_WARNING);
123                 if (!strcmp (output, ""))
124                         strcpy (output, "nslookup returned error status");
125         }
127         (void) time (&end_time);
129         if (result == STATE_OK)
130                 printf ("DNS ok - %d seconds response time (%s)\n",
131                                                 (int) (end_time - start_time), output);
132         else if (result == STATE_WARNING)
133                 printf ("DNS WARNING - %s\n",
134                                                 !strcmp (output,
135                                                                                  "") ? " Probably a non-existent host/domain" : output);
136         else if (result == STATE_CRITICAL)
137                 printf ("DNS CRITICAL - %s\n",
138                                                 !strcmp (output,
139                                                                                  "") ? " Probably a non-existent host/domain" : output);
140         else
141                 printf ("DNS problem - %s\n",
142                                                 !strcmp (output,
143                                                                                  "") ? " Probably a non-existent host/domain" : output);
145         return result;
148 /* process command-line arguments */
149 int
150 process_arguments (int argc, char **argv)
152         int c;
154 #ifdef HAVE_GETOPT_H
155         int option_index = 0;
156         static struct option long_options[] = {
157                 {"hostname", required_argument, 0, 'H'},
158                 {"query_address", required_argument, 0, 'e'},
159                 {"verbose", no_argument, 0, 'v'},
160                 {"version", no_argument, 0, 'V'},
161                 {"help", no_argument, 0, 'h'},
162                 {0, 0, 0, 0}
163         };
164 #endif
166         if (argc < 2)
167                 return ERROR;
169         while (1) {
170 #ifdef HAVE_GETOPT_H
171                 c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
172 #else
173                 c = getopt (argc, argv, "hVvt:l:H:");
174 #endif
176                 if (c == -1 || c == EOF)
177                         break;
179                 switch (c) {
180                 case '?':                                                                       /* help */
181                         usage3 ("Unknown argument", optopt);
182                 case 'H':                                                                       /* hostname */
183                         if (is_host (optarg)) {
184                                 dns_server = optarg;
185                         }
186                         else {
187                                 usage ("Invalid host name\n");
188                         }
189                         break;
190                 case 'l':                                                                       /* username */
191                         query_address = optarg;
192                         break;
193                 case 'v':                                                                       /* verbose */
194                         verbose = TRUE;
195                         break;
196                 case 't':                                                                       /* timeout */
197                         if (is_intnonneg (optarg)) {
198                                 timeout_interval = atoi (optarg);
199                         }
200                         else {
201                                 usage ("Time interval must be a nonnegative integer\n");
202                         }
203                         break;
204                 case 'V':                                                                       /* version */
205                         print_revision (PROGNAME, "$Revision$");
206                         exit (STATE_OK);
207                 case 'h':                                                                       /* help */
208                         print_help ();
209                         exit (STATE_OK);
210                 }
211         }
213         c = optind;
214         if (dns_server == NULL) {
215                 if (c < argc) {
216                         if (is_host (argv[c])) {
217                                 dns_server = argv[c];
218                         }
219                         else {
220                                 usage ("Invalid host name");
221                         }
222                 }
223                 else {
224                         dns_server = strscpy (NULL, "127.0.0.1");
225                 }
226         }
228         return validate_arguments ();
235 int
236 validate_arguments (void)
238         return OK;
245 void
246 print_help (void)
248         print_revision (PROGNAME, "$Revision$");
249         printf
250                 ("Copyright (c) %s %s <%s>\n\n%s\n",
251                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
252         print_usage ();
253         printf
254                 ("\nOptions:\n"
255                  " -H, --hostname=STRING or IPADDRESS\n"
256                  "   Check server on the indicated host\n"
257                  " -l, --lookup=STRING\n"
258                  "   machine name to lookup\n"
259                  " -t, --timeout=INTEGER\n"
260                  "   Seconds before connection attempt times out (default: %d)\n"
261                  " -v, --verbose\n"
262                  "   Print extra information (command-line use only)\n"
263                  " -h, --help\n"
264                  "   Print detailed help screen\n"
265                  " -V, --version\n"
266                  "   Print version information\n\n", DEFAULT_SOCKET_TIMEOUT);
267         support ();
274 void
275 print_usage (void)
277         printf
278                 ("Usage: %s -H host -l lookup [-t timeout] [-v]\n"
279                  "       %s --help\n"
280                  "       %s --version\n", PROGNAME, PROGNAME, PROGNAME);