check_dns.c 15 KB

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