check_dns.c 15 KB

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