check_dig.c 7.0 KB

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