check_dns.c 14 KB

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