check_dns.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*****************************************************************************
  2. *
  3. * Nagios check_dns plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2008 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_dns plugin
  11. *
  12. * LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which
  13. * will not be picked up by this plugin
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. *
  30. *****************************************************************************/
  31. const char *progname = "check_dns";
  32. const char *copyright = "2000-2008";
  33. const char *email = "devel@nagios-plugins.org";
  34. #include "common.h"
  35. #include "utils.h"
  36. #include "utils_base.h"
  37. #include "netutils.h"
  38. #include "runcmd.h"
  39. int process_arguments (int, char **);
  40. int validate_arguments (void);
  41. int error_scan (char *);
  42. void print_help (void);
  43. void print_usage (void);
  44. #define ADDRESS_LENGTH 384
  45. char query_address[ADDRESS_LENGTH] = "";
  46. char dns_server[ADDRESS_LENGTH] = "";
  47. char ptr_server[ADDRESS_LENGTH] = "";
  48. char query_type[16] = "";
  49. int verbose = FALSE;
  50. char **expected_address = NULL;
  51. int expected_address_cnt = 0;
  52. int expect_authority = FALSE;
  53. int accept_cname = FALSE;
  54. thresholds *time_thresholds = NULL;
  55. static int
  56. qstrcmp(const void *p1, const void *p2)
  57. {
  58. /* The actual arguments to this function are "pointers to
  59. pointers to char", but strcmp() arguments are "pointers
  60. to char", hence the following cast plus dereference */
  61. return strcmp(* (char * const *) p1, * (char * const *) p2);
  62. }
  63. char *
  64. check_new_address(char *temp_buffer)
  65. {
  66. temp_buffer++;
  67. /* Strip leading spaces */
  68. for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
  69. /* NOOP */;
  70. strip(temp_buffer);
  71. if (temp_buffer==NULL || strlen(temp_buffer)==0) {
  72. die (STATE_CRITICAL,
  73. _("DNS CRITICAL - '%s' returned empty host name string\n"),
  74. NSLOOKUP_COMMAND);
  75. }
  76. return temp_buffer;
  77. }
  78. int
  79. main (int argc, char **argv)
  80. {
  81. char *command_line = NULL;
  82. char input_buffer[MAX_INPUT_BUFFER];
  83. char *address = NULL; /* comma seperated str with addrs/ptrs (sorted) */
  84. char **addresses = NULL;
  85. int n_addresses = 0;
  86. char *msg = NULL;
  87. char *temp_buffer = NULL;
  88. int non_authoritative = FALSE;
  89. int result = STATE_UNKNOWN;
  90. double elapsed_time;
  91. long microsec;
  92. struct timeval tv;
  93. int multi_address;
  94. int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
  95. output chld_out, chld_err;
  96. size_t i;
  97. setlocale (LC_ALL, "");
  98. bindtextdomain (PACKAGE, LOCALEDIR);
  99. textdomain (PACKAGE);
  100. /* Set signal handling and alarm */
  101. if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) {
  102. usage_va(_("Cannot catch SIGALRM"));
  103. }
  104. /* Parse extra opts if any */
  105. argv=np_extra_opts (&argc, argv, progname);
  106. if (process_arguments (argc, argv) == ERROR) {
  107. usage_va(_("Could not parse arguments"));
  108. }
  109. /* get the command to run */
  110. xasprintf (&command_line, "%s %s %s %s", NSLOOKUP_COMMAND, query_type, query_address, dns_server);
  111. alarm (timeout_interval);
  112. gettimeofday (&tv, NULL);
  113. if (verbose)
  114. printf ("%s\n", command_line);
  115. /* run the command */
  116. if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
  117. msg = (char *)_("nslookup returned an error status");
  118. result = STATE_WARNING;
  119. }
  120. /* scan stdout */
  121. for(i = 0; i < chld_out.lines; i++) {
  122. if (addresses == NULL)
  123. addresses = malloc(sizeof(*addresses)*10);
  124. else if (!(n_addresses % 10))
  125. addresses = realloc(addresses,sizeof(*addresses) * (n_addresses + 10));
  126. if (verbose)
  127. puts(chld_out.line[i]);
  128. if (strstr (chld_out.line[i], ".in-addr.arpa")) {
  129. if ((temp_buffer = strstr (chld_out.line[i], "name = ")))
  130. addresses[n_addresses++] = strdup (temp_buffer + 7);
  131. else {
  132. msg = (char *)_("Warning plugin error");
  133. result = STATE_WARNING;
  134. }
  135. }
  136. if (strstr (chld_out.line[i], "Authoritative answers can be found from:"))
  137. break;
  138. /* the server is responding, we just got the host name... */
  139. if (strstr (chld_out.line[i], "Name:"))
  140. parse_address = TRUE;
  141. else if (strstr (chld_out.line[i], "AAAA address")) {
  142. temp_buffer = rindex (chld_out.line[i], ' ');
  143. addresses[n_addresses++] = check_new_address(temp_buffer);
  144. }
  145. else if (strstr (chld_out.line[i], "text =") || strstr (chld_out.line[i], "exchanger =") \
  146. || strstr (chld_out.line[i], "service =") || (accept_cname && strstr (chld_out.line[i], "name ="))) {
  147. temp_buffer = index (chld_out.line[i], '=');
  148. addresses[n_addresses++] = check_new_address(temp_buffer);
  149. }
  150. else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") ||
  151. strstr (chld_out.line[i], "Addresses:"))) {
  152. temp_buffer = index (chld_out.line[i], ':');
  153. addresses[n_addresses++] = check_new_address(temp_buffer);
  154. }
  155. if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
  156. non_authoritative = TRUE;
  157. }
  158. result = error_scan (chld_out.line[i]);
  159. if (result != STATE_OK) {
  160. msg = strchr (chld_out.line[i], ':');
  161. if(msg) msg++;
  162. break;
  163. }
  164. }
  165. /* scan stderr */
  166. for(i = 0; i < chld_err.lines; i++) {
  167. if (verbose)
  168. puts(chld_err.line[i]);
  169. if (error_scan (chld_err.line[i]) != STATE_OK) {
  170. result = max_state (result, error_scan (chld_err.line[i]));
  171. msg = strchr(input_buffer, ':');
  172. if(msg) msg++;
  173. }
  174. }
  175. if (addresses) {
  176. int i,slen;
  177. char *adrp;
  178. qsort(addresses, n_addresses, sizeof(*addresses), qstrcmp);
  179. for(i=0, slen=1; i < n_addresses; i++) {
  180. slen += strlen(addresses[i])+1;
  181. }
  182. adrp = address = malloc(slen);
  183. for(i=0; i < n_addresses; i++) {
  184. if (i) *adrp++ = ',';
  185. strcpy(adrp, addresses[i]);
  186. adrp += strlen(addresses[i]);
  187. }
  188. *adrp = 0;
  189. } else
  190. die (STATE_CRITICAL,
  191. _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
  192. NSLOOKUP_COMMAND);
  193. /* compare to expected address */
  194. if (result == STATE_OK && expected_address_cnt > 0) {
  195. result = STATE_CRITICAL;
  196. temp_buffer = "";
  197. for (i=0; i<expected_address_cnt; i++) {
  198. /* check if we get a match and prepare an error string */
  199. if (strcmp(address, expected_address[i]) == 0) result = STATE_OK;
  200. xasprintf(&temp_buffer, "%s%s; ", temp_buffer, expected_address[i]);
  201. }
  202. if (result == STATE_CRITICAL) {
  203. /* Strip off last semicolon... */
  204. temp_buffer[strlen(temp_buffer)-2] = '\0';
  205. xasprintf(&msg, _("expected '%s' but got '%s'"), temp_buffer, address);
  206. }
  207. }
  208. /* check if authoritative */
  209. if (result == STATE_OK && expect_authority && non_authoritative) {
  210. result = STATE_CRITICAL;
  211. if (strncmp(dns_server, "", 2))
  212. xasprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
  213. else
  214. xasprintf(&msg, _("there is no authoritative server for %s"), query_address);
  215. }
  216. microsec = deltime (tv);
  217. elapsed_time = (double)microsec / 1.0e6;
  218. if (result == STATE_OK) {
  219. if (strchr (address, ',') == NULL)
  220. multi_address = FALSE;
  221. else
  222. multi_address = TRUE;
  223. result = get_status(elapsed_time, time_thresholds);
  224. if (result == STATE_OK) {
  225. printf ("DNS %s: ", _("OK"));
  226. } else if (result == STATE_WARNING) {
  227. printf ("DNS %s: ", _("WARNING"));
  228. } else if (result == STATE_CRITICAL) {
  229. printf ("DNS %s: ", _("CRITICAL"));
  230. }
  231. printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
  232. printf (_(". %s returns %s"), query_address, address);
  233. printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  234. }
  235. else if (result == STATE_WARNING)
  236. printf (_("DNS WARNING - %s\n"),
  237. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  238. else if (result == STATE_CRITICAL)
  239. printf (_("DNS CRITICAL - %s\n"),
  240. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  241. else
  242. printf (_("DNS UNKNOWN - %s\n"),
  243. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  244. return result;
  245. }
  246. int
  247. error_scan (char *input_buffer)
  248. {
  249. /* the DNS lookup timed out */
  250. if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
  251. strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
  252. strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
  253. return STATE_OK;
  254. /* DNS server is not running... */
  255. else if (strstr (input_buffer, "No response from server"))
  256. die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
  257. /* Host name is valid, but server doesn't have records... */
  258. else if (strstr (input_buffer, "No records") || strstr (input_buffer, "No answer"))
  259. die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
  260. /* Connection was refused */
  261. else if (strstr (input_buffer, "Connection refused") ||
  262. strstr (input_buffer, "Couldn't find server") ||
  263. strstr (input_buffer, "Refused") ||
  264. (strstr (input_buffer, "** server can't find") &&
  265. strstr (input_buffer, ": REFUSED")))
  266. die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
  267. /* Query refused (usually by an ACL in the namserver) */
  268. else if (strstr (input_buffer, "Query refused"))
  269. die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
  270. /* No information (e.g. nameserver IP has two PTR records) */
  271. else if (strstr (input_buffer, "No information"))
  272. die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
  273. /* Host or domain name does not exist */
  274. else if (strstr (input_buffer, "Non-existent") ||
  275. strstr (input_buffer, "** server can't find") ||
  276. strstr (input_buffer,"NXDOMAIN"))
  277. die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
  278. /* Network is unreachable */
  279. else if (strstr (input_buffer, "Network is unreachable"))
  280. die (STATE_CRITICAL, _("Network is unreachable\n"));
  281. /* Internal server failure */
  282. else if (strstr (input_buffer, "Server failure"))
  283. die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
  284. /* Request error or the DNS lookup timed out */
  285. else if (strstr (input_buffer, "Format error") ||
  286. strstr (input_buffer, "Timed out"))
  287. return STATE_WARNING;
  288. return STATE_OK;
  289. }
  290. /* process command-line arguments */
  291. int
  292. process_arguments (int argc, char **argv)
  293. {
  294. int c;
  295. char *warning = NULL;
  296. char *critical = NULL;
  297. int opt_index = 0;
  298. static struct option long_opts[] = {
  299. {"help", no_argument, 0, 'h'},
  300. {"version", no_argument, 0, 'V'},
  301. {"verbose", no_argument, 0, 'v'},
  302. {"timeout", required_argument, 0, 't'},
  303. {"hostname", required_argument, 0, 'H'},
  304. {"server", required_argument, 0, 's'},
  305. {"reverse-server", required_argument, 0, 'r'},
  306. {"querytype", required_argument, 0, 'q'},
  307. {"expected-address", required_argument, 0, 'a'},
  308. {"expect-authority", no_argument, 0, 'A'},
  309. {"accept-cname", no_argument, 0, 'n'},
  310. {"warning", required_argument, 0, 'w'},
  311. {"critical", required_argument, 0, 'c'},
  312. {0, 0, 0, 0}
  313. };
  314. if (argc < 2)
  315. return ERROR;
  316. for (c = 1; c < argc; c++)
  317. if (strcmp ("-to", argv[c]) == 0)
  318. strcpy (argv[c], "-t");
  319. while (1) {
  320. c = getopt_long (argc, argv, "hVvAnt:H:s:r:a:q:w:c:", long_opts, &opt_index);
  321. if (c == -1 || c == EOF)
  322. break;
  323. switch (c) {
  324. case 'h': /* help */
  325. print_help ();
  326. exit (STATE_OK);
  327. case 'V': /* version */
  328. print_revision (progname, NP_VERSION);
  329. exit (STATE_OK);
  330. case 'v': /* version */
  331. verbose = TRUE;
  332. break;
  333. case 't': /* timeout period */
  334. timeout_interval = atoi (optarg);
  335. break;
  336. case 'H': /* hostname */
  337. if (strlen (optarg) >= ADDRESS_LENGTH)
  338. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  339. strcpy (query_address, optarg);
  340. break;
  341. case 's': /* server name */
  342. /* TODO: this host_or_die check is probably unnecessary.
  343. * Better to confirm nslookup response matches */
  344. host_or_die(optarg);
  345. if (strlen (optarg) >= ADDRESS_LENGTH)
  346. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  347. strcpy (dns_server, optarg);
  348. break;
  349. case 'r': /* reverse server name */
  350. /* TODO: Is this host_or_die necessary? */
  351. host_or_die(optarg);
  352. if (strlen (optarg) >= ADDRESS_LENGTH)
  353. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  354. strcpy (ptr_server, optarg);
  355. break;
  356. case 'a': /* expected address */
  357. if (strlen (optarg) >= ADDRESS_LENGTH)
  358. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  359. expected_address = (char **)realloc(expected_address, (expected_address_cnt+1) * sizeof(char**));
  360. expected_address[expected_address_cnt] = strdup(optarg);
  361. expected_address_cnt++;
  362. break;
  363. case 'q': /* querytype -- A or AAAA or ANY or SRV or TXT, etc. */
  364. if (strlen (optarg) < 1 || strlen (optarg) > 4)
  365. die (STATE_UNKNOWN, _("Missing valid querytype parameter. Try using 'A' or 'AAAA' or 'SRV'\n"));
  366. strcpy(query_type, "-querytype=");
  367. strcat(query_type, optarg);
  368. break;
  369. case 'A': /* expect authority */
  370. expect_authority = TRUE;
  371. break;
  372. case 'n': /* accept cname responses as a result */
  373. accept_cname = TRUE;
  374. break;
  375. case 'w':
  376. warning = optarg;
  377. break;
  378. case 'c':
  379. critical = optarg;
  380. break;
  381. default: /* args not parsable */
  382. usage5();
  383. }
  384. }
  385. c = optind;
  386. if (strlen(query_address)==0 && c<argc) {
  387. if (strlen(argv[c])>=ADDRESS_LENGTH)
  388. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  389. strcpy (query_address, argv[c++]);
  390. }
  391. if (strlen(dns_server)==0 && c<argc) {
  392. /* TODO: See -s option */
  393. host_or_die(argv[c]);
  394. if (strlen(argv[c]) >= ADDRESS_LENGTH)
  395. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  396. strcpy (dns_server, argv[c++]);
  397. }
  398. set_thresholds(&time_thresholds, warning, critical);
  399. return validate_arguments ();
  400. }
  401. int
  402. validate_arguments ()
  403. {
  404. if (query_address[0] == 0)
  405. return ERROR;
  406. return OK;
  407. }
  408. void
  409. print_help (void)
  410. {
  411. print_revision (progname, NP_VERSION);
  412. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  413. printf (COPYRIGHT, copyright, email);
  414. printf ("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
  415. printf ("%s\n", _("An optional DNS server to use may be specified."));
  416. printf ("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
  417. printf ("\n\n");
  418. print_usage ();
  419. printf (UT_HELP_VRSN);
  420. printf (UT_EXTRA_OPTS);
  421. printf (" -H, --hostname=HOST\n");
  422. printf (" %s\n", _("The name or address you want to query"));
  423. printf (" -s, --server=HOST\n");
  424. printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
  425. printf (" -q, --querytype=TYPE\n");
  426. printf (" %s\n", _("Optional DNS record query type where TYPE =(A, AAAA, SRV, TXT, MX, ANY)"));
  427. printf (" %s\n", _("The default query type is 'A' (IPv4 host entry)"));
  428. printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
  429. printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with"));
  430. printf (" %s\n", _("a dot (.). This option can be repeated multiple times (Returns OK if any"));
  431. printf (" %s\n", _("value match). If multiple addresses are returned at once, you have to match"));
  432. printf (" %s\n", _("the whole string of addresses separated with commas (sorted alphabetically)."));
  433. printf (" %s\n", _("If you would like to test for the presence of a cname, combine with -n param."));
  434. printf (" -A, --expect-authority\n");
  435. printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
  436. printf (" -n, --accept-cname\n");
  437. printf (" %s\n", _("Optionally accept cname responses as a valid result to a query"));
  438. printf (" %s\n", _("The default is to ignore cname responses as part of the result"));
  439. printf (" -w, --warning=seconds\n");
  440. printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
  441. printf (" -c, --critical=seconds\n");
  442. printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
  443. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  444. printf (UT_SUPPORT);
  445. }
  446. void
  447. print_usage (void)
  448. {
  449. printf ("%s\n", _("Usage:"));
  450. printf ("%s -H host [-s server] [-q type ] [-a expected-address] [-A] [-n] [-t timeout] [-w warn] [-c crit]\n", progname);
  451. }