check_dig.c 7.0 KB

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