check_dig.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. /* If we get anything on STDERR, at least set warning */
  127. if(chld_err.buflen > 0) {
  128. result = max_state(result, STATE_WARNING);
  129. if(!msg) for(i = 0; i < chld_err.lines; i++) {
  130. msg = strchr(chld_err.line[0], ':');
  131. if(msg) {
  132. msg++;
  133. break;
  134. }
  135. }
  136. }
  137. microsec = deltime (tv);
  138. elapsed_time = (double)microsec / 1.0e6;
  139. if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
  140. result = STATE_CRITICAL;
  141. else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
  142. result = STATE_WARNING;
  143. printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
  144. state_text (result), elapsed_time,
  145. msg ? msg : _("Probably a non-existent host/domain"),
  146. fperfdata("time", elapsed_time, "s",
  147. (warning_interval>UNDEFINED?TRUE:FALSE),
  148. warning_interval,
  149. (critical_interval>UNDEFINED?TRUE:FALSE),
  150. critical_interval,
  151. TRUE, 0, FALSE, 0));
  152. return result;
  153. }
  154. /* process command-line arguments */
  155. int
  156. process_arguments (int argc, char **argv)
  157. {
  158. int c;
  159. int option = 0;
  160. static struct option longopts[] = {
  161. {"hostname", required_argument, 0, 'H'},
  162. {"query_address", required_argument, 0, 'l'},
  163. {"warning", required_argument, 0, 'w'},
  164. {"critical", required_argument, 0, 'c'},
  165. {"timeout", required_argument, 0, 't'},
  166. {"dig-arguments", required_argument, 0, 'A'},
  167. {"verbose", no_argument, 0, 'v'},
  168. {"version", no_argument, 0, 'V'},
  169. {"help", no_argument, 0, 'h'},
  170. {"record_type", required_argument, 0, 'T'},
  171. {"expected_address", required_argument, 0, 'a'},
  172. {"port", required_argument, 0, 'p'},
  173. {0, 0, 0, 0}
  174. };
  175. if (argc < 2)
  176. return ERROR;
  177. while (1) {
  178. c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:p:a:A:", longopts, &option);
  179. if (c == -1 || c == EOF)
  180. break;
  181. switch (c) {
  182. case 'h': /* help */
  183. print_help ();
  184. exit (STATE_OK);
  185. case 'V': /* version */
  186. print_revision (progname, revision);
  187. exit (STATE_OK);
  188. case 'H': /* hostname */
  189. host_or_die(optarg);
  190. dns_server = optarg;
  191. break;
  192. case 'p': /* server port */
  193. if (is_intpos (optarg)) {
  194. server_port = atoi (optarg);
  195. }
  196. else {
  197. usage_va(_("Port must be a positive integer - %s"), optarg);
  198. }
  199. break;
  200. case 'l': /* address to lookup */
  201. query_address = optarg;
  202. break;
  203. case 'w': /* warning */
  204. if (is_nonnegative (optarg)) {
  205. warning_interval = strtod (optarg, NULL);
  206. }
  207. else {
  208. usage_va(_("Warning interval must be a positive integer - %s"), optarg);
  209. }
  210. break;
  211. case 'c': /* critical */
  212. if (is_nonnegative (optarg)) {
  213. critical_interval = strtod (optarg, NULL);
  214. }
  215. else {
  216. usage_va(_("Critical interval must be a positive integer - %s"), optarg);
  217. }
  218. break;
  219. case 't': /* timeout */
  220. if (is_intnonneg (optarg)) {
  221. timeout_interval = atoi (optarg);
  222. }
  223. else {
  224. usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
  225. }
  226. break;
  227. case 'A': /* dig arguments */
  228. dig_args = strdup(optarg);
  229. break;
  230. case 'v': /* verbose */
  231. verbose = TRUE;
  232. break;
  233. case 'T':
  234. record_type = optarg;
  235. break;
  236. case 'a':
  237. expected_address = optarg;
  238. break;
  239. default: /* usage5 */
  240. usage5();
  241. }
  242. }
  243. c = optind;
  244. if (dns_server == NULL) {
  245. if (c < argc) {
  246. host_or_die(argv[c]);
  247. dns_server = argv[c];
  248. }
  249. else {
  250. dns_server = strdup ("127.0.0.1");
  251. }
  252. }
  253. return validate_arguments ();
  254. }
  255. int
  256. validate_arguments (void)
  257. {
  258. return OK;
  259. }
  260. void
  261. print_help (void)
  262. {
  263. char *myport;
  264. asprintf (&myport, "%d", DEFAULT_PORT);
  265. print_revision (progname, revision);
  266. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  267. printf (COPYRIGHT, copyright, email);
  268. printf (_("This plugin test the DNS service on the specified host using dig"));
  269. printf ("\n\n");
  270. print_usage ();
  271. printf (_(UT_HELP_VRSN));
  272. printf (_(UT_EXTRA_OPTS));
  273. printf (_(UT_HOST_PORT), 'p', myport);
  274. printf (" %s\n","-l, --query_address=STRING");
  275. printf (" %s\n",_("Machine name to lookup"));
  276. printf (" %s\n","-T, --record_type=STRING");
  277. printf (" %s\n",_("Record type to lookup (default: A)"));
  278. printf (" %s\n","-a, --expected_address=STRING");
  279. printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
  280. printf (" %s\n",_("was in -l"));
  281. printf (" %s\n","-A, --dig-arguments=STRING");
  282. printf (" %s\n",_("Pass STRING as argument(s) to dig"));
  283. printf (_(UT_WARN_CRIT));
  284. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  285. printf (_(UT_VERBOSE));
  286. printf ("\n");
  287. printf ("%s\n", _("Examples:"));
  288. printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
  289. printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
  290. #ifdef NP_EXTRA_OPTS
  291. printf ("\n");
  292. printf ("%s\n", _("Notes:"));
  293. printf (_(UT_EXTRA_OPTS_NOTES));
  294. #endif
  295. printf (_(UT_SUPPORT));
  296. }
  297. void
  298. print_usage (void)
  299. {
  300. printf (_("Usage:"));
  301. printf ("%s -H <host> -l <query_address> [-p <server port>]\n", progname);
  302. printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
  303. printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");
  304. }