check_dns.c 12 KB

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