check_dig.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*****************************************************************************
  2. *
  3. * Nagios check_dig plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_dig plugin
  13. *
  14. * License Information:
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. * $Id$
  31. *
  32. *****************************************************************************/
  33. /* Hackers note:
  34. * There are typecasts to (char *) from _("foo bar") in this file.
  35. * They prevent compiler warnings. Never (ever), permute strings obtained
  36. * that are typecast from (const char *) (which happens when --disable-nls)
  37. * because on some architectures those strings are in non-writable memory */
  38. const char *progname = "check_dig";
  39. const char *revision = "$Revision$";
  40. const char *copyright = "2002-2006";
  41. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  42. #include "common.h"
  43. #include "netutils.h"
  44. #include "utils.h"
  45. #include "runcmd.h"
  46. int process_arguments (int, char **);
  47. int validate_arguments (void);
  48. void print_help (void);
  49. void print_usage (void);
  50. #define UNDEFINED 0
  51. #define DEFAULT_PORT 53
  52. char *query_address = NULL;
  53. char *record_type = "A";
  54. char *expected_address = NULL;
  55. char *dns_server = NULL;
  56. int verbose = FALSE;
  57. int server_port = DEFAULT_PORT;
  58. double warning_interval = UNDEFINED;
  59. double critical_interval = UNDEFINED;
  60. struct timeval tv;
  61. int
  62. main (int argc, char **argv)
  63. {
  64. char *command_line;
  65. output chld_out, chld_err;
  66. char *msg = NULL;
  67. size_t i;
  68. char *t;
  69. long microsec;
  70. double elapsed_time;
  71. int result = STATE_UNKNOWN;
  72. setlocale (LC_ALL, "");
  73. bindtextdomain (PACKAGE, LOCALEDIR);
  74. textdomain (PACKAGE);
  75. /* Set signal handling and alarm */
  76. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  77. usage_va(_("Cannot catch SIGALRM"));
  78. if (process_arguments (argc, argv) == ERROR)
  79. usage_va(_("Could not parse arguments"));
  80. /* get the command to run */
  81. asprintf (&command_line, "%s @%s -p %d %s -t %s",
  82. PATH_TO_DIG, dns_server, server_port, query_address, record_type);
  83. alarm (timeout_interval);
  84. gettimeofday (&tv, NULL);
  85. if (verbose) {
  86. printf ("%s\n", command_line);
  87. if(expected_address != NULL) {
  88. printf (_("Looking for: '%s'\n"), expected_address);
  89. } else {
  90. printf (_("Looking for: '%s'\n"), query_address);
  91. }
  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. if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
  101. /* loop through the whole 'ANSWER SECTION' */
  102. for(; i < chld_out.lines; i++) {
  103. /* get the host address */
  104. if (verbose)
  105. printf ("%s\n", chld_out.line[i]);
  106. if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
  107. msg = chld_out.line[i];
  108. result = STATE_OK;
  109. /* Translate output TAB -> SPACE */
  110. t = msg;
  111. while ((t = strchr(t, '\t')) != NULL) *t = ' ';
  112. break;
  113. }
  114. }
  115. if (result == STATE_UNKNOWN) {
  116. msg = (char *)_("Server not found in ANSWER SECTION");
  117. result = STATE_WARNING;
  118. }
  119. /* we found the answer section, so break out of the loop */
  120. break;
  121. }
  122. }
  123. if (result == STATE_UNKNOWN)
  124. msg = (char *)_("No ANSWER SECTION found");
  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. {"verbose", no_argument, 0, 'v'},
  166. {"version", no_argument, 0, 'V'},
  167. {"help", no_argument, 0, 'h'},
  168. {"record_type", required_argument, 0, 'T'},
  169. {"expected_address", required_argument, 0, 'a'},
  170. {"port", required_argument, 0, 'p'},
  171. {0, 0, 0, 0}
  172. };
  173. if (argc < 2)
  174. return ERROR;
  175. while (1) {
  176. c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:", longopts, &option);
  177. if (c == -1 || c == EOF)
  178. break;
  179. switch (c) {
  180. case 'h': /* help */
  181. print_help ();
  182. exit (STATE_OK);
  183. case 'V': /* version */
  184. print_revision (progname, revision);
  185. exit (STATE_OK);
  186. case 'H': /* hostname */
  187. host_or_die(optarg);
  188. dns_server = optarg;
  189. break;
  190. case 'p': /* server port */
  191. if (is_intpos (optarg)) {
  192. server_port = atoi (optarg);
  193. }
  194. else {
  195. usage_va(_("Port must be a positive integer - %s"), optarg);
  196. }
  197. break;
  198. case 'l': /* address to lookup */
  199. query_address = optarg;
  200. break;
  201. case 'w': /* warning */
  202. if (is_nonnegative (optarg)) {
  203. warning_interval = strtod (optarg, NULL);
  204. }
  205. else {
  206. usage_va(_("Warning interval must be a positive integer - %s"), optarg);
  207. }
  208. break;
  209. case 'c': /* critical */
  210. if (is_nonnegative (optarg)) {
  211. critical_interval = strtod (optarg, NULL);
  212. }
  213. else {
  214. usage_va(_("Critical interval must be a positive integer - %s"), optarg);
  215. }
  216. break;
  217. case 't': /* timeout */
  218. if (is_intnonneg (optarg)) {
  219. timeout_interval = atoi (optarg);
  220. }
  221. else {
  222. usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
  223. }
  224. break;
  225. case 'v': /* verbose */
  226. verbose = TRUE;
  227. break;
  228. case 'T':
  229. record_type = optarg;
  230. break;
  231. case 'a':
  232. expected_address = optarg;
  233. break;
  234. default: /* usage5 */
  235. usage5();
  236. }
  237. }
  238. c = optind;
  239. if (dns_server == NULL) {
  240. if (c < argc) {
  241. host_or_die(argv[c]);
  242. dns_server = argv[c];
  243. }
  244. else {
  245. dns_server = strdup ("127.0.0.1");
  246. }
  247. }
  248. return validate_arguments ();
  249. }
  250. int
  251. validate_arguments (void)
  252. {
  253. return OK;
  254. }
  255. void
  256. print_help (void)
  257. {
  258. char *myport;
  259. asprintf (&myport, "%d", DEFAULT_PORT);
  260. print_revision (progname, revision);
  261. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  262. printf (COPYRIGHT, copyright, email);
  263. printf (_("This plugin test the DNS service on the specified host using dig"));
  264. printf ("\n\n");
  265. print_usage ();
  266. printf (_(UT_HELP_VRSN));
  267. printf (_(UT_HOST_PORT), 'p', myport);
  268. printf (" %s\n","-l, --lookup=STRING");
  269. printf (" %s\n",_("machine name to lookup"));
  270. printf (" %s\n","-T, --record_type=STRING");
  271. printf (" %s\n",_("record type to lookup (default: A)"));
  272. printf (" %s\n","-a, --expected_address=STRING");
  273. printf (" %s\n",_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
  274. printf (_(UT_WARN_CRIT));
  275. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  276. printf (_(UT_VERBOSE));
  277. printf (_(UT_SUPPORT));
  278. }
  279. void
  280. print_usage (void)
  281. {
  282. printf (_("Usage:"));
  283. printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname);
  284. printf (" [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
  285. printf (" [-a <expected answer address>] [-v]\n");
  286. }