check_udp.c 7.1 KB

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