check_udp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /******************************************************************************
  2. *
  3. * CHECK_UDP.C
  4. *
  5. * Program: UDP port plugin for Nagios
  6. * License: GPL
  7. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  8. *
  9. * Last Modified: $Date$
  10. *
  11. * Command line: CHECK_UDP <host_address> [-p port] [-s send] [-e expect] \
  12. * [-wt warn_time] [-ct crit_time] [-to to_sec]
  13. *
  14. * Description:
  15. *
  16. * This plugin will attempt to connect to the specified port
  17. * on the host. Successul connects return STATE_OK, refusals
  18. * and timeouts return STATE_CRITICAL, other errors return
  19. * STATE_UNKNOWN.
  20. *
  21. * License Information:
  22. *
  23. * This program is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36. *
  37. *****************************************************************************/
  38. #include "config.h"
  39. #include "common.h"
  40. #include "netutils.h"
  41. #include "utils.h"
  42. #define PROGNAME "check_udp"
  43. int warning_time = 0;
  44. int check_warning_time = FALSE;
  45. int critical_time = 0;
  46. int check_critical_time = FALSE;
  47. int process_arguments (int, char **);
  48. int call_getopt (int, char **);
  49. void print_usage (void);
  50. void print_help (void);
  51. int verbose = FALSE;
  52. int server_port = 0;
  53. char *server_address = NULL;
  54. char *server_expect = NULL;
  55. char *server_send = NULL;
  56. int
  57. main (int argc, char **argv)
  58. {
  59. int result;
  60. char recv_buffer[MAX_INPUT_BUFFER];
  61. if (process_arguments (argc, argv) == ERROR)
  62. usage ("\n");
  63. /* initialize alarm signal handling */
  64. signal (SIGALRM, socket_timeout_alarm_handler);
  65. /* set socket timeout */
  66. alarm (socket_timeout);
  67. time (&start_time);
  68. result =
  69. process_udp_request (server_address, server_port, server_send,
  70. recv_buffer, MAX_INPUT_BUFFER - 1);
  71. time (&end_time);
  72. if (result != STATE_OK) {
  73. printf ("No response from host on port %d\n", server_port);
  74. result = STATE_CRITICAL;
  75. }
  76. else {
  77. /* check to see if we got the response we wanted */
  78. if (server_expect) {
  79. if (!strstr (recv_buffer, server_expect)) {
  80. printf ("Invalid response received from host on port %d\n",
  81. server_port);
  82. result = STATE_CRITICAL;
  83. }
  84. }
  85. }
  86. /* we connected, so close connection before exiting */
  87. if (result == STATE_OK) {
  88. if (check_critical_time == TRUE
  89. && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
  90. else if (check_warning_time == TRUE
  91. && (end_time - start_time) > warning_time) result =
  92. STATE_WARNING;
  93. printf ("Connection %s on port %d - %d second response time\n",
  94. (result == STATE_OK) ? "accepted" : "problem", server_port,
  95. (int) (end_time - start_time));
  96. }
  97. /* reset the alarm */
  98. alarm (0);
  99. return result;
  100. }
  101. /* process command-line arguments */
  102. int
  103. process_arguments (int argc, char **argv)
  104. {
  105. int c;
  106. if (argc < 2)
  107. usage ("\n");
  108. for (c = 1; c < argc; c++) {
  109. if (strcmp ("-to", argv[c]) == 0)
  110. strcpy (argv[c], "-t");
  111. else if (strcmp ("-wt", argv[c]) == 0)
  112. strcpy (argv[c], "-w");
  113. else if (strcmp ("-ct", argv[c]) == 0)
  114. strcpy (argv[c], "-c");
  115. }
  116. c = 0;
  117. while ((c += call_getopt (argc - c, &argv[c])) < argc) {
  118. if (is_option (argv[c]))
  119. continue;
  120. if (server_address == NULL) {
  121. if (argc > c) {
  122. if (is_host (argv[c]) == FALSE)
  123. usage ("Invalid host name/address\n");
  124. server_address = argv[c];
  125. }
  126. else {
  127. usage ("Host name was not supplied\n");
  128. }
  129. }
  130. }
  131. if (server_send == NULL)
  132. server_send = strscpy (server_send, "");
  133. return OK;
  134. }
  135. int
  136. call_getopt (int argc, char **argv)
  137. {
  138. int c, i = 0;
  139. #ifdef HAVE_GETOPT_H
  140. int option_index = 0;
  141. static struct option long_options[] = {
  142. {"hostname", required_argument, 0, 'H'},
  143. {"critical", required_argument, 0, 'c'},
  144. {"warning", required_argument, 0, 'w'},
  145. {"timeout", required_argument, 0, 't'},
  146. {"port", required_argument, 0, 'p'},
  147. {"expect", required_argument, 0, 'e'},
  148. {"send", required_argument, 0, 's'},
  149. {"verbose", no_argument, 0, 'v'},
  150. {"version", no_argument, 0, 'V'},
  151. {"help", no_argument, 0, 'h'},
  152. {0, 0, 0, 0}
  153. };
  154. #endif
  155. while (1) {
  156. #ifdef HAVE_GETOPT_H
  157. c =
  158. getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options,
  159. &option_index);
  160. #else
  161. c = getopt (argc, argv, "+hVvH:e:s:c:w:t:p:");
  162. #endif
  163. i++;
  164. if (c == -1 || c == EOF || c == 1)
  165. break;
  166. switch (c) {
  167. case 'H':
  168. case 'c':
  169. case 'w':
  170. case 't':
  171. case 'p':
  172. case 'e':
  173. case 's':
  174. i++;
  175. }
  176. switch (c) {
  177. case '?': /* print short usage statement if args not parsable */
  178. printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
  179. print_usage ();
  180. exit (STATE_UNKNOWN);
  181. case 'h': /* help */
  182. print_help ();
  183. exit (STATE_OK);
  184. case 'V': /* version */
  185. print_revision (my_basename (argv[0]), "$Revision$");
  186. exit (STATE_OK);
  187. case 'v': /* verbose mode */
  188. verbose = TRUE;
  189. break;
  190. case 'H': /* hostname */
  191. if (is_host (optarg) == FALSE)
  192. usage ("Invalid host name/address\n");
  193. server_address = optarg;
  194. break;
  195. case 'c': /* critical */
  196. if (!is_intnonneg (optarg))
  197. usage ("Critical threshold must be a nonnegative integer\n");
  198. critical_time = atoi (optarg);
  199. check_critical_time = TRUE;
  200. break;
  201. case 'w': /* warning */
  202. if (!is_intnonneg (optarg))
  203. usage ("Warning threshold must be a nonnegative integer\n");
  204. warning_time = atoi (optarg);
  205. check_warning_time = TRUE;
  206. break;
  207. case 't': /* timeout */
  208. if (!is_intnonneg (optarg))
  209. usage ("Timeout interval must be a nonnegative integer\n");
  210. socket_timeout = atoi (optarg);
  211. break;
  212. case 'p': /* port */
  213. if (!is_intnonneg (optarg))
  214. usage ("Serevr port must be a nonnegative integer\n");
  215. server_port = atoi (optarg);
  216. break;
  217. case 'e': /* expect */
  218. server_expect = optarg;
  219. break;
  220. case 's': /* send */
  221. server_send = optarg;
  222. break;
  223. }
  224. }
  225. return i;
  226. }
  227. void
  228. print_usage (void)
  229. {
  230. printf
  231. ("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n"
  232. " [-e expect] [-s send] [-t to_sec] [-v]\n", PROGNAME);
  233. }
  234. void
  235. print_help (void)
  236. {
  237. print_revision (PROGNAME, "$Revision$");
  238. printf
  239. ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
  240. "This plugin tests an UDP connection with the specified host.\n\n");
  241. print_usage ();
  242. printf
  243. ("Options:\n"
  244. " -H, --hostname=ADDRESS\n"
  245. " Host name argument for servers using host headers (use numeric\n"
  246. " address if possible to bypass DNS lookup).\n"
  247. " -p, --port=INTEGER\n"
  248. " Port number\n"
  249. " -e, --expect=STRING <optional>\n"
  250. " String to expect in first line of server response\n"
  251. " -s, --send=STRING <optional>\n"
  252. " String to send to the server when initiating the connection\n"
  253. " -w, --warning=INTEGER <optional>\n"
  254. " Response time to result in warning status (seconds)\n"
  255. " -c, --critical=INTEGER <optional>\n"
  256. " Response time to result in critical status (seconds)\n"
  257. " -t, --timeout=INTEGER <optional>\n"
  258. " Seconds before connection times out (default: %d)\n"
  259. " -v, --verbose <optional>\n"
  260. " Show details for command-line debugging (do not use with nagios server)\n"
  261. " -h, --help\n"
  262. " Print detailed help screen and exit\n"
  263. " -V, --version\n"
  264. " Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT);
  265. }