check_dns.c 13 KB

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