check_udp.c 6.7 KB

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