check_dns.c 12 KB

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