check_udp.c 6.7 KB

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