check_dns.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which will not
  14. be picked up by this plugin
  15. $Id$
  16. ******************************************************************************/
  17. const char *progname = "check_dns";
  18. const char *revision = "$Revision$";
  19. const char *copyright = "2000-2005";
  20. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  21. #include "common.h"
  22. #include "utils.h"
  23. #include "netutils.h"
  24. #include "runcmd.h"
  25. int process_arguments (int, char **);
  26. int validate_arguments (void);
  27. int error_scan (char *);
  28. void print_help (void);
  29. void print_usage (void);
  30. #define ADDRESS_LENGTH 256
  31. char query_address[ADDRESS_LENGTH] = "";
  32. char dns_server[ADDRESS_LENGTH] = "";
  33. char ptr_server[ADDRESS_LENGTH] = "";
  34. int verbose = FALSE;
  35. char expected_address[ADDRESS_LENGTH] = "";
  36. int match_expected_address = FALSE;
  37. int expect_authority = FALSE;
  38. int
  39. main (int argc, char **argv)
  40. {
  41. char *command_line = NULL;
  42. char input_buffer[MAX_INPUT_BUFFER];
  43. char *address = NULL;
  44. char *msg = NULL;
  45. char *temp_buffer = NULL;
  46. int non_authoritative = FALSE;
  47. int result = STATE_UNKNOWN;
  48. double elapsed_time;
  49. long microsec;
  50. struct timeval tv;
  51. int multi_address;
  52. int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
  53. output chld_out, chld_err;
  54. size_t i;
  55. setlocale (LC_ALL, "");
  56. bindtextdomain (PACKAGE, LOCALEDIR);
  57. textdomain (PACKAGE);
  58. /* Set signal handling and alarm */
  59. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  60. usage_va(_("Cannot catch SIGALRM"));
  61. }
  62. if (process_arguments (argc, argv) == ERROR) {
  63. usage_va(_("Could not parse arguments"));
  64. }
  65. /* get the command to run */
  66. asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
  67. alarm (timeout_interval);
  68. gettimeofday (&tv, NULL);
  69. if (verbose)
  70. printf ("%s\n", command_line);
  71. /* run the command */
  72. if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
  73. msg = (char *)_("nslookup returned an error status");
  74. result = STATE_WARNING;
  75. }
  76. /* scan stdout */
  77. for(i = 0; i < chld_out.lines; i++) {
  78. if (verbose)
  79. puts(chld_out.line[i]);
  80. if (strstr (chld_out.line[i], ".in-addr.arpa")) {
  81. if ((temp_buffer = strstr (chld_out.line[i], "name = ")))
  82. address = strdup (temp_buffer + 7);
  83. else {
  84. msg = (char *)_("Warning plugin error");
  85. result = STATE_WARNING;
  86. }
  87. }
  88. /* the server is responding, we just got the host name... */
  89. if (strstr (chld_out.line[i], "Name:"))
  90. parse_address = TRUE;
  91. else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") ||
  92. strstr (chld_out.line[i], "Addresses:"))) {
  93. temp_buffer = index (chld_out.line[i], ':');
  94. temp_buffer++;
  95. /* Strip leading spaces */
  96. for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
  97. /* NOOP */;
  98. strip(temp_buffer);
  99. if (temp_buffer==NULL || strlen(temp_buffer)==0) {
  100. die (STATE_CRITICAL,
  101. _("DNS CRITICAL - '%s' returned empty host name string\n"),
  102. NSLOOKUP_COMMAND);
  103. }
  104. if (address == NULL)
  105. address = strdup (temp_buffer);
  106. else
  107. asprintf(&address, "%s,%s", address, temp_buffer);
  108. }
  109. else if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
  110. non_authoritative = TRUE;
  111. }
  112. result = error_scan (chld_out.line[i]);
  113. if (result != STATE_OK) {
  114. msg = strchr (chld_out.line[i], ':');
  115. if(msg) msg++;
  116. break;
  117. }
  118. }
  119. /* scan stderr */
  120. for(i = 0; i < chld_err.lines; i++) {
  121. if (verbose)
  122. puts(chld_err.line[i]);
  123. if (error_scan (chld_err.line[i]) != STATE_OK) {
  124. result = max_state (result, error_scan (chld_err.line[i]));
  125. msg = strchr(input_buffer, ':');
  126. if(msg) msg++;
  127. }
  128. }
  129. /* If we got here, we should have an address string,
  130. * and we can segfault if we do not */
  131. if (address==NULL || strlen(address)==0)
  132. die (STATE_CRITICAL,
  133. _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
  134. NSLOOKUP_COMMAND);
  135. /* compare to expected address */
  136. if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
  137. result = STATE_CRITICAL;
  138. asprintf(&msg, _("expected %s but got %s"), expected_address, address);
  139. }
  140. /* check if authoritative */
  141. if (result == STATE_OK && expect_authority && non_authoritative) {
  142. result = STATE_CRITICAL;
  143. asprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
  144. }
  145. microsec = deltime (tv);
  146. elapsed_time = (double)microsec / 1.0e6;
  147. if (result == STATE_OK) {
  148. if (strchr (address, ',') == NULL)
  149. multi_address = FALSE;
  150. else
  151. multi_address = TRUE;
  152. printf ("DNS %s: ", _("OK"));
  153. printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
  154. printf (_(". %s returns %s"), query_address, address);
  155. printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  156. }
  157. else if (result == STATE_WARNING)
  158. printf (_("DNS WARNING - %s\n"),
  159. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  160. else if (result == STATE_CRITICAL)
  161. printf (_("DNS CRITICAL - %s\n"),
  162. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  163. else
  164. printf (_("DNS UNKNOW - %s\n"),
  165. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  166. return result;
  167. }
  168. int
  169. error_scan (char *input_buffer)
  170. {
  171. /* the DNS lookup timed out */
  172. if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
  173. strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
  174. strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
  175. return STATE_OK;
  176. /* DNS server is not running... */
  177. else if (strstr (input_buffer, "No response from server"))
  178. die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
  179. /* Host name is valid, but server doesn't have records... */
  180. else if (strstr (input_buffer, "No records"))
  181. die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
  182. /* Connection was refused */
  183. else if (strstr (input_buffer, "Connection refused") ||
  184. strstr (input_buffer, "Couldn't find server") ||
  185. strstr (input_buffer, "Refused") ||
  186. (strstr (input_buffer, "** server can't find") &&
  187. strstr (input_buffer, ": REFUSED")))
  188. die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
  189. /* Query refused (usually by an ACL in the namserver) */
  190. else if (strstr (input_buffer, "Query refused"))
  191. die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
  192. /* No information (e.g. nameserver IP has two PTR records) */
  193. else if (strstr (input_buffer, "No information"))
  194. die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
  195. /* Host or domain name does not exist */
  196. else if (strstr (input_buffer, "Non-existent") ||
  197. strstr (input_buffer, "** server can't find") ||
  198. strstr (input_buffer,"NXDOMAIN"))
  199. die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
  200. /* Network is unreachable */
  201. else if (strstr (input_buffer, "Network is unreachable"))
  202. die (STATE_CRITICAL, _("Network is unreachable\n"));
  203. /* Internal server failure */
  204. else if (strstr (input_buffer, "Server failure"))
  205. die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
  206. /* Request error or the DNS lookup timed out */
  207. else if (strstr (input_buffer, "Format error") ||
  208. strstr (input_buffer, "Timed out"))
  209. return STATE_WARNING;
  210. return STATE_OK;
  211. }
  212. /* process command-line arguments */
  213. int
  214. process_arguments (int argc, char **argv)
  215. {
  216. int c;
  217. int opt_index = 0;
  218. static struct option long_opts[] = {
  219. {"help", no_argument, 0, 'h'},
  220. {"version", no_argument, 0, 'V'},
  221. {"verbose", no_argument, 0, 'v'},
  222. {"timeout", required_argument, 0, 't'},
  223. {"hostname", required_argument, 0, 'H'},
  224. {"server", required_argument, 0, 's'},
  225. {"reverse-server", required_argument, 0, 'r'},
  226. {"expected-address", required_argument, 0, 'a'},
  227. {"expect-authority", no_argument, 0, 'A'},
  228. {0, 0, 0, 0}
  229. };
  230. if (argc < 2)
  231. return ERROR;
  232. for (c = 1; c < argc; c++)
  233. if (strcmp ("-to", argv[c]) == 0)
  234. strcpy (argv[c], "-t");
  235. while (1) {
  236. c = getopt_long (argc, argv, "hVvAt:H:s:r:a:", long_opts, &opt_index);
  237. if (c == -1 || c == EOF)
  238. break;
  239. switch (c) {
  240. case 'h': /* help */
  241. print_help ();
  242. exit (STATE_OK);
  243. case 'V': /* version */
  244. print_revision (progname, revision);
  245. exit (STATE_OK);
  246. case 'v': /* version */
  247. verbose = TRUE;
  248. break;
  249. case 't': /* timeout period */
  250. timeout_interval = atoi (optarg);
  251. break;
  252. case 'H': /* hostname */
  253. if (strlen (optarg) >= ADDRESS_LENGTH)
  254. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  255. strcpy (query_address, optarg);
  256. break;
  257. case 's': /* server name */
  258. /* TODO: this host_or_die check is probably unnecessary.
  259. * Better to confirm nslookup response matches */
  260. host_or_die(optarg);
  261. if (strlen (optarg) >= ADDRESS_LENGTH)
  262. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  263. strcpy (dns_server, optarg);
  264. break;
  265. case 'r': /* reverse server name */
  266. /* TODO: Is this host_or_die necessary? */
  267. host_or_die(optarg);
  268. if (strlen (optarg) >= ADDRESS_LENGTH)
  269. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  270. strcpy (ptr_server, optarg);
  271. break;
  272. case 'a': /* expected address */
  273. if (strlen (optarg) >= ADDRESS_LENGTH)
  274. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  275. strcpy (expected_address, optarg);
  276. match_expected_address = TRUE;
  277. break;
  278. case 'A': /* expect authority */
  279. expect_authority = TRUE;
  280. break;
  281. default: /* args not parsable */
  282. usage_va(_("Unknown argument - %s"), optarg);
  283. }
  284. }
  285. c = optind;
  286. if (strlen(query_address)==0 && c<argc) {
  287. if (strlen(argv[c])>=ADDRESS_LENGTH)
  288. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  289. strcpy (query_address, argv[c++]);
  290. }
  291. if (strlen(dns_server)==0 && c<argc) {
  292. /* TODO: See -s option */
  293. host_or_die(argv[c]);
  294. if (strlen(argv[c]) >= ADDRESS_LENGTH)
  295. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  296. strcpy (dns_server, argv[c++]);
  297. }
  298. return validate_arguments ();
  299. }
  300. int
  301. validate_arguments ()
  302. {
  303. if (query_address[0] == 0)
  304. return ERROR;
  305. return OK;
  306. }
  307. void
  308. print_help (void)
  309. {
  310. print_revision (progname, revision);
  311. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  312. printf (COPYRIGHT, copyright, email);
  313. printf (_("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
  314. printf ("\n");
  315. printf (_("An optional DNS server to use may be specified."));
  316. printf ("\n");
  317. printf (_("If no DNS server is specified, the default server(s)specified in /etc/resolv.conf will be used."));
  318. printf ("\n\n");
  319. print_usage ();
  320. printf (_(UT_HELP_VRSN));
  321. printf (" -H, --hostname=HOST");
  322. printf ("\n");
  323. printf (_("the name or address you want to query"));
  324. printf ("\n");
  325. printf (" -s, --server=HOST");
  326. printf ("\n");
  327. printf (_("optional DNS server you want to use for the lookup"));
  328. printf ("\n");
  329. printf (" -a, --expected-address=IP-ADDRESS");
  330. printf ("\n");
  331. printf (_("optional IP address you expect the DNS server to return"));
  332. printf ("\n");
  333. printf (" -A, --expect-authority");
  334. printf ("\n");
  335. printf (_("optionally expect the DNS server to be authoritative for the lookup"));
  336. printf ("\n");
  337. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  338. printf (_(UT_SUPPORT));
  339. }
  340. void
  341. print_usage (void)
  342. {
  343. printf (_("Usage:"));
  344. printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n", progname);
  345. }