Code

cause snmpget try try for 1 second less than the timeout (allowing plugin to force...
[nagiosplug.git] / plugins / check_dig.c
1 /*****************************************************************************
2 *
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.
7 *
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.
12 *
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.
16 *
17 *****************************************************************************/
19 const char *progname = "check_dig";
20 const char *revision = "$Revision$";
21 const char *copyright = "2002-2003";
22 const char *authors = "Nagios Plugin Development Team";
23 const char *email = "nagiosplug-devel@lists.sourceforge.net";
25 const char *summary = "Test the DNS service on the specified host using dig\n";
27 const char *option_summary = "-H host -l lookup [-t timeout] [-v]";
29 const char *options = "\
30  -H, --hostname=STRING or IPADDRESS\n\
31    Check server on the indicated host\n\
32  -l, --lookup=STRING\n\
33    machine name to lookup\n\
34  -t, --timeout=INTEGER\n\
35    Seconds before connection attempt times out (default: %d)\n\
36  -v, --verbose\n\
37    Print extra information (command-line use only)\n";
39 const char *standard_options = "\
40  -h, --help\n\
41     Print detailed help screen\n\
42  -V, --version\n\
43     Print version information\n\n";
45 #include "config.h"
46 #include "common.h"
47 #include "utils.h"
48 #include "popen.h"
50 int process_arguments (int, char **);
51 int validate_arguments (void);
52 void print_help (void);
53 void print_usage (void);
55 char *query_address = NULL;
56 char *dns_server = NULL;
57 int verbose = FALSE;
59 int
60 main (int argc, char **argv)
61 {
62         char input_buffer[MAX_INPUT_BUFFER];
63         char *command_line = NULL;
64         char *output = "";
65         int result = STATE_UNKNOWN;
67         /* Set signal handling and alarm */
68         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
69                 usage ("Cannot catch SIGALRM\n");
71         if (process_arguments (argc, argv) != OK)
72                 usage ("Could not parse arguments\n");
74         /* get the command to run */
75         asprintf (&command_line, "%s @%s %s", PATH_TO_DIG, dns_server, query_address);
77         alarm (timeout_interval);
78         time (&start_time);
80         if (verbose)
81                 printf ("%s\n", command_line);
82         /* run the command */
83         child_process = spopen (command_line);
84         if (child_process == NULL) {
85                 printf ("Could not open pipe: %s\n", command_line);
86                 return STATE_UNKNOWN;
87         }
89         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
90         if (child_stderr == NULL)
91                 printf ("Could not open stderr for %s\n", command_line);
93         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
95                 /* the server is responding, we just got the host name... */
96                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
98                         /* get the host address */
99                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
100                                 break;
102                         if (strpbrk (input_buffer, "\r\n"))
103                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
105                         if (strstr (input_buffer, query_address) == input_buffer) {
106                                 asprintf (&output, input_buffer);
107                                 result = STATE_OK;
108                         }
109                         else {
110                                 asprintf (&output, "Server not found in ANSWER SECTION");
111                                 result = STATE_WARNING;
112                         }
114                         continue;
115                 }
117         }
119         if (result != STATE_OK) {
120                 asprintf (&output, "No ANSWER SECTION found");
121         }
123         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
124                 /* If we get anything on STDERR, at least set warning */
125                 result = max_state (result, STATE_WARNING);
126                 printf ("%s", input_buffer);
127                 if (strlen (output) == 0)
128                         asprintf (&output, 1 + index (input_buffer, ':'));
129         }
131         (void) fclose (child_stderr);
133         /* close the pipe */
134         if (spclose (child_process)) {
135                 result = max_state (result, STATE_WARNING);
136                 if (strlen (output) == 0)
137                         asprintf (&output, "dig returned error status");
138         }
140         (void) time (&end_time);
142         if (output == NULL || strlen (output) == 0)
143                 asprintf (&output, " Probably a non-existent host/domain");
145         if (result == STATE_OK)
146                 printf ("DNS OK - %d seconds response time (%s)\n",
147                                                 (int) (end_time - start_time), output);
148         else if (result == STATE_WARNING)
149                 printf ("DNS WARNING - %s\n", output);
150         else if (result == STATE_CRITICAL)
151                 printf ("DNS CRITICAL - %s\n", output);
152         else
153                 printf ("DNS problem - %s\n", output);
155         return result;
158 /* process command-line arguments */
159 int
160 process_arguments (int argc, char **argv)
162         int c;
164         int option_index = 0;
165         static struct option long_options[] = {
166                 {"hostname", required_argument, 0, 'H'},
167                 {"query_address", required_argument, 0, 'e'},
168                 {"verbose", no_argument, 0, 'v'},
169                 {"version", no_argument, 0, 'V'},
170                 {"help", no_argument, 0, 'h'},
171                 {0, 0, 0, 0}
172         };
174         if (argc < 2)
175                 return ERROR;
177         while (1) {
178                 c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
180                 if (c == -1 || c == EOF)
181                         break;
183                 switch (c) {
184                 case '?':                                                                       /* help */
185                         usage3 ("Unknown argument", optopt);
186                 case 'H':                                                                       /* hostname */
187                         if (is_host (optarg)) {
188                                 dns_server = optarg;
189                         }
190                         else {
191                                 usage ("Invalid host name\n");
192                         }
193                         break;
194                 case 'l':                                                                       /* username */
195                         query_address = optarg;
196                         break;
197                 case 'v':                                                                       /* verbose */
198                         verbose = TRUE;
199                         break;
200                 case 't':                                                                       /* timeout */
201                         if (is_intnonneg (optarg)) {
202                                 timeout_interval = atoi (optarg);
203                         }
204                         else {
205                                 usage ("Time interval must be a nonnegative integer\n");
206                         }
207                         break;
208                 case 'V':                                                                       /* version */
209                         print_revision (progname, "$Revision$");
210                         exit (STATE_OK);
211                 case 'h':                                                                       /* help */
212                         print_help ();
213                         exit (STATE_OK);
214                 }
215         }
217         c = optind;
218         if (dns_server == NULL) {
219                 if (c < argc) {
220                         if (is_host (argv[c])) {
221                                 dns_server = argv[c];
222                         }
223                         else {
224                                 usage ("Invalid host name");
225                         }
226                 }
227                 else {
228                         dns_server = strdup ("127.0.0.1");
229                 }
230         }
232         return validate_arguments ();
239 int
240 validate_arguments (void)
242         return OK;
244 \f
249 void
250 print_help (void)
252         print_revision (progname, revision);
253         printf ("Copyright (c) %s %s\n\t<%s>\n\n", copyright, authors, email);
254         printf (summary);
255         print_usage ();
256         printf ("\nOptions:\n");
257         printf (options, DEFAULT_SOCKET_TIMEOUT);
258         printf (standard_options);
259         support ();
266 void
267 print_usage (void)
269         printf ("Usage: %s %s\n", progname, option_summary);
270         printf ("       %s (-h|--help)\n", progname);
271         printf ("       %s (-V|--version)\n", progname);