check_dns.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*****************************************************************************
  2. *
  3. * Nagios check_dns plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_dns plugin
  11. *
  12. * LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which
  13. * will not be picked up by this plugin
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. *
  30. *****************************************************************************/
  31. const char *progname = "check_dns";
  32. const char *copyright = "2000-2014";
  33. const char *email = "devel@nagios-plugins.org";
  34. #include "common.h"
  35. #include "utils.h"
  36. #include "utils_base.h"
  37. #include "netutils.h"
  38. #include "runcmd.h"
  39. int process_arguments (int, char **);
  40. int validate_arguments (void);
  41. int error_scan (char *);
  42. void print_help (void);
  43. void print_usage (void);
  44. #define ADDRESS_LENGTH 384
  45. char query_address[ADDRESS_LENGTH] = "";
  46. char dns_server[ADDRESS_LENGTH] = "";
  47. char ptr_server[ADDRESS_LENGTH] = "";
  48. char query_type[16] = "";
  49. int verbose = FALSE;
  50. char **expected_address = NULL;
  51. int expected_address_cnt = 0;
  52. int expect_authority = FALSE;
  53. int accept_cname = FALSE;
  54. thresholds *time_thresholds = NULL;
  55. static int
  56. qstrcmp(const void *p1, const void *p2)
  57. {
  58. /* The actual arguments to this function are "pointers to
  59. pointers to char", but strcmp() arguments are "pointers
  60. to char", hence the following cast plus dereference */
  61. return strcmp(* (char * const *) p1, * (char * const *) p2);
  62. }
  63. char *
  64. check_new_address(char *temp_buffer)
  65. {
  66. temp_buffer++;
  67. /* Strip leading spaces */
  68. for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
  69. /* NOOP */;
  70. strip(temp_buffer);
  71. if (temp_buffer==NULL || strlen(temp_buffer)==0) {
  72. die (STATE_CRITICAL,
  73. _("DNS CRITICAL - '%s' returned empty host name string\n"),
  74. NSLOOKUP_COMMAND);
  75. }
  76. return temp_buffer;
  77. }
  78. int
  79. main (int argc, char **argv)
  80. {
  81. char *command_line = NULL;
  82. char input_buffer[MAX_INPUT_BUFFER];
  83. char *address = NULL; /* comma seperated str with addrs/ptrs (sorted) */
  84. char **addresses = NULL;
  85. int n_addresses = 0;
  86. char *msg = NULL;
  87. char query_found[16] = "";
  88. int query_size = 16;
  89. char *temp_buffer = NULL;
  90. int non_authoritative = TRUE;
  91. int result = STATE_UNKNOWN;
  92. double elapsed_time;
  93. long microsec;
  94. struct timeval tv;
  95. int multi_address;
  96. int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
  97. output chld_out, chld_err;
  98. size_t i;
  99. setlocale (LC_ALL, "");
  100. bindtextdomain (PACKAGE, LOCALEDIR);
  101. textdomain (PACKAGE);
  102. /* Set signal handling and alarm */
  103. if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) {
  104. usage_va(_("Cannot catch SIGALRM"));
  105. }
  106. /* Parse extra opts if any */
  107. argv=np_extra_opts (&argc, argv, progname);
  108. if (process_arguments (argc, argv) == ERROR) {
  109. usage_va(_("Could not parse arguments"));
  110. }
  111. /* get the command to run */
  112. xasprintf (&command_line, "%s %s %s %s", NSLOOKUP_COMMAND, query_type, query_address, dns_server);
  113. alarm (timeout_interval);
  114. gettimeofday (&tv, NULL);
  115. if (verbose)
  116. printf ("%s\n", command_line);
  117. /* run the command */
  118. if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
  119. msg = (char *)_("nslookup returned an error status");
  120. result = STATE_WARNING;
  121. }
  122. /* scan stdout */
  123. for(i = 0; i < chld_out.lines; i++) {
  124. if (addresses == NULL)
  125. addresses = malloc(sizeof(*addresses)*10);
  126. else if (!(n_addresses % 10))
  127. addresses = realloc(addresses,sizeof(*addresses) * (n_addresses + 10));
  128. if (verbose)
  129. puts(chld_out.line[i]);
  130. if (strstr (chld_out.line[i], "Authoritative answers can be found from:")) {
  131. non_authoritative = FALSE;
  132. break;
  133. }
  134. /* the server is responding, we just got the host name...*/
  135. if (strstr (chld_out.line[i], "Name:"))
  136. parse_address = TRUE;
  137. /* begin handling types of records */
  138. if (strstr (chld_out.line[i], "AAAA address") && (strncmp(query_type, "-querytype=AAAA", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  139. if (verbose) printf("Found AAAA record\n");
  140. temp_buffer = rindex (chld_out.line[i], ' ');
  141. addresses[n_addresses++] = check_new_address(temp_buffer);
  142. strncpy(query_found, "-querytype=AAAA", sizeof(query_found));
  143. }
  144. else if (strstr (chld_out.line[i], "exchanger =") && (strncmp(query_type, "-querytype=MX", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  145. if (verbose) printf("Found MX record\n");
  146. temp_buffer = index (chld_out.line[i], '=');
  147. addresses[n_addresses++] = check_new_address(temp_buffer);
  148. strncpy(query_found, "-querytype=MX", sizeof(query_found));
  149. }
  150. else if (strstr (chld_out.line[i], "service =") && (strncmp(query_type, "-querytype=SRV", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  151. if (verbose) printf("Found SRV record\n");
  152. temp_buffer = index (chld_out.line[i], '=');
  153. addresses[n_addresses++] = check_new_address(temp_buffer);
  154. strncpy(query_found, "-querytype=SRV", sizeof(query_found));
  155. }
  156. else if (accept_cname && strstr (chld_out.line[i], "canonical name =") && (strncmp(query_type, "-querytype=CNAME", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  157. if (verbose) printf("Found CNAME record\n");
  158. temp_buffer = index (chld_out.line[i], '=');
  159. addresses[n_addresses++] = check_new_address(temp_buffer);
  160. strncpy(query_found, "-querytype=CNAME", sizeof(query_found));
  161. }
  162. /* does not need strncmp as we want A at all times unless another record match */
  163. else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") || strstr (chld_out.line[i], "Addresses:"))) {
  164. if (verbose) printf("Found A record\n");
  165. temp_buffer = index (chld_out.line[i], ':');
  166. addresses[n_addresses++] = check_new_address(temp_buffer);
  167. strncpy(query_found, "-querytype=A", sizeof(query_found));
  168. }
  169. else if (strstr (chld_out.line[i], "nameserver =") && (strncmp(query_type, "-querytype=NS", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  170. if (verbose) printf("Found NS record\n");
  171. temp_buffer = index (chld_out.line[i], '=');
  172. addresses[n_addresses++] = check_new_address(temp_buffer);
  173. strncpy(query_found, "-querytype=NS", sizeof(query_found));
  174. }
  175. else if (strstr (chld_out.line[i], "dname =") && (strncmp(query_type, "-querytype=DNAME", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  176. if (verbose) printf("Found DNAME record\n");
  177. temp_buffer = index (chld_out.line[i], '=');
  178. addresses[n_addresses++] = check_new_address(temp_buffer);
  179. strncpy(query_found, "-querytype=DNAME", sizeof(query_found));
  180. }
  181. /* must be after other records with "name" as an identifier, as ptr does not spefify */
  182. else if (strstr (chld_out.line[i], "name =") && (strncmp(query_type, "-querytype=PTR", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  183. if (verbose) printf("Found PTR record\n");
  184. temp_buffer = index (chld_out.line[i], '=');
  185. addresses[n_addresses++] = check_new_address(temp_buffer);
  186. strncpy(query_found, "-querytype=PTR", sizeof(query_found));
  187. }
  188. else if (strstr (chld_out.line[i], "protocol =") && (strncmp(query_type, "-querytype=WKS", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  189. if (verbose) printf("Found WKS record\n");
  190. temp_buffer = index (chld_out.line[i], '=');
  191. addresses[n_addresses++] = check_new_address(temp_buffer);
  192. strncpy(query_found, "-querytype=WKS", sizeof(query_found));
  193. }
  194. /* TODO: needs to be changed to handle txt output and max size of txt recrods */
  195. else if (strstr (chld_out.line[i], "text =") && (strncmp(query_type, "-querytype=TXT", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  196. if (verbose) printf("Found TXT record\n");
  197. temp_buffer = index (chld_out.line[i], '=');
  198. addresses[n_addresses++] = check_new_address(temp_buffer);
  199. strncpy(query_found, "-querytype=TXT", sizeof(query_found));
  200. }
  201. /* only matching for origin records, if requested other fields could be included at a later date */
  202. else if (strstr (chld_out.line[i], "origin =") && (strncmp(query_type, "-querytype=SOA", query_size) == 0 || strncmp(query_type, "-querytype=ALL", query_size) == 0)) {
  203. if (verbose) printf("Found SOA record\n");
  204. temp_buffer = index(chld_out.line[i], '=');
  205. addresses[n_addresses++] = check_new_address(temp_buffer);
  206. strncpy(query_found, "-querytype=SOA", sizeof(query_found));
  207. }
  208. if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
  209. non_authoritative = TRUE;
  210. }
  211. result = error_scan (chld_out.line[i]);
  212. if (result != STATE_OK) {
  213. msg = strchr (chld_out.line[i], ':');
  214. if(msg) msg++;
  215. break;
  216. }
  217. }
  218. /* scan stderr */
  219. for(i = 0; i < chld_err.lines; i++) {
  220. if (verbose)
  221. puts(chld_err.line[i]);
  222. if (error_scan (chld_err.line[i]) != STATE_OK) {
  223. result = max_state (result, error_scan (chld_err.line[i]));
  224. msg = strchr(input_buffer, ':');
  225. if(msg) msg++;
  226. }
  227. }
  228. if (addresses) {
  229. int i,slen;
  230. char *adrp;
  231. qsort(addresses, n_addresses, sizeof(*addresses), qstrcmp);
  232. for(i=0, slen=1; i < n_addresses; i++) {
  233. slen += strlen(addresses[i])+1;
  234. }
  235. adrp = address = malloc(slen);
  236. for(i=0; i < n_addresses; i++) {
  237. if (i) *adrp++ = ',';
  238. strcpy(adrp, addresses[i]);
  239. adrp += strlen(addresses[i]);
  240. }
  241. *adrp = 0;
  242. } else
  243. die (STATE_CRITICAL,
  244. _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
  245. NSLOOKUP_COMMAND);
  246. /* compare to expected address */
  247. if (result == STATE_OK && expected_address_cnt > 0) {
  248. result = STATE_CRITICAL;
  249. temp_buffer = "";
  250. for (i=0; i<expected_address_cnt; i++) {
  251. /* check if we get a match and prepare an error string */
  252. if (strcmp(address, expected_address[i]) == 0) result = STATE_OK;
  253. xasprintf(&temp_buffer, "%s%s; ", temp_buffer, expected_address[i]);
  254. }
  255. if (result == STATE_CRITICAL) {
  256. /* Strip off last semicolon... */
  257. temp_buffer[strlen(temp_buffer)-2] = '\0';
  258. xasprintf(&msg, _("expected '%s' but got '%s'"), temp_buffer, address);
  259. }
  260. }
  261. /* check if authoritative */
  262. if (result == STATE_OK && expect_authority && !non_authoritative) {
  263. result = STATE_CRITICAL;
  264. if (strncmp(dns_server, "", 1))
  265. xasprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
  266. else
  267. xasprintf(&msg, _("there is no authoritative server for %s"), query_address);
  268. }
  269. /* compare query type to query found, if query type is ANY we can skip as any record is accepted*/
  270. if (result == STATE_OK && strncmp(query_type, "", 1) && (strncmp(query_type, "-querytype=ANY", 15) != 0)) {
  271. if (strncmp(query_type, query_found, 16) != 0) {
  272. if (verbose)
  273. printf( "Failed query for %s only found %s, or nothing\n", query_type, query_found);
  274. result = STATE_CRITICAL;
  275. xasprintf(&msg, _("query type of %s was not found for %s"), query_type, query_address);
  276. }
  277. }
  278. microsec = deltime (tv);
  279. elapsed_time = (double)microsec / 1.0e6;
  280. if (result == STATE_OK) {
  281. if (strchr (address, ',') == NULL)
  282. multi_address = FALSE;
  283. else
  284. multi_address = TRUE;
  285. result = get_status(elapsed_time, time_thresholds);
  286. if (result == STATE_OK) {
  287. printf ("DNS %s: ", _("OK"));
  288. } else if (result == STATE_WARNING) {
  289. printf ("DNS %s: ", _("WARNING"));
  290. } else if (result == STATE_CRITICAL) {
  291. printf ("DNS %s: ", _("CRITICAL"));
  292. }
  293. printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
  294. printf (_(". %s returns %s"), query_address, address);
  295. if ((time_thresholds->warning != NULL) && (time_thresholds->critical != NULL)) {
  296. printf ("|%s\n", fperfdata ("time", elapsed_time, "s",
  297. TRUE, time_thresholds->warning->end,
  298. TRUE, time_thresholds->critical->end,
  299. TRUE, 0, FALSE, 0));
  300. } else if ((time_thresholds->warning == NULL) && (time_thresholds->critical != NULL)) {
  301. printf ("|%s\n", fperfdata ("time", elapsed_time, "s",
  302. FALSE, 0,
  303. TRUE, time_thresholds->critical->end,
  304. TRUE, 0, FALSE, 0));
  305. } else if ((time_thresholds->warning != NULL) && (time_thresholds->critical == NULL)) {
  306. printf ("|%s\n", fperfdata ("time", elapsed_time, "s",
  307. TRUE, time_thresholds->warning->end,
  308. FALSE, 0,
  309. TRUE, 0, FALSE, 0));
  310. } else
  311. printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  312. }
  313. else if (result == STATE_WARNING)
  314. printf (_("DNS WARNING - %s\n"),
  315. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  316. else if (result == STATE_CRITICAL)
  317. printf (_("DNS CRITICAL - %s\n"),
  318. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  319. else
  320. printf (_("DNS UNKNOWN - %s\n"),
  321. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  322. return result;
  323. }
  324. int
  325. error_scan (char *input_buffer)
  326. {
  327. /* the DNS lookup timed out */
  328. if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
  329. strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
  330. strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
  331. return STATE_OK;
  332. /* DNS server is not running... */
  333. else if (strstr (input_buffer, "No response from server"))
  334. die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
  335. /* Host name is valid, but server doesn't have records... */
  336. else if (strstr (input_buffer, "No records") || strstr (input_buffer, "No answer"))
  337. die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
  338. /* Connection was refused */
  339. else if (strstr (input_buffer, "Connection refused") ||
  340. strstr (input_buffer, "Couldn't find server") ||
  341. strstr (input_buffer, "Refused") ||
  342. (strstr (input_buffer, "** server can't find") &&
  343. strstr (input_buffer, ": REFUSED")))
  344. die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
  345. /* Query refused (usually by an ACL in the namserver) */
  346. else if (strstr (input_buffer, "Query refused"))
  347. die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
  348. /* No information (e.g. nameserver IP has two PTR records) */
  349. else if (strstr (input_buffer, "No information"))
  350. die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
  351. /* Host or domain name does not exist */
  352. else if (strstr (input_buffer, "Non-existent") ||
  353. strstr (input_buffer, "** server can't find") ||
  354. strstr (input_buffer,"NXDOMAIN"))
  355. die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
  356. /* Network is unreachable */
  357. else if (strstr (input_buffer, "Network is unreachable"))
  358. die (STATE_CRITICAL, _("Network is unreachable\n"));
  359. /* Internal server failure */
  360. else if (strstr (input_buffer, "Server failure"))
  361. die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
  362. /* Request error or the DNS lookup timed out */
  363. else if (strstr (input_buffer, "Format error") ||
  364. strstr (input_buffer, "Timed out"))
  365. return STATE_WARNING;
  366. return STATE_OK;
  367. }
  368. /* process command-line arguments */
  369. int
  370. process_arguments (int argc, char **argv)
  371. {
  372. int c;
  373. char *warning = NULL;
  374. char *critical = NULL;
  375. int opt_index = 0;
  376. static struct option long_opts[] = {
  377. {"help", no_argument, 0, 'h'},
  378. {"version", no_argument, 0, 'V'},
  379. {"verbose", no_argument, 0, 'v'},
  380. {"timeout", required_argument, 0, 't'},
  381. {"hostname", required_argument, 0, 'H'},
  382. {"server", required_argument, 0, 's'},
  383. {"reverse-server", required_argument, 0, 'r'},
  384. {"querytype", required_argument, 0, 'q'},
  385. {"expected-address", required_argument, 0, 'a'},
  386. {"expect-authority", no_argument, 0, 'A'},
  387. {"accept-cname", no_argument, 0, 'n'},
  388. {"warning", required_argument, 0, 'w'},
  389. {"critical", required_argument, 0, 'c'},
  390. {0, 0, 0, 0}
  391. };
  392. if (argc < 2)
  393. return ERROR;
  394. for (c = 1; c < argc; c++)
  395. if (strcmp ("-to", argv[c]) == 0)
  396. strcpy (argv[c], "-t");
  397. while (1) {
  398. c = getopt_long (argc, argv, "hVvAnt:H:s:r:a:q:w:c:", long_opts, &opt_index);
  399. if (c == -1 || c == EOF)
  400. break;
  401. switch (c) {
  402. case 'h': /* help */
  403. print_help ();
  404. exit (STATE_OK);
  405. case 'V': /* version */
  406. print_revision (progname, NP_VERSION);
  407. exit (STATE_OK);
  408. case 'v': /* version */
  409. verbose = TRUE;
  410. break;
  411. case 't': /* timeout period */
  412. timeout_interval = atoi (optarg);
  413. break;
  414. case 'H': /* hostname */
  415. if (strlen (optarg) >= ADDRESS_LENGTH)
  416. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  417. strcpy (query_address, optarg);
  418. break;
  419. case 's': /* server name */
  420. /* TODO: this host_or_die check is probably unnecessary.
  421. * Better to confirm nslookup response matches */
  422. host_or_die(optarg);
  423. if (strlen (optarg) >= ADDRESS_LENGTH)
  424. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  425. strcpy (dns_server, optarg);
  426. break;
  427. case 'r': /* reverse server name */
  428. /* TODO: Is this host_or_die necessary? */
  429. host_or_die(optarg);
  430. if (strlen (optarg) >= ADDRESS_LENGTH)
  431. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  432. strcpy (ptr_server, optarg);
  433. break;
  434. case 'a': /* expected address */
  435. if (strlen (optarg) >= ADDRESS_LENGTH)
  436. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  437. expected_address = (char **)realloc(expected_address, (expected_address_cnt+1) * sizeof(char**));
  438. expected_address[expected_address_cnt] = strdup(optarg);
  439. expected_address_cnt++;
  440. break;
  441. case 'q': /* querytype -- A or AAAA or ANY or SRV or TXT, etc. */
  442. if (strlen (optarg) < 1 || strlen (optarg) > 5)
  443. die (STATE_UNKNOWN, _("Missing valid querytype parameter. Try using 'A' or 'AAAA' or 'SRV'\n"));
  444. strntoupper(optarg, sizeof(optarg));
  445. strcpy(query_type, "-querytype=");
  446. strcat(query_type, optarg);
  447. break;
  448. case 'A': /* expect authority */
  449. expect_authority = TRUE;
  450. break;
  451. case 'n': /* accept cname responses as a result */
  452. accept_cname = TRUE;
  453. break;
  454. case 'w':
  455. warning = optarg;
  456. break;
  457. case 'c':
  458. critical = optarg;
  459. break;
  460. default: /* args not parsable */
  461. usage5();
  462. }
  463. }
  464. c = optind;
  465. if (strlen(query_address)==0 && c<argc) {
  466. if (strlen(argv[c])>=ADDRESS_LENGTH)
  467. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  468. strcpy (query_address, argv[c++]);
  469. }
  470. if (strlen(dns_server)==0 && c<argc) {
  471. /* TODO: See -s option */
  472. host_or_die(argv[c]);
  473. if (strlen(argv[c]) >= ADDRESS_LENGTH)
  474. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  475. strcpy (dns_server, argv[c++]);
  476. }
  477. set_thresholds(&time_thresholds, warning, critical);
  478. return validate_arguments ();
  479. }
  480. int
  481. validate_arguments ()
  482. {
  483. if (query_address[0] == 0)
  484. return ERROR;
  485. return OK;
  486. }
  487. void
  488. print_help (void)
  489. {
  490. print_revision (progname, NP_VERSION);
  491. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  492. printf (COPYRIGHT, copyright, email);
  493. printf ("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
  494. printf ("%s\n", _("An optional DNS server to use may be specified."));
  495. printf ("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
  496. printf ("\n\n");
  497. print_usage ();
  498. printf (UT_HELP_VRSN);
  499. printf (UT_EXTRA_OPTS);
  500. printf (" -H, --hostname=HOST\n");
  501. printf (" %s\n", _("The name or address you want to query"));
  502. printf (" -s, --server=HOST\n");
  503. printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
  504. printf (" -q, --querytype=TYPE\n");
  505. printf (" %s\n", _("Optional DNS record query type where TYPE =(A, AAAA, SRV, TXT, MX, ANY)"));
  506. printf (" %s\n", _("The default query type is 'A' (IPv4 host entry)"));
  507. printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
  508. printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with"));
  509. printf (" %s\n", _("a dot (.). This option can be repeated multiple times (Returns OK if any"));
  510. printf (" %s\n", _("value match). If multiple addresses are returned at once, you have to match"));
  511. printf (" %s\n", _("the whole string of addresses separated with commas (sorted alphabetically)."));
  512. printf (" %s\n", _("If you would like to test for the presence of a cname, combine with -n param."));
  513. printf (" -A, --expect-authority\n");
  514. printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
  515. printf (" -n, --accept-cname\n");
  516. printf (" %s\n", _("Optionally accept cname responses as a valid result to a query"));
  517. printf (" %s\n", _("The default is to ignore cname responses as part of the result"));
  518. printf (" -w, --warning=seconds\n");
  519. printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
  520. printf (" -c, --critical=seconds\n");
  521. printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
  522. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  523. printf (UT_SUPPORT);
  524. }
  525. void
  526. print_usage (void)
  527. {
  528. printf ("%s\n", _("Usage:"));
  529. printf ("%s -H host [-s server] [-q type ] [-a expected-address] [-A] [-n] [-t timeout] [-w warn] [-c crit]\n", progname);
  530. }