check_dns.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 = "nagiosplug-devel@lists.sourceforge.net";
  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 256
  45. char query_address[ADDRESS_LENGTH] = "";
  46. char dns_server[ADDRESS_LENGTH] = "";
  47. char ptr_server[ADDRESS_LENGTH] = "";
  48. int verbose = FALSE;
  49. char **expected_address = NULL;
  50. int expected_address_cnt = 0;
  51. int expect_authority = FALSE;
  52. thresholds *time_thresholds = NULL;
  53. static int
  54. qstrcmp(const void *p1, const void *p2)
  55. {
  56. /* The actual arguments to this function are "pointers to
  57. pointers to char", but strcmp() arguments are "pointers
  58. to char", hence the following cast plus dereference */
  59. return strcmp(* (char * const *) p1, * (char * const *) p2);
  60. }
  61. int
  62. main (int argc, char **argv)
  63. {
  64. char *command_line = NULL;
  65. char input_buffer[MAX_INPUT_BUFFER];
  66. char *address = NULL; /* comma seperated str with addrs/ptrs (sorted) */
  67. char **addresses = NULL;
  68. int n_addresses = 0;
  69. char *msg = NULL;
  70. char *temp_buffer = NULL;
  71. int non_authoritative = FALSE;
  72. int result = STATE_UNKNOWN;
  73. double elapsed_time;
  74. long microsec;
  75. struct timeval tv;
  76. int multi_address;
  77. int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
  78. output chld_out, chld_err;
  79. size_t i;
  80. setlocale (LC_ALL, "");
  81. bindtextdomain (PACKAGE, LOCALEDIR);
  82. textdomain (PACKAGE);
  83. /* Set signal handling and alarm */
  84. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  85. usage_va(_("Cannot catch SIGALRM"));
  86. }
  87. /* Parse extra opts if any */
  88. argv=np_extra_opts (&argc, argv, progname);
  89. if (process_arguments (argc, argv) == ERROR) {
  90. usage_va(_("Could not parse arguments"));
  91. }
  92. /* get the command to run */
  93. xasprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
  94. alarm (timeout_interval);
  95. gettimeofday (&tv, NULL);
  96. if (verbose)
  97. printf ("%s\n", command_line);
  98. /* run the command */
  99. if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
  100. msg = (char *)_("nslookup returned an error status");
  101. result = STATE_WARNING;
  102. }
  103. /* scan stdout */
  104. for(i = 0; i < chld_out.lines; i++) {
  105. if (addresses == NULL)
  106. addresses = malloc(sizeof(*addresses)*10);
  107. else if (!(n_addresses % 10))
  108. addresses = realloc(addresses,sizeof(*addresses) * (n_addresses + 10));
  109. if (verbose)
  110. puts(chld_out.line[i]);
  111. if (strstr (chld_out.line[i], ".in-addr.arpa")) {
  112. if ((temp_buffer = strstr (chld_out.line[i], "name = ")))
  113. addresses[n_addresses++] = strdup (temp_buffer + 7);
  114. else {
  115. msg = (char *)_("Warning plugin error");
  116. result = STATE_WARNING;
  117. }
  118. }
  119. /* the server is responding, we just got the host name... */
  120. if (strstr (chld_out.line[i], "Name:"))
  121. parse_address = TRUE;
  122. else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") ||
  123. strstr (chld_out.line[i], "Addresses:"))) {
  124. temp_buffer = index (chld_out.line[i], ':');
  125. temp_buffer++;
  126. /* Strip leading spaces */
  127. for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
  128. /* NOOP */;
  129. strip(temp_buffer);
  130. if (temp_buffer==NULL || strlen(temp_buffer)==0) {
  131. die (STATE_CRITICAL,
  132. _("DNS CRITICAL - '%s' returned empty host name string\n"),
  133. NSLOOKUP_COMMAND);
  134. }
  135. addresses[n_addresses++] = strdup(temp_buffer);
  136. }
  137. else if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
  138. non_authoritative = TRUE;
  139. }
  140. result = error_scan (chld_out.line[i]);
  141. if (result != STATE_OK) {
  142. msg = strchr (chld_out.line[i], ':');
  143. if(msg) msg++;
  144. break;
  145. }
  146. }
  147. /* scan stderr */
  148. for(i = 0; i < chld_err.lines; i++) {
  149. if (verbose)
  150. puts(chld_err.line[i]);
  151. if (error_scan (chld_err.line[i]) != STATE_OK) {
  152. result = max_state (result, error_scan (chld_err.line[i]));
  153. msg = strchr(input_buffer, ':');
  154. if(msg) msg++;
  155. }
  156. }
  157. if (addresses) {
  158. int i,slen;
  159. char *adrp;
  160. qsort(addresses, n_addresses, sizeof(*addresses), qstrcmp);
  161. for(i=0, slen=1; i < n_addresses; i++) {
  162. slen += strlen(addresses[i])+1;
  163. }
  164. adrp = address = malloc(slen);
  165. for(i=0; i < n_addresses; i++) {
  166. if (i) *adrp++ = ',';
  167. strcpy(adrp, addresses[i]);
  168. adrp += strlen(addresses[i]);
  169. }
  170. *adrp = 0;
  171. } else
  172. die (STATE_CRITICAL,
  173. _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
  174. NSLOOKUP_COMMAND);
  175. /* compare to expected address */
  176. if (result == STATE_OK && expected_address_cnt > 0) {
  177. result = STATE_CRITICAL;
  178. temp_buffer = "";
  179. for (i=0; i<expected_address_cnt; i++) {
  180. /* check if we get a match and prepare an error string */
  181. if (strcmp(address, expected_address[i]) == 0) result = STATE_OK;
  182. xasprintf(&temp_buffer, "%s%s; ", temp_buffer, expected_address[i]);
  183. }
  184. if (result == STATE_CRITICAL) {
  185. /* Strip off last semicolon... */
  186. temp_buffer[strlen(temp_buffer)-2] = '\0';
  187. xasprintf(&msg, _("expected '%s' but got '%s'"), temp_buffer, address);
  188. }
  189. }
  190. /* check if authoritative */
  191. if (result == STATE_OK && expect_authority && non_authoritative) {
  192. result = STATE_CRITICAL;
  193. xasprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
  194. }
  195. microsec = deltime (tv);
  196. elapsed_time = (double)microsec / 1.0e6;
  197. if (result == STATE_OK) {
  198. if (strchr (address, ',') == NULL)
  199. multi_address = FALSE;
  200. else
  201. multi_address = TRUE;
  202. result = get_status(elapsed_time, time_thresholds);
  203. if (result == STATE_OK) {
  204. printf ("DNS %s: ", _("OK"));
  205. } else if (result == STATE_WARNING) {
  206. printf ("DNS %s: ", _("WARNING"));
  207. } else if (result == STATE_CRITICAL) {
  208. printf ("DNS %s: ", _("CRITICAL"));
  209. }
  210. printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
  211. printf (_(". %s returns %s"), query_address, address);
  212. printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  213. }
  214. else if (result == STATE_WARNING)
  215. printf (_("DNS WARNING - %s\n"),
  216. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  217. else if (result == STATE_CRITICAL)
  218. printf (_("DNS CRITICAL - %s\n"),
  219. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  220. else
  221. printf (_("DNS UNKNOWN - %s\n"),
  222. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  223. return result;
  224. }
  225. int
  226. error_scan (char *input_buffer)
  227. {
  228. /* the DNS lookup timed out */
  229. if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
  230. strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
  231. strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
  232. return STATE_OK;
  233. /* DNS server is not running... */
  234. else if (strstr (input_buffer, "No response from server"))
  235. die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
  236. /* Host name is valid, but server doesn't have records... */
  237. else if (strstr (input_buffer, "No records"))
  238. die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
  239. /* Connection was refused */
  240. else if (strstr (input_buffer, "Connection refused") ||
  241. strstr (input_buffer, "Couldn't find server") ||
  242. strstr (input_buffer, "Refused") ||
  243. (strstr (input_buffer, "** server can't find") &&
  244. strstr (input_buffer, ": REFUSED")))
  245. die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
  246. /* Query refused (usually by an ACL in the namserver) */
  247. else if (strstr (input_buffer, "Query refused"))
  248. die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
  249. /* No information (e.g. nameserver IP has two PTR records) */
  250. else if (strstr (input_buffer, "No information"))
  251. die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
  252. /* Host or domain name does not exist */
  253. else if (strstr (input_buffer, "Non-existent") ||
  254. strstr (input_buffer, "** server can't find") ||
  255. strstr (input_buffer,"NXDOMAIN"))
  256. die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
  257. /* Network is unreachable */
  258. else if (strstr (input_buffer, "Network is unreachable"))
  259. die (STATE_CRITICAL, _("Network is unreachable\n"));
  260. /* Internal server failure */
  261. else if (strstr (input_buffer, "Server failure"))
  262. die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
  263. /* Request error or the DNS lookup timed out */
  264. else if (strstr (input_buffer, "Format error") ||
  265. strstr (input_buffer, "Timed out"))
  266. return STATE_WARNING;
  267. return STATE_OK;
  268. }
  269. /* process command-line arguments */
  270. int
  271. process_arguments (int argc, char **argv)
  272. {
  273. int c;
  274. char *warning = NULL;
  275. char *critical = NULL;
  276. int opt_index = 0;
  277. static struct option long_opts[] = {
  278. {"help", no_argument, 0, 'h'},
  279. {"version", no_argument, 0, 'V'},
  280. {"verbose", no_argument, 0, 'v'},
  281. {"timeout", required_argument, 0, 't'},
  282. {"hostname", required_argument, 0, 'H'},
  283. {"server", required_argument, 0, 's'},
  284. {"reverse-server", required_argument, 0, 'r'},
  285. {"expected-address", required_argument, 0, 'a'},
  286. {"expect-authority", no_argument, 0, 'A'},
  287. {"warning", required_argument, 0, 'w'},
  288. {"critical", required_argument, 0, 'c'},
  289. {0, 0, 0, 0}
  290. };
  291. if (argc < 2)
  292. return ERROR;
  293. for (c = 1; c < argc; c++)
  294. if (strcmp ("-to", argv[c]) == 0)
  295. strcpy (argv[c], "-t");
  296. while (1) {
  297. c = getopt_long (argc, argv, "hVvAt:H:s:r:a:w:c:", long_opts, &opt_index);
  298. if (c == -1 || c == EOF)
  299. break;
  300. switch (c) {
  301. case 'h': /* help */
  302. print_help ();
  303. exit (STATE_OK);
  304. case 'V': /* version */
  305. print_revision (progname, NP_VERSION);
  306. exit (STATE_OK);
  307. case 'v': /* version */
  308. verbose = TRUE;
  309. break;
  310. case 't': /* timeout period */
  311. timeout_interval = atoi (optarg);
  312. break;
  313. case 'H': /* hostname */
  314. if (strlen (optarg) >= ADDRESS_LENGTH)
  315. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  316. strcpy (query_address, optarg);
  317. break;
  318. case 's': /* server name */
  319. /* TODO: this host_or_die check is probably unnecessary.
  320. * Better to confirm nslookup response matches */
  321. host_or_die(optarg);
  322. if (strlen (optarg) >= ADDRESS_LENGTH)
  323. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  324. strcpy (dns_server, optarg);
  325. break;
  326. case 'r': /* reverse server name */
  327. /* TODO: Is this host_or_die necessary? */
  328. host_or_die(optarg);
  329. if (strlen (optarg) >= ADDRESS_LENGTH)
  330. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  331. strcpy (ptr_server, optarg);
  332. break;
  333. case 'a': /* expected address */
  334. if (strlen (optarg) >= ADDRESS_LENGTH)
  335. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  336. expected_address = (char **)realloc(expected_address, (expected_address_cnt+1) * sizeof(char**));
  337. expected_address[expected_address_cnt] = strdup(optarg);
  338. expected_address_cnt++;
  339. break;
  340. case 'A': /* expect authority */
  341. expect_authority = TRUE;
  342. break;
  343. case 'w':
  344. warning = optarg;
  345. break;
  346. case 'c':
  347. critical = optarg;
  348. break;
  349. default: /* args not parsable */
  350. usage5();
  351. }
  352. }
  353. c = optind;
  354. if (strlen(query_address)==0 && c<argc) {
  355. if (strlen(argv[c])>=ADDRESS_LENGTH)
  356. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  357. strcpy (query_address, argv[c++]);
  358. }
  359. if (strlen(dns_server)==0 && c<argc) {
  360. /* TODO: See -s option */
  361. host_or_die(argv[c]);
  362. if (strlen(argv[c]) >= ADDRESS_LENGTH)
  363. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  364. strcpy (dns_server, argv[c++]);
  365. }
  366. set_thresholds(&time_thresholds, warning, critical);
  367. return validate_arguments ();
  368. }
  369. int
  370. validate_arguments ()
  371. {
  372. if (query_address[0] == 0)
  373. return ERROR;
  374. return OK;
  375. }
  376. void
  377. print_help (void)
  378. {
  379. print_revision (progname, NP_VERSION);
  380. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  381. printf (COPYRIGHT, copyright, email);
  382. printf ("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
  383. printf ("%s\n", _("An optional DNS server to use may be specified."));
  384. printf ("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
  385. printf ("\n\n");
  386. print_usage ();
  387. printf (UT_HELP_VRSN);
  388. printf (UT_EXTRA_OPTS);
  389. printf (" -H, --hostname=HOST\n");
  390. printf (" %s\n", _("The name or address you want to query"));
  391. printf (" -s, --server=HOST\n");
  392. printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
  393. printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
  394. printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with"));
  395. printf (" %s\n", _("a dot (.). This option can be repeated multiple times (Returns OK if any"));
  396. printf (" %s\n", _("value match). If multiple addresses are returned at once, you have to match"));
  397. printf (" %s\n", _("the whole string of addresses separated with commas (sorted alphabetically)."));
  398. printf (" -A, --expect-authority\n");
  399. printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
  400. printf (" -w, --warning=seconds\n");
  401. printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
  402. printf (" -c, --critical=seconds\n");
  403. printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
  404. printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  405. printf (UT_SUPPORT);
  406. }
  407. void
  408. print_usage (void)
  409. {
  410. printf ("%s\n", _("Usage:"));
  411. printf ("%s -H host [-s server] [-a expected-address] [-A] [-t timeout] [-w warn] [-c crit]\n", progname);
  412. }