check_dns.c 14 KB

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