check_dig.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. DEFAULT_PORT = 53
  28. };
  29. char *query_address = NULL;
  30. char *dns_server = NULL;
  31. int verbose = FALSE;
  32. int server_port = DEFAULT_PORT;
  33. int warning_interval = -1;
  34. int critical_interval = -1;
  35. struct timeval tv;
  36. int
  37. main (int argc, char **argv)
  38. {
  39. char input_buffer[MAX_INPUT_BUFFER];
  40. char *command_line;
  41. char *output;
  42. long microsec;
  43. double elapsed_time;
  44. int result = STATE_UNKNOWN;
  45. output = strdup ("");
  46. setlocale (LC_ALL, "");
  47. bindtextdomain (PACKAGE, LOCALEDIR);
  48. textdomain (PACKAGE);
  49. /* Set signal handling and alarm */
  50. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  51. usage (_("Cannot catch SIGALRM\n"));
  52. if (process_arguments (argc, argv) != OK)
  53. usage (_("Could not parse arguments\n"));
  54. /* get the command to run */
  55. asprintf (&command_line, "%s @%s -p %d %s",
  56. PATH_TO_DIG, dns_server, server_port, query_address);
  57. alarm (timeout_interval);
  58. gettimeofday (&tv, NULL);
  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. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  71. /* the server is responding, we just got the host name... */
  72. if (strstr (input_buffer, ";; ANSWER SECTION:")) {
  73. /* get the host address */
  74. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  75. break;
  76. if (strpbrk (input_buffer, "\r\n"))
  77. input_buffer[strcspn (input_buffer, "\r\n")] = '\0';
  78. if (strstr (input_buffer, query_address) == input_buffer) {
  79. output = strdup(input_buffer);
  80. result = STATE_OK;
  81. }
  82. else {
  83. asprintf (&output, _("Server not found in ANSWER SECTION"));
  84. result = STATE_WARNING;
  85. }
  86. continue;
  87. }
  88. }
  89. if (result != STATE_OK) {
  90. asprintf (&output, _("No ANSWER SECTION found"));
  91. }
  92. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  93. /* If we get anything on STDERR, at least set warning */
  94. result = max_state (result, STATE_WARNING);
  95. printf ("%s", input_buffer);
  96. if (strlen (output) == 0)
  97. output = strdup (1 + index (input_buffer, ':'));
  98. }
  99. (void) fclose (child_stderr);
  100. /* close the pipe */
  101. if (spclose (child_process)) {
  102. result = max_state (result, STATE_WARNING);
  103. if (strlen (output) == 0)
  104. asprintf (&output, _("dig returned error status"));
  105. }
  106. microsec = deltime (tv);
  107. elapsed_time = (double)microsec / 1.0e6;
  108. if (output == NULL || strlen (output) == 0)
  109. asprintf (&output, _(" Probably a non-existent host/domain"));
  110. if (critical_interval > 0 && elapsed_time > critical_interval)
  111. printf (_("DNS OK - %.3f seconds response time (%s)"), elapsed_time, output);
  112. else if (result == STATE_CRITICAL)
  113. printf (_("DNS CRITICAL - %s"), output);
  114. else if (warning_interval > 0 && elapsed_time > warning_interval)
  115. printf (_("DNS OK - %.3f seconds response time (%s)"), elapsed_time, output);
  116. else if (result == STATE_WARNING)
  117. printf (_("DNS WARNING - %s"), output);
  118. else if (result == STATE_OK)
  119. printf (_("DNS OK - %.3f seconds response time (%s)"),
  120. elapsed_time, output);
  121. else
  122. printf (_("DNS problem - %s"), output);
  123. printf ("|%s\n",
  124. perfdata("time", microsec, "us",
  125. (warning_interval>0?TRUE:FALSE),
  126. (int)1e6*warning_interval,
  127. (critical_interval>0?TRUE:FALSE),
  128. (int)1e6*critical_interval,
  129. TRUE, 0, FALSE, 0));
  130. return result;
  131. }
  132. /* process command-line arguments */
  133. int
  134. process_arguments (int argc, char **argv)
  135. {
  136. int c;
  137. int option = 0;
  138. static struct option longopts[] = {
  139. {"hostname", required_argument, 0, 'H'},
  140. {"query_address", required_argument, 0, 'l'},
  141. {"warning", required_argument, 0, 'w'},
  142. {"critical", required_argument, 0, 'c'},
  143. {"timeout", required_argument, 0, 't'},
  144. {"verbose", no_argument, 0, 'v'},
  145. {"version", no_argument, 0, 'V'},
  146. {"help", no_argument, 0, 'h'},
  147. {0, 0, 0, 0}
  148. };
  149. if (argc < 2)
  150. return ERROR;
  151. while (1) {
  152. c = getopt_long (argc, argv, "hVvt:l:H:w:c:", longopts, &option);
  153. if (c == -1 || c == EOF)
  154. break;
  155. switch (c) {
  156. case '?': /* help */
  157. usage3 (_("Unknown argument"), optopt);
  158. case 'h': /* help */
  159. print_help ();
  160. exit (STATE_OK);
  161. case 'V': /* version */
  162. print_revision (progname, "$Revision$");
  163. exit (STATE_OK);
  164. case 'H': /* hostname */
  165. if (is_host (optarg)) {
  166. dns_server = optarg;
  167. }
  168. else {
  169. usage2 (_("Invalid host name"), optarg);
  170. }
  171. break;
  172. case 'p': /* server port */
  173. if (is_intpos (optarg)) {
  174. server_port = atoi (optarg);
  175. }
  176. else {
  177. usage2 (_("Server port must be a nonnegative integer\n"), optarg);
  178. }
  179. break;
  180. case 'l': /* address to lookup */
  181. query_address = optarg;
  182. break;
  183. case 'w': /* warning */
  184. if (is_intnonneg (optarg)) {
  185. warning_interval = atoi (optarg);
  186. }
  187. else {
  188. usage2 (_("Warning interval must be a nonnegative integer\n"), optarg);
  189. }
  190. break;
  191. case 'c': /* critical */
  192. if (is_intnonneg (optarg)) {
  193. critical_interval = atoi (optarg);
  194. }
  195. else {
  196. usage2 (_("Critical interval must be a nonnegative integer\n"), optarg);
  197. }
  198. break;
  199. case 't': /* timeout */
  200. if (is_intnonneg (optarg)) {
  201. timeout_interval = atoi (optarg);
  202. }
  203. else {
  204. usage2 (_("Time interval must be a nonnegative integer\n"), optarg);
  205. }
  206. break;
  207. case 'v': /* verbose */
  208. verbose = TRUE;
  209. break;
  210. }
  211. }
  212. c = optind;
  213. if (dns_server == NULL) {
  214. if (c < argc) {
  215. if (is_host (argv[c])) {
  216. dns_server = argv[c];
  217. }
  218. else {
  219. usage2 (_("Invalid host name"), argv[c]);
  220. }
  221. }
  222. else {
  223. dns_server = strdup ("127.0.0.1");
  224. }
  225. }
  226. return validate_arguments ();
  227. }
  228. int
  229. validate_arguments (void)
  230. {
  231. return OK;
  232. }
  233. void
  234. print_help (void)
  235. {
  236. char *myport;
  237. asprintf (&myport, "%d", DEFAULT_PORT);
  238. print_revision (progname, revision);
  239. printf (_("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n"));
  240. printf (_(COPYRIGHT), copyright, email);
  241. printf (_("Test the DNS service on the specified host using dig\n\n"));
  242. print_usage ();
  243. printf (_(UT_HELP_VRSN));
  244. printf (_(UT_HOST_PORT), 'P', myport);
  245. printf (_("\
  246. -l, --lookup=STRING\n\
  247. machine name to lookup\n"));
  248. printf (_(UT_WARN_CRIT));
  249. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  250. printf (_(UT_VERBOSE));
  251. printf (_(UT_SUPPORT));
  252. }
  253. void
  254. print_usage (void)
  255. {
  256. printf (_("\
  257. Usage: %s -H host -l lookup [-p <server port>] [-w <warning interval>]\n\
  258. [-c <critical interval>] [-t <timeout>] [-v]\n"),
  259. progname);
  260. printf (" %s (-h|--help)\n", progname);
  261. printf (" %s (-V|--version)\n", progname);
  262. }