check_dns.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /******************************************************************************
  2. *
  3. * CHECK_DNS.C
  4. *
  5. * Program: DNS plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Notes:
  12. * - Safe popen added by Karl DeBisschop 9-11-99
  13. * - expected-address parameter added by Alex Chaffee - 7 Oct 2002
  14. *
  15. * Command line: (see print_usage)
  16. *
  17. * Description:
  18. *
  19. * This program will use the nslookup program to obtain the IP address
  20. * for a given host name. A optional DNS server may be specified. If
  21. * no DNS server is specified, the default server(s) for the system
  22. * are used.
  23. *
  24. * Return Values:
  25. * OK The DNS query was successful (host IP address was returned).
  26. * WARNING The DNS server responded, but could not fulfill the request.
  27. * CRITICAL The DNS server is not responding or encountered an error.
  28. *
  29. * License Information:
  30. *
  31. * This program is free software; you can redistribute it and/or modify
  32. * it under the terms of the GNU General Public License as published by
  33. * the Free Software Foundation; either version 2 of the License, or
  34. * (at your option) any later version.
  35. *
  36. * This program is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU General Public License
  42. * along with this program; if not, write to the Free Software
  43. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  44. *
  45. *****************************************************************************/
  46. #include "common.h"
  47. #include "popen.h"
  48. #include "utils.h"
  49. const char *progname = "check_dns";
  50. #define REVISION "$Revision$"
  51. #define COPYRIGHT "2000-2002"
  52. int process_arguments (int, char **);
  53. int validate_arguments (void);
  54. void print_usage (void);
  55. void print_help (void);
  56. int error_scan (char *);
  57. #define ADDRESS_LENGTH 256
  58. char query_address[ADDRESS_LENGTH] = "";
  59. char dns_server[ADDRESS_LENGTH] = "";
  60. char ptr_server[ADDRESS_LENGTH] = "";
  61. int verbose = FALSE;
  62. char expected_address[ADDRESS_LENGTH] = "";
  63. int match_expected_address = FALSE;
  64. int
  65. main (int argc, char **argv)
  66. {
  67. char *command_line = NULL;
  68. char input_buffer[MAX_INPUT_BUFFER];
  69. char *output = NULL;
  70. char *address = NULL;
  71. char *temp_buffer = NULL;
  72. int result = STATE_UNKNOWN;
  73. double elapsed_time;
  74. struct timeval tv;
  75. int multi_address;
  76. /* Set signal handling and alarm */
  77. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  78. printf ("Cannot catch SIGALRM");
  79. return STATE_UNKNOWN;
  80. }
  81. if (process_arguments (argc, argv) != OK) {
  82. print_usage ();
  83. return STATE_UNKNOWN;
  84. }
  85. /* get the command to run */
  86. asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
  87. alarm (timeout_interval);
  88. gettimeofday (&tv, NULL);
  89. if (verbose)
  90. printf ("%s\n", command_line);
  91. /* run the command */
  92. child_process = spopen (command_line);
  93. if (child_process == NULL) {
  94. printf ("Could not open pipe: %s\n", command_line);
  95. return STATE_UNKNOWN;
  96. }
  97. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  98. if (child_stderr == NULL)
  99. printf ("Could not open stderr for %s\n", command_line);
  100. /* scan stdout */
  101. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  102. if (verbose)
  103. printf ("%s\n", input_buffer);
  104. if (strstr (input_buffer, ".in-addr.arpa")) {
  105. if ((temp_buffer = strstr (input_buffer, "name = ")))
  106. address = strscpy (address, temp_buffer + 7);
  107. else {
  108. output = strscpy (output, "Unknown error (plugin)");
  109. result = STATE_WARNING;
  110. }
  111. }
  112. /* the server is responding, we just got the host name... */
  113. if (strstr (input_buffer, "Name:")) {
  114. /* get the host address */
  115. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  116. break;
  117. if (verbose)
  118. printf ("%s\n", input_buffer);
  119. if ((temp_buffer = index (input_buffer, ':'))) {
  120. temp_buffer++;
  121. /* Strip leading spaces */
  122. for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
  123. /* NOOP */;
  124. address = strscpy (address, temp_buffer);
  125. strip (address);
  126. result = STATE_OK;
  127. }
  128. else {
  129. output = strscpy (output, "Unknown error (plugin)");
  130. result = STATE_WARNING;
  131. }
  132. break;
  133. }
  134. result = error_scan (input_buffer);
  135. if (result != STATE_OK) {
  136. output = strscpy (output, 1 + index (input_buffer, ':'));
  137. break;
  138. }
  139. }
  140. /* scan stderr */
  141. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  142. if (error_scan (input_buffer) != STATE_OK) {
  143. result = max_state (result, error_scan (input_buffer));
  144. output = strscpy (output, 1 + index (input_buffer, ':'));
  145. }
  146. }
  147. /* close stderr */
  148. (void) fclose (child_stderr);
  149. /* close stdout */
  150. if (spclose (child_process)) {
  151. result = max_state (result, STATE_WARNING);
  152. if (!strcmp (output, ""))
  153. output = strscpy (output, "nslookup returned error status");
  154. }
  155. /* compare to expected address */
  156. if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
  157. result = STATE_CRITICAL;
  158. asprintf(&output, "expected %s but got %s", expected_address, address);
  159. }
  160. elapsed_time = delta_time (tv);
  161. if (result == STATE_OK) {
  162. if (strchr (address, ',') == NULL)
  163. multi_address = FALSE;
  164. else
  165. multi_address = TRUE;
  166. printf ("DNS ok - %-7.3f seconds response time, address%s %s|time=%-7.3f\n",
  167. elapsed_time, (multi_address==TRUE ? "es are" : " is"), address, elapsed_time);
  168. }
  169. else if (result == STATE_WARNING)
  170. printf ("DNS WARNING - %s\n",
  171. !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
  172. else if (result == STATE_CRITICAL)
  173. printf ("DNS CRITICAL - %s\n",
  174. !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
  175. else
  176. printf ("DNS problem - %s\n",
  177. !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
  178. return result;
  179. }
  180. int
  181. error_scan (char *input_buffer)
  182. {
  183. /* the DNS lookup timed out */
  184. if (strstr (input_buffer,
  185. "Note: nslookup is deprecated and may be removed from future releases.")
  186. || strstr (input_buffer,
  187. "Consider using the `dig' or `host' programs instead. Run nslookup with")
  188. || strstr (input_buffer,
  189. "the `-sil[ent]' option to prevent this message from appearing."))
  190. return STATE_OK;
  191. /* the DNS lookup timed out */
  192. else if (strstr (input_buffer, "Timed out"))
  193. return STATE_WARNING;
  194. /* DNS server is not running... */
  195. else if (strstr (input_buffer, "No response from server"))
  196. return STATE_CRITICAL;
  197. /* Host name is valid, but server doesn't have records... */
  198. else if (strstr (input_buffer, "No records"))
  199. return STATE_WARNING;
  200. /* Host or domain name does not exist */
  201. else if (strstr (input_buffer, "Non-existent"))
  202. return STATE_CRITICAL;
  203. else if (strstr (input_buffer, "** server can't find"))
  204. return STATE_CRITICAL;
  205. else if(strstr(input_buffer,"NXDOMAIN")) /* 9.x */
  206. return STATE_CRITICAL;
  207. /* Connection was refused */
  208. else if (strstr (input_buffer, "Connection refused"))
  209. return STATE_CRITICAL;
  210. /* Network is unreachable */
  211. else if (strstr (input_buffer, "Network is unreachable"))
  212. return STATE_CRITICAL;
  213. /* Internal server failure */
  214. else if (strstr (input_buffer, "Server failure"))
  215. return STATE_CRITICAL;
  216. /* DNS server refused to service request */
  217. else if (strstr (input_buffer, "Refused"))
  218. return STATE_CRITICAL;
  219. /* Request error */
  220. else if (strstr (input_buffer, "Format error"))
  221. return STATE_WARNING;
  222. else
  223. return STATE_OK;
  224. }
  225. /* process command-line arguments */
  226. int
  227. process_arguments (int argc, char **argv)
  228. {
  229. int c;
  230. int opt_index = 0;
  231. static struct option long_opts[] = {
  232. {"help", no_argument, 0, 'h'},
  233. {"version", no_argument, 0, 'V'},
  234. {"verbose", no_argument, 0, 'v'},
  235. {"timeout", required_argument, 0, 't'},
  236. {"hostname", required_argument, 0, 'H'},
  237. {"server", required_argument, 0, 's'},
  238. {"reverse-server", required_argument, 0, 'r'},
  239. {"expected-address", required_argument, 0, 'a'},
  240. {0, 0, 0, 0}
  241. };
  242. if (argc < 2)
  243. return ERROR;
  244. for (c = 1; c < argc; c++)
  245. if (strcmp ("-to", argv[c]) == 0)
  246. strcpy (argv[c], "-t");
  247. while (1) {
  248. c = getopt_long (argc, argv, "hVvt:H:s:r:a:", long_opts, &opt_index);
  249. if (c == -1 || c == EOF)
  250. break;
  251. switch (c) {
  252. case '?': /* args not parsable */
  253. printf ("%s: Unknown argument: %s\n\n", progname, optarg);
  254. print_usage ();
  255. exit (STATE_UNKNOWN);
  256. case 'h': /* help */
  257. print_help ();
  258. exit (STATE_OK);
  259. case 'V': /* version */
  260. print_revision (progname, REVISION);
  261. exit (STATE_OK);
  262. case 'v': /* version */
  263. verbose = TRUE;
  264. break;
  265. case 't': /* timeout period */
  266. timeout_interval = atoi (optarg);
  267. break;
  268. case 'H': /* hostname */
  269. if (is_host (optarg) == FALSE) {
  270. printf ("Invalid host name/address\n\n");
  271. print_usage ();
  272. exit (STATE_UNKNOWN);
  273. }
  274. if (strlen (optarg) >= ADDRESS_LENGTH)
  275. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  276. strcpy (query_address, optarg);
  277. break;
  278. case 's': /* server name */
  279. if (is_host (optarg) == FALSE) {
  280. printf ("Invalid server name/address\n\n");
  281. print_usage ();
  282. exit (STATE_UNKNOWN);
  283. }
  284. if (strlen (optarg) >= ADDRESS_LENGTH)
  285. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  286. strcpy (dns_server, optarg);
  287. break;
  288. case 'r': /* reverse server name */
  289. if (is_host (optarg) == FALSE) {
  290. printf ("Invalid host name/address\n\n");
  291. print_usage ();
  292. exit (STATE_UNKNOWN);
  293. }
  294. if (strlen (optarg) >= ADDRESS_LENGTH)
  295. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  296. strcpy (ptr_server, optarg);
  297. break;
  298. case 'a': /* expected address */
  299. if (is_addr (optarg) == FALSE) {
  300. printf ("Invalid expected address\n\n");
  301. print_usage ();
  302. exit (STATE_UNKNOWN);
  303. }
  304. if (strlen (optarg) >= ADDRESS_LENGTH)
  305. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  306. strcpy (expected_address, optarg);
  307. match_expected_address = TRUE;
  308. break;
  309. }
  310. }
  311. c = optind;
  312. if (strlen(query_address)==0 && c<argc) {
  313. if (is_host(argv[c])==FALSE) {
  314. printf ("Invalid name/address: %s\n\n", argv[c]);
  315. return ERROR;
  316. }
  317. if (strlen(argv[c])>=ADDRESS_LENGTH)
  318. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  319. strcpy (query_address, argv[c++]);
  320. }
  321. if (strlen(dns_server)==0 && c<argc) {
  322. if (is_host(argv[c]) == FALSE) {
  323. printf ("Invalid name/address: %s\n\n", argv[c]);
  324. return ERROR;
  325. }
  326. if (strlen(argv[c]) >= ADDRESS_LENGTH)
  327. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  328. strcpy (dns_server, argv[c++]);
  329. }
  330. return validate_arguments ();
  331. }
  332. int
  333. validate_arguments ()
  334. {
  335. if (query_address[0] == 0)
  336. return ERROR;
  337. else
  338. return OK;
  339. }
  340. void
  341. print_usage (void)
  342. {
  343. printf ("Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n" " %s --help\n"
  344. " %s --version\n", progname, progname, progname);
  345. }
  346. void
  347. print_help (void)
  348. {
  349. print_revision (progname, REVISION);
  350. printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n");
  351. print_usage ();
  352. printf
  353. ("\nOptions:\n"
  354. "-H, --hostname=HOST\n"
  355. " The name or address you want to query\n"
  356. "-s, --server=HOST\n"
  357. " Optional DNS server you want to use for the lookup\n"
  358. "-a, --expected-address=IP-ADDRESS\n"
  359. " Optional IP address you expect the DNS server to return\n"
  360. "-t, --timeout=INTEGER\n"
  361. " Seconds before connection times out (default: %d)\n"
  362. "-h, --help\n"
  363. " Print detailed help\n"
  364. "-V, --version\n"
  365. " Print version numbers and license information\n"
  366. "\n"
  367. "This plugin uses the nslookup program to obtain the IP address\n"
  368. "for the given host/domain query. A optional DNS server to use may\n"
  369. "be specified. If no DNS server is specified, the default server(s)\n"
  370. "specified in /etc/resolv.conf will be used.\n", DEFAULT_SOCKET_TIMEOUT);
  371. }