check_dns.c 12 KB

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