check_udp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  117. print_usage ();
  118. exit (STATE_UNKNOWN);
  119. case 'h': /* help */
  120. print_help ();
  121. exit (STATE_OK);
  122. case 'V': /* version */
  123. print_revision (progname, revision);
  124. exit (STATE_OK);
  125. case 'v': /* verbose mode */
  126. verbose = TRUE;
  127. break;
  128. case 'H': /* hostname */
  129. if (is_host (optarg) == FALSE)
  130. usage2 (_("Invalid hostname/address"), optarg);
  131. server_address = optarg;
  132. break;
  133. case 'c': /* critical */
  134. if (!is_intnonneg (optarg))
  135. usage (_("Critical threshold must be a positive integer\n"));
  136. else
  137. critical_time = atoi (optarg);
  138. check_critical_time = TRUE;
  139. break;
  140. case 'w': /* warning */
  141. if (!is_intnonneg (optarg))
  142. usage (_("Warning threshold must be a positive integer\n"));
  143. else
  144. warning_time = atoi (optarg);
  145. check_warning_time = TRUE;
  146. break;
  147. case 't': /* timeout */
  148. if (!is_intnonneg (optarg))
  149. usage2 (_("Timeout interval must be a positive integer"), optarg);
  150. else
  151. socket_timeout = atoi (optarg);
  152. break;
  153. case 'p': /* port */
  154. if (!is_intnonneg (optarg))
  155. usage (_("Port must be a positive integer\n"));
  156. else
  157. server_port = atoi (optarg);
  158. break;
  159. case 'e': /* expect */
  160. server_expect = optarg;
  161. break;
  162. case 's': /* send */
  163. server_send = optarg;
  164. break;
  165. }
  166. }
  167. c = optind;
  168. if (server_address == NULL && c < argc && argv[c]) {
  169. if (is_host (argv[c]) == FALSE)
  170. usage2 (_("Invalid hostname/address"), optarg);
  171. server_address = argv[c++];
  172. }
  173. if (server_address == NULL)
  174. usage4 (_("Hostname was not supplied"));
  175. if (server_send == NULL)
  176. server_send = strdup("");
  177. return c;
  178. }
  179. void
  180. print_help (void)
  181. {
  182. print_revision (progname, revision);
  183. printf ("Copyright (c) 1999 Ethan Galstad\n");
  184. printf (COPYRIGHT, copyright, email);
  185. printf (_("\
  186. This plugin tests an UDP connection with the specified host.\n\n"));
  187. print_usage ();
  188. printf (_(UT_HELP_VRSN));
  189. printf (_(UT_HOST_PORT), 'p', "none");
  190. printf (_("\
  191. -e, --expect=STRING <optional>\n\
  192. String to expect in first line of server response\n\
  193. -s, --send=STRING <optional>\n\
  194. String to send to the server when initiating the connection\n"));
  195. printf (_(UT_WARN_CRIT));
  196. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  197. printf (_(UT_VERBOSE));
  198. printf (_("\
  199. This plugin will attempt to connect to the specified port on the host.\n\
  200. Successful connects return STATE_OK, refusals and timeouts return\n\
  201. STATE_CRITICAL, other errors return STATE_UNKNOWN.\n\n"));
  202. printf(_(UT_SUPPORT));
  203. }
  204. /* Original Command line:
  205. check_udp <host_address> [-p port] [-s send] [-e expect] \
  206. [-wt warn_time] [-ct crit_time] [-to to_sec] */
  207. void
  208. print_usage (void)
  209. {
  210. printf ("\
  211. Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n\
  212. [-e expect] [-s send] [-t to_sec] [-v]\n", progname);
  213. }