check_dns.c 12 KB

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