check_dig.c 6.4 KB

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