check_dig.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. $Id$
  14. *****************************************************************************/
  15. /* Hackers note:
  16. * There are typecasts to (char *) from _("foo bar") in this file.
  17. * They prevent compiler warnings. Never (ever), permute strings obtained
  18. * that are typecast from (const char *) (which happens when --disable-nls)
  19. * because on some architectures those strings are in non-writable memory */
  20. const char *progname = "check_dig";
  21. const char *revision = "$Revision$";
  22. const char *copyright = "2002-2005";
  23. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  24. #include "common.h"
  25. #include "netutils.h"
  26. #include "utils.h"
  27. #include "runcmd.h"
  28. int process_arguments (int, char **);
  29. int validate_arguments (void);
  30. void print_help (void);
  31. void print_usage (void);
  32. #define UNDEFINED 0
  33. #define DEFAULT_PORT 53
  34. char *query_address = NULL;
  35. char *record_type = "A";
  36. char *expected_address = NULL;
  37. char *dns_server = NULL;
  38. int verbose = FALSE;
  39. int server_port = DEFAULT_PORT;
  40. double warning_interval = UNDEFINED;
  41. double critical_interval = UNDEFINED;
  42. struct timeval tv;
  43. int
  44. main (int argc, char **argv)
  45. {
  46. char *command_line;
  47. output chld_out, chld_err;
  48. char *msg = NULL;
  49. size_t i;
  50. char *t;
  51. long microsec;
  52. double elapsed_time;
  53. int result = STATE_UNKNOWN;
  54. setlocale (LC_ALL, "");
  55. bindtextdomain (PACKAGE, LOCALEDIR);
  56. textdomain (PACKAGE);
  57. /* Set signal handling and alarm */
  58. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR)
  59. usage_va(_("Cannot catch SIGALRM"));
  60. if (process_arguments (argc, argv) == ERROR)
  61. usage_va(_("Could not parse arguments"));
  62. /* get the command to run */
  63. asprintf (&command_line, "%s @%s -p %d %s -t %s",
  64. PATH_TO_DIG, dns_server, server_port, query_address, record_type);
  65. alarm (timeout_interval);
  66. gettimeofday (&tv, NULL);
  67. if (verbose) {
  68. printf ("%s\n", command_line);
  69. if(expected_address != NULL) {
  70. printf (_("Looking for: '%s'\n"), expected_address);
  71. } else {
  72. printf (_("Looking for: '%s'\n"), query_address);
  73. }
  74. }
  75. /* run the command */
  76. if(np_runcmd(command_line, &chld_out, &chld_err, 0) != 0) {
  77. result = STATE_WARNING;
  78. msg = (char *)_("dig returned an error status");
  79. }
  80. for(i = 0; i < chld_out.lines; i++) {
  81. /* the server is responding, we just got the host name... */
  82. if (strstr (chld_out.line[i], ";; ANSWER SECTION:")) {
  83. /* loop through the whole 'ANSWER SECTION' */
  84. for(; i < chld_out.lines; i++) {
  85. /* get the host address */
  86. if (verbose)
  87. printf ("%s\n", chld_out.line[i]);
  88. if (strstr (chld_out.line[i], (expected_address == NULL ? query_address : expected_address)) != NULL) {
  89. msg = chld_out.line[i];
  90. result = STATE_OK;
  91. /* Translate output TAB -> SPACE */
  92. t = msg;
  93. while ((t = strchr(t, '\t')) != NULL) *t = ' ';
  94. break;
  95. }
  96. }
  97. if (result == STATE_UNKNOWN) {
  98. msg = (char *)_("Server not found in ANSWER SECTION");
  99. result = STATE_WARNING;
  100. }
  101. /* we found the answer section, so break out of the loop */
  102. break;
  103. }
  104. }
  105. if (result == STATE_UNKNOWN)
  106. msg = (char *)_("No ANSWER SECTION found");
  107. /* If we get anything on STDERR, at least set warning */
  108. if(chld_err.buflen > 0) {
  109. result = max_state(result, STATE_WARNING);
  110. if(!msg) for(i = 0; i < chld_err.lines; i++) {
  111. msg = strchr(chld_err.line[0], ':');
  112. if(msg) {
  113. msg++;
  114. break;
  115. }
  116. }
  117. }
  118. microsec = deltime (tv);
  119. elapsed_time = (double)microsec / 1.0e6;
  120. if (critical_interval > UNDEFINED && elapsed_time > critical_interval)
  121. result = STATE_CRITICAL;
  122. else if (warning_interval > UNDEFINED && elapsed_time > warning_interval)
  123. result = STATE_WARNING;
  124. printf ("DNS %s - %.3f seconds response time (%s)|%s\n",
  125. state_text (result), elapsed_time,
  126. msg ? msg : _("Probably a non-existent host/domain"),
  127. fperfdata("time", elapsed_time, "s",
  128. (warning_interval>UNDEFINED?TRUE:FALSE),
  129. warning_interval,
  130. (critical_interval>UNDEFINED?TRUE:FALSE),
  131. critical_interval,
  132. TRUE, 0, FALSE, 0));
  133. return result;
  134. }
  135. /* process command-line arguments */
  136. int
  137. process_arguments (int argc, char **argv)
  138. {
  139. int c;
  140. int option = 0;
  141. static struct option longopts[] = {
  142. {"hostname", required_argument, 0, 'H'},
  143. {"query_address", required_argument, 0, 'l'},
  144. {"warning", required_argument, 0, 'w'},
  145. {"critical", required_argument, 0, 'c'},
  146. {"timeout", required_argument, 0, 't'},
  147. {"verbose", no_argument, 0, 'v'},
  148. {"version", no_argument, 0, 'V'},
  149. {"help", no_argument, 0, 'h'},
  150. {"record_type", required_argument, 0, 'T'},
  151. {"expected_address", required_argument, 0, 'a'},
  152. {0, 0, 0, 0}
  153. };
  154. if (argc < 2)
  155. return ERROR;
  156. while (1) {
  157. c = getopt_long (argc, argv, "hVvt:l:H:w:c:T:a:", longopts, &option);
  158. if (c == -1 || c == EOF)
  159. break;
  160. switch (c) {
  161. case 'h': /* help */
  162. print_help ();
  163. exit (STATE_OK);
  164. case 'V': /* version */
  165. print_revision (progname, revision);
  166. exit (STATE_OK);
  167. case 'H': /* hostname */
  168. host_or_die(optarg);
  169. dns_server = optarg;
  170. break;
  171. case 'p': /* server port */
  172. if (is_intpos (optarg)) {
  173. server_port = atoi (optarg);
  174. }
  175. else {
  176. usage_va(_("Port must be a positive integer - %s"), optarg);
  177. }
  178. break;
  179. case 'l': /* address to lookup */
  180. query_address = optarg;
  181. break;
  182. case 'w': /* warning */
  183. if (is_nonnegative (optarg)) {
  184. warning_interval = strtod (optarg, NULL);
  185. }
  186. else {
  187. usage_va(_("Warning interval must be a positive integer - %s"), optarg);
  188. }
  189. break;
  190. case 'c': /* critical */
  191. if (is_nonnegative (optarg)) {
  192. critical_interval = strtod (optarg, NULL);
  193. }
  194. else {
  195. usage_va(_("Critical interval must be a positive integer - %s"), optarg);
  196. }
  197. break;
  198. case 't': /* timeout */
  199. if (is_intnonneg (optarg)) {
  200. timeout_interval = atoi (optarg);
  201. }
  202. else {
  203. usage_va(_("Timeout interval must be a positive integer - %s"), optarg);
  204. }
  205. break;
  206. case 'v': /* verbose */
  207. verbose = TRUE;
  208. break;
  209. case 'T':
  210. record_type = optarg;
  211. break;
  212. case 'a':
  213. expected_address = optarg;
  214. break;
  215. default: /* usage_va */
  216. usage_va(_("Unknown argument - %s"), optarg);
  217. }
  218. }
  219. c = optind;
  220. if (dns_server == NULL) {
  221. if (c < argc) {
  222. host_or_die(argv[c]);
  223. dns_server = argv[c];
  224. }
  225. else {
  226. dns_server = strdup ("127.0.0.1");
  227. }
  228. }
  229. return validate_arguments ();
  230. }
  231. int
  232. validate_arguments (void)
  233. {
  234. return OK;
  235. }
  236. void
  237. print_help (void)
  238. {
  239. char *myport;
  240. asprintf (&myport, "%d", DEFAULT_PORT);
  241. print_revision (progname, revision);
  242. printf ("Copyright (c) 2000 Karl DeBisschop <kdebisschop@users.sourceforge.net>\n");
  243. printf (COPYRIGHT, copyright, email);
  244. printf (_("This plugin test the DNS service on the specified host using dig"));
  245. printf ("\n\n");
  246. print_usage ();
  247. printf (_(UT_HELP_VRSN));
  248. printf (_(UT_HOST_PORT), 'P', myport);
  249. printf ("-l, --lookup=STRING");
  250. printf ("\n");
  251. printf (_("machine name to lookup"));
  252. printf ("\n");
  253. printf ("-T, --record_type=STRING");
  254. printf ("\n");
  255. printf (_("record type to lookup (default: A)"));
  256. printf ("\n");
  257. printf ("-a, --expected_address=STRING");
  258. printf ("\n");
  259. printf (_("an address expected to be in the answer section.if not set, uses whatever was in -l"));
  260. printf ("\n");
  261. printf (_(UT_WARN_CRIT));
  262. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  263. printf (_(UT_VERBOSE));
  264. printf (_(UT_SUPPORT));
  265. }
  266. void
  267. print_usage (void)
  268. {
  269. printf (_("Usage:"));
  270. printf ("%s -H host -l lookup [-p <server port>] [-T <query type>]", progname);
  271. printf (" [-w <warning interval>] [-c <critical interval>] [-t <timeout>]");
  272. printf (" [-a <expected answer address>] [-v]\n");
  273. }