check_dig.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #define 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 = NULL;
  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. output = strscpy (output, "");
  67. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  68. /* the server is responding, we just got the host name... */
  69. if (strstr (input_buffer, ";; ANSWER SECTION:")) {
  70. /* get the host address */
  71. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  72. break;
  73. if (strpbrk (input_buffer, "\r\n"))
  74. input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
  75. if (strstr (input_buffer, query_address) == input_buffer) {
  76. output = strscpy (output, input_buffer);
  77. result = STATE_OK;
  78. }
  79. else {
  80. strcpy (output, "Server not found in ANSWER SECTION");
  81. result = STATE_WARNING;
  82. }
  83. continue;
  84. }
  85. }
  86. if (result != STATE_OK) {
  87. strcpy (output, "No ANSWER SECTION found");
  88. }
  89. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  90. /* If we get anything on STDERR, at least set warning */
  91. result = max_state (result, STATE_WARNING);
  92. printf ("%s", input_buffer);
  93. if (!strcmp (output, ""))
  94. strcpy (output, 1 + index (input_buffer, ':'));
  95. }
  96. (void) fclose (child_stderr);
  97. /* close the pipe */
  98. if (spclose (child_process)) {
  99. result = max_state (result, STATE_WARNING);
  100. if (!strcmp (output, ""))
  101. strcpy (output, "nslookup returned error status");
  102. }
  103. (void) time (&end_time);
  104. if (result == STATE_OK)
  105. printf ("DNS ok - %d seconds response time (%s)\n",
  106. (int) (end_time - start_time), output);
  107. else if (result == STATE_WARNING)
  108. printf ("DNS WARNING - %s\n",
  109. !strcmp (output,
  110. "") ? " Probably a non-existent host/domain" : output);
  111. else if (result == STATE_CRITICAL)
  112. printf ("DNS CRITICAL - %s\n",
  113. !strcmp (output,
  114. "") ? " Probably a non-existent host/domain" : output);
  115. else
  116. printf ("DNS problem - %s\n",
  117. !strcmp (output,
  118. "") ? " Probably a non-existent host/domain" : output);
  119. return result;
  120. }
  121. /* process command-line arguments */
  122. int
  123. process_arguments (int argc, char **argv)
  124. {
  125. int c;
  126. #ifdef HAVE_GETOPT_H
  127. int option_index = 0;
  128. static struct option long_options[] = {
  129. {"hostname", required_argument, 0, 'H'},
  130. {"query_address", required_argument, 0, 'e'},
  131. {"verbose", no_argument, 0, 'v'},
  132. {"version", no_argument, 0, 'V'},
  133. {"help", no_argument, 0, 'h'},
  134. {0, 0, 0, 0}
  135. };
  136. #endif
  137. if (argc < 2)
  138. return ERROR;
  139. while (1) {
  140. #ifdef HAVE_GETOPT_H
  141. c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
  142. #else
  143. c = getopt (argc, argv, "hVvt:l:H:");
  144. #endif
  145. if (c == -1 || c == EOF)
  146. break;
  147. switch (c) {
  148. case '?': /* help */
  149. usage3 ("Unknown argument", optopt);
  150. case 'H': /* hostname */
  151. if (is_host (optarg)) {
  152. dns_server = optarg;
  153. }
  154. else {
  155. usage ("Invalid host name\n");
  156. }
  157. break;
  158. case 'l': /* username */
  159. query_address = optarg;
  160. break;
  161. case 'v': /* verbose */
  162. verbose = TRUE;
  163. break;
  164. case 't': /* timeout */
  165. if (is_intnonneg (optarg)) {
  166. timeout_interval = atoi (optarg);
  167. }
  168. else {
  169. usage ("Time interval must be a nonnegative integer\n");
  170. }
  171. break;
  172. case 'V': /* version */
  173. print_revision (PROGNAME, "$Revision$");
  174. exit (STATE_OK);
  175. case 'h': /* help */
  176. print_help ();
  177. exit (STATE_OK);
  178. }
  179. }
  180. c = optind;
  181. if (dns_server == NULL) {
  182. if (c < argc) {
  183. if (is_host (argv[c])) {
  184. dns_server = argv[c];
  185. }
  186. else {
  187. usage ("Invalid host name");
  188. }
  189. }
  190. else {
  191. dns_server = strscpy (NULL, "127.0.0.1");
  192. }
  193. }
  194. return validate_arguments ();
  195. }
  196. int
  197. validate_arguments (void)
  198. {
  199. return OK;
  200. }
  201. void
  202. print_help (void)
  203. {
  204. print_revision (PROGNAME, "$Revision$");
  205. printf
  206. ("Copyright (c) %s %s <%s>\n\n%s\n",
  207. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  208. print_usage ();
  209. printf
  210. ("\nOptions:\n"
  211. " -H, --hostname=STRING or IPADDRESS\n"
  212. " Check server on the indicated host\n"
  213. " -l, --lookup=STRING\n"
  214. " machine name to lookup\n"
  215. " -t, --timeout=INTEGER\n"
  216. " Seconds before connection attempt times out (default: %d)\n"
  217. " -v, --verbose\n"
  218. " Print extra information (command-line use only)\n"
  219. " -h, --help\n"
  220. " Print detailed help screen\n"
  221. " -V, --version\n"
  222. " Print version information\n\n", DEFAULT_SOCKET_TIMEOUT);
  223. support ();
  224. }
  225. void
  226. print_usage (void)
  227. {
  228. printf
  229. ("Usage: %s -H host -l lookup [-t timeout] [-v]\n"
  230. " %s --help\n"
  231. " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
  232. }