check_dns.c 13 KB

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