check_dns.c 23 KB

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