check_udp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. printf (_(UT_HLP_VRS), progname, progname);
  31. }
  32. void
  33. print_help (void)
  34. {
  35. print_revision (progname, revision);
  36. printf (_("Copyright (c) 1999 Ethan Galstad\n"));
  37. printf (_(COPYRIGHT), copyright, email);
  38. printf (_("\
  39. This plugin tests an UDP connection with the specified host.\n\n"));
  40. print_usage ();
  41. printf (_(UT_HELP_VRSN));
  42. printf (_(UT_HOST_PORT), 'p', "none");
  43. printf (_("\
  44. -e, --expect=STRING <optional>\n\
  45. String to expect in first line of server response\n\
  46. -s, --send=STRING <optional>\n\
  47. String to send to the server when initiating the connection\n"));
  48. printf (_(UT_WARN_CRIT));
  49. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  50. printf (_(UT_VERBOSE));
  51. printf (_("\
  52. This plugin will attempt to connect to the specified port on the host.\n\
  53. Successful connects return STATE_OK, refusals and timeouts return\n\
  54. STATE_CRITICAL, other errors return STATE_UNKNOWN.\n\n"));
  55. printf(_(UT_SUPPORT));
  56. }
  57. int process_arguments (int, char **);
  58. int warning_time = 0;
  59. int check_warning_time = FALSE;
  60. int critical_time = 0;
  61. int check_critical_time = FALSE;
  62. int verbose = FALSE;
  63. int server_port = 0;
  64. char *server_address = NULL;
  65. char *server_expect = NULL;
  66. char *server_send = "";
  67. int
  68. main (int argc, char **argv)
  69. {
  70. int result;
  71. char recv_buffer[MAX_INPUT_BUFFER];
  72. if (process_arguments (argc, argv) == ERROR)
  73. usage ("\n");
  74. /* initialize alarm signal handling */
  75. signal (SIGALRM, socket_timeout_alarm_handler);
  76. /* set socket timeout */
  77. alarm (socket_timeout);
  78. time (&start_time);
  79. result = process_udp_request (server_address, server_port, server_send,
  80. recv_buffer, MAX_INPUT_BUFFER - 1);
  81. time (&end_time);
  82. if (result != STATE_OK) {
  83. printf ("No response from host on port %d\n", server_port);
  84. result = STATE_CRITICAL;
  85. }
  86. else {
  87. /* check to see if we got the response we wanted */
  88. if (server_expect) {
  89. if (!strstr (recv_buffer, server_expect)) {
  90. printf ("Invalid response received from host on port %d\n",
  91. server_port);
  92. result = STATE_CRITICAL;
  93. }
  94. }
  95. }
  96. /* we connected, so close connection before exiting */
  97. if (result == STATE_OK) {
  98. if (check_critical_time == TRUE
  99. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  100. else if (check_warning_time == TRUE
  101. && (end_time - start_time) > warning_time) result =
  102. STATE_WARNING;
  103. printf (_("Connection %s on port %d - %d second response time\n"),
  104. (result == STATE_OK) ? _("accepted") : _("problem"), server_port,
  105. (int) (end_time - start_time));
  106. }
  107. /* reset the alarm */
  108. alarm (0);
  109. return result;
  110. }
  111. /* process command-line arguments */
  112. int
  113. process_arguments (int argc, char **argv)
  114. {
  115. int c;
  116. int option_index = 0;
  117. static struct option long_options[] = {
  118. {"hostname", required_argument, 0, 'H'},
  119. {"critical", required_argument, 0, 'c'},
  120. {"warning", required_argument, 0, 'w'},
  121. {"timeout", required_argument, 0, 't'},
  122. {"port", required_argument, 0, 'p'},
  123. {"expect", required_argument, 0, 'e'},
  124. {"send", required_argument, 0, 's'},
  125. {"verbose", no_argument, 0, 'v'},
  126. {"version", no_argument, 0, 'V'},
  127. {"help", no_argument, 0, 'h'},
  128. {0, 0, 0, 0}
  129. };
  130. if (argc < 2)
  131. usage ("\n");
  132. for (c = 1; c < argc; c++) {
  133. if (strcmp ("-to", argv[c]) == 0)
  134. strcpy (argv[c], "-t");
  135. else if (strcmp ("-wt", argv[c]) == 0)
  136. strcpy (argv[c], "-w");
  137. else if (strcmp ("-ct", argv[c]) == 0)
  138. strcpy (argv[c], "-c");
  139. }
  140. while (1) {
  141. c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
  142. if (c == -1 || c == EOF || c == 1)
  143. break;
  144. switch (c) {
  145. case '?': /* print short usage statement if args not parsable */
  146. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  147. print_usage ();
  148. exit (STATE_UNKNOWN);
  149. case 'h': /* help */
  150. print_help ();
  151. exit (STATE_OK);
  152. case 'V': /* version */
  153. print_revision (progname, revision);
  154. exit (STATE_OK);
  155. case 'v': /* verbose mode */
  156. verbose = TRUE;
  157. break;
  158. case 'H': /* hostname */
  159. if (is_host (optarg) == FALSE)
  160. usage (_("Invalid host name/address\n"));
  161. server_address = optarg;
  162. break;
  163. case 'c': /* critical */
  164. if (!is_intnonneg (optarg))
  165. usage (_("Critical threshold must be a nonnegative integer\n"));
  166. critical_time = atoi (optarg);
  167. check_critical_time = TRUE;
  168. break;
  169. case 'w': /* warning */
  170. if (!is_intnonneg (optarg))
  171. usage (_("Warning threshold must be a nonnegative integer\n"));
  172. warning_time = atoi (optarg);
  173. check_warning_time = TRUE;
  174. break;
  175. case 't': /* timeout */
  176. if (!is_intnonneg (optarg))
  177. usage (_("Timeout interval must be a nonnegative integer\n"));
  178. socket_timeout = atoi (optarg);
  179. break;
  180. case 'p': /* port */
  181. if (!is_intnonneg (optarg))
  182. usage (_("Server port must be a nonnegative integer\n"));
  183. server_port = atoi (optarg);
  184. break;
  185. case 'e': /* expect */
  186. server_expect = optarg;
  187. break;
  188. case 's': /* send */
  189. server_send = optarg;
  190. break;
  191. }
  192. }
  193. c = optind;
  194. if (server_address == NULL && c < argc && argv[c]) {
  195. if (is_host (argv[c]) == FALSE)
  196. usage (_("Invalid host name/address\n"));
  197. server_address = argv[c++];
  198. }
  199. if (server_address == NULL)
  200. usage (_("Host name was not supplied\n"));
  201. return c;
  202. }