check_dig.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*****************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *****************************************************************************/
  14. #include "common.h"
  15. #include "netutils.h"
  16. #include "utils.h"
  17. #include "popen.h"
  18. int process_arguments (int, char **);
  19. int validate_arguments (void);
  20. void print_help (void);
  21. void print_usage (void);
  22. const char *progname = "check_dig";
  23. const char *revision = "$Revision$";
  24. const char *copyright = "2002-2003";
  25. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  26. enum {
  27. UNDEFINED = 0,
  28. DEFAULT_PORT = 53
  29. };
  30. char *query_address = NULL;
  31. char *dns_server = NULL;
  32. int verbose = FALSE;
  33. int server_port = DEFAULT_PORT;
  34. double warning_interval = UNDEFINED;
  35. double critical_interval = UNDEFINED;
  36. struct timeval tv;
  37. int
  38. main (int argc, char **argv)
  39. {
  40. char input_buffer[MAX_INPUT_BUFFER];
  41. char *command_line;
  42. char *output;
  43. long microsec;
  44. double elapsed_time;
  45. int result = STATE_UNKNOWN;
  46. output = strdup ("");
  47. setlocale (LC_ALL, "");
  48. bindtextdomain (PACKAGE, LOCALEDIR);
  49. textdomain (PACKAGE);
  50. /* Set signal handling and alarm */
  51. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  52. usage (_("Cannot catch SIGALRM\n"));
  53. if (process_arguments (argc, argv) != OK)
  54. usage (_("Could not parse arguments\n"));
  55. /* get the command to run */
  56. asprintf (&command_line, "%s @%s -p %d %s",
  57. PATH_TO_DIG, dns_server, server_port, query_address);
  58. alarm (timeout_interval);
  59. gettimeofday (&tv, NULL);
  60. if (verbose)
  61. printf ("%s\n", command_line);
  62. /* run the command */
  63. child_process = spopen (command_line);
  64. if (child_process == NULL) {
  65. printf (_("Could not open pipe: %s\n"), command_line);
  66. return STATE_UNKNOWN;
  67. }
  68. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  69. if (child_stderr == NULL)
  70. printf (_("Could not open stderr for %s\n"), command_line);
  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 = strdup(input_buffer);
  81. result = STATE_OK;
  82. }
  83. else {
  84. asprintf (&output, _("Server not found in ANSWER SECTION"));
  85. result = STATE_WARNING;
  86. }
  87. continue;
  88. }
  89. }
  90. if (result != STATE_OK) {
  91. asprintf (&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 (strlen (output) == 0)
  98. output = strdup (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 (strlen (output) == 0)
  105. asprintf (&output, _("dig returned error status"));
  106. }
  107. microsec = deltime (tv);
  108. elapsed_time = (double)microsec / 1.0e6;
  109. if (output == NULL || strlen (output) == 0)
  110. asprintf (&output, _(" Probably a non-existent host/domain"));
  111. if (critical_interval != UNDEFINED && elapsed_time > critical_interval)
  112. result = STATE_CRITICAL;
  113. else if (warning_interval != UNDEFINED && elapsed_time > warning_interval)
  114. result = STATE_WARNING;
  115. asprintf (&output, _("%.3f seconds response time (%s)"), elapsed_time, output);
  116. printf ("DNS %s - %s|%s\n",
  117. state_text (result), output,
  118. perfdata("time", microsec, "us",
  119. (warning_interval!=UNDEFINED?TRUE:FALSE),
  120. (int)(1e6*warning_interval),
  121. (critical_interval!=UNDEFINED?TRUE:FALSE),
  122. (int)(1e6*critical_interval),
  123. TRUE, 0, FALSE, 0));
  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 = 0;
  132. static struct option longopts[] = {
  133. {"hostname", required_argument, 0, 'H'},
  134. {"query_address", required_argument, 0, 'l'},
  135. {"warning", required_argument, 0, 'w'},
  136. {"critical", required_argument, 0, 'c'},
  137. {"timeout", required_argument, 0, 't'},
  138. {"verbose", no_argument, 0, 'v'},
  139. {"version", no_argument, 0, 'V'},
  140. {"help", no_argument, 0, 'h'},
  141. {0, 0, 0, 0}
  142. };
  143. if (argc < 2)
  144. return ERROR;
  145. while (1) {
  146. c = getopt_long (argc, argv, "hVvt:l:H:w:c:", longopts, &option);
  147. if (c == -1 || c == EOF)
  148. break;
  149. switch (c) {
  150. case '?': /* help */
  151. usage3 (_("Unknown argument"), optopt);
  152. case 'h': /* help */
  153. print_help ();
  154. exit (STATE_OK);
  155. case 'V': /* version */
  156. print_revision (progname, "$Revision$");
  157. exit (STATE_OK);
  158. case 'H': /* hostname */
  159. if (is_host (optarg)) {
  160. dns_server = optarg;
  161. }
  162. else {
  163. usage2 (_("Invalid host name"), optarg);
  164. }
  165. break;
  166. case 'p': /* server port */
  167. if (is_intpos (optarg)) {
  168. server_port = atoi (optarg);
  169. }
  170. else {
  171. usage2 (_("Server port must be a nonnegative integer"), optarg);
  172. }
  173. break;
  174. case 'l': /* address to lookup */
  175. query_address = optarg;
  176. break;
  177. case 'w': /* warning */
  178. if (is_nonnegative (optarg)) {
  179. warning_interval = strtod (optarg, NULL);
  180. if (warning_interval == HUGE_VAL)
  181. usage2 (_("Input causes overflow in warning interval"), optarg);
  182. }
  183. else {
  184. usage2 (_("Warning interval must be a nonnegative integer"), optarg);
  185. }
  186. break;
  187. case 'c': /* critical */
  188. if (is_nonnegative (optarg)) {
  189. critical_interval = strtod (optarg, NULL);
  190. if (critical_interval == HUGE_VAL)
  191. usage2 (_("Input causes overflow in critical interval"), optarg);
  192. }
  193. else {
  194. usage2 (_("Critical interval must be a nonnegative integer"), optarg);
  195. }
  196. break;
  197. case 't': /* timeout */
  198. if (is_intnonneg (optarg)) {
  199. timeout_interval = atoi (optarg);
  200. }
  201. else {
  202. usage2 (_("Time interval must be a nonnegative integer"), optarg);
  203. }
  204. break;
  205. case 'v': /* verbose */
  206. verbose = TRUE;
  207. break;
  208. }
  209. }
  210. c = optind;
  211. if (dns_server == NULL) {
  212. if (c < argc) {
  213. if (is_host (argv[c])) {
  214. dns_server = argv[c];
  215. }
  216. else {
  217. usage2 (_("Invalid host name"), argv[c]);
  218. }
  219. }
  220. else {
  221. dns_server = strdup ("127.0.0.1");
  222. }
  223. }
  224. return validate_arguments ();
  225. }
  226. int
  227. validate_arguments (void)
  228. {
  229. return OK;
  230. }
  231. void
  232. print_help (void)
  233. {
  234. char *myport;
  235. asprintf (&myport, "%d", DEFAULT_PORT);
  236. print_revision (progname, revision);
  237. printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
  238. printf (_(COPYRIGHT), copyright, email);
  239. printf (_("Test the DNS service on the specified host using dig\n\n"));
  240. print_usage ();
  241. printf (_(UT_HELP_VRSN));
  242. printf (_(UT_HOST_PORT), 'P', myport);
  243. printf (_("\
  244. -l, --lookup=STRING\n\
  245. machine name to lookup\n"));
  246. printf (_(UT_WARN_CRIT));
  247. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  248. printf (_(UT_VERBOSE));
  249. printf (_(UT_SUPPORT));
  250. }
  251. void
  252. print_usage (void)
  253. {
  254. printf (_("\
  255. Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
  256. [-c <critical interval>] [-t <timeout>] [-v]\n"),
  257. progname);
  258. printf (" %s (-h|--help)\n", progname);
  259. printf (" %s (-V|--version)\n", progname);
  260. }