4
0

check_dns.c 22 KB

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