check_udp.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 = process_udp_request (server_address, server_port, server_send,
  68. recv_buffer, MAX_INPUT_BUFFER - 1);
  69. time (&end_time);
  70. if (result != STATE_OK) {
  71. printf ("No response from host on port %d\n", server_port);
  72. result = STATE_CRITICAL;
  73. }
  74. else {
  75. /* check to see if we got the response we wanted */
  76. if (server_expect) {
  77. if (!strstr (recv_buffer, server_expect)) {
  78. printf ("Invalid response received from host on port %d\n",
  79. server_port);
  80. result = STATE_CRITICAL;
  81. }
  82. }
  83. }
  84. /* we connected, so close connection before exiting */
  85. if (result == STATE_OK) {
  86. if (check_critical_time == TRUE
  87. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  88. else if (check_warning_time == TRUE
  89. && (end_time - start_time) > warning_time) result =
  90. STATE_WARNING;
  91. printf ("Connection %s on port %d - %d second response time\n",
  92. (result == STATE_OK) ? "accepted" : "problem", server_port,
  93. (int) (end_time - start_time));
  94. }
  95. /* reset the alarm */
  96. alarm (0);
  97. return result;
  98. }
  99. /* process command-line arguments */
  100. int
  101. process_arguments (int argc, char **argv)
  102. {
  103. int c;
  104. int option_index = 0;
  105. static struct option long_options[] = {
  106. {"hostname", required_argument, 0, 'H'},
  107. {"critical", required_argument, 0, 'c'},
  108. {"warning", required_argument, 0, 'w'},
  109. {"timeout", required_argument, 0, 't'},
  110. {"port", required_argument, 0, 'p'},
  111. {"expect", required_argument, 0, 'e'},
  112. {"send", required_argument, 0, 's'},
  113. {"verbose", no_argument, 0, 'v'},
  114. {"version", no_argument, 0, 'V'},
  115. {"help", no_argument, 0, 'h'},
  116. {0, 0, 0, 0}
  117. };
  118. if (argc < 2)
  119. usage ("\n");
  120. for (c = 1; c < argc; c++) {
  121. if (strcmp ("-to", argv[c]) == 0)
  122. strcpy (argv[c], "-t");
  123. else if (strcmp ("-wt", argv[c]) == 0)
  124. strcpy (argv[c], "-w");
  125. else if (strcmp ("-ct", argv[c]) == 0)
  126. strcpy (argv[c], "-c");
  127. }
  128. while (1) {
  129. c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
  130. if (c == -1 || c == EOF || c == 1)
  131. break;
  132. switch (c) {
  133. case '?': /* print short usage statement if args not parsable */
  134. printf ("%s: Unknown argument: %s\n\n", progname, optarg);
  135. print_usage ();
  136. exit (STATE_UNKNOWN);
  137. case 'h': /* help */
  138. print_help ();
  139. exit (STATE_OK);
  140. case 'V': /* version */
  141. print_revision (progname, "$Revision$");
  142. exit (STATE_OK);
  143. case 'v': /* verbose mode */
  144. verbose = TRUE;
  145. break;
  146. case 'H': /* hostname */
  147. if (is_host (optarg) == FALSE)
  148. usage ("Invalid host name/address\n");
  149. server_address = optarg;
  150. break;
  151. case 'c': /* critical */
  152. if (!is_intnonneg (optarg))
  153. usage ("Critical threshold must be a nonnegative integer\n");
  154. critical_time = atoi (optarg);
  155. check_critical_time = TRUE;
  156. break;
  157. case 'w': /* warning */
  158. if (!is_intnonneg (optarg))
  159. usage ("Warning threshold must be a nonnegative integer\n");
  160. warning_time = atoi (optarg);
  161. check_warning_time = TRUE;
  162. break;
  163. case 't': /* timeout */
  164. if (!is_intnonneg (optarg))
  165. usage ("Timeout interval must be a nonnegative integer\n");
  166. socket_timeout = atoi (optarg);
  167. break;
  168. case 'p': /* port */
  169. if (!is_intnonneg (optarg))
  170. usage ("Serevr port must be a nonnegative integer\n");
  171. server_port = atoi (optarg);
  172. break;
  173. case 'e': /* expect */
  174. server_expect = optarg;
  175. break;
  176. case 's': /* send */
  177. server_send = optarg;
  178. break;
  179. }
  180. }
  181. c = optind;
  182. if (server_address == NULL && c < argc && argv[c]) {
  183. if (is_host (argv[c]) == FALSE)
  184. usage ("Invalid host name/address\n");
  185. server_address = argv[c++];
  186. }
  187. if (server_address == NULL)
  188. usage ("Host name was not supplied\n");
  189. return c;
  190. }
  191. void
  192. print_usage (void)
  193. {
  194. printf
  195. ("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n"
  196. " [-e expect] [-s send] [-t to_sec] [-v]\n", progname);
  197. }
  198. void
  199. print_help (void)
  200. {
  201. print_revision (progname, "$Revision$");
  202. printf
  203. ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
  204. "This plugin tests an UDP connection with the specified host.\n\n");
  205. print_usage ();
  206. printf
  207. ("Options:\n"
  208. " -H, --hostname=ADDRESS\n"
  209. " Host name argument for servers using host headers (use numeric\n"
  210. " address if possible to bypass DNS lookup).\n"
  211. " -p, --port=INTEGER\n"
  212. " Port number\n"
  213. " -e, --expect=STRING <optional>\n"
  214. " String to expect in first line of server response\n"
  215. " -s, --send=STRING <optional>\n"
  216. " String to send to the server when initiating the connection\n"
  217. " -w, --warning=INTEGER <optional>\n"
  218. " Response time to result in warning status (seconds)\n"
  219. " -c, --critical=INTEGER <optional>\n"
  220. " Response time to result in critical status (seconds)\n"
  221. " -t, --timeout=INTEGER <optional>\n"
  222. " Seconds before connection times out (default: %d)\n"
  223. " -v, --verbose <optional>\n"
  224. " Show details for command-line debugging (do not use with nagios server)\n"
  225. " -h, --help\n"
  226. " Print detailed help screen and exit\n"
  227. " -V, --version\n"
  228. " Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT);
  229. }