check_dig.c 10 KB

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