Code

- forgot to declare struct timeval tv for hires timing
[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;
44 struct timeval tv;
46 int
47 main (int argc, char **argv)
48 {
49         char input_buffer[MAX_INPUT_BUFFER];
50         char *command_line;
51         char *output;
52         long microsec;
53         double elapsed_time;
54         int result = STATE_UNKNOWN;
56         output = strdup ("");
58         setlocale (LC_ALL, "");
59         bindtextdomain (PACKAGE, LOCALEDIR);
60         textdomain (PACKAGE);
62         /* Set signal handling and alarm */
63         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
64                 usage (_("Cannot catch SIGALRM\n"));
66         if (process_arguments (argc, argv) != OK)
67                 usage (_("Could not parse arguments\n"));
69         /* get the command to run */
70         asprintf (&command_line, "%s @%s -p %d %s",
71                   PATH_TO_DIG, dns_server, server_port, query_address);
73         alarm (timeout_interval);
74         gettimeofday (&tv, NULL);
76         if (verbose)
77                 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                                 output = strdup(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                         output = strdup (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         microsec = deltime (tv);
138         elapsed_time = (double)microsec / 1.0e6;
140         if (output == NULL || strlen (output) == 0)
141                 asprintf (&output, _(" Probably a non-existent host/domain"));
143         if (elapsed_time > critical_interval)
144                 die (STATE_CRITICAL,
145                      _("DNS OK - %d seconds response time (%s)|time=%ldus\n"),
146                      elapsed_time, output, microsec);
148         else if (result == STATE_CRITICAL)
149                 printf (_("DNS CRITICAL - %s|time=%ldus\n"), output);
151         else if (elapsed_time > warning_interval)
152                 die (STATE_WARNING,
153                      _("DNS OK - %d seconds response time (%s)|time=%ldus\n"),
154                      elapsed_time, output, microsec);
156         else if (result == STATE_WARNING)
157                 printf (_("DNS WARNING - %s|time=%ldus\n"), output);
159         else if (result == STATE_OK)
160                 printf (_("DNS OK - %d seconds response time (%s)|time=%ldus\n"),
161                                                 elapsed_time, output, microsec);
163         else
164                 printf (_("DNS problem - %s|time=%ldus\n"), output);
166         return result;
173 \f
174 /* process command-line arguments */
175 int
176 process_arguments (int argc, char **argv)
178         int c;
180         int option = 0;
181         static struct option longopts[] = {
182                 {"hostname", required_argument, 0, 'H'},
183                 {"query_address", required_argument, 0, 'e'},
184                 {"verbose", no_argument, 0, 'v'},
185                 {"version", no_argument, 0, 'V'},
186                 {"help", no_argument, 0, 'h'},
187                 {0, 0, 0, 0}
188         };
190         if (argc < 2)
191                 return ERROR;
193         while (1) {
194                 c = getopt_long (argc, argv, "hVvt:l:H:", longopts, &option);
196                 if (c == -1 || c == EOF)
197                         break;
199                 switch (c) {
200                 case '?':                                                                       /* help */
201                         usage3 (_("Unknown argument"), optopt);
202                 case 'h':                                                                       /* help */
203                         print_help ();
204                         exit (STATE_OK);
205                 case 'V':                                                                       /* version */
206                         print_revision (progname, "$Revision$");
207                         exit (STATE_OK);
208                 case 'H':                                                                       /* hostname */
209                         if (is_host (optarg)) {
210                                 dns_server = optarg;
211                         }
212                         else {
213                                 usage2 (_("Invalid host name"), optarg);
214                         }
215                         break;
216                 case 'p':
217                         if (is_intpos (optarg)) {
218                                 server_port = atoi (optarg);
219                         }
220                         else {
221                                 usage2 (_("Server port must be a nonnegative integer\n"), optarg);
222                         }
223                         break;
224                 case 'l':                                                                       /* username */
225                         query_address = optarg;
226                         break;
227                 case 'w':                                                                       /* timeout */
228                         if (is_intnonneg (optarg)) {
229                                 warning_interval = atoi (optarg);
230                         }
231                         else {
232                                 usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
233                         }
234                         break;
235                 case 'c':                                                                       /* timeout */
236                         if (is_intnonneg (optarg)) {
237                                 critical_interval = atoi (optarg);
238                         }
239                         else {
240                                 usage2 (_("Critical interval must be a nonnegative integer\n"), optarg);
241                         }
242                         break;
243                 case 't':                                                                       /* timeout */
244                         if (is_intnonneg (optarg)) {
245                                 timeout_interval = atoi (optarg);
246                         }
247                         else {
248                                 usage2 (_("Time interval must be a nonnegative integer\n"), optarg);
249                         }
250                         break;
251                 case 'v':                                                                       /* verbose */
252                         verbose = TRUE;
253                         break;
254                 }
255         }
257         c = optind;
258         if (dns_server == NULL) {
259                 if (c < argc) {
260                         if (is_host (argv[c])) {
261                                 dns_server = argv[c];
262                         }
263                         else {
264                                 usage2 (_("Invalid host name"), argv[c]);
265                         }
266                 }
267                 else {
268                         dns_server = strdup ("127.0.0.1");
269                 }
270         }
272         return validate_arguments ();
279 int
280 validate_arguments (void)
282         return OK;
290 \f
291 void
292 print_help (void)
294         char *myport;
296         asprintf (&myport, "%d", DEFAULT_PORT);
298         print_revision (progname, revision);
300         printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
301         printf (_(COPYRIGHT), copyright, email);
303         printf (_("Test the DNS service on the specified host using dig\n\n"));
305         print_usage ();
307         printf (_(UT_HELP_VRSN));
309         printf (_(UT_HOST_PORT), 'P', myport);
311         printf (_("\
312  -l, --lookup=STRING\n\
313    machine name to lookup\n"));
315         printf (_(UT_WARN_CRIT));
317         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
319         printf (_(UT_VERBOSE));
321         printf (_(UT_SUPPORT));
327 void
328 print_usage (void)
330         printf (_("\
331 Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
332          [-c <critical interval>] [-t <timeout>] [-v]\n"),
333                 progname);
334         printf ("       %s (-h|--help)\n", progname);
335         printf ("       %s (-V|--version)\n", progname);