4
0

check_dig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 server_port = DEFAULT_PORT;
  55. int number_tries = DEFAULT_TRIES;
  56. double warning_interval = UNDEFINED;
  57. double critical_interval = UNDEFINED;
  58. struct timeval tv;
  59. int
  60. main (int argc, char **argv)
  61. {
  62. char *command_line;
  63. output chld_out, chld_err;
  64. char *msg = NULL;
  65. size_t i;
  66. char *t, *tt, *ex;
  67. long microsec;
  68. double elapsed_time;
  69. int result = STATE_UNKNOWN;
  70. int timeout_interval_dig;
  71. setlocale (LC_ALL, "");
  72. bindtextdomain (PACKAGE, LOCALEDIR);
  73. textdomain (PACKAGE);
  74. /* Set signal handling and alarm */
  75. if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR)
  76. usage_va(_("Cannot catch SIGALRM"));
  77. /* Parse extra opts if any */
  78. argv=np_extra_opts (&argc, argv, progname);
  79. if (process_arguments (argc, argv) == ERROR)
  80. usage_va(_("Could not parse arguments"));
  81. /* dig applies the timeout to each try, so we need to work around this */
  82. timeout_interval_dig = ceil((double) timeout_interval / (double) number_tries);
  83. /* get the command to run */
  84. xasprintf (&command_line, "%s %s %s -p %d @%s %s %s +tries=%d +time=%d",
  85. PATH_TO_DIG, dig_args, query_transport, server_port, dns_server, query_address, record_type, number_tries, timeout_interval_dig);
  86. alarm (timeout_interval);
  87. gettimeofday (&tv, NULL);
  88. ex = ( (expected_address != NULL) ? expected_address : query_address );
  89. if (verbose) {
  90. printf ("%s\n", command_line);
  91. printf (_("Looking for: '%s' length %lu\n"), ex, strlen( ex ));
  92. }
  93. /* run the command */
  94. if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
  95. result = STATE_WARNING;
  96. msg = (char *)_("dig returned an error status");
  97. }
  98. for(i = 0; i < chld_out.lines; i++) {
  99. /* the server is responding, we just got the host name... */
  100. /* ";; ANSWER SECTION:" is an exact, full-line match: */
  101. if (strcmp (chld_out.line[i], ";; ANSWER SECTION:") == 0) {
  102. /* loop through the whole 'ANSWER SECTION', which ends with a zero-length line */
  103. for(; i < chld_out.lines; i++) {
  104. if (verbose)
  105. printf ("%s\n", chld_out.line[i]);
  106. if (strlen(chld_out.line[i]) == 0) /* end of answer section */
  107. break;
  108. /* drill's answer section is tab-delimited, change them to spaces */
  109. t = chld_out.line[i];
  110. while ((tt = strchr(t, '\t')) != NULL) {
  111. *tt = ' '; /* TAB -> SPACE */
  112. t = ++tt; /* t -> remainder of string */
  113. }
  114. tt = t; /* tt points to the rightmost tab-delimited token */
  115. /* left match: does ex appear, followed by ". " ? */
  116. t = chld_out.line[i];
  117. if ((strcasestr( t, ex )) == t) {
  118. t += strlen( ex );
  119. if (strstr( t, ". " ) == t) {
  120. result = STATE_OK;
  121. msg = chld_out.line[i];
  122. break;
  123. }
  124. }
  125. t = tt; /* consider the right-side token, does it match ex? */
  126. if ( (strcasestr( t, ex ) == t) && (strlen( t ) == strlen( ex )) ) {
  127. result = STATE_OK;
  128. msg = chld_out.line[i];
  129. break;
  130. }
  131. }
  132. if (result == STATE_UNKNOWN) {
  133. msg = (char *)_("Server not found in ANSWER SECTION");
  134. result = STATE_WARNING;
  135. }
  136. /* we found the answer section, so break out of the loop */
  137. break;
  138. }
  139. }
  140. if (result == STATE_UNKNOWN) {
  141. msg = (char *)_("No ANSWER SECTION found");
  142. result = STATE_CRITICAL;
  143. }
  144. /* If we get anything on STDERR, at least set warning */
  145. if(chld_err.buflen > 0) {
  146. result = max_state(result, STATE_WARNING);
  147. if(!msg) for(i = 0; i < chld_err.lines; i++) {
  148. msg = strchr(chld_err.line[0], ':');
  149. if(msg) {
  150. msg++;
  151. break;
  152. }
  153. }
  154. }
  155. microsec = deltime (tv);
  156. elapsed_time = (double)microsec / 1.0e6;
  157. if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
  158. result = STATE_CRITICAL;
  159. else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
  160. result = STATE_WARNING;
  161. printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
  162. state_text (result), elapsed_time,
  163. msg ? msg : _("Probably a non-existent host/domain"),
  164. fperfdata("time", elapsed_time, "s",
  165. (warning_interval>UNDEFINED?TRUE:FALSE),
  166. warning_interval,
  167. (critical_interval>UNDEFINED?TRUE:FALSE),
  168. critical_interval,
  169. TRUE, 0, FALSE, 0));
  170. return result;
  171. }
  172. /* process command-line arguments */
  173. int
  174. process_arguments (int argc, char **argv)
  175. {
  176. int c;
  177. int option = 0;
  178. static struct option longopts[] = {
  179. {"hostname", required_argument, 0, 'H'},
  180. {"query_address", required_argument, 0, 'l'},
  181. {"warning", required_argument, 0, 'w'},
  182. {"critical", required_argument, 0, 'c'},
  183. {"timeout", required_argument, 0, 't'},
  184. {"retries", required_argument, 0, 'r'},
  185. {"dig-arguments", required_argument, 0, 'A'},
  186. {"verbose", no_argument, 0, 'v'},
  187. {"version", no_argument, 0, 'V'},
  188. {"help", no_argument, 0, 'h'},
  189. {"record_type", required_argument, 0, 'T'},
  190. {"expected_address", required_argument, 0, 'a'},
  191. {"port", required_argument, 0, 'p'},
  192. {"use-ipv4", no_argument, 0, '4'},
  193. {"use-ipv6", no_argument, 0, '6'},
  194. {0, 0, 0, 0}
  195. };
  196. if (argc < 2)
  197. return ERROR;
  198. while (1) {
  199. c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:46r:", longopts, &option);
  200. if (c == -1 || c == EOF)
  201. break;
  202. switch (c) {
  203. case 'h': /* help */
  204. print_help ();
  205. exit (STATE_OK);
  206. case 'V': /* version */
  207. print_revision (progname, NP_VERSION);
  208. exit (STATE_OK);
  209. case 'H': /* hostname */
  210. host_or_die(optarg);
  211. dns_server = optarg;
  212. break;
  213. case 'p': /* server port */
  214. if (is_intpos (optarg)) {
  215. server_port = atoi (optarg);
  216. }
  217. else {
  218. usage_va(_("Port must be a positive integer - %s"), optarg);
  219. }
  220. break;
  221. case 'l': /* address to lookup */
  222. query_address = optarg;
  223. break;
  224. case 'w': /* warning */
  225. if (is_nonnegative (optarg)) {
  226. warning_interval = strtod (optarg, NULL);
  227. }
  228. else {
  229. usage_va(_("Warning interval must be a positive integer - %s"), optarg);
  230. }
  231. break;
  232. case 'c': /* critical */
  233. if (is_nonnegative (optarg)) {
  234. critical_interval = strtod (optarg, NULL);
  235. }
  236. else {
  237. usage_va(_("Critical interval must be a positive integer - %s"), optarg);
  238. }
  239. break;
  240. case 't': /* timeout */
  241. timeout_interval = parse_timeout_string (optarg);
  242. break;
  243. case 'r': /* retires */
  244. if (is_intnonneg (optarg)) {
  245. number_tries = atoi (optarg);
  246. }
  247. else {
  248. usage_va(_("Number of retries must be a positive integer - %s"), optarg);
  249. }
  250. break;
  251. case 'A': /* dig arguments */
  252. dig_args = strdup(optarg);
  253. break;
  254. case 'v': /* verbose */
  255. verbose = TRUE;
  256. break;
  257. case 'T':
  258. record_type = optarg;
  259. break;
  260. case 'a':
  261. expected_address = optarg;
  262. break;
  263. case '4':
  264. query_transport = "-4";
  265. break;
  266. case '6':
  267. query_transport = "-6";
  268. break;
  269. default: /* usage5 */
  270. usage5();
  271. }
  272. }
  273. c = optind;
  274. if (dns_server == NULL) {
  275. if (c < argc) {
  276. host_or_die(argv[c]);
  277. dns_server = argv[c];
  278. }
  279. else {
  280. if (strcmp(query_transport,"-6") == 0)
  281. dns_server = strdup("::1");
  282. else
  283. dns_server = strdup ("127.0.0.1");
  284. }
  285. }
  286. return validate_arguments ();
  287. }
  288. int
  289. validate_arguments (void)
  290. {
  291. if (query_address != NULL)
  292. return OK;
  293. else
  294. return ERROR;
  295. }
  296. void
  297. print_help (void)
  298. {
  299. char *myport;
  300. xasprintf (&myport, "%d", DEFAULT_PORT);
  301. print_revision (progname, NP_VERSION);
  302. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  303. printf (COPYRIGHT, copyright, email);
  304. printf (_("This plugin test the DNS service on the specified host using dig"));
  305. printf ("\n\n");
  306. print_usage ();
  307. printf (UT_HELP_VRSN);
  308. printf (UT_EXTRA_OPTS);
  309. printf (UT_HOST_PORT, 'p', myport);
  310. printf (" %s\n","-4, --use-ipv4");
  311. printf (" %s\n",_("Force dig to only use IPv4 query transport"));
  312. printf (" %s\n","-6, --use-ipv6");
  313. printf (" %s\n",_("Force dig to only use IPv6 query transport"));
  314. printf (" %s\n","-l, --query_address=STRING");
  315. printf (" %s\n",_("Machine name to lookup"));
  316. printf (" %s\n","-T, --record_type=STRING");
  317. printf (" %s\n",_("Record type to lookup (default: A)"));
  318. printf (" %s\n","-a, --expected_address=STRING");
  319. printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
  320. printf (" %s\n",_("was in -l"));
  321. printf (" %s\n","-A, --dig-arguments=STRING");
  322. printf (" %s\n",_("Pass STRING as argument(s) to dig"));
  323. printf (" %s\n","-r, --retries=INTEGER");
  324. printf (" %s\n",_("Number of retries passed to dig, timeout is divided by this value (Default: 3)"));
  325. printf (UT_WARN_CRIT);
  326. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  327. printf (UT_VERBOSE);
  328. printf ("\n");
  329. printf ("%s\n", _("Examples:"));
  330. printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
  331. printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
  332. printf (UT_SUPPORT);
  333. }
  334. void
  335. print_usage (void)
  336. {
  337. printf ("%s\n", _("Usage:"));
  338. printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
  339. printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
  340. printf (" [-t <timeout>] [-r <retries>] [-a <expected answer address>] [-v]\n");
  341. }