check_dig.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. const char *standard_options = "\
  35. -h, --help\n\
  36. Print detailed help screen\n\
  37. -V, --version\n\
  38. Print version information\n\n";
  39. #include "config.h"
  40. #include "common.h"
  41. #include "utils.h"
  42. #include "popen.h"
  43. int process_arguments (int, char **);
  44. int validate_arguments (void);
  45. void print_help (void);
  46. void print_usage (void);
  47. char *query_address = NULL;
  48. char *dns_server = NULL;
  49. int verbose = FALSE;
  50. int
  51. main (int argc, char **argv)
  52. {
  53. char input_buffer[MAX_INPUT_BUFFER];
  54. char *command_line = NULL;
  55. char *output = "";
  56. int result = STATE_UNKNOWN;
  57. /* Set signal handling and alarm */
  58. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  59. usage ("Cannot catch SIGALRM\n");
  60. if (process_arguments (argc, argv) != OK)
  61. usage ("Could not parse arguments\n");
  62. /* get the command to run */
  63. asprintf (&command_line, "%s @%s %s", PATH_TO_DIG, dns_server, query_address);
  64. alarm (timeout_interval);
  65. time (&start_time);
  66. if (verbose)
  67. printf ("%s\n", command_line);
  68. /* run the command */
  69. child_process = spopen (command_line);
  70. if (child_process == NULL) {
  71. printf ("Could not open pipe: %s\n", command_line);
  72. return STATE_UNKNOWN;
  73. }
  74. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  75. if (child_stderr == NULL)
  76. printf ("Could not open stderr for %s\n", command_line);
  77. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  78. /* the server is responding, we just got the host name... */
  79. if (strstr (input_buffer, ";; ANSWER SECTION:")) {
  80. /* get the host address */
  81. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  82. break;
  83. if (strpbrk (input_buffer, "\r\n"))
  84. input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
  85. if (strstr (input_buffer, query_address) == input_buffer) {
  86. asprintf (&output, input_buffer);
  87. result = STATE_OK;
  88. }
  89. else {
  90. asprintf (&output, "Server not found in ANSWER SECTION");
  91. result = STATE_WARNING;
  92. }
  93. continue;
  94. }
  95. }
  96. if (result != STATE_OK) {
  97. asprintf (&output, "No ANSWER SECTION found");
  98. }
  99. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  100. /* If we get anything on STDERR, at least set warning */
  101. result = max_state (result, STATE_WARNING);
  102. printf ("%s", input_buffer);
  103. if (strlen (output) == 0)
  104. asprintf (&output, 1 + index (input_buffer, ':'));
  105. }
  106. (void) fclose (child_stderr);
  107. /* close the pipe */
  108. if (spclose (child_process)) {
  109. result = max_state (result, STATE_WARNING);
  110. if (strlen (output) == 0)
  111. asprintf (&output, "dig returned error status");
  112. }
  113. (void) time (&end_time);
  114. if (output == NULL || strlen (output) == 0)
  115. asprintf (&output, " Probably a non-existent host/domain");
  116. if (result == STATE_OK)
  117. printf ("DNS OK - %d seconds response time (%s)\n",
  118. (int) (end_time - start_time), output);
  119. else if (result == STATE_WARNING)
  120. printf ("DNS WARNING - %s\n", output);
  121. else if (result == STATE_CRITICAL)
  122. printf ("DNS CRITICAL - %s\n", output);
  123. else
  124. printf ("DNS problem - %s\n", output);
  125. return result;
  126. }
  127. /* process command-line arguments */
  128. int
  129. process_arguments (int argc, char **argv)
  130. {
  131. int c;
  132. int option_index = 0;
  133. static struct option long_options[] = {
  134. {"hostname", required_argument, 0, 'H'},
  135. {"query_address", required_argument, 0, 'e'},
  136. {"verbose", no_argument, 0, 'v'},
  137. {"version", no_argument, 0, 'V'},
  138. {"help", no_argument, 0, 'h'},
  139. {0, 0, 0, 0}
  140. };
  141. if (argc < 2)
  142. return ERROR;
  143. while (1) {
  144. c = getopt_long (argc, argv, "hVvt:l:H:", long_options, &option_index);
  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 = strdup ("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 ("Copyright (c) %s %s\n\t<%s>\n\n", copyright, authors, email);
  206. printf (summary);
  207. print_usage ();
  208. printf ("\nOptions:\n");
  209. printf (options, DEFAULT_SOCKET_TIMEOUT);
  210. printf (standard_options);
  211. support ();
  212. }
  213. void
  214. print_usage (void)
  215. {
  216. printf ("Usage: %s %s\n", progname, option_summary);
  217. printf (" %s (-h|--help)\n", progname);
  218. printf (" %s (-V|--version)\n", progname);
  219. }