Code

initial merging of ae's np_runcmd code into selected plugins.
[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  $Id$
18  
19 *****************************************************************************/
21 /* Hackers note:
22  *  There are typecasts to (char *) from _("foo bar") in this file.
23  *  They prevent compiler warnings. Never (ever), permute strings obtained
24  *  that are typecast from (const char *) (which happens when --disable-nls)
25  *  because on some architectures those strings are in non-writable memory */
27 const char *progname = "check_dig";
28 const char *revision = "$Revision$";
29 const char *copyright = "2002-2004";
30 const char *email = "nagiosplug-devel@lists.sourceforge.net";
32 #include "common.h"
33 #include "netutils.h"
34 #include "utils.h"
35 #include "runcmd.h"
37 int process_arguments (int, char **);
38 int validate_arguments (void);
39 void print_help (void);
40 void print_usage (void);
42 #define UNDEFINED 0
43 #define DEFAULT_PORT 53
45 char *query_address = NULL;
46 char *record_type = "A";
47 char *expected_address = NULL;
48 char *dns_server = NULL;
49 int verbose = FALSE;
50 int server_port = DEFAULT_PORT;
51 double warning_interval = UNDEFINED;
52 double critical_interval = UNDEFINED;
53 struct timeval tv;
55 int
56 main (int argc, char **argv)
57 {
58         char *command_line;
59         output chld_out, chld_err;
60         char *msg = NULL;
61         size_t i;
62         char *t;
63         long microsec;
64         double elapsed_time;
65         int result = STATE_UNKNOWN;
67         setlocale (LC_ALL, "");
68         bindtextdomain (PACKAGE, LOCALEDIR);
69         textdomain (PACKAGE);
71         /* Set signal handling and alarm */
72         if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
73                 usage_va(_("Cannot catch SIGALRM"));
75         if (process_arguments (argc, argv) == ERROR)
76                 usage_va(_("Could not parse arguments"));
78         /* get the command to run */
79         asprintf (&command_line, "%s @%s -p %d %s -t %s",
80                   PATH_TO_DIG, dns_server, server_port, query_address, record_type);
82         alarm (timeout_interval);
83         gettimeofday (&tv, NULL);
85         if (verbose) {
86                 printf ("%s\n", command_line);
87                 if(expected_address != NULL) {
88                         printf (_("Looking for: '%s'\n"), expected_address);
89                 } else {
90                         printf (_("Looking for: '%s'\n"), query_address);
91                 }
92         }
94         /* run the command */
95         if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
96                 result = STATE_WARNING;
97                 msg = (char *)_("dig returned an error status");
98         }
100         for(i = 0; i < chld_out.lines; i++) {
101                 /* the server is responding, we just got the host name... */
102                 if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
104                         /* loop through the whole 'ANSWER SECTION' */
105                         for(; i < chld_out.lines; i++) {
106                                 /* get the host address */
107                                 if (verbose)
108                                         printf ("%s\n", chld_out.line[i]);
110                                 if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
111                                         msg = chld_out.line[i];
112                                         result = STATE_OK;
114                                         /* Translate output TAB -> SPACE */
115                                         t = msg;
116                                         while ((t = strchr(t, '\t')) != NULL) *t = ' ';
117                                         break;
118                                 }
119                         }
121                         if (result == STATE_UNKNOWN) {
122                                 msg = (char *)_("Server not found in ANSWER SECTION");
123                                 result = STATE_WARNING;
124                         }
126                         /* we found the answer section, so break out of the loop */
127                         break;
128                 }
129         }
131         if (result == STATE_UNKNOWN)
132                 msg = (char *)_("No ANSWER SECTION found");
134         /* If we get anything on STDERR, at least set warning */
135         if(chld_err.buflen > 0) {
136                 result = max_state(result, STATE_WARNING);
137                 if(!msg) for(i = 0; i < chld_err.lines; i++) {
138                         msg = strchr(chld_err.line[0], ':');
139                         if(msg) {
140                                 msg++;
141                                 break;
142                         }
143                 }
144         }
146         microsec = deltime (tv);
147         elapsed_time = (double)microsec / 1.0e6;
149         if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
150                 result = STATE_CRITICAL;
152         else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
153                 result = STATE_WARNING;
155         printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
156                 state_text (result), elapsed_time,
157                 msg ? msg : _("Probably a non-existent host/domain"),
158                 fperfdata("time", elapsed_time, "s",
159                           (warning_interval>UNDEFINED?TRUE:FALSE),
160                           warning_interval,
161                           (critical_interval>UNDEFINED?TRUE:FALSE),
162                                           critical_interval,
163                                           TRUE, 0, FALSE, 0));
164         return result;
169 /* process command-line arguments */
170 int
171 process_arguments (int argc, char **argv)
173         int c;
175         int option = 0;
176         static struct option longopts[] = {
177                 {"hostname", required_argument, 0, 'H'},
178                 {"query_address", required_argument, 0, 'l'},
179                 {"warning", required_argument, 0, 'w'},
180                 {"critical", required_argument, 0, 'c'},
181                 {"timeout", required_argument, 0, 't'},
182                 {"verbose", no_argument, 0, 'v'},
183                 {"version", no_argument, 0, 'V'},
184                 {"help", no_argument, 0, 'h'},
185                 {"record_type", required_argument, 0, 'T'},
186                 {"expected_address", required_argument, 0, 'a'},
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:w:c:T:a:", longopts, &option);
196                 if (c == -1 || c == EOF)
197                         break;
199                 switch (c) {
200                 case 'h':                                                                       /* help */
201                         print_help ();
202                         exit (STATE_OK);
203                 case 'V':                                                                       /* version */
204                         print_revision (progname, revision);
205                         exit (STATE_OK);
206                 case 'H':                                                                       /* hostname */
207                         host_or_die(optarg);
208                         dns_server = optarg;
209                         break;
210                 case 'p':                 /* server port */
211                         if (is_intpos (optarg)) {
212                                 server_port = atoi (optarg);
213                         }
214                         else {
215                                 usage_va(_("Port must be a positive integer - %s"), optarg);
216                         }
217                         break;
218                 case 'l':                                                                       /* address to lookup */
219                         query_address = optarg;
220                         break;
221                 case 'w':                                                                       /* warning */
222                         if (is_nonnegative (optarg)) {
223                                 warning_interval = strtod (optarg, NULL);
224                         }
225                         else {
226                                 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
227                         }
228                         break;
229                 case 'c':                                                                       /* critical */
230                         if (is_nonnegative (optarg)) {
231                                 critical_interval = strtod (optarg, NULL);
232                         }
233                         else {
234                                 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
235                         }
236                         break;
237                 case 't':                                                                       /* timeout */
238                         if (is_intnonneg (optarg)) {
239                                 timeout_interval = atoi (optarg);
240                         }
241                         else {
242                                 usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
243                         }
244                         break;
245                 case 'v':                                                                       /* verbose */
246                         verbose = TRUE;
247                         break;
248                 case 'T':
249                         record_type = optarg;
250                         break;
251                 case 'a':
252                         expected_address = optarg;
253                         break;
254                 default:                                                                        /* usage_va */
255                         usage_va(_("Unknown argument - %s"), optarg);
256                 }
257         }
259         c = optind;
260         if (dns_server == NULL) {
261                 if (c < argc) {
262                         host_or_die(argv[c]);
263                         dns_server = argv[c];
264                 }
265                 else {
266                         dns_server = strdup ("127.0.0.1");
267                 }
268         }
270         return validate_arguments ();
275 int
276 validate_arguments (void)
278         return OK;
283 void
284 print_help (void)
286         char *myport;
288         asprintf (&myport, "%d", DEFAULT_PORT);
290         print_revision (progname, revision);
292         printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
293         printf (COPYRIGHT, copyright, email);
295         printf (_("Test the DNS service on the specified host using dig\n\n"));
297         print_usage ();
299         printf (_(UT_HELP_VRSN));
301         printf (_(UT_HOST_PORT), 'P', myport);
303         printf (_("\
304  -l, --lookup=STRING\n\
305    machine name to lookup\n"));
307         printf (_("\
308  -T, --record_type=STRING\n\
309    record type to lookup (default: A)\n"));
311         printf (_("\
312  -a, --expected_address=STRING\n\
313    an address expected to be in the answer section.\n\
314    if not set, uses whatever was in -l\n"));
316         printf (_(UT_WARN_CRIT));
318         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
320         printf (_(UT_VERBOSE));
322         printf (_(UT_SUPPORT));
327 void
328 print_usage (void)
330         printf ("\
331 Usage: %s -H host -l lookup [-p <server port>] [-T <query type>]\n\
332               [-w <warning interval>] [-c <critical interval>] [-t <timeout>]\n\
333               [-a <expected answer address>] [-v]\n", progname);