check_dig.c 6.7 KB

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