check_dig.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /******************************************************************************
  2. *
  3. * Program: SNMP plugin for Nagios
  4. * License: GPL
  5. *
  6. * License Information:
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *****************************************************************************/
  22. #include "config.h"
  23. #include "common.h"
  24. #include "utils.h"
  25. #include "popen.h"
  26. const char *progname = "check_dig";
  27. #define REVISION "$Revision$"
  28. #define COPYRIGHT "2000-2002"
  29. #define AUTHOR "Karl DeBisschop"
  30. #define EMAIL "karl@debisschop.net"
  31. #define SUMMARY "Test the DNS service on the specified host using dig\n"
  32. int process_arguments (int, char **);
  33. int validate_arguments (void);
  34. void print_help (void);
  35. void print_usage (void);
  36. char *query_address = NULL;
  37. char *dns_server = NULL;
  38. int verbose = FALSE;
  39. int
  40. main (int argc, char **argv)
  41. {
  42. char input_buffer[MAX_INPUT_BUFFER];
  43. char *command_line = NULL;
  44. char *output = "";
  45. int result = STATE_UNKNOWN;
  46. /* Set signal handling and alarm */
  47. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  48. usage ("Cannot catch SIGALRM\n");
  49. if (process_arguments (argc, argv) != OK)
  50. usage ("Could not parse arguments\n");
  51. /* get the command to run */
  52. asprintf (&command_line, "%s @%s %s", PATH_TO_DIG, dns_server, query_address);
  53. alarm (timeout_interval);
  54. time (&start_time);
  55. if (verbose)
  56. printf ("%s\n", command_line);
  57. /* run the command */
  58. child_process = spopen (command_line);
  59. if (child_process == NULL) {
  60. printf ("Could not open pipe: %s\n", command_line);
  61. return STATE_UNKNOWN;
  62. }
  63. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  64. if (child_stderr == NULL)
  65. printf ("Could not open stderr for %s\n", command_line);
  66. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  67. /* the server is responding, we just got the host name... */
  68. if (strstr (input_buffer, ";; ANSWER SECTION:")) {
  69. /* get the host address */
  70. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  71. break;
  72. if (strpbrk (input_buffer, "\r\n"))
  73. input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
  74. if (strstr (input_buffer, query_address) == input_buffer) {
  75. asprintf (&output, input_buffer);
  76. result = STATE_OK;
  77. }
  78. else {
  79. asprintf (&output, "Server not found in ANSWER SECTION");
  80. result = STATE_WARNING;
  81. }
  82. continue;
  83. }
  84. }
  85. if (result != STATE_OK) {
  86. asprintf (&output, "No ANSWER SECTION found");
  87. }
  88. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  89. /* If we get anything on STDERR, at least set warning */
  90. result = max_state (result, STATE_WARNING);
  91. printf ("%s", input_buffer);
  92. if (strlen (output) == 0)
  93. asprintf (&output, 1 + index (input_buffer, ':'));
  94. }
  95. (void) fclose (child_stderr);
  96. /* close the pipe */
  97. if (spclose (child_process)) {
  98. result = max_state (result, STATE_WARNING);
  99. if (strlen (output) == 0)
  100. asprintf (&output, "dig returned error status");
  101. }
  102. (void) time (&end_time);
  103. if (output == NULL || strlen (output) == 0)
  104. asprintf (&output, " Probably a non-existent host/domain");
  105. if (result == STATE_OK)
  106. printf ("DNS ok - %d seconds response time (%s)\n",
  107. (int) (end_time - start_time), output);
  108. else if (result == STATE_WARNING)
  109. printf ("DNS WARNING - %s\n", output);
  110. else if (result == STATE_CRITICAL)
  111. printf ("DNS CRITICAL - %s\n", output);
  112. else
  113. printf ("DNS problem - %s\n", output);
  114. return result;
  115. }
  116. /* process command-line arguments */
  117. int
  118. process_arguments (int argc, char **argv)
  119. {
  120. int c;
  121. #ifdef HAVE_GETOPT_H
  122. int option_index = 0;
  123. static struct option long_options[] = {
  124. {"hostname", required_argument, 0, 'H'},
  125. {"query_address", required_argument, 0, 'e'},
  126. {"verbose", no_argument, 0, 'v'},
  127. {"version", no_argument, 0, 'V'},
  128. {"help", no_argument, 0, 'h'},
  129. {0, 0, 0, 0}
  130. };
  131. #endif
  132. if (argc < 2)
  133. return ERROR;
  134. while (1) {
  135. #ifdef HAVE_GETOPT_H
  136. c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
  137. #else
  138. c = getopt (argc, argv, "hVvt:l:H:");
  139. #endif
  140. if (c == -1 || c == EOF)
  141. break;
  142. switch (c) {
  143. case '?': /* help */
  144. usage3 ("Unknown argument", optopt);
  145. case 'H': /* hostname */
  146. if (is_host (optarg)) {
  147. dns_server = optarg;
  148. }
  149. else {
  150. usage ("Invalid host name\n");
  151. }
  152. break;
  153. case 'l': /* username */
  154. query_address = optarg;
  155. break;
  156. case 'v': /* verbose */
  157. verbose = TRUE;
  158. break;
  159. case 't': /* timeout */
  160. if (is_intnonneg (optarg)) {
  161. timeout_interval = atoi (optarg);
  162. }
  163. else {
  164. usage ("Time interval must be a nonnegative integer\n");
  165. }
  166. break;
  167. case 'V': /* version */
  168. print_revision (progname, "$Revision$");
  169. exit (STATE_OK);
  170. case 'h': /* help */
  171. print_help ();
  172. exit (STATE_OK);
  173. }
  174. }
  175. c = optind;
  176. if (dns_server == NULL) {
  177. if (c < argc) {
  178. if (is_host (argv[c])) {
  179. dns_server = argv[c];
  180. }
  181. else {
  182. usage ("Invalid host name");
  183. }
  184. }
  185. else {
  186. asprintf (&dns_server, "127.0.0.1");
  187. }
  188. }
  189. return validate_arguments ();
  190. }
  191. int
  192. validate_arguments (void)
  193. {
  194. return OK;
  195. }
  196. void
  197. print_help (void)
  198. {
  199. print_revision (progname, "$Revision$");
  200. printf
  201. ("Copyright (c) %s %s <%s>\n\n%s\n",
  202. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  203. print_usage ();
  204. printf
  205. ("\nOptions:\n"
  206. " -H, --hostname=STRING or IPADDRESS\n"
  207. " Check server on the indicated host\n"
  208. " -l, --lookup=STRING\n"
  209. " machine name to lookup\n"
  210. " -t, --timeout=INTEGER\n"
  211. " Seconds before connection attempt times out (default: %d)\n"
  212. " -v, --verbose\n"
  213. " Print extra information (command-line use only)\n"
  214. " -h, --help\n"
  215. " Print detailed help screen\n"
  216. " -V, --version\n"
  217. " Print version information\n\n", DEFAULT_SOCKET_TIMEOUT);
  218. support ();
  219. }
  220. void
  221. print_usage (void)
  222. {
  223. printf
  224. ("Usage: %s -H host -l lookup [-t timeout] [-v]\n"
  225. " %s --help\n"
  226. " %s --version\n", progname, progname, progname);
  227. }