check_udp.c 6.7 KB

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