check_dig.c 6.2 KB

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