check_dig.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*****************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. $Id$
  14. *****************************************************************************/
  15. const char *progname = "check_dig";
  16. const char *revision = "$Revision$";
  17. const char *copyright = "2002-2004";
  18. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  19. #include "common.h"
  20. #include "netutils.h"
  21. #include "utils.h"
  22. #include "popen.h"
  23. int process_arguments (int, char **);
  24. int validate_arguments (void);
  25. void print_help (void);
  26. void print_usage (void);
  27. enum {
  28. UNDEFINED = 0,
  29. DEFAULT_PORT = 53
  30. };
  31. char *query_address = NULL;
  32. char *record_type = "A";
  33. char *expected_address = NULL;
  34. char *dns_server = NULL;
  35. int verbose = FALSE;
  36. int server_port = DEFAULT_PORT;
  37. double warning_interval = UNDEFINED;
  38. double critical_interval = UNDEFINED;
  39. struct timeval tv;
  40. int
  41. main (int argc, char **argv)
  42. {
  43. char input_buffer[MAX_INPUT_BUFFER];
  44. char *command_line;
  45. char *output;
  46. char *t;
  47. long microsec;
  48. double elapsed_time;
  49. int result = STATE_UNKNOWN;
  50. output = strdup ("");
  51. setlocale (LC_ALL, "");
  52. bindtextdomain (PACKAGE, LOCALEDIR);
  53. textdomain (PACKAGE);
  54. /* Set signal handling and alarm */
  55. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  56. usage4 (_("Cannot catch SIGALRM"));
  57. if (process_arguments (argc, argv) == ERROR)
  58. usage4 (_("Could not parse arguments"));
  59. /* get the command to run */
  60. asprintf (&command_line, "%s @%s -p %d %s -t %s",
  61. PATH_TO_DIG, dns_server, server_port, query_address, record_type);
  62. alarm (timeout_interval);
  63. gettimeofday (&tv, NULL);
  64. if (verbose) {
  65. printf ("%s\n", command_line);
  66. if(expected_address != NULL) {
  67. printf (_("Looking for: '%s'\n"), expected_address);
  68. } else {
  69. printf (_("Looking for: '%s'\n"), query_address);
  70. }
  71. }
  72. /* run the command */
  73. child_process = spopen (command_line);
  74. if (child_process == NULL) {
  75. printf (_("Could not open pipe: %s\n"), command_line);
  76. return STATE_UNKNOWN;
  77. }
  78. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  79. if (child_stderr == NULL)
  80. printf (_("Could not open stderr for %s\n"), command_line);
  81. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  82. /* the server is responding, we just got the host name... */
  83. if (strstr (input_buffer, ";; ANSWER SECTION:")) {
  84. /* loop through the whole 'ANSWER SECTION' */
  85. do {
  86. /* get the host address */
  87. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  88. break;
  89. if (strpbrk (input_buffer, "\r\n"))
  90. input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
  91. if (verbose && !strstr (input_buffer, ";; "))
  92. printf ("%s\n", input_buffer);
  93. if (expected_address==NULL && strstr (input_buffer, query_address) != NULL) {
  94. output = strdup(input_buffer);
  95. result = STATE_OK;
  96. }
  97. else if (expected_address != NULL && strstr (input_buffer, expected_address) != NULL) {
  98. output = strdup(input_buffer);
  99. result = STATE_OK;
  100. }
  101. /* Translate output TAB -> SPACE */
  102. t = output;
  103. while ((t = index(t, '\t')) != NULL)
  104. *t = ' ';
  105. } while (!strstr (input_buffer, ";; "));
  106. if (result == STATE_UNKNOWN) {
  107. asprintf (&output, _("Server not found in ANSWER SECTION"));
  108. result = STATE_WARNING;
  109. }
  110. }
  111. }
  112. if (result == STATE_UNKNOWN) {
  113. asprintf (&output, _("No ANSWER SECTION found"));
  114. }
  115. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  116. /* If we get anything on STDERR, at least set warning */
  117. result = max_state (result, STATE_WARNING);
  118. printf ("%s", input_buffer);
  119. if (strlen (output) == 0)
  120. output = strdup (1 + index (input_buffer, ':'));
  121. }
  122. (void) fclose (child_stderr);
  123. /* close the pipe */
  124. if (spclose (child_process)) {
  125. result = max_state (result, STATE_WARNING);
  126. if (strlen (output) == 0)
  127. asprintf (&output, _("dig returned an error status"));
  128. }
  129. microsec = deltime (tv);
  130. elapsed_time = (double)microsec / 1.0e6;
  131. if (output == NULL || strlen (output) == 0)
  132. asprintf (&output, _(" Probably a non-existent host/domain"));
  133. if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
  134. result = STATE_CRITICAL;
  135. else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
  136. result = STATE_WARNING;
  137. asprintf (&output, _("%.3f seconds response time (%s)"), elapsed_time, output);
  138. printf ("DNS %s - %s|%s\n",
  139. state_text (result), output,
  140. fperfdata("time", elapsed_time, "s",
  141. (warning_interval>UNDEFINED?TRUE:FALSE),
  142. warning_interval,
  143. (critical_interval>UNDEFINED?TRUE:FALSE),
  144. critical_interval,
  145. TRUE, 0, FALSE, 0));
  146. return result;
  147. }
  148. /* process command-line arguments */
  149. int
  150. process_arguments (int argc, char **argv)
  151. {
  152. int c;
  153. int option = 0;
  154. static struct option longopts[] = {
  155. {"hostname", required_argument, 0, 'H'},
  156. {"query_address", required_argument, 0, 'l'},
  157. {"warning", required_argument, 0, 'w'},
  158. {"critical", required_argument, 0, 'c'},
  159. {"timeout", required_argument, 0, 't'},
  160. {"verbose", no_argument, 0, 'v'},
  161. {"version", no_argument, 0, 'V'},
  162. {"help", no_argument, 0, 'h'},
  163. {"record_type", required_argument, 0, 'T'},
  164. {"expected_address", required_argument, 0, 'a'},
  165. {0, 0, 0, 0}
  166. };
  167. if (argc < 2)
  168. return ERROR;
  169. while (1) {
  170. c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:a:", longopts, &option);
  171. if (c == -1 || c == EOF)
  172. break;
  173. switch (c) {
  174. case '?': /* help */
  175. usage2 (_("Unknown argument"), optarg);
  176. case 'h': /* help */
  177. print_help ();
  178. exit (STATE_OK);
  179. case 'V': /* version */
  180. print_revision (progname, revision);
  181. exit (STATE_OK);
  182. case 'H': /* hostname */
  183. if (is_host (optarg)) {
  184. dns_server = optarg;
  185. }
  186. else {
  187. usage2 (_("Invalid hostname/address"), optarg);
  188. }
  189. break;
  190. case 'p': /* server port */
  191. if (is_intpos (optarg)) {
  192. server_port = atoi (optarg);
  193. }
  194. else {
  195. usage2 (_("Port must be a positive integer"), 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. usage2 (_("Warning interval must be a positive integer"), optarg);
  207. }
  208. break;
  209. case 'c': /* critical */
  210. if (is_nonnegative (optarg)) {
  211. critical_interval = strtod (optarg, NULL);
  212. }
  213. else {
  214. usage2 (_("Critical interval must be a positive integer"), optarg);
  215. }
  216. break;
  217. case 't': /* timeout */
  218. if (is_intnonneg (optarg)) {
  219. timeout_interval = atoi (optarg);
  220. }
  221. else {
  222. usage2 (_("Timeout interval must be a positive integer"), 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. }
  235. }
  236. c = optind;
  237. if (dns_server == NULL) {
  238. if (c < argc) {
  239. if (is_host (argv[c])) {
  240. dns_server = argv[c];
  241. }
  242. else {
  243. usage2 (_("Invalid hostname/address"), argv[c]);
  244. }
  245. }
  246. else {
  247. dns_server = strdup ("127.0.0.1");
  248. }
  249. }
  250. return validate_arguments ();
  251. }
  252. int
  253. validate_arguments (void)
  254. {
  255. return OK;
  256. }
  257. void
  258. print_help (void)
  259. {
  260. char *myport;
  261. asprintf (&myport, "%d", DEFAULT_PORT);
  262. print_revision (progname, revision);
  263. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  264. printf (COPYRIGHT, copyright, email);
  265. printf (_("Test the DNS service on the specified host using dig\n\n"));
  266. print_usage ();
  267. printf (_(UT_HELP_VRSN));
  268. printf (_(UT_HOST_PORT), 'P', myport);
  269. printf (_("\
  270. -l, --lookup=STRING\n\
  271. machine name to lookup\n"));
  272. printf (_("\
  273. -T, --record_type=STRING\n\
  274. record type to lookup (default: A)\n"));
  275. printf (_("\
  276. -a, --expected_address=STRING\n\
  277. an address expected to be in the answer section.\n\
  278. if not set, uses whatever was in -l\n"));
  279. printf (_(UT_WARN_CRIT));
  280. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  281. printf (_(UT_VERBOSE));
  282. printf (_(UT_SUPPORT));
  283. }
  284. void
  285. print_usage (void)
  286. {
  287. printf ("\
  288. Usage: %s -H host -l lookup [-p <server port>] [-T <query type>]\n\
  289. [-w <warning interval>] [-c <critical interval>] [-t <timeout>]\n\
  290. [-a <expected answer address>] [-v]\n", progname);
  291. }