check_dig.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. int option_index = 0;
  122. static struct option long_options[] = {
  123. {"hostname", required_argument, 0, 'H'},
  124. {"query_address", required_argument, 0, 'e'},
  125. {"verbose", no_argument, 0, 'v'},
  126. {"version", no_argument, 0, 'V'},
  127. {"help", no_argument, 0, 'h'},
  128. {0, 0, 0, 0}
  129. };
  130. if (argc < 2)
  131. return ERROR;
  132. while (1) {
  133. c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
  134. if (c == -1 || c == EOF)
  135. break;
  136. switch (c) {
  137. case '?': /* help */
  138. usage3 ("Unknown argument", optopt);
  139. case 'H': /* hostname */
  140. if (is_host (optarg)) {
  141. dns_server = optarg;
  142. }
  143. else {
  144. usage ("Invalid host name\n");
  145. }
  146. break;
  147. case 'l': /* username */
  148. query_address = optarg;
  149. break;
  150. case 'v': /* verbose */
  151. verbose = TRUE;
  152. break;
  153. case 't': /* timeout */
  154. if (is_intnonneg (optarg)) {
  155. timeout_interval = atoi (optarg);
  156. }
  157. else {
  158. usage ("Time interval must be a nonnegative integer\n");
  159. }
  160. break;
  161. case 'V': /* version */
  162. print_revision (progname, "$Revision$");
  163. exit (STATE_OK);
  164. case 'h': /* help */
  165. print_help ();
  166. exit (STATE_OK);
  167. }
  168. }
  169. c = optind;
  170. if (dns_server == NULL) {
  171. if (c < argc) {
  172. if (is_host (argv[c])) {
  173. dns_server = argv[c];
  174. }
  175. else {
  176. usage ("Invalid host name");
  177. }
  178. }
  179. else {
  180. asprintf (&dns_server, "127.0.0.1");
  181. }
  182. }
  183. return validate_arguments ();
  184. }
  185. int
  186. validate_arguments (void)
  187. {
  188. return OK;
  189. }
  190. void
  191. print_help (void)
  192. {
  193. print_revision (progname, "$Revision$");
  194. printf
  195. ("Copyright (c) %s %s <%s>\n\n%s\n",
  196. COPYRIGHT, AUTHOR, EMAIL, SUMMARY);
  197. print_usage ();
  198. printf
  199. ("\nOptions:\n"
  200. " -H, --hostname=STRING or IPADDRESS\n"
  201. " Check server on the indicated host\n"
  202. " -l, --lookup=STRING\n"
  203. " machine name to lookup\n"
  204. " -t, --timeout=INTEGER\n"
  205. " Seconds before connection attempt times out (default: %d)\n"
  206. " -v, --verbose\n"
  207. " Print extra information (command-line use only)\n"
  208. " -h, --help\n"
  209. " Print detailed help screen\n"
  210. " -V, --version\n"
  211. " Print version information\n\n", DEFAULT_SOCKET_TIMEOUT);
  212. support ();
  213. }
  214. void
  215. print_usage (void)
  216. {
  217. printf
  218. ("Usage: %s -H host -l lookup [-t timeout] [-v]\n"
  219. " %s --help\n"
  220. " %s --version\n", progname, progname, progname);
  221. }