Code

markup for translation
[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 #include "config.h"
20 #include "common.h"
21 #include "netutils.h"
22 #include "utils.h"
23 #include "popen.h"
25 int process_arguments (int, char **);
26 int validate_arguments (void);
27 void print_help (void);
28 void print_usage (void);
30 const char *progname = "check_dig";
31 const char *revision = "$Revision$";
32 const char *copyright = "2002-2003";
33 const char *email = "nagiosplug-devel@lists.sourceforge.net";
35 enum {
36         DEFAULT_PORT = 53
37 };
39 void
40 print_usage (void)
41 {
42         printf (_("\
43 Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
44          [-c <critical interval>] [-t <timeout>] [-v]\n"),
45                 progname);
46         printf ("       %s (-h|--help)\n", progname);
47         printf ("       %s (-V|--version)\n", progname);
48 }
50 void
51 print_help (void)
52 {
53         char *myport;
55         asprintf (&myport, "%d", DEFAULT_PORT);
57         print_revision (progname, revision);
59         printf (_(COPYRIGHT), copyright, email);
61         printf (_("Test the DNS service on the specified host using dig\n\n"));
63         print_usage ();
65         printf (_(UT_HELP_VRSN));
67         printf (_(UT_HOST_PORT), 'P', myport);
69         printf (_("\
70  -l, --lookup=STRING\n\
71    machine name to lookup\n"));
73         printf (_(UT_WARN_CRIT));
75         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
77         printf (_(UT_VERBOSE));
79         support ();
80 }
81 \f
83 char *query_address = NULL;
84 char *dns_server = NULL;
85 int verbose = FALSE;
86 int server_port = DEFAULT_PORT;
87 int warning_interval = -1;
88 int critical_interval = -1;
91 int
92 main (int argc, char **argv)
93 {
94         char input_buffer[MAX_INPUT_BUFFER];
95         char *command_line = NULL;
96         char *output = "";
97         int result = STATE_UNKNOWN;
99         /* Set signal handling and alarm */
100         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
101                 usage (_("Cannot catch SIGALRM\n"));
103         if (process_arguments (argc, argv) != OK)
104                 usage (_("Could not parse arguments\n"));
106         /* get the command to run */
107         asprintf (&command_line, "%s @%s -p %d %s",
108                   PATH_TO_DIG, dns_server, server_port, query_address);
110         alarm (timeout_interval);
111         time (&start_time);
113         if (verbose)
114                 printf ("%s\n", command_line);
115         /* run the command */
116         child_process = spopen (command_line);
117         if (child_process == NULL) {
118                 printf (_("Could not open pipe: %s\n"), command_line);
119                 return STATE_UNKNOWN;
120         }
122         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
123         if (child_stderr == NULL)
124                 printf (_("Could not open stderr for %s\n"), command_line);
126         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
128                 /* the server is responding, we just got the host name... */
129                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
131                         /* get the host address */
132                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
133                                 break;
135                         if (strpbrk (input_buffer, "\r\n"))
136                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
138                         if (strstr (input_buffer, query_address) == input_buffer) {
139                                 asprintf (&output, input_buffer);
140                                 result = STATE_OK;
141                         }
142                         else {
143                                 asprintf (&output, _("Server not found in ANSWER SECTION"));
144                                 result = STATE_WARNING;
145                         }
147                         continue;
148                 }
150         }
152         if (result != STATE_OK) {
153                 asprintf (&output, _("No ANSWER SECTION found"));
154         }
156         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
157                 /* If we get anything on STDERR, at least set warning */
158                 result = max_state (result, STATE_WARNING);
159                 printf ("%s", input_buffer);
160                 if (strlen (output) == 0)
161                         asprintf (&output, 1 + index (input_buffer, ':'));
162         }
164         (void) fclose (child_stderr);
166         /* close the pipe */
167         if (spclose (child_process)) {
168                 result = max_state (result, STATE_WARNING);
169                 if (strlen (output) == 0)
170                         asprintf (&output, _("dig returned error status"));
171         }
173         (void) time (&end_time);
175         if (output == NULL || strlen (output) == 0)
176                 asprintf (&output, _(" Probably a non-existent host/domain"));
178         if (result == STATE_OK)
179                 printf (_("DNS OK - %d seconds response time (%s)\n"),
180                                                 (int) (end_time - start_time), output);
181         else if (result == STATE_WARNING)
182                 printf (_("DNS WARNING - %s\n"), output);
183         else if (result == STATE_CRITICAL)
184                 printf (_("DNS CRITICAL - %s\n"), output);
185         else
186                 printf (_("DNS problem - %s\n"), output);
188         return result;
191 /* process command-line arguments */
192 int
193 process_arguments (int argc, char **argv)
195         int c;
197         int option_index = 0;
198         static struct option long_options[] = {
199                 {"hostname", required_argument, 0, 'H'},
200                 {"query_address", required_argument, 0, 'e'},
201                 {"verbose", no_argument, 0, 'v'},
202                 {"version", no_argument, 0, 'V'},
203                 {"help", no_argument, 0, 'h'},
204                 {0, 0, 0, 0}
205         };
207         if (argc < 2)
208                 return ERROR;
210         while (1) {
211                 c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
213                 if (c == -1 || c == EOF)
214                         break;
216                 switch (c) {
217                 case '?':                                                                       /* help */
218                         usage3 (_("Unknown argument"), optopt);
219                 case 'h':                                                                       /* help */
220                         print_help ();
221                         exit (STATE_OK);
222                 case 'V':                                                                       /* version */
223                         print_revision (progname, "$Revision$");
224                         exit (STATE_OK);
225                 case 'H':                                                                       /* hostname */
226                         if (is_host (optarg)) {
227                                 dns_server = optarg;
228                         }
229                         else {
230                                 usage2 (_("Invalid host name"), optarg);
231                         }
232                         break;
233                 case 'p':
234                         if (is_intpos (optarg)) {
235                                 server_port = atoi (optarg);
236                         }
237                         else {
238                                 usage2 (_("Server port must be a nonnegative integer\n"), optarg);
239                         }
240                         break;
241                 case 'l':                                                                       /* username */
242                         query_address = optarg;
243                         break;
244                 case 'w':                                                                       /* timeout */
245                         if (is_intnonneg (optarg)) {
246                                 warning_interval = atoi (optarg);
247                         }
248                         else {
249                                 usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
250                         }
251                         break;
252                 case 'c':                                                                       /* timeout */
253                         if (is_intnonneg (optarg)) {
254                                 critical_interval = atoi (optarg);
255                         }
256                         else {
257                                 usage2 (_("Critical interval must be a nonnegative integer\n"), optarg);
258                         }
259                         break;
260                 case 't':                                                                       /* timeout */
261                         if (is_intnonneg (optarg)) {
262                                 timeout_interval = atoi (optarg);
263                         }
264                         else {
265                                 usage2 (_("Time interval must be a nonnegative integer\n"), optarg);
266                         }
267                         break;
268                 case 'v':                                                                       /* verbose */
269                         verbose = TRUE;
270                         break;
271                 }
272         }
274         c = optind;
275         if (dns_server == NULL) {
276                 if (c < argc) {
277                         if (is_host (argv[c])) {
278                                 dns_server = argv[c];
279                         }
280                         else {
281                                 usage2 (_("Invalid host name"), argv[c]);
282                         }
283                 }
284                 else {
285                         dns_server = strdup ("127.0.0.1");
286                 }
287         }
289         return validate_arguments ();
296 int
297 validate_arguments (void)
299         return OK;
301 \f