Code

more pedantic compiler warns
[nagiosplug.git] / plugins / check_dig.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 *****************************************************************************/
19 #include "common.h"
20 #include "netutils.h"
21 #include "utils.h"
22 #include "popen.h"
24 int process_arguments (int, char **);
25 int validate_arguments (void);
26 void print_help (void);
27 void print_usage (void);
29 const char *progname = "check_dig";
30 const char *revision = "$Revision$";
31 const char *copyright = "2002-2003";
32 const char *email = "nagiosplug-devel@lists.sourceforge.net";
34 enum {
35         DEFAULT_PORT = 53
36 };
38 char *query_address = NULL;
39 char *dns_server = NULL;
40 int verbose = FALSE;
41 int server_port = DEFAULT_PORT;
42 int warning_interval = -1;
43 int critical_interval = -1;
49 \f
50 int
51 main (int argc, char **argv)
52 {
53         char input_buffer[MAX_INPUT_BUFFER];
54         char *command_line;
55         char *output;
56         int result = STATE_UNKNOWN;
58         output = strdup ("");
60         /* Set signal handling and alarm */
61         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
62                 usage (_("Cannot catch SIGALRM\n"));
64         if (process_arguments (argc, argv) != OK)
65                 usage (_("Could not parse arguments\n"));
67         /* get the command to run */
68         asprintf (&command_line, "%s @%s -p %d %s",
69                   PATH_TO_DIG, dns_server, server_port, query_address);
71         alarm (timeout_interval);
72         time (&start_time);
74         if (verbose)
75                 printf ("%s\n", command_line);
76         /* run the command */
77         child_process = spopen (command_line);
78         if (child_process == NULL) {
79                 printf (_("Could not open pipe: %s\n"), command_line);
80                 return STATE_UNKNOWN;
81         }
83         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
84         if (child_stderr == NULL)
85                 printf (_("Could not open stderr for %s\n"), command_line);
87         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
89                 /* the server is responding, we just got the host name... */
90                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
92                         /* get the host address */
93                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
94                                 break;
96                         if (strpbrk (input_buffer, "\r\n"))
97                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
99                         if (strstr (input_buffer, query_address) == input_buffer) {
100                                 output = strdup(input_buffer);
101                                 result = STATE_OK;
102                         }
103                         else {
104                                 asprintf (&output, _("Server not found in ANSWER SECTION"));
105                                 result = STATE_WARNING;
106                         }
108                         continue;
109                 }
111         }
113         if (result != STATE_OK) {
114                 asprintf (&output, _("No ANSWER SECTION found"));
115         }
117         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
118                 /* If we get anything on STDERR, at least set warning */
119                 result = max_state (result, STATE_WARNING);
120                 printf ("%s", input_buffer);
121                 if (strlen (output) == 0)
122                         output = strdup (1 + index (input_buffer, ':'));
123         }
125         (void) fclose (child_stderr);
127         /* close the pipe */
128         if (spclose (child_process)) {
129                 result = max_state (result, STATE_WARNING);
130                 if (strlen (output) == 0)
131                         asprintf (&output, _("dig returned error status"));
132         }
134         (void) time (&end_time);
136         if (output == NULL || strlen (output) == 0)
137                 asprintf (&output, _(" Probably a non-existent host/domain"));
139         if (result == STATE_OK)
140                 printf (_("DNS OK - %d seconds response time (%s)\n"),
141                                                 (int) (end_time - start_time), output);
142         else if (result == STATE_WARNING)
143                 printf (_("DNS WARNING - %s\n"), output);
144         else if (result == STATE_CRITICAL)
145                 printf (_("DNS CRITICAL - %s\n"), output);
146         else
147                 printf (_("DNS problem - %s\n"), output);
149         return result;
156 \f
157 /* process command-line arguments */
158 int
159 process_arguments (int argc, char **argv)
161         int c;
163         int option_index = 0;
164         static struct option long_options[] = {
165                 {"hostname", required_argument, 0, 'H'},
166                 {"query_address", required_argument, 0, 'e'},
167                 {"verbose", no_argument, 0, 'v'},
168                 {"version", no_argument, 0, 'V'},
169                 {"help", no_argument, 0, 'h'},
170                 {0, 0, 0, 0}
171         };
173         if (argc < 2)
174                 return ERROR;
176         while (1) {
177                 c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
179                 if (c == -1 || c == EOF)
180                         break;
182                 switch (c) {
183                 case '?':                                                                       /* help */
184                         usage3 (_("Unknown argument"), optopt);
185                 case 'h':                                                                       /* help */
186                         print_help ();
187                         exit (STATE_OK);
188                 case 'V':                                                                       /* version */
189                         print_revision (progname, "$Revision$");
190                         exit (STATE_OK);
191                 case 'H':                                                                       /* hostname */
192                         if (is_host (optarg)) {
193                                 dns_server = optarg;
194                         }
195                         else {
196                                 usage2 (_("Invalid host name"), optarg);
197                         }
198                         break;
199                 case 'p':
200                         if (is_intpos (optarg)) {
201                                 server_port = atoi (optarg);
202                         }
203                         else {
204                                 usage2 (_("Server port must be a nonnegative integer\n"), optarg);
205                         }
206                         break;
207                 case 'l':                                                                       /* username */
208                         query_address = optarg;
209                         break;
210                 case 'w':                                                                       /* timeout */
211                         if (is_intnonneg (optarg)) {
212                                 warning_interval = atoi (optarg);
213                         }
214                         else {
215                                 usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
216                         }
217                         break;
218                 case 'c':                                                                       /* timeout */
219                         if (is_intnonneg (optarg)) {
220                                 critical_interval = atoi (optarg);
221                         }
222                         else {
223                                 usage2 (_("Critical interval must be a nonnegative integer\n"), optarg);
224                         }
225                         break;
226                 case 't':                                                                       /* timeout */
227                         if (is_intnonneg (optarg)) {
228                                 timeout_interval = atoi (optarg);
229                         }
230                         else {
231                                 usage2 (_("Time interval must be a nonnegative integer\n"), optarg);
232                         }
233                         break;
234                 case 'v':                                                                       /* verbose */
235                         verbose = TRUE;
236                         break;
237                 }
238         }
240         c = optind;
241         if (dns_server == NULL) {
242                 if (c < argc) {
243                         if (is_host (argv[c])) {
244                                 dns_server = argv[c];
245                         }
246                         else {
247                                 usage2 (_("Invalid host name"), argv[c]);
248                         }
249                 }
250                 else {
251                         dns_server = strdup ("127.0.0.1");
252                 }
253         }
255         return validate_arguments ();
262 int
263 validate_arguments (void)
265         return OK;
273 \f
274 void
275 print_help (void)
277         char *myport;
279         asprintf (&myport, "%d", DEFAULT_PORT);
281         print_revision (progname, revision);
283         printf (_(COPYRIGHT), copyright, email);
285         printf (_("Test the DNS service on the specified host using dig\n\n"));
287         print_usage ();
289         printf (_(UT_HELP_VRSN));
291         printf (_(UT_HOST_PORT), 'P', myport);
293         printf (_("\
294  -l, --lookup=STRING\n\
295    machine name to lookup\n"));
297         printf (_(UT_WARN_CRIT));
299         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
301         printf (_(UT_VERBOSE));
303         support ();
309 void
310 print_usage (void)
312         printf (_("\
313 Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
314          [-c <critical interval>] [-t <timeout>] [-v]\n"),
315                 progname);
316         printf ("       %s (-h|--help)\n", progname);
317         printf ("       %s (-V|--version)\n", progname);