check_dig.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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-2003";
  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. long microsec;
  47. double elapsed_time;
  48. int result = STATE_UNKNOWN;
  49. output = strdup ("");
  50. setlocale (LC_ALL, "");
  51. bindtextdomain (PACKAGE, LOCALEDIR);
  52. textdomain (PACKAGE);
  53. /* Set signal handling and alarm */
  54. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  55. usage4 (_("Cannot catch SIGALRM"));
  56. if (process_arguments (argc, argv) != TRUE)
  57. usage4 (_("Could not parse arguments"));
  58. /* get the command to run */
  59. asprintf (&command_line, "%s @%s -p %d %s -t %s",
  60. PATH_TO_DIG, dns_server, server_port, query_address, record_type);
  61. alarm (timeout_interval);
  62. gettimeofday (&tv, NULL);
  63. if (verbose) {
  64. printf ("%s\n", command_line);
  65. if(expected_address != NULL) {
  66. printf ("Looking for: '%s'\n", expected_address);
  67. } else {
  68. printf ("Looking for: '%s'\n", query_address);
  69. }
  70. }
  71. /* run the command */
  72. child_process = spopen (command_line);
  73. if (child_process == NULL) {
  74. printf (_("Could not open pipe: %s\n"), command_line);
  75. return STATE_UNKNOWN;
  76. }
  77. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  78. if (child_stderr == NULL)
  79. printf (_("Could not open stderr for %s\n"), command_line);
  80. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  81. /* the server is responding, we just got the host name... */
  82. if (strstr (input_buffer, ";; ANSWER SECTION:")) {
  83. /* loop through the whole 'ANSWER SECTION' */
  84. do {
  85. /* get the host address */
  86. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  87. break;
  88. if (strpbrk (input_buffer, "\r\n"))
  89. input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
  90. if (verbose && !strstr (input_buffer, ";; "))
  91. printf ("%s\n", input_buffer);
  92. if (expected_address==NULL && strstr (input_buffer, query_address) != NULL) {
  93. output = strdup(input_buffer);
  94. result = STATE_OK;
  95. }
  96. else if (expected_address != NULL && strstr (input_buffer, expected_address) != NULL) {
  97. output = strdup(input_buffer);
  98. result = STATE_OK;
  99. }
  100. } while (!strstr (input_buffer, ";; "));
  101. if (result == STATE_UNKNOWN) {
  102. asprintf (&output, _("Server not found in ANSWER SECTION"));
  103. result = STATE_WARNING;
  104. }
  105. }
  106. }
  107. if (result == STATE_UNKNOWN) {
  108. asprintf (&output, _("No ANSWER SECTION found"));
  109. }
  110. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  111. /* If we get anything on STDERR, at least set warning */
  112. result = max_state (result, STATE_WARNING);
  113. printf ("%s", input_buffer);
  114. if (strlen (output) == 0)
  115. output = strdup (1 + index (input_buffer, ':'));
  116. }
  117. (void) fclose (child_stderr);
  118. /* close the pipe */
  119. if (spclose (child_process)) {
  120. result = max_state (result, STATE_WARNING);
  121. if (strlen (output) == 0)
  122. asprintf (&output, _("dig returned an error status"));
  123. }
  124. microsec = deltime (tv);
  125. elapsed_time = (double)microsec / 1.0e6;
  126. if (output == NULL || strlen (output) == 0)
  127. asprintf (&output, _(" Probably a non-existent host/domain"));
  128. if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
  129. result = STATE_CRITICAL;
  130. else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
  131. result = STATE_WARNING;
  132. asprintf (&output, _("%.3f seconds response time (%s)"), elapsed_time, output);
  133. printf ("DNS %s - %s|%s\n",
  134. state_text (result), output,
  135. fperfdata("time", elapsed_time, "s",
  136. (warning_interval>UNDEFINED?TRUE:FALSE),
  137. warning_interval,
  138. (critical_interval>UNDEFINED?TRUE:FALSE),
  139. critical_interval,
  140. TRUE, 0, FALSE, 0));
  141. return result;
  142. }
  143. /* process command-line arguments */
  144. int
  145. process_arguments (int argc, char **argv)
  146. {
  147. int c;
  148. int option = 0;
  149. static struct option longopts[] = {
  150. {"hostname", required_argument, 0, 'H'},
  151. {"query_address", required_argument, 0, 'l'},
  152. {"warning", required_argument, 0, 'w'},
  153. {"critical", required_argument, 0, 'c'},
  154. {"timeout", required_argument, 0, 't'},
  155. {"verbose", no_argument, 0, 'v'},
  156. {"version", no_argument, 0, 'V'},
  157. {"help", no_argument, 0, 'h'},
  158. {"record_type", required_argument, 0, 'T'},
  159. {"expected_address", required_argument, 0, 'a'},
  160. {0, 0, 0, 0}
  161. };
  162. if (argc < 2)
  163. return ERROR;
  164. while (1) {
  165. c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:a:", longopts, &option);
  166. if (c == -1 || c == EOF)
  167. break;
  168. switch (c) {
  169. case '?': /* help */
  170. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  171. print_usage ();
  172. exit (STATE_UNKNOWN);
  173. case 'h': /* help */
  174. print_help ();
  175. exit (STATE_OK);
  176. case 'V': /* version */
  177. print_revision (progname, "$Revision$");
  178. exit (STATE_OK);
  179. case 'H': /* hostname */
  180. if (is_host (optarg)) {
  181. dns_server = optarg;
  182. }
  183. else {
  184. usage2 (_("Invalid hostname/address"), optarg);
  185. }
  186. break;
  187. case 'p': /* server port */
  188. if (is_intpos (optarg)) {
  189. server_port = atoi (optarg);
  190. }
  191. else {
  192. usage2 (_("Port must be a positive integer"), optarg);
  193. }
  194. break;
  195. case 'l': /* address to lookup */
  196. query_address = optarg;
  197. break;
  198. case 'w': /* warning */
  199. if (is_nonnegative (optarg)) {
  200. warning_interval = strtod (optarg, NULL);
  201. }
  202. else {
  203. usage2 (_("Warning interval must be a positive integer"), optarg);
  204. }
  205. break;
  206. case 'c': /* critical */
  207. if (is_nonnegative (optarg)) {
  208. critical_interval = strtod (optarg, NULL);
  209. }
  210. else {
  211. usage2 (_("Critical interval must be a positive integer"), optarg);
  212. }
  213. break;
  214. case 't': /* timeout */
  215. if (is_intnonneg (optarg)) {
  216. timeout_interval = atoi (optarg);
  217. }
  218. else {
  219. usage2 (_("Timeout interval must be a positive integer"), optarg);
  220. }
  221. break;
  222. case 'v': /* verbose */
  223. verbose = TRUE;
  224. break;
  225. case 'T':
  226. record_type = optarg;
  227. break;
  228. case 'a':
  229. expected_address = optarg;
  230. break;
  231. }
  232. }
  233. c = optind;
  234. if (dns_server == NULL) {
  235. if (c < argc) {
  236. if (is_host (argv[c])) {
  237. dns_server = argv[c];
  238. }
  239. else {
  240. usage2 (_("Invalid hostname/address"), argv[c]);
  241. }
  242. }
  243. else {
  244. dns_server = strdup ("127.0.0.1");
  245. }
  246. }
  247. return validate_arguments ();
  248. }
  249. int
  250. validate_arguments (void)
  251. {
  252. return OK;
  253. }
  254. void
  255. print_help (void)
  256. {
  257. char *myport;
  258. asprintf (&myport, "%d", DEFAULT_PORT);
  259. print_revision (progname, revision);
  260. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  261. printf (COPYRIGHT, copyright, email);
  262. printf (_("Test the DNS service on the specified host using dig\n\n"));
  263. print_usage ();
  264. printf (_(UT_HELP_VRSN));
  265. printf (_(UT_HOST_PORT), 'P', myport);
  266. printf (_("\
  267. -l, --lookup=STRING\n\
  268. machine name to lookup\n"));
  269. printf (_("\
  270. -T, --record_type=STRING\n\
  271. record type to lookup (default: A)\n"));
  272. printf (_("\
  273. -a, --expected_address=STRING\n\
  274. an address expected to be in the asnwer section.\n\
  275. if not set, uses whatever was in -l\n"));
  276. printf (_(UT_WARN_CRIT));
  277. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  278. printf (_(UT_VERBOSE));
  279. printf (_(UT_SUPPORT));
  280. }
  281. void
  282. print_usage (void)
  283. {
  284. printf (_("\
  285. Usage: %s -H host -l lookup [-p <server port>] [-T <query type>]\n\
  286. [-w <warning interval>] [-c <critical interval>] [-t <timeout>]\n\
  287. [-a <expected answer address>] [-v]\n"),
  288. progname);
  289. printf (" %s (-h|--help)\n", progname);
  290. printf (" %s (-V|--version)\n", progname);
  291. }