Code

semicolon needed where praogname define was replced
[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 const char *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 = "";
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         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
80                 /* the server is responding, we just got the host name... */
81                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
83                         /* get the host address */
84                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
85                                 break;
87                         if (strpbrk (input_buffer, "\r\n"))
88                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
90                         if (strstr (input_buffer, query_address) == input_buffer) {
91                                 asprintf (&output, input_buffer);
92                                 result = STATE_OK;
93                         }
94                         else {
95                                 asprintf (&output, "Server not found in ANSWER SECTION");
96                                 result = STATE_WARNING;
97                         }
99                         continue;
100                 }
102         }
104         if (result != STATE_OK) {
105                 asprintf (&output, "No ANSWER SECTION found");
106         }
108         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
109                 /* If we get anything on STDERR, at least set warning */
110                 result = max_state (result, STATE_WARNING);
111                 printf ("%s", input_buffer);
112                 if (strlen (output) == 0)
113                         asprintf (&output, 1 + index (input_buffer, ':'));
114         }
116         (void) fclose (child_stderr);
118         /* close the pipe */
119         if (spclose (child_process)) {
120                 result = max_state (result, STATE_WARNING);
121                 if (strlen (output) == 0)
122                         asprintf (&output, "dig returned error status");
123         }
125         (void) time (&end_time);
127         if (output == NULL || strlen (output) == 0)
128                 asprintf (&output, " Probably a non-existent host/domain");
130         if (result == STATE_OK)
131                 printf ("DNS ok - %d seconds response time (%s)\n",
132                                                 (int) (end_time - start_time), output);
133         else if (result == STATE_WARNING)
134                 printf ("DNS WARNING - %s\n", output);
135         else if (result == STATE_CRITICAL)
136                 printf ("DNS CRITICAL - %s\n", output);
137         else
138                 printf ("DNS problem - %s\n", output);
140         return result;
143 /* process command-line arguments */
144 int
145 process_arguments (int argc, char **argv)
147         int c;
149 #ifdef HAVE_GETOPT_H
150         int option_index = 0;
151         static struct option long_options[] = {
152                 {"hostname", required_argument, 0, 'H'},
153                 {"query_address", required_argument, 0, 'e'},
154                 {"verbose", no_argument, 0, 'v'},
155                 {"version", no_argument, 0, 'V'},
156                 {"help", no_argument, 0, 'h'},
157                 {0, 0, 0, 0}
158         };
159 #endif
161         if (argc < 2)
162                 return ERROR;
164         while (1) {
165 #ifdef HAVE_GETOPT_H
166                 c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
167 #else
168                 c = getopt (argc, argv, "hVvt:l:H:");
169 #endif
171                 if (c == -1 || c == EOF)
172                         break;
174                 switch (c) {
175                 case '?':                                                                       /* help */
176                         usage3 ("Unknown argument", optopt);
177                 case 'H':                                                                       /* hostname */
178                         if (is_host (optarg)) {
179                                 dns_server = optarg;
180                         }
181                         else {
182                                 usage ("Invalid host name\n");
183                         }
184                         break;
185                 case 'l':                                                                       /* username */
186                         query_address = optarg;
187                         break;
188                 case 'v':                                                                       /* verbose */
189                         verbose = TRUE;
190                         break;
191                 case 't':                                                                       /* timeout */
192                         if (is_intnonneg (optarg)) {
193                                 timeout_interval = atoi (optarg);
194                         }
195                         else {
196                                 usage ("Time interval must be a nonnegative integer\n");
197                         }
198                         break;
199                 case 'V':                                                                       /* version */
200                         print_revision (progname, "$Revision$");
201                         exit (STATE_OK);
202                 case 'h':                                                                       /* help */
203                         print_help ();
204                         exit (STATE_OK);
205                 }
206         }
208         c = optind;
209         if (dns_server == NULL) {
210                 if (c < argc) {
211                         if (is_host (argv[c])) {
212                                 dns_server = argv[c];
213                         }
214                         else {
215                                 usage ("Invalid host name");
216                         }
217                 }
218                 else {
219                         asprintf (&dns_server, "127.0.0.1");
220                 }
221         }
223         return validate_arguments ();
230 int
231 validate_arguments (void)
233         return OK;
240 void
241 print_help (void)
243         print_revision (progname, "$Revision$");
244         printf
245                 ("Copyright (c) %s %s <%s>\n\n%s\n",
246                  COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
247         print_usage ();
248         printf
249                 ("\nOptions:\n"
250                  " -H, --hostname=STRING or IPADDRESS\n"
251                  "   Check server on the indicated host\n"
252                  " -l, --lookup=STRING\n"
253                  "   machine name to lookup\n"
254                  " -t, --timeout=INTEGER\n"
255                  "   Seconds before connection attempt times out (default: %d)\n"
256                  " -v, --verbose\n"
257                  "   Print extra information (command-line use only)\n"
258                  " -h, --help\n"
259                  "   Print detailed help screen\n"
260                  " -V, --version\n"
261                  "   Print version information\n\n", DEFAULT_SOCKET_TIMEOUT);
262         support ();
269 void
270 print_usage (void)
272         printf
273                 ("Usage: %s -H host -l lookup [-t timeout] [-v]\n"
274                  "       %s --help\n"
275                  "       %s --version\n", progname, progname, progname);