check_dns.c 12 KB

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