check_dig.c 8.9 KB

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