check_dns.c 12 KB

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