check_dns.c 11 KB

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