check_dig.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 = "nagiosplug-devel@lists.sourceforge.net";
  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. int verbose = FALSE;
  52. int server_port = DEFAULT_PORT;
  53. double warning_interval = UNDEFINED;
  54. double critical_interval = UNDEFINED;
  55. struct timeval tv;
  56. int
  57. main (int argc, char **argv)
  58. {
  59. char *command_line;
  60. output chld_out, chld_err;
  61. char *msg = NULL;
  62. size_t i;
  63. char *t;
  64. long microsec;
  65. double elapsed_time;
  66. int result = STATE_UNKNOWN;
  67. setlocale (LC_ALL, "");
  68. bindtextdomain (PACKAGE, LOCALEDIR);
  69. textdomain (PACKAGE);
  70. /* Set signal handling and alarm */
  71. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  72. usage_va(_("Cannot catch SIGALRM"));
  73. /* Parse extra opts if any */
  74. argv=np_extra_opts (&argc, argv, progname);
  75. if (process_arguments (argc, argv) == ERROR)
  76. usage_va(_("Could not parse arguments"));
  77. /* get the command to run */
  78. asprintf (&command_line, "%s @%s -p %d %s -t %s %s",
  79. PATH_TO_DIG, dns_server, server_port, query_address, record_type, dig_args);
  80. alarm (timeout_interval);
  81. gettimeofday (&tv, NULL);
  82. if (verbose) {
  83. printf ("%s\n", command_line);
  84. if(expected_address != NULL) {
  85. printf (_("Looking for: '%s'\n"), expected_address);
  86. } else {
  87. printf (_("Looking for: '%s'\n"), query_address);
  88. }
  89. }
  90. /* run the command */
  91. if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
  92. result = STATE_WARNING;
  93. msg = (char *)_("dig returned an error status");
  94. }
  95. for(i = 0; i < chld_out.lines; i++) {
  96. /* the server is responding, we just got the host name... */
  97. if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
  98. /* loop through the whole 'ANSWER SECTION' */
  99. for(; i < chld_out.lines; i++) {
  100. /* get the host address */
  101. if (verbose)
  102. printf ("%s\n", chld_out.line[i]);
  103. if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
  104. msg = chld_out.line[i];
  105. result = STATE_OK;
  106. /* Translate output TAB -> SPACE */
  107. t = msg;
  108. while ((t = strchr(t, '\t')) != NULL) *t = ' ';
  109. break;
  110. }
  111. }
  112. if (result == STATE_UNKNOWN) {
  113. msg = (char *)_("Server not found in ANSWER SECTION");
  114. result = STATE_WARNING;
  115. }
  116. /* we found the answer section, so break out of the loop */
  117. break;
  118. }
  119. }
  120. if (result == STATE_UNKNOWN) {
  121. msg = (char *)_("No ANSWER SECTION found");
  122. result = STATE_CRITICAL;
  123. }
  124. /* If we get anything on STDERR, at least set warning */
  125. if(chld_err.buflen > 0) {
  126. result = max_state(result, STATE_WARNING);
  127. if(!msg) for(i = 0; i < chld_err.lines; i++) {
  128. msg = strchr(chld_err.line[0], ':');
  129. if(msg) {
  130. msg++;
  131. break;
  132. }
  133. }
  134. }
  135. microsec = deltime (tv);
  136. elapsed_time = (double)microsec / 1.0e6;
  137. if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
  138. result = STATE_CRITICAL;
  139. else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
  140. result = STATE_WARNING;
  141. printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
  142. state_text (result), elapsed_time,
  143. msg ? msg : _("Probably a non-existent host/domain"),
  144. fperfdata("time", elapsed_time, "s",
  145. (warning_interval>UNDEFINED?TRUE:FALSE),
  146. warning_interval,
  147. (critical_interval>UNDEFINED?TRUE:FALSE),
  148. critical_interval,
  149. TRUE, 0, FALSE, 0));
  150. return result;
  151. }
  152. /* process command-line arguments */
  153. int
  154. process_arguments (int argc, char **argv)
  155. {
  156. int c;
  157. int option = 0;
  158. static struct option longopts[] = {
  159. {"hostname", required_argument, 0, 'H'},
  160. {"query_address", required_argument, 0, 'l'},
  161. {"warning", required_argument, 0, 'w'},
  162. {"critical", required_argument, 0, 'c'},
  163. {"timeout", required_argument, 0, 't'},
  164. {"dig-arguments", required_argument, 0, 'A'},
  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: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, NP_VERSION);
  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 'A': /* dig arguments */
  226. dig_args = strdup(optarg);
  227. break;
  228. case 'v': /* verbose */
  229. verbose = TRUE;
  230. break;
  231. case 'T':
  232. record_type = optarg;
  233. break;
  234. case 'a':
  235. expected_address = optarg;
  236. break;
  237. default: /* usage5 */
  238. usage5();
  239. }
  240. }
  241. c = optind;
  242. if (dns_server == NULL) {
  243. if (c < argc) {
  244. host_or_die(argv[c]);
  245. dns_server = argv[c];
  246. }
  247. else {
  248. dns_server = strdup ("127.0.0.1");
  249. }
  250. }
  251. return validate_arguments ();
  252. }
  253. int
  254. validate_arguments (void)
  255. {
  256. if (query_address != NULL)
  257. return OK;
  258. else
  259. return ERROR;
  260. }
  261. void
  262. print_help (void)
  263. {
  264. char *myport;
  265. asprintf (&myport, "%d", DEFAULT_PORT);
  266. print_revision (progname, NP_VERSION);
  267. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  268. printf (COPYRIGHT, copyright, email);
  269. printf (_("This plugin test the DNS service on the specified host using dig"));
  270. printf ("\n\n");
  271. print_usage ();
  272. printf (UT_HELP_VRSN);
  273. printf (UT_EXTRA_OPTS);
  274. printf (UT_HOST_PORT, 'p', myport);
  275. printf (" %s\n","-l, --query_address=STRING");
  276. printf (" %s\n",_("Machine name to lookup"));
  277. printf (" %s\n","-T, --record_type=STRING");
  278. printf (" %s\n",_("Record type to lookup (default: A)"));
  279. printf (" %s\n","-a, --expected_address=STRING");
  280. printf (" %s\n",_("An address expected to be in the answer section. If not set, uses whatever"));
  281. printf (" %s\n",_("was in -l"));
  282. printf (" %s\n","-A, --dig-arguments=STRING");
  283. printf (" %s\n",_("Pass STRING as argument(s) to dig"));
  284. printf (UT_WARN_CRIT);
  285. printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  286. printf (UT_VERBOSE);
  287. printf ("\n");
  288. printf ("%s\n", _("Examples:"));
  289. printf (" %s\n", "check_dig -H DNSSERVER -l www.example.com -A \"+tcp\"");
  290. printf (" %s\n", "This will send a tcp query to DNSSERVER for www.example.com");
  291. #ifdef NP_EXTRA_OPTS
  292. printf ("\n");
  293. printf ("%s\n", _("Notes:"));
  294. printf (UT_EXTRA_OPTS_NOTES);
  295. #endif
  296. printf (UT_SUPPORT);
  297. }
  298. void
  299. print_usage (void)
  300. {
  301. printf (_("Usage:"));
  302. printf ("%s -l <query_address> [-H <host>] [-p <server port>]\n", progname);
  303. printf (" [-T <query type>] [-w <warning interval>] [-c <critical interval>]\n");
  304. printf (" [-t <timeout>] [-a <expected answer address>] [-v]\n");
  305. }