check_dig.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*****************************************************************************
  2. *
  3. * Nagios check_dig plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2002-2007 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_dig plugin
  13. *
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. * $Id$
  29. *
  30. *****************************************************************************/
  31. /* Hackers note:
  32. * There are typecasts to (char *) from _("foo bar") in this file.
  33. * They prevent compiler warnings. Never (ever), permute strings obtained
  34. * that are typecast from (const char *) (which happens when --disable-nls)
  35. * because on some architectures those strings are in non-writable memory */
  36. const char *progname = "check_dig";
  37. const char *revision = "$Revision$";
  38. const char *copyright = "2002-2007";
  39. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  40. #include "common.h"
  41. #include "netutils.h"
  42. #include "utils.h"
  43. #include "runcmd.h"
  44. int process_arguments (int, char **);
  45. int validate_arguments (void);
  46. void print_help (void);
  47. void print_usage (void);
  48. #define UNDEFINED 0
  49. #define DEFAULT_PORT 53
  50. char *query_address = NULL;
  51. char *record_type = "A";
  52. char *expected_address = NULL;
  53. char *dns_server = NULL;
  54. int verbose = FALSE;
  55. int server_port = DEFAULT_PORT;
  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, popen_timeout_alarm_handler) == SIG_ERR)
  75. usage_va(_("Cannot catch SIGALRM"));
  76. if (process_arguments (argc, argv) == ERROR)
  77. usage_va(_("Could not parse arguments"));
  78. /* get the command to run */
  79. asprintf (&command_line, "%s @%s -p %d %s -t %s",
  80. PATH_TO_DIG, dns_server, server_port, query_address, record_type);
  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. /* If we get anything on STDERR, at least set warning */
  124. if(chld_err.buflen > 0) {
  125. result = max_state(result, STATE_WARNING);
  126. if(!msg) for(i = 0; i < chld_err.lines; i++) {
  127. msg = strchr(chld_err.line[0], ':');
  128. if(msg) {
  129. msg++;
  130. break;
  131. }
  132. }
  133. }
  134. microsec = deltime (tv);
  135. elapsed_time = (double)microsec / 1.0e6;
  136. if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
  137. result = STATE_CRITICAL;
  138. else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
  139. result = STATE_WARNING;
  140. printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
  141. state_text (result), elapsed_time,
  142. msg ? msg : _("Probably a non-existent host/domain"),
  143. fperfdata("time", elapsed_time, "s",
  144. (warning_interval>UNDEFINED?TRUE:FALSE),
  145. warning_interval,
  146. (critical_interval>UNDEFINED?TRUE:FALSE),
  147. critical_interval,
  148. TRUE, 0, FALSE, 0));
  149. return result;
  150. }
  151. /* process command-line arguments */
  152. int
  153. process_arguments (int argc, char **argv)
  154. {
  155. int c;
  156. int option = 0;
  157. static struct option longopts[] = {
  158. {"hostname", required_argument, 0, 'H'},
  159. {"query_address", required_argument, 0, 'l'},
  160. {"warning", required_argument, 0, 'w'},
  161. {"critical", required_argument, 0, 'c'},
  162. {"timeout", required_argument, 0, 't'},
  163. {"verbose", no_argument, 0, 'v'},
  164. {"version", no_argument, 0, 'V'},
  165. {"help", no_argument, 0, 'h'},
  166. {"record_type", required_argument, 0, 'T'},
  167. {"expected_address", required_argument, 0, 'a'},
  168. {"port", required_argument, 0, 'p'},
  169. {0, 0, 0, 0}
  170. };
  171. if (argc < 2)
  172. return ERROR;
  173. while (1) {
  174. c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:", longopts, &option);
  175. if (c == -1 || c == EOF)
  176. break;
  177. switch (c) {
  178. case 'h': /* help */
  179. print_help ();
  180. exit (STATE_OK);
  181. case 'V': /* version */
  182. print_revision (progname, revision);
  183. exit (STATE_OK);
  184. case 'H': /* hostname */
  185. host_or_die(optarg);
  186. dns_server = optarg;
  187. break;
  188. case 'p': /* server port */
  189. if (is_intpos (optarg)) {
  190. server_port = atoi (optarg);
  191. }
  192. else {
  193. usage_va(_("Port must be a positive integer - %s"), optarg);
  194. }
  195. break;
  196. case 'l': /* address to lookup */
  197. query_address = optarg;
  198. break;
  199. case 'w': /* warning */
  200. if (is_nonnegative (optarg)) {
  201. warning_interval = strtod (optarg, NULL);
  202. }
  203. else {
  204. usage_va(_("Warning interval must be a positive integer - %s"), optarg);
  205. }
  206. break;
  207. case 'c': /* critical */
  208. if (is_nonnegative (optarg)) {
  209. critical_interval = strtod (optarg, NULL);
  210. }
  211. else {
  212. usage_va(_("Critical interval must be a positive integer - %s"), optarg);
  213. }
  214. break;
  215. case 't': /* timeout */
  216. if (is_intnonneg (optarg)) {
  217. timeout_interval = atoi (optarg);
  218. }
  219. else {
  220. usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
  221. }
  222. break;
  223. case 'v': /* verbose */
  224. verbose = TRUE;
  225. break;
  226. case 'T':
  227. record_type = optarg;
  228. break;
  229. case 'a':
  230. expected_address = optarg;
  231. break;
  232. default: /* usage5 */
  233. usage5();
  234. }
  235. }
  236. c = optind;
  237. if (dns_server == NULL) {
  238. if (c < argc) {
  239. host_or_die(argv[c]);
  240. dns_server = argv[c];
  241. }
  242. else {
  243. dns_server = strdup ("127.0.0.1");
  244. }
  245. }
  246. return validate_arguments ();
  247. }
  248. int
  249. validate_arguments (void)
  250. {
  251. return OK;
  252. }
  253. void
  254. print_help (void)
  255. {
  256. char *myport;
  257. asprintf (&myport, "%d", DEFAULT_PORT);
  258. print_revision (progname, revision);
  259. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  260. printf (COPYRIGHT, copyright, email);
  261. printf (_("This plugin test the DNS service on the specified host using dig"));
  262. printf ("\n\n");
  263. print_usage ();
  264. printf (_(UT_HELP_VRSN));
  265. printf (_(UT_HOST_PORT), 'p', myport);
  266. printf (" %s\n","-l, --lookup=STRING");
  267. printf (" %s\n",_("machine name to lookup"));
  268. printf (" %s\n","-T, --record_type=STRING");
  269. printf (" %s\n",_("record type to lookup (default: A)"));
  270. printf (" %s\n","-a, --expected_address=STRING");
  271. printf (" %s\n",_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
  272. printf (_(UT_WARN_CRIT));
  273. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  274. printf (_(UT_VERBOSE));
  275. printf (_(UT_SUPPORT));
  276. }
  277. void
  278. print_usage (void)
  279. {
  280. printf (_("Usage:"));
  281. printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname);
  282. printf (" [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
  283. printf (" [-a <expected answer address>] [-v]\n");
  284. }