check_dig.c 9.8 KB

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