Code

code cleanup to clear strict compiler warnings
[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";
24 const char *summary = "Test the DNS service on the specified host using dig\n";
26 const char *option_summary = "-H host -l lookup [-t timeout] [-v]";
28 const char *options = "\
29  -H, --hostname=STRING or IPADDRESS\n\
30    Check server on the indicated host\n\
31  -l, --lookup=STRING\n\
32    machine name to lookup\n\
33  -t, --timeout=INTEGER\n\
34    Seconds before connection attempt times out (default: %d)\n\
35  -v, --verbose\n\
36    Print extra information (command-line use only)\n\
37  -h, --help\n\
38    Print detailed help screen\n\
39  -V, --version\n\
40    Print version information\n\n";
42 #include "config.h"
43 #include "common.h"
44 #include "utils.h"
45 #include "popen.h"
47 int process_arguments (int, char **);
48 int validate_arguments (void);
49 void print_help (void);
50 void print_usage (void);
52 char *query_address = NULL;
53 char *dns_server = NULL;
54 int verbose = FALSE;
56 int
57 main (int argc, char **argv)
58 {
59         char input_buffer[MAX_INPUT_BUFFER];
60         char *command_line = NULL;
61         char *output = "";
62         int result = STATE_UNKNOWN;
64         /* Set signal handling and alarm */
65         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
66                 usage ("Cannot catch SIGALRM\n");
68         if (process_arguments (argc, argv) != OK)
69                 usage ("Could not parse arguments\n");
71         /* get the command to run */
72         asprintf (&command_line, "%s @%s %s", PATH_TO_DIG, dns_server, query_address);
74         alarm (timeout_interval);
75         time (&start_time);
77         if (verbose)
78                 printf ("%s\n", command_line);
79         /* run the command */
80         child_process = spopen (command_line);
81         if (child_process == NULL) {
82                 printf ("Could not open pipe: %s\n", command_line);
83                 return STATE_UNKNOWN;
84         }
86         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
87         if (child_stderr == NULL)
88                 printf ("Could not open stderr for %s\n", command_line);
90         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
92                 /* the server is responding, we just got the host name... */
93                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
95                         /* get the host address */
96                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
97                                 break;
99                         if (strpbrk (input_buffer, "\r\n"))
100                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
102                         if (strstr (input_buffer, query_address) == input_buffer) {
103                                 asprintf (&output, input_buffer);
104                                 result = STATE_OK;
105                         }
106                         else {
107                                 asprintf (&output, "Server not found in ANSWER SECTION");
108                                 result = STATE_WARNING;
109                         }
111                         continue;
112                 }
114         }
116         if (result != STATE_OK) {
117                 asprintf (&output, "No ANSWER SECTION found");
118         }
120         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
121                 /* If we get anything on STDERR, at least set warning */
122                 result = max_state (result, STATE_WARNING);
123                 printf ("%s", input_buffer);
124                 if (strlen (output) == 0)
125                         asprintf (&output, 1 + index (input_buffer, ':'));
126         }
128         (void) fclose (child_stderr);
130         /* close the pipe */
131         if (spclose (child_process)) {
132                 result = max_state (result, STATE_WARNING);
133                 if (strlen (output) == 0)
134                         asprintf (&output, "dig returned error status");
135         }
137         (void) time (&end_time);
139         if (output == NULL || strlen (output) == 0)
140                 asprintf (&output, " Probably a non-existent host/domain");
142         if (result == STATE_OK)
143                 printf ("DNS OK - %d seconds response time (%s)\n",
144                                                 (int) (end_time - start_time), output);
145         else if (result == STATE_WARNING)
146                 printf ("DNS WARNING - %s\n", output);
147         else if (result == STATE_CRITICAL)
148                 printf ("DNS CRITICAL - %s\n", output);
149         else
150                 printf ("DNS problem - %s\n", output);
152         return result;
155 /* process command-line arguments */
156 int
157 process_arguments (int argc, char **argv)
159         int c;
161         int option_index = 0;
162         static struct option long_options[] = {
163                 {"hostname", required_argument, 0, 'H'},
164                 {"query_address", required_argument, 0, 'e'},
165                 {"verbose", no_argument, 0, 'v'},
166                 {"version", no_argument, 0, 'V'},
167                 {"help", no_argument, 0, 'h'},
168                 {0, 0, 0, 0}
169         };
171         if (argc < 2)
172                 return ERROR;
174         while (1) {
175                 c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
177                 if (c == -1 || c == EOF)
178                         break;
180                 switch (c) {
181                 case '?':                                                                       /* help */
182                         usage3 ("Unknown argument", optopt);
183                 case 'H':                                                                       /* hostname */
184                         if (is_host (optarg)) {
185                                 dns_server = optarg;
186                         }
187                         else {
188                                 usage ("Invalid host name\n");
189                         }
190                         break;
191                 case 'l':                                                                       /* username */
192                         query_address = optarg;
193                         break;
194                 case 'v':                                                                       /* verbose */
195                         verbose = TRUE;
196                         break;
197                 case 't':                                                                       /* timeout */
198                         if (is_intnonneg (optarg)) {
199                                 timeout_interval = atoi (optarg);
200                         }
201                         else {
202                                 usage ("Time interval must be a nonnegative integer\n");
203                         }
204                         break;
205                 case 'V':                                                                       /* version */
206                         print_revision (progname, "$Revision$");
207                         exit (STATE_OK);
208                 case 'h':                                                                       /* help */
209                         print_help ();
210                         exit (STATE_OK);
211                 }
212         }
214         c = optind;
215         if (dns_server == NULL) {
216                 if (c < argc) {
217                         if (is_host (argv[c])) {
218                                 dns_server = argv[c];
219                         }
220                         else {
221                                 usage ("Invalid host name");
222                         }
223                 }
224                 else {
225                         dns_server = strdup ("127.0.0.1");
226                 }
227         }
229         return validate_arguments ();
236 int
237 validate_arguments (void)
239         return OK;
246 void
247 print_help (void)
249         print_revision (progname, revision);
250         printf
251                 ("Copyright (c) %s %s <%s>\n\n%s\n",
252                  copyright, authors, email, summary);
253         print_usage ();
254         printf ("\nOptions:\n");
255         printf (options, DEFAULT_SOCKET_TIMEOUT);
256         support ();
263 void
264 print_usage (void)
266         printf ("Usage: %s %s\n", progname, option_summary);
267         printf ("       %s (-h|--help)\n", progname);
268         printf ("       %s (-V|--version)\n", progname);