check_dig.c 11 KB

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