check_dig.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. 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) == ERROR)
  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. usage2 (_("Unknown argument"), optarg);
  171. case 'h': /* help */
  172. print_help ();
  173. exit (STATE_OK);
  174. case 'V': /* version */
  175. print_revision (progname, revision);
  176. exit (STATE_OK);
  177. case 'H': /* hostname */
  178. if (is_host (optarg)) {
  179. dns_server = optarg;
  180. }
  181. else {
  182. usage2 (_("Invalid hostname/address"), optarg);
  183. }
  184. break;
  185. case 'p': /* server port */
  186. if (is_intpos (optarg)) {
  187. server_port = atoi (optarg);
  188. }
  189. else {
  190. usage2 (_("Port must be a positive integer"), optarg);
  191. }
  192. break;
  193. case 'l': /* address to lookup */
  194. query_address = optarg;
  195. break;
  196. case 'w': /* warning */
  197. if (is_nonnegative (optarg)) {
  198. warning_interval = strtod (optarg, NULL);
  199. }
  200. else {
  201. usage2 (_("Warning interval must be a positive integer"), optarg);
  202. }
  203. break;
  204. case 'c': /* critical */
  205. if (is_nonnegative (optarg)) {
  206. critical_interval = strtod (optarg, NULL);
  207. }
  208. else {
  209. usage2 (_("Critical interval must be a positive integer"), optarg);
  210. }
  211. break;
  212. case 't': /* timeout */
  213. if (is_intnonneg (optarg)) {
  214. timeout_interval = atoi (optarg);
  215. }
  216. else {
  217. usage2 (_("Timeout interval must be a positive integer"), optarg);
  218. }
  219. break;
  220. case 'v': /* verbose */
  221. verbose = TRUE;
  222. break;
  223. case 'T':
  224. record_type = optarg;
  225. break;
  226. case 'a':
  227. expected_address = optarg;
  228. break;
  229. }
  230. }
  231. c = optind;
  232. if (dns_server == NULL) {
  233. if (c < argc) {
  234. if (is_host (argv[c])) {
  235. dns_server = argv[c];
  236. }
  237. else {
  238. usage2 (_("Invalid hostname/address"), argv[c]);
  239. }
  240. }
  241. else {
  242. dns_server = strdup ("127.0.0.1");
  243. }
  244. }
  245. return validate_arguments ();
  246. }
  247. int
  248. validate_arguments (void)
  249. {
  250. return OK;
  251. }
  252. void
  253. print_help (void)
  254. {
  255. char *myport;
  256. asprintf (&myport, "%d", DEFAULT_PORT);
  257. print_revision (progname, revision);
  258. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  259. printf (COPYRIGHT, copyright, email);
  260. printf (_("Test the DNS service on the specified host using dig\n\n"));
  261. print_usage ();
  262. printf (_(UT_HELP_VRSN));
  263. printf (_(UT_HOST_PORT), 'P', myport);
  264. printf (_("\
  265. -l, --lookup=STRING\n\
  266. machine name to lookup\n"));
  267. printf (_("\
  268. -T, --record_type=STRING\n\
  269. record type to lookup (default: A)\n"));
  270. printf (_("\
  271. -a, --expected_address=STRING\n\
  272. an address expected to be in the asnwer section.\n\
  273. if not set, uses whatever was in -l\n"));
  274. printf (_(UT_WARN_CRIT));
  275. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  276. printf (_(UT_VERBOSE));
  277. printf (_(UT_SUPPORT));
  278. }
  279. void
  280. print_usage (void)
  281. {
  282. printf ("\
  283. Usage: %s -H host -l lookup [-p <server port>] [-T <query type>]\n\
  284. [-w <warning interval>] [-c <critical interval>] [-t <timeout>]\n\
  285. [-a <expected answer address>] [-v]\n", progname);
  286. }