check_dig.c 7.6 KB

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