check_dns.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. int process_arguments (int, char **);
  50. int call_getopt (int, char **);
  51. int validate_arguments (void);
  52. void print_usage (char *);
  53. void print_help (char *);
  54. int error_scan (char *);
  55. #define ADDRESS_LENGTH 256
  56. char query_address[ADDRESS_LENGTH] = "";
  57. char dns_server[ADDRESS_LENGTH] = "";
  58. char ptr_server[ADDRESS_LENGTH] = "";
  59. int verbose = FALSE;
  60. char expected_address[ADDRESS_LENGTH] = "";
  61. int match_expected_address = FALSE;
  62. int
  63. main (int argc, char **argv)
  64. {
  65. char *command_line = NULL;
  66. char input_buffer[MAX_INPUT_BUFFER];
  67. char *output = NULL;
  68. char *address = NULL;
  69. char *temp_buffer = NULL;
  70. int result = STATE_UNKNOWN;
  71. /* Set signal handling and alarm */
  72. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  73. printf ("Cannot catch SIGALRM");
  74. return STATE_UNKNOWN;
  75. }
  76. if (process_arguments (argc, argv) != OK) {
  77. print_usage (my_basename (argv[0]));
  78. return STATE_UNKNOWN;
  79. }
  80. /* get the command to run */
  81. command_line = ssprintf (command_line, "%s %s %s", NSLOOKUP_COMMAND,
  82. query_address, dns_server);
  83. alarm (timeout_interval);
  84. time (&start_time);
  85. if (verbose)
  86. printf ("%s\n", command_line);
  87. /* run the command */
  88. child_process = spopen (command_line);
  89. if (child_process == NULL) {
  90. printf ("Could not open pipe: %s\n", command_line);
  91. return STATE_UNKNOWN;
  92. }
  93. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  94. if (child_stderr == NULL)
  95. printf ("Could not open stderr for %s\n", command_line);
  96. /* scan stdout */
  97. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  98. if (verbose)
  99. printf ("%s\n", input_buffer);
  100. if (strstr (input_buffer, ".in-addr.arpa")) {
  101. if ((temp_buffer = strstr (input_buffer, "name = ")))
  102. address = strscpy (address, temp_buffer + 7);
  103. else {
  104. output = strscpy (output, "Unknown error (plugin)");
  105. result = STATE_WARNING;
  106. }
  107. }
  108. /* the server is responding, we just got the host name... */
  109. if (strstr (input_buffer, "Name:")) {
  110. /* get the host address */
  111. if (!fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
  112. break;
  113. if (verbose)
  114. printf ("%s\n", input_buffer);
  115. if ((temp_buffer = index (input_buffer, ':'))) {
  116. address = strscpy (address, temp_buffer + 2);
  117. strip (address);
  118. result = STATE_OK;
  119. }
  120. else {
  121. output = strscpy (output, "Unknown error (plugin)");
  122. result = STATE_WARNING;
  123. }
  124. break;
  125. }
  126. result = error_scan (input_buffer);
  127. if (result != STATE_OK) {
  128. output = strscpy (output, 1 + index (input_buffer, ':'));
  129. break;
  130. }
  131. }
  132. /* scan stderr */
  133. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  134. if (error_scan (input_buffer) != STATE_OK) {
  135. result = max_state (result, error_scan (input_buffer));
  136. output = strscpy (output, 1 + index (input_buffer, ':'));
  137. }
  138. }
  139. /* close stderr */
  140. (void) fclose (child_stderr);
  141. /* close stdout */
  142. if (spclose (child_process)) {
  143. result = max_state (result, STATE_WARNING);
  144. if (!strcmp (output, ""))
  145. output = strscpy (output, "nslookup returned error status");
  146. }
  147. /* compare to expected address */
  148. if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
  149. result = STATE_CRITICAL;
  150. output = ssprintf(output, "expected %s but got %s", expected_address, address);
  151. }
  152. (void) time (&end_time);
  153. if (result == STATE_OK)
  154. printf ("DNS ok - %d seconds response time, Address(es) is/are %s\n",
  155. (int) (end_time - start_time), address);
  156. else if (result == STATE_WARNING)
  157. printf ("DNS WARNING - %s\n",
  158. !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
  159. else if (result == STATE_CRITICAL)
  160. printf ("DNS CRITICAL - %s\n",
  161. !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
  162. else
  163. printf ("DNS problem - %s\n",
  164. !strcmp (output, "") ? " Probably a non-existent host/domain" : output);
  165. return result;
  166. }
  167. int
  168. error_scan (char *input_buffer)
  169. {
  170. /* the DNS lookup timed out */
  171. if (strstr (input_buffer,
  172. "Note: nslookup is deprecated and may be removed from future releases.")
  173. || strstr (input_buffer,
  174. "Consider using the `dig' or `host' programs instead. Run nslookup with")
  175. || strstr (input_buffer,
  176. "the `-sil[ent]' option to prevent this message from appearing."))
  177. return STATE_OK;
  178. /* the DNS lookup timed out */
  179. else if (strstr (input_buffer, "Timed out"))
  180. return STATE_WARNING;
  181. /* DNS server is not running... */
  182. else if (strstr (input_buffer, "No response from server"))
  183. return STATE_CRITICAL;
  184. /* Host name is valid, but server doesn't have records... */
  185. else if (strstr (input_buffer, "No records"))
  186. return STATE_WARNING;
  187. /* Host or domain name does not exist */
  188. else if (strstr (input_buffer, "Non-existent"))
  189. return STATE_CRITICAL;
  190. else if (strstr (input_buffer, "** server can't find"))
  191. return STATE_CRITICAL;
  192. else if(strstr(input_buffer,"NXDOMAIN")) /* 9.x */
  193. return STATE_CRITICAL;
  194. /* Connection was refused */
  195. else if (strstr (input_buffer, "Connection refused"))
  196. return STATE_CRITICAL;
  197. /* Network is unreachable */
  198. else if (strstr (input_buffer, "Network is unreachable"))
  199. return STATE_CRITICAL;
  200. /* Internal server failure */
  201. else if (strstr (input_buffer, "Server failure"))
  202. return STATE_CRITICAL;
  203. /* DNS server refused to service request */
  204. else if (strstr (input_buffer, "Refused"))
  205. return STATE_CRITICAL;
  206. /* Request error */
  207. else if (strstr (input_buffer, "Format error"))
  208. return STATE_WARNING;
  209. else
  210. return STATE_OK;
  211. }
  212. /* process command-line arguments */
  213. int
  214. process_arguments (int argc, char **argv)
  215. {
  216. int c;
  217. if (argc < 2)
  218. return ERROR;
  219. for (c = 1; c < argc; c++)
  220. if (strcmp ("-to", argv[c]) == 0)
  221. strcpy (argv[c], "-t");
  222. c = 0;
  223. while (c += (call_getopt (argc - c, &argv[c]))) {
  224. if (argc <= c)
  225. break;
  226. if (query_address[0] == 0) {
  227. if (is_host (argv[c]) == FALSE) {
  228. printf ("Invalid name/address: %s\n\n", argv[c]);
  229. return ERROR;
  230. }
  231. if (strlen (argv[c]) >= ADDRESS_LENGTH)
  232. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  233. strcpy (query_address, argv[c]);
  234. }
  235. else if (dns_server[0] == 0) {
  236. if (is_host (argv[c]) == FALSE) {
  237. printf ("Invalid name/address: %s\n\n", argv[c]);
  238. return ERROR;
  239. }
  240. if (strlen (argv[c]) >= ADDRESS_LENGTH)
  241. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  242. strcpy (dns_server, argv[c]);
  243. }
  244. }
  245. return validate_arguments ();
  246. }
  247. int
  248. call_getopt (int argc, char **argv)
  249. {
  250. int c, i = 1;
  251. #ifdef HAVE_GETOPT_H
  252. int opt_index = 0;
  253. static struct option long_opts[] = {
  254. {"help", no_argument, 0, 'h'},
  255. {"version", no_argument, 0, 'V'},
  256. {"verbose", no_argument, 0, 'v'},
  257. {"timeout", required_argument, 0, 't'},
  258. {"hostname", required_argument, 0, 'H'},
  259. {"server", required_argument, 0, 's'},
  260. {"reverse-server", required_argument, 0, 'r'},
  261. {"expected-address", required_argument, 0, 'a'},
  262. {0, 0, 0, 0}
  263. };
  264. #endif
  265. while (1) {
  266. #ifdef HAVE_GETOPT_H
  267. c = getopt_long (argc, argv, "+?hVvt:H:s:r:a:", long_opts, &opt_index);
  268. #else
  269. c = getopt (argc, argv, "+?hVvt:H:s:r:a:");
  270. #endif
  271. if (c == -1 || c == EOF)
  272. break;
  273. i++;
  274. switch (c) {
  275. case 't':
  276. case 'H':
  277. case 's':
  278. case 'r':
  279. case 'a':
  280. i++;
  281. }
  282. switch (c) {
  283. case '?': /* args not parsable */
  284. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  285. print_usage (my_basename (argv[0]));
  286. exit (STATE_UNKNOWN);
  287. case 'h': /* help */
  288. print_help (my_basename (argv[0]));
  289. exit (STATE_OK);
  290. case 'V': /* version */
  291. print_revision (my_basename (argv[0]), "$Revision$");
  292. exit (STATE_OK);
  293. case 'v': /* version */
  294. verbose = TRUE;
  295. break;
  296. case 't': /* timeout period */
  297. timeout_interval = atoi (optarg);
  298. break;
  299. case 'H': /* hostname */
  300. if (is_host (optarg) == FALSE) {
  301. printf ("Invalid host name/address\n\n");
  302. print_usage (my_basename (argv[0]));
  303. exit (STATE_UNKNOWN);
  304. }
  305. if (strlen (optarg) >= ADDRESS_LENGTH)
  306. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  307. strcpy (query_address, optarg);
  308. break;
  309. case 's': /* server name */
  310. if (is_host (optarg) == FALSE) {
  311. printf ("Invalid server name/address\n\n");
  312. print_usage (my_basename (argv[0]));
  313. exit (STATE_UNKNOWN);
  314. }
  315. if (strlen (optarg) >= ADDRESS_LENGTH)
  316. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  317. strcpy (dns_server, optarg);
  318. break;
  319. case 'r': /* reverse server name */
  320. if (is_host (optarg) == FALSE) {
  321. printf ("Invalid host name/address\n\n");
  322. print_usage (my_basename (argv[0]));
  323. exit (STATE_UNKNOWN);
  324. }
  325. if (strlen (optarg) >= ADDRESS_LENGTH)
  326. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  327. strcpy (ptr_server, optarg);
  328. break;
  329. case 'a': /* expected address */
  330. if (is_dotted_quad (optarg) == FALSE) {
  331. printf ("Invalid expected address\n\n");
  332. print_usage (my_basename (argv[0]));
  333. exit (STATE_UNKNOWN);
  334. }
  335. if (strlen (optarg) >= ADDRESS_LENGTH)
  336. terminate (STATE_UNKNOWN, "Input buffer overflow\n");
  337. strcpy (expected_address, optarg);
  338. match_expected_address = TRUE;
  339. break;
  340. }
  341. }
  342. return i;
  343. }
  344. int
  345. validate_arguments ()
  346. {
  347. if (query_address[0] == 0)
  348. return ERROR;
  349. else
  350. return OK;
  351. }
  352. void
  353. print_usage (char *cmd)
  354. {
  355. printf ("Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n" " %s --help\n"
  356. " %s --version\n", cmd, cmd, cmd);
  357. }
  358. void
  359. print_help (char *cmd)
  360. {
  361. print_revision (cmd, "$Revision$");
  362. printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n");
  363. print_usage (cmd);
  364. printf ("\n");
  365. printf
  366. ("-H, --hostname=HOST\n"
  367. " The name or address you want to query\n"
  368. "-s, --server=HOST\n"
  369. " Optional DNS server you want to use for the lookup\n"
  370. "-a, --expected-address=IP-ADDRESS\n"
  371. " Optional IP address you expect the DNS server to return\n"
  372. "-t, --timeout=INTEGER\n"
  373. " Seconds before connection times out (default: %d)\n"
  374. "-h, --help\n"
  375. " Print detailed help\n"
  376. "-V, --version\n"
  377. " Print version numbers and license information\n"
  378. "\n"
  379. "This plugin uses the nslookup program to obtain the IP address\n"
  380. "for the given host/domain query. A optional DNS server to use may\n"
  381. "be specified. If no DNS server is specified, the default server(s)\n"
  382. "specified in /etc/resolv.conf will be used.\n", DEFAULT_SOCKET_TIMEOUT);
  383. }