Code

- bindtextdomain for gettext, a few other smale cleanups here and there
[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;
45 int
46 main (int argc, char **argv)
47 {
48         char input_buffer[MAX_INPUT_BUFFER];
49         char *command_line;
50         char *output;
51         int result = STATE_UNKNOWN;
53         output = strdup ("");
55         setlocale (LC_ALL, "");
56         bindtextdomain (PACKAGE, LOCALEDIR);
57         textdomain (PACKAGE);
59         /* Set signal handling and alarm */
60         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
61                 usage (_("Cannot catch SIGALRM\n"));
63         if (process_arguments (argc, argv) != OK)
64                 usage (_("Could not parse arguments\n"));
66         /* get the command to run */
67         asprintf (&command_line, "%s @%s -p %d %s",
68                   PATH_TO_DIG, dns_server, server_port, query_address);
70         alarm (timeout_interval);
71         time (&start_time);
73         if (verbose)
74                 printf ("%s\n", command_line);
75         /* run the command */
76         child_process = spopen (command_line);
77         if (child_process == NULL) {
78                 printf (_("Could not open pipe: %s\n"), command_line);
79                 return STATE_UNKNOWN;
80         }
82         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
83         if (child_stderr == NULL)
84                 printf (_("Could not open stderr for %s\n"), command_line);
86         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
88                 /* the server is responding, we just got the host name... */
89                 if (strstr (input_buffer, ";; ANSWER SECTION:")) {
91                         /* get the host address */
92                         if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
93                                 break;
95                         if (strpbrk (input_buffer, "\r\n"))
96                                 input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
98                         if (strstr (input_buffer, query_address) == input_buffer) {
99                                 output = strdup(input_buffer);
100                                 result = STATE_OK;
101                         }
102                         else {
103                                 asprintf (&output, _("Server not found in ANSWER SECTION"));
104                                 result = STATE_WARNING;
105                         }
107                         continue;
108                 }
110         }
112         if (result != STATE_OK) {
113                 asprintf (&output, _("No ANSWER SECTION found"));
114         }
116         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
117                 /* If we get anything on STDERR, at least set warning */
118                 result = max_state (result, STATE_WARNING);
119                 printf ("%s", input_buffer);
120                 if (strlen (output) == 0)
121                         output = strdup (1 + index (input_buffer, ':'));
122         }
124         (void) fclose (child_stderr);
126         /* close the pipe */
127         if (spclose (child_process)) {
128                 result = max_state (result, STATE_WARNING);
129                 if (strlen (output) == 0)
130                         asprintf (&output, _("dig returned error status"));
131         }
133         (void) time (&end_time);
135         if (output == NULL || strlen (output) == 0)
136                 asprintf (&output, _(" Probably a non-existent host/domain"));
138         if (result == STATE_OK)
139                 printf (_("DNS OK - %d seconds response time (%s)\n"),
140                                                 (int) (end_time - start_time), output);
141         else if (result == STATE_WARNING)
142                 printf (_("DNS WARNING - %s\n"), output);
143         else if (result == STATE_CRITICAL)
144                 printf (_("DNS CRITICAL - %s\n"), output);
145         else
146                 printf (_("DNS problem - %s\n"), output);
148         return result;
155 \f
156 /* process command-line arguments */
157 int
158 process_arguments (int argc, char **argv)
160         int c;
162         int option = 0;
163         static struct option longopts[] = {
164                 {"hostname", required_argument, 0, 'H'},
165                 {"query_address", required_argument, 0, 'e'},
166                 {"verbose", no_argument, 0, 'v'},
167                 {"version", no_argument, 0, 'V'},
168                 {"help", no_argument, 0, 'h'},
169                 {0, 0, 0, 0}
170         };
172         if (argc < 2)
173                 return ERROR;
175         while (1) {
176                 c = getopt_long (argc, argv, "hVvt:l:H:", longopts, &option);
178                 if (c == -1 || c == EOF)
179                         break;
181                 switch (c) {
182                 case '?':                                                                       /* help */
183                         usage3 (_("Unknown argument"), optopt);
184                 case 'h':                                                                       /* help */
185                         print_help ();
186                         exit (STATE_OK);
187                 case 'V':                                                                       /* version */
188                         print_revision (progname, "$Revision$");
189                         exit (STATE_OK);
190                 case 'H':                                                                       /* hostname */
191                         if (is_host (optarg)) {
192                                 dns_server = optarg;
193                         }
194                         else {
195                                 usage2 (_("Invalid host name"), optarg);
196                         }
197                         break;
198                 case 'p':
199                         if (is_intpos (optarg)) {
200                                 server_port = atoi (optarg);
201                         }
202                         else {
203                                 usage2 (_("Server port must be a nonnegative integer\n"), optarg);
204                         }
205                         break;
206                 case 'l':                                                                       /* username */
207                         query_address = optarg;
208                         break;
209                 case 'w':                                                                       /* timeout */
210                         if (is_intnonneg (optarg)) {
211                                 warning_interval = atoi (optarg);
212                         }
213                         else {
214                                 usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
215                         }
216                         break;
217                 case 'c':                                                                       /* timeout */
218                         if (is_intnonneg (optarg)) {
219                                 critical_interval = atoi (optarg);
220                         }
221                         else {
222                                 usage2 (_("Critical interval must be a nonnegative integer\n"), optarg);
223                         }
224                         break;
225                 case 't':                                                                       /* timeout */
226                         if (is_intnonneg (optarg)) {
227                                 timeout_interval = atoi (optarg);
228                         }
229                         else {
230                                 usage2 (_("Time interval must be a nonnegative integer\n"), optarg);
231                         }
232                         break;
233                 case 'v':                                                                       /* verbose */
234                         verbose = TRUE;
235                         break;
236                 }
237         }
239         c = optind;
240         if (dns_server == NULL) {
241                 if (c < argc) {
242                         if (is_host (argv[c])) {
243                                 dns_server = argv[c];
244                         }
245                         else {
246                                 usage2 (_("Invalid host name"), argv[c]);
247                         }
248                 }
249                 else {
250                         dns_server = strdup ("127.0.0.1");
251                 }
252         }
254         return validate_arguments ();
261 int
262 validate_arguments (void)
264         return OK;
272 \f
273 void
274 print_help (void)
276         char *myport;
278         asprintf (&myport, "%d", DEFAULT_PORT);
280         print_revision (progname, revision);
282         printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
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         printf (_(UT_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);