check_udp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /******************************************************************************
  2. *
  3. * CHECK_UDP.C
  4. *
  5. * Program: UDP port plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Command line: CHECK_UDP <host_address> [-p port] [-s send] [-e expect] \
  12. * [-wt warn_time] [-ct crit_time] [-to to_sec]
  13. *
  14. * Description:
  15. *
  16. * This plugin will attempt to connect to the specified port
  17. * on the host. Successul connects return STATE_OK, refusals
  18. * and timeouts return STATE_CRITICAL, other errors return
  19. * STATE_UNKNOWN.
  20. *
  21. * License Information:
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. *
  37. *****************************************************************************/
  38. #include "config.h"
  39. #include "common.h"
  40. #include "netutils.h"
  41. #include "utils.h"
  42. const char *progname = "check_udp";
  43. int warning_time = 0;
  44. int check_warning_time = FALSE;
  45. int critical_time = 0;
  46. int check_critical_time = FALSE;
  47. int process_arguments (int, char **);
  48. void print_usage (void);
  49. void print_help (void);
  50. int verbose = FALSE;
  51. int server_port = 0;
  52. char *server_address = NULL;
  53. char *server_expect = NULL;
  54. char *server_send = "";
  55. int
  56. main (int argc, char **argv)
  57. {
  58. int result;
  59. char recv_buffer[MAX_INPUT_BUFFER];
  60. if (process_arguments (argc, argv) == ERROR)
  61. usage ("\n");
  62. /* initialize alarm signal handling */
  63. signal (SIGALRM, socket_timeout_alarm_handler);
  64. /* set socket timeout */
  65. alarm (socket_timeout);
  66. time (&start_time);
  67. result =
  68. process_udp_request (server_address, server_port, server_send,
  69. recv_buffer, MAX_INPUT_BUFFER - 1);
  70. time (&end_time);
  71. if (result != STATE_OK) {
  72. printf ("No response from host on port %d\n", server_port);
  73. result = STATE_CRITICAL;
  74. }
  75. else {
  76. /* check to see if we got the response we wanted */
  77. if (server_expect) {
  78. if (!strstr (recv_buffer, server_expect)) {
  79. printf ("Invalid response received from host on port %d\n",
  80. server_port);
  81. result = STATE_CRITICAL;
  82. }
  83. }
  84. }
  85. /* we connected, so close connection before exiting */
  86. if (result == STATE_OK) {
  87. if (check_critical_time == TRUE
  88. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  89. else if (check_warning_time == TRUE
  90. && (end_time - start_time) > warning_time) result =
  91. STATE_WARNING;
  92. printf ("Connection %s on port %d - %d second response time\n",
  93. (result == STATE_OK) ? "accepted" : "problem", server_port,
  94. (int) (end_time - start_time));
  95. }
  96. /* reset the alarm */
  97. alarm (0);
  98. return result;
  99. }
  100. /* process command-line arguments */
  101. int
  102. process_arguments (int argc, char **argv)
  103. {
  104. int c;
  105. #ifdef HAVE_GETOPT_H
  106. int option_index = 0;
  107. static struct option long_options[] = {
  108. {"hostname", required_argument, 0, 'H'},
  109. {"critical", required_argument, 0, 'c'},
  110. {"warning", required_argument, 0, 'w'},
  111. {"timeout", required_argument, 0, 't'},
  112. {"port", required_argument, 0, 'p'},
  113. {"expect", required_argument, 0, 'e'},
  114. {"send", required_argument, 0, 's'},
  115. {"verbose", no_argument, 0, 'v'},
  116. {"version", no_argument, 0, 'V'},
  117. {"help", no_argument, 0, 'h'},
  118. {0, 0, 0, 0}
  119. };
  120. #endif
  121. if (argc < 2)
  122. usage ("\n");
  123. for (c = 1; c < argc; c++) {
  124. if (strcmp ("-to", argv[c]) == 0)
  125. strcpy (argv[c], "-t");
  126. else if (strcmp ("-wt", argv[c]) == 0)
  127. strcpy (argv[c], "-w");
  128. else if (strcmp ("-ct", argv[c]) == 0)
  129. strcpy (argv[c], "-c");
  130. }
  131. while (1) {
  132. #ifdef HAVE_GETOPT_H
  133. c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
  134. #else
  135. c = getopt (argc, argv, "+hVvH:e:s:c:w:t:p:");
  136. #endif
  137. switch (c) {
  138. case '?': /* print short usage statement if args not parsable */
  139. printf ("%s: Unknown argument: %s\n\n", progname, optarg);
  140. print_usage ();
  141. exit (STATE_UNKNOWN);
  142. case 'h': /* help */
  143. print_help ();
  144. exit (STATE_OK);
  145. case 'V': /* version */
  146. print_revision (progname, "$Revision$");
  147. exit (STATE_OK);
  148. case 'v': /* verbose mode */
  149. verbose = TRUE;
  150. break;
  151. case 'H': /* hostname */
  152. if (is_host (optarg) == FALSE)
  153. usage ("Invalid host name/address\n");
  154. server_address = optarg;
  155. break;
  156. case 'c': /* critical */
  157. if (!is_intnonneg (optarg))
  158. usage ("Critical threshold must be a nonnegative integer\n");
  159. critical_time = atoi (optarg);
  160. check_critical_time = TRUE;
  161. break;
  162. case 'w': /* warning */
  163. if (!is_intnonneg (optarg))
  164. usage ("Warning threshold must be a nonnegative integer\n");
  165. warning_time = atoi (optarg);
  166. check_warning_time = TRUE;
  167. break;
  168. case 't': /* timeout */
  169. if (!is_intnonneg (optarg))
  170. usage ("Timeout interval must be a nonnegative integer\n");
  171. socket_timeout = atoi (optarg);
  172. break;
  173. case 'p': /* port */
  174. if (!is_intnonneg (optarg))
  175. usage ("Serevr port must be a nonnegative integer\n");
  176. server_port = atoi (optarg);
  177. break;
  178. case 'e': /* expect */
  179. server_expect = optarg;
  180. break;
  181. case 's': /* send */
  182. server_send = optarg;
  183. break;
  184. }
  185. }
  186. c = optind;
  187. if (server_address == NULL && argv[c]) {
  188. if (is_host (argv[c]) == FALSE)
  189. usage ("Invalid host name/address\n");
  190. server_address = argv[c++];
  191. }
  192. else {
  193. usage ("Host name was not supplied\n");
  194. }
  195. return c;
  196. }
  197. void
  198. print_usage (void)
  199. {
  200. printf
  201. ("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n"
  202. " [-e expect] [-s send] [-t to_sec] [-v]\n", progname);
  203. }
  204. void
  205. print_help (void)
  206. {
  207. print_revision (progname, "$Revision$");
  208. printf
  209. ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
  210. "This plugin tests an UDP connection with the specified host.\n\n");
  211. print_usage ();
  212. printf
  213. ("Options:\n"
  214. " -H, --hostname=ADDRESS\n"
  215. " Host name argument for servers using host headers (use numeric\n"
  216. " address if possible to bypass DNS lookup).\n"
  217. " -p, --port=INTEGER\n"
  218. " Port number\n"
  219. " -e, --expect=STRING <optional>\n"
  220. " String to expect in first line of server response\n"
  221. " -s, --send=STRING <optional>\n"
  222. " String to send to the server when initiating the connection\n"
  223. " -w, --warning=INTEGER <optional>\n"
  224. " Response time to result in warning status (seconds)\n"
  225. " -c, --critical=INTEGER <optional>\n"
  226. " Response time to result in critical status (seconds)\n"
  227. " -t, --timeout=INTEGER <optional>\n"
  228. " Seconds before connection times out (default: %d)\n"
  229. " -v, --verbose <optional>\n"
  230. " Show details for command-line debugging (do not use with nagios server)\n"
  231. " -h, --help\n"
  232. " Print detailed help screen and exit\n"
  233. " -V, --version\n"
  234. " Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT);
  235. }