check_dig.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*****************************************************************************
  2. *
  3. * Nagios check_dig plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2002-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_dig plugin
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *
  27. *****************************************************************************/
  28. /* Hackers note:
  29. * There are typecasts to (char *) from _("foo bar") in this file.
  30. * They prevent compiler warnings. Never (ever), permute strings obtained
  31. * that are typecast from (const char *) (which happens when --disable-nls)
  32. * because on some architectures those strings are in non-writable memory */
  33. const char *progname = "check_dig";
  34. const char *copyright = "2002-2014";
  35. const char *email = "devel@nagios-plugins.org";
  36. #include "common.h"
  37. #include "netutils.h"
  38. #include "utils.h"
  39. #include "runcmd.h"
  40. int process_arguments (int, char **);
  41. int validate_arguments (void);
  42. void print_help (void);
  43. void print_usage (void);
  44. #define UNDEFINED 0
  45. #define DEFAULT_PORT 53
  46. #define DEFAULT_TRIES 3
  47. char *query_address = NULL;
  48. char *record_type = "A";
  49. char *expected_address = NULL;
  50. char *dns_server = NULL;
  51. char *dig_args = "";
  52. char *query_transport = "";
  53. int verbose = FALSE;
  54. int exact = FALSE;
  55. int server_port = DEFAULT_PORT;
  56. int number_tries = DEFAULT_TRIES;
  57. double warning_interval = UNDEFINED;
  58. double critical_interval = UNDEFINED;
  59. struct timeval tv;
  60. int
  61. main (int argc, char **argv)
  62. {
  63. char *command_line;
  64. output chld_out, chld_err;
  65. char *msg = NULL;
  66. size_t i;
  67. char *t, *tt, *ex;
  68. long microsec;
  69. double elapsed_time;
  70. int result = STATE_UNKNOWN;
  71. int timeout_interval_dig;
  72. setlocale (LC_ALL, "");
  73. bindtextdomain (PACKAGE, LOCALEDIR);
  74. textdomain (PACKAGE);
  75. /* Set signal handling and alarm */
  76. if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR)
  77. usage_va(_("Cannot catch SIGALRM"));
  78. /* Parse extra opts if any */
  79. argv=np_extra_opts (&argc, argv, progname);
  80. if (process_arguments (argc, argv) == ERROR)
  81. usage_va(_("Could not parse arguments"));
  82. /* dig applies the timeout to each try, so we need to work around this */
  83. timeout_interval_dig = ceil((double) timeout_interval / (double) number_tries);
  84. /* get the command to run */
  85. xasprintf (&command_line, "%s %s %s -p %d @%s %s %s +tries=%d +time=%d",
  86. PATH_TO_DIG, dig_args, query_transport, server_port, dns_server, query_address, record_type, number_tries, timeout_interval_dig);
  87. alarm (timeout_interval);
  88. gettimeofday (&tv, NULL);
  89. ex = ( (expected_address != NULL) ? expected_address : query_address );
  90. if (verbose) {
  91. printf ("%s\n", command_line);
  92. printf (_("Looking for: '%s' length %lu\n"), ex, strlen( ex ));
  93. }
  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. }
  99. for(i = 0; i < chld_out.lines; i++) {
  100. /* the server is responding, we just got the host name... */
  101. /* ";; ANSWER SECTION:" is an exact, full-line match: */
  102. if (strcmp (chld_out.line[i], ";; ANSWER SECTION:") == 0) {
  103. /* loop through the whole 'ANSWER SECTION', which ends with a zero-length line */
  104. for(; i < chld_out.lines; i++) {
  105. if (verbose)
  106. printf ("%s\n", chld_out.line[i]);
  107. if (strlen(chld_out.line[i]) == 0) /* end of answer section */
  108. break;
  109. /* drill's answer section is tab-delimited, change them to spaces */
  110. t = chld_out.line[i];
  111. while ((tt = strchr(t, '\t')) != NULL) {
  112. *tt = ' '; /* TAB -> SPACE */
  113. t = ++tt; /* t -> remainder of string */
  114. }
  115. tt = t; /* tt points to the rightmost tab-delimited token */
  116. /* left match: does ex appear, followed by ". " ? */
  117. t = chld_out.line[i];
  118. if ((strcasestr( t, ex )) == t) {
  119. t += strlen( ex );
  120. if (strstr( t, ". " ) == t) {
  121. result = STATE_OK;
  122. msg = chld_out.line[i];
  123. break;
  124. }
  125. }
  126. t = tt; /* consider the right-side token, does it match ex? */
  127. if ( (!exact && strcasestr( t, ex)) || ((strcasestr( t, ex ) == t) && (strlen( t ) == strlen( ex ))) ) {
  128. result = STATE_OK;
  129. msg = chld_out.line[i];
  130. break;
  131. }
  132. }
  133. if (result == STATE_UNKNOWN) {
  134. msg = (char *)_("Server not found in ANSWER SECTION");
  135. result = STATE_WARNING;
  136. }
  137. /* we found the answer section, so break out of the loop */
  138. break;
  139. }
  140. }
  141. if (result == STATE_UNKNOWN) {
  142. msg = (char *)_("No ANSWER SECTION found");
  143. result = STATE_CRITICAL;
  144. }
  145. /* If we get anything on STDERR, at least set warning */
  146. if(chld_err.buflen > 0) {
  147. result = max_state(result, STATE_WARNING);
  148. if(!msg) for(i = 0; i < chld_err.lines; i++) {
  149. msg = strchr(chld_err.line[0], ':');
  150. if(msg) {
  151. msg++;
  152. break;
  153. }
  154. }
  155. }
  156. microsec = deltime (tv);
  157. elapsed_time = (double)microsec / 1.0e6;
  158. if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
  159. result = STATE_CRITICAL;
  160. else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
  161. result = STATE_WARNING;
  162. printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
  163. state_text (result), elapsed_time,
  164. msg ? msg : _("Probably a non-existent host/domain"),
  165. fperfdata("time", elapsed_time, "s",
  166. (warning_interval>UNDEFINED?TRUE:FALSE),
  167. warning_interval,
  168. (critical_interval>UNDEFINED?TRUE:FALSE),
  169. critical_interval,
  170. TRUE, 0, FALSE, 0));
  171. return result;
  172. }
  173. /* process command-line arguments */
  174. int
  175. process_arguments (int argc, char **argv)
  176. {
  177. int c;
  178. int option = 0;
  179. static struct option longopts[] = {
  180. {"hostname", required_argument, 0, 'H'},
  181. {"query_address", required_argument, 0, 'l'},
  182. {"warning", required_argument, 0, 'w'},
  183. {"critical", required_argument, 0, 'c'},
  184. {"timeout", required_argument, 0, 't'},
  185. {"retries", required_argument, 0, 'r'},
  186. {"dig-arguments", required_argument, 0, 'A'},
  187. {"verbose", no_argument, 0, 'v'},
  188. {"version", no_argument, 0, 'V'},
  189. {"help", no_argument, 0, 'h'},
  190. {"exact", no_argument, 0, 'e'},
  191. {"record_type", required_argument, 0, 'T'},
  192. {"expected_address", required_argument, 0, 'a'},
  193. {"port", required_argument, 0, 'p'},
  194. {"use-ipv4", no_argument, 0, '4'},
  195. {"use-ipv6", no_argument, 0, '6'},
  196. {0, 0, 0, 0}
  197. };
  198. if (argc < 2)
  199. return ERROR;
  200. while (1) {
  201. c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:46r:e", longopts, &option);
  202. if (c == -1 || c == EOF)
  203. break;
  204. switch (c) {
  205. case 'h': /* help */
  206. print_help ();
  207. exit (STATE_OK);
  208. case 'V': /* version */
  209. print_revision (progname, NP_VERSION);
  210. exit (STATE_OK);
  211. case 'H': /* hostname */
  212. host_or_die(optarg);
  213. dns_server = optarg;
  214. break;
  215. case 'p': /* server port */
  216. if (is_intpos (optarg)) {
  217. server_port = atoi (optarg);
  218. }
  219. else {
  220. usage_va(_("Port must be a positive integer - %s"), optarg);
  221. }
  222. break;
  223. case 'l': /* address to lookup */
  224. query_address = optarg;
  225. break;
  226. case 'w': /* warning */
  227. if (is_nonnegative (optarg)) {
  228. warning_interval = strtod (optarg, NULL);
  229. }
  230. else {
  231. usage_va(_("Warning interval must be a positive integer - %s"), optarg);
  232. }
  233. break;
  234. case 'c': /* critical */
  235. if (is_nonnegative (optarg)) {
  236. critical_interval = strtod (optarg, NULL);
  237. }
  238. else {
  239. usage_va(_("Critical interval must be a positive integer - %s"), optarg);
  240. }
  241. break;
  242. case 't': /* timeout */
  243. timeout_interval = parse_timeout_string (optarg);
  244. break;
  245. case 'r': /* retires */
  246. if (is_intnonneg (optarg)) {
  247. number_tries = atoi (optarg);
  248. }
  249. else {
  250. usage_va(_("Number of retries must be a positive integer - %s"), optarg);
  251. }
  252. break;
  253. case 'A': /* dig arguments */
  254. dig_args = strdup(optarg);
  255. break;
  256. case 'v': /* verbose */
  257. verbose = TRUE;
  258. break;
  259. case 'e':
  260. exact = TRUE;
  261. break;
  262. case 'T':
  263. record_type = optarg;
  264. break;
  265. case 'a':
  266. expected_address = optarg;
  267. break;
  268. case '4':
  269. query_transport = "-4";
  270. break;
  271. case '6':
  272. query_transport = "-6";
  273. break;
  274. default: /* usage5 */
  275. usage5();
  276. }
  277. }
  278. c = optind;
  279. if (dns_server == NULL) {
  280. if (c < argc) {
  281. host_or_die(argv[c]);
  282. dns_server = argv[c];
  283. }
  284. else {
  285. if (strcmp(query_transport,"-6") == 0)
  286. dns_server = strdup("::1");
  287. else
  288. dns_server = strdup ("127.0.0.1");
  289. }
  290. }
  291. return validate_arguments ();
  292. }
  293. int
  294. validate_arguments (void)
  295. {
  296. if (query_address != NULL)
  297. return OK;
  298. else
  299. return ERROR;
  300. }
  301. void
  302. print_help (void)
  303. {
  304. char *myport;
  305. xasprintf (&myport, "%d", DEFAULT_PORT);
  306. print_revision (progname, NP_VERSION);
  307. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  308. printf (COPYRIGHT, copyright, email);
  309. printf (_("This plugin test the DNS service on the specified host using dig"));
  310. printf ("\n\n");
  311. print_usage ();
  312. printf (UT_HELP_VRSN);
  313. printf (UT_EXTRA_OPTS);
  314. printf (UT_HOST_PORT, 'p', myport);
  315. printf (" %s\n","-4, --use-ipv4");
  316. printf (" %s\n",_("Force dig to only use IPv4 query transport"));
  317. printf (" %s\n","-6, --use-ipv6");
  318. printf (" %s\n",_("Force dig to only use IPv6 query transport"));
  319. printf (" %s\n","-l, --query_address=STRING");
  320. printf (" %s\n",_("Machine name to lookup"));
  321. printf (" %s\n","-T, --record_type=STRING");
  322. printf (" %s\n",_("Record type to lookup (default: A)"));
  323. printf (" %s\n","-a, --expected_address=STRING");
  324. printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
  325. printf (" %s\n",_("was in -l"));
  326. printf (" %s\n","-A, --dig-arguments=STRING");
  327. printf (" %s\n",_("Pass STRING as argument(s) to dig"));
  328. printf (" %s\n","-r, --retries=INTEGER");
  329. printf (" %s\n",_("Number of retries passed to dig, timeout is divided by this value (Default: 3)"));
  330. printf (" %s\n","-e, --exact");
  331. printf (" %s\n",_("Match records exactly. If not set, fuzzy matches."));
  332. printf (UT_WARN_CRIT);
  333. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  334. printf (UT_VERBOSE);
  335. printf ("\n");
  336. printf ("%s\n", _("Examples:"));
  337. printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
  338. printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
  339. printf (UT_SUPPORT);
  340. }
  341. void
  342. print_usage (void)
  343. {
  344. printf ("%s\n", _("Usage:"));
  345. printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
  346. printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
  347. printf (" [-t <timeout>] [-r <retries>] [-a <expected answer address>] [-v]\n");
  348. }