check_udp.c 6.5 KB

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