Not picking up solaris systems correctly for check_dhcp. Only
[monitoring-plugins.git] / plugins / check_dig.c
blobe3f7adbd394218cebbdc49761275bab391b7e7dc
1 /*****************************************************************************
2 *
3 * Nagios check_dig plugin
4 *
5 * License: GPL
6 * Copyright (c) 2002-2008 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
12 * This file contains the check_dig plugin
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * $Id$
30 *****************************************************************************/
32 /* Hackers note:
33 * There are typecasts to (char *) from _("foo bar") in this file.
34 * They prevent compiler warnings. Never (ever), permute strings obtained
35 * that are typecast from (const char *) (which happens when --disable-nls)
36 * because on some architectures those strings are in non-writable memory */
38 const char *progname = "check_dig";
39 const char *revision = "$Revision$";
40 const char *copyright = "2002-2008";
41 const char *email = "nagiosplug-devel@lists.sourceforge.net";
43 #include "common.h"
44 #include "netutils.h"
45 #include "utils.h"
46 #include "runcmd.h"
48 int process_arguments (int, char **);
49 int validate_arguments (void);
50 void print_help (void);
51 void print_usage (void);
53 #define UNDEFINED 0
54 #define DEFAULT_PORT 53
56 char *query_address = NULL;
57 char *record_type = "A";
58 char *expected_address = NULL;
59 char *dns_server = NULL;
60 char *dig_args = "";
61 int verbose = FALSE;
62 int server_port = DEFAULT_PORT;
63 double warning_interval = UNDEFINED;
64 double critical_interval = UNDEFINED;
65 struct timeval tv;
67 int
68 main (int argc, char **argv)
70 char *command_line;
71 output chld_out, chld_err;
72 char *msg = NULL;
73 size_t i;
74 char *t;
75 long microsec;
76 double elapsed_time;
77 int result = STATE_UNKNOWN;
79 setlocale (LC_ALL, "");
80 bindtextdomain (PACKAGE, LOCALEDIR);
81 textdomain (PACKAGE);
83 /* Set signal handling and alarm */
84 if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
85 usage_va(_("Cannot catch SIGALRM"));
87 /* Parse extra opts if any */
88 argv=np_extra_opts (&argc, argv, progname);
90 if (process_arguments (argc, argv) == ERROR)
91 usage_va(_("Could not parse arguments"));
93 /* get the command to run */
94 asprintf (&command_line, "%s @%s -p %d %s -t %s %s",
95 PATH_TO_DIG, dns_server, server_port, query_address, record_type, dig_args);
97 alarm (timeout_interval);
98 gettimeofday (&tv, NULL);
100 if (verbose) {
101 printf ("%s\n", command_line);
102 if(expected_address != NULL) {
103 printf (_("Looking for: '%s'\n"), expected_address);
104 } else {
105 printf (_("Looking for: '%s'\n"), query_address);
109 /* run the command */
110 if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
111 result = STATE_WARNING;
112 msg = (char *)_("dig returned an error status");
115 for(i = 0; i < chld_out.lines; i++) {
116 /* the server is responding, we just got the host name... */
117 if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
119 /* loop through the whole 'ANSWER SECTION' */
120 for(; i < chld_out.lines; i++) {
121 /* get the host address */
122 if (verbose)
123 printf ("%s\n", chld_out.line[i]);
125 if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
126 msg = chld_out.line[i];
127 result = STATE_OK;
129 /* Translate output TAB -> SPACE */
130 t = msg;
131 while ((t = strchr(t, '\t')) != NULL) *t = ' ';
132 break;
136 if (result == STATE_UNKNOWN) {
137 msg = (char *)_("Server not found in ANSWER SECTION");
138 result = STATE_WARNING;
141 /* we found the answer section, so break out of the loop */
142 break;
146 if (result == STATE_UNKNOWN)
147 msg = (char *)_("No ANSWER SECTION found");
149 /* If we get anything on STDERR, at least set warning */
150 if(chld_err.buflen > 0) {
151 result = max_state(result, STATE_WARNING);
152 if(!msg) for(i = 0; i < chld_err.lines; i++) {
153 msg = strchr(chld_err.line[0], ':');
154 if(msg) {
155 msg++;
156 break;
161 microsec = deltime (tv);
162 elapsed_time = (double)microsec / 1.0e6;
164 if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
165 result = STATE_CRITICAL;
167 else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
168 result = STATE_WARNING;
170 printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
171 state_text (result), elapsed_time,
172 msg ? msg : _("Probably a non-existent host/domain"),
173 fperfdata("time", elapsed_time, "s",
174 (warning_interval>UNDEFINED?TRUE:FALSE),
175 warning_interval,
176 (critical_interval>UNDEFINED?TRUE:FALSE),
177 critical_interval,
178 TRUE, 0, FALSE, 0));
179 return result;
184 /* process command-line arguments */
186 process_arguments (int argc, char **argv)
188 int c;
190 int option = 0;
191 static struct option longopts[] = {
192 {"hostname", required_argument, 0, 'H'},
193 {"query_address", required_argument, 0, 'l'},
194 {"warning", required_argument, 0, 'w'},
195 {"critical", required_argument, 0, 'c'},
196 {"timeout", required_argument, 0, 't'},
197 {"dig-arguments", required_argument, 0, 'A'},
198 {"verbose", no_argument, 0, 'v'},
199 {"version", no_argument, 0, 'V'},
200 {"help", no_argument, 0, 'h'},
201 {"record_type", required_argument, 0, 'T'},
202 {"expected_address", required_argument, 0, 'a'},
203 {"port", required_argument, 0, 'p'},
204 {0, 0, 0, 0}
207 if (argc < 2)
208 return ERROR;
210 while (1) {
211 c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:", longopts, &option);
213 if (c == -1 || c == EOF)
214 break;
216 switch (c) {
217 case 'h': /* help */
218 print_help ();
219 exit (STATE_OK);
220 case 'V': /* version */
221 print_revision (progname, revision);
222 exit (STATE_OK);
223 case 'H': /* hostname */
224 host_or_die(optarg);
225 dns_server = optarg;
226 break;
227 case 'p': /* server port */
228 if (is_intpos (optarg)) {
229 server_port = atoi (optarg);
231 else {
232 usage_va(_("Port must be a positive integer - %s"), optarg);
234 break;
235 case 'l': /* address to lookup */
236 query_address = optarg;
237 break;
238 case 'w': /* warning */
239 if (is_nonnegative (optarg)) {
240 warning_interval = strtod (optarg, NULL);
242 else {
243 usage_va(_("Warning interval must be a positive integer - %s"), optarg);
245 break;
246 case 'c': /* critical */
247 if (is_nonnegative (optarg)) {
248 critical_interval = strtod (optarg, NULL);
250 else {
251 usage_va(_("Critical interval must be a positive integer - %s"), optarg);
253 break;
254 case 't': /* timeout */
255 if (is_intnonneg (optarg)) {
256 timeout_interval = atoi (optarg);
258 else {
259 usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
261 break;
262 case 'A': /* dig arguments */
263 dig_args = strdup(optarg);
264 break;
265 case 'v': /* verbose */
266 verbose = TRUE;
267 break;
268 case 'T':
269 record_type = optarg;
270 break;
271 case 'a':
272 expected_address = optarg;
273 break;
274 default: /* usage5 */
275 usage5();
279 c = optind;
280 if (dns_server == NULL) {
281 if (c < argc) {
282 host_or_die(argv[c]);
283 dns_server = argv[c];
285 else {
286 dns_server = strdup ("127.0.0.1");
290 return validate_arguments ();
296 validate_arguments (void)
298 return OK;
303 void
304 print_help (void)
306 char *myport;
308 asprintf (&myport, "%d", DEFAULT_PORT);
310 print_revision (progname, revision);
312 printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
313 printf (COPYRIGHT, copyright, email);
315 printf (_("This plugin test the DNS service on the specified host using dig"));
317 printf ("\n\n");
319 print_usage ();
321 printf (_(UT_HELP_VRSN));
323 printf (_(UT_EXTRA_OPTS));
325 printf (_(UT_HOST_PORT), 'p', myport);
327 printf (" %s\n","-l, --query_address=STRING");
328 printf (" %s\n",_("Machine name to lookup"));
329 printf (" %s\n","-T, --record_type=STRING");
330 printf (" %s\n",_("Record type to lookup (default: A)"));
331 printf (" %s\n","-a, --expected_address=STRING");
332 printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
333 printf (" %s\n",_("was in -l"));
334 printf (" %s\n","-A, --dig-arguments=STRING");
335 printf (" %s\n",_("Pass STRING as argument(s) to dig"));
336 printf (_(UT_WARN_CRIT));
337 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
338 printf (_(UT_VERBOSE));
340 printf ("\n");
341 printf ("%s\n", _("Examples:"));
342 printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
343 printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
345 #ifdef NP_EXTRA_OPTS
346 printf ("\n");
347 printf ("%s\n", _("Notes:"));
348 printf (_(UT_EXTRA_OPTS_NOTES));
349 #endif
351 printf (_(UT_SUPPORT));
356 void
357 print_usage (void)
359 printf (_("Usage:"));
360 printf ("%s -H <host> -l <query_address> [-p <server port>]\n", progname);
361 printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
362 printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");