check_dig.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*****************************************************************************
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *
  17. *****************************************************************************/
  18. #include "config.h"
  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. const char *progname = "check_dig";
  28. const char *revision = "$Revision$";
  29. const char *copyright = "2002-2003";
  30. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  31. enum {
  32. DEFAULT_PORT = 53
  33. };
  34. void
  35. print_usage (void)
  36. {
  37. printf (_("\
  38. Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
  39. [-c <critical interval>] [-t <timeout>] [-v]\n"),
  40. progname);
  41. printf (" %s (-h|--help)\n", progname);
  42. printf (" %s (-V|--version)\n", progname);
  43. }
  44. void
  45. print_help (void)
  46. {
  47. char *myport;
  48. asprintf (&myport, "%d", DEFAULT_PORT);
  49. print_revision (progname, revision);
  50. printf (_(COPYRIGHT), copyright, email);
  51. printf (_("Test the DNS service on the specified host using dig\n\n"));
  52. print_usage ();
  53. printf (_(UT_HELP_VRSN));
  54. printf (_(UT_HOST_PORT), 'P', myport);
  55. printf (_("\
  56. -l, --lookup=STRING\n\
  57. machine name to lookup\n"));
  58. printf (_(UT_WARN_CRIT));
  59. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  60. printf (_(UT_VERBOSE));
  61. support ();
  62. }
  63. char *query_address = NULL;
  64. char *dns_server = NULL;
  65. int verbose = FALSE;
  66. int server_port = DEFAULT_PORT;
  67. int warning_interval = -1;
  68. int critical_interval = -1;
  69. int
  70. main (int argc, char **argv)
  71. {
  72. char input_buffer[MAX_INPUT_BUFFER];
  73. char *command_line = NULL;
  74. char *output = "";
  75. int result = STATE_UNKNOWN;
  76. /* Set signal handling and alarm */
  77. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  78. usage (_("Cannot catch SIGALRM\n"));
  79. if (process_arguments (argc, argv) != OK)
  80. usage (_("Could not parse arguments\n"));
  81. /* get the command to run */
  82. asprintf (&command_line, "%s @%s -p %d %s",
  83. PATH_TO_DIG, dns_server, server_port, query_address);
  84. alarm (timeout_interval);
  85. time (&start_time);
  86. if (verbose)
  87. printf ("%s\n", command_line);
  88. /* run the command */
  89. child_process = spopen (command_line);
  90. if (child_process == NULL) {
  91. printf (_("Could not open pipe: %s\n"), command_line);
  92. return STATE_UNKNOWN;
  93. }
  94. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  95. if (child_stderr == NULL)
  96. printf (_("Could not open stderr for %s\n"), command_line);
  97. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  98. /* the server is responding, we just got the host name... */
  99. if (strstr (input_buffer, ";; ANSWER SECTION:")) {
  100. /* get the host address */
  101. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  102. break;
  103. if (strpbrk (input_buffer, "\r\n"))
  104. input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
  105. if (strstr (input_buffer, query_address) == input_buffer) {
  106. asprintf (&output, input_buffer);
  107. result = STATE_OK;
  108. }
  109. else {
  110. asprintf (&output, _("Server not found in ANSWER SECTION"));
  111. result = STATE_WARNING;
  112. }
  113. continue;
  114. }
  115. }
  116. if (result != STATE_OK) {
  117. asprintf (&output, _("No ANSWER SECTION found"));
  118. }
  119. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  120. /* If we get anything on STDERR, at least set warning */
  121. result = max_state (result, STATE_WARNING);
  122. printf ("%s", input_buffer);
  123. if (strlen (output) == 0)
  124. asprintf (&output, 1 + index (input_buffer, ':'));
  125. }
  126. (void) fclose (child_stderr);
  127. /* close the pipe */
  128. if (spclose (child_process)) {
  129. result = max_state (result, STATE_WARNING);
  130. if (strlen (output) == 0)
  131. asprintf (&output, _("dig returned error status"));
  132. }
  133. (void) time (&end_time);
  134. if (output == NULL || strlen (output) == 0)
  135. asprintf (&output, _(" Probably a non-existent host/domain"));
  136. if (result == STATE_OK)
  137. printf (_("DNS OK - %d seconds response time (%s)\n"),
  138. (int) (end_time - start_time), output);
  139. else if (result == STATE_WARNING)
  140. printf (_("DNS WARNING - %s\n"), output);
  141. else if (result == STATE_CRITICAL)
  142. printf (_("DNS CRITICAL - %s\n"), output);
  143. else
  144. printf (_("DNS problem - %s\n"), output);
  145. return result;
  146. }
  147. /* process command-line arguments */
  148. int
  149. process_arguments (int argc, char **argv)
  150. {
  151. int c;
  152. int option_index = 0;
  153. static struct option long_options[] = {
  154. {"hostname", required_argument, 0, 'H'},
  155. {"query_address", required_argument, 0, 'e'},
  156. {"verbose", no_argument, 0, 'v'},
  157. {"version", no_argument, 0, 'V'},
  158. {"help", no_argument, 0, 'h'},
  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:", long_options, &option_index);
  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':
  185. if (is_intpos (optarg)) {
  186. server_port = atoi (optarg);
  187. }
  188. else {
  189. usage2 (_("Server port must be a nonnegative integer\n"), optarg);
  190. }
  191. break;
  192. case 'l': /* username */
  193. query_address = optarg;
  194. break;
  195. case 'w': /* timeout */
  196. if (is_intnonneg (optarg)) {
  197. warning_interval = atoi (optarg);
  198. }
  199. else {
  200. usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
  201. }
  202. break;
  203. case 'c': /* timeout */
  204. if (is_intnonneg (optarg)) {
  205. critical_interval = atoi (optarg);
  206. }
  207. else {
  208. usage2 (_("Critical interval must be a nonnegative integer\n"), 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\n"), optarg);
  217. }
  218. break;
  219. case 'v': /* verbose */
  220. verbose = TRUE;
  221. break;
  222. }
  223. }
  224. c = optind;
  225. if (dns_server == NULL) {
  226. if (c < argc) {
  227. if (is_host (argv[c])) {
  228. dns_server = argv[c];
  229. }
  230. else {
  231. usage2 (_("Invalid host name"), argv[c]);
  232. }
  233. }
  234. else {
  235. dns_server = strdup ("127.0.0.1");
  236. }
  237. }
  238. return validate_arguments ();
  239. }
  240. int
  241. validate_arguments (void)
  242. {
  243. return OK;
  244. }