check_dig.c 10 KB

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