check_dns.c 13 KB

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