f6b528dcaaa3ced63e5ac30d0d59f835ceb366a4
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 *****************************************************************************/
39 #include "config.h"
40 #include "common.h"
41 #include "netutils.h"
42 #include "utils.h"
44 #define PROGNAME "check_udp"
46 int warning_time = 0;
47 int check_warning_time = FALSE;
48 int critical_time = 0;
49 int check_critical_time = FALSE;
51 int process_arguments (int, char **);
52 void print_usage (void);
53 void print_help (void);
55 int verbose = FALSE;
56 int server_port = 0;
57 char *server_address = NULL;
58 char *server_expect = NULL;
59 char *server_send = "";
61 int
62 main (int argc, char **argv)
63 {
64 int result;
65 char recv_buffer[MAX_INPUT_BUFFER];
67 if (process_arguments (argc, argv) == ERROR)
68 usage ("\n");
70 /* initialize alarm signal handling */
71 signal (SIGALRM, socket_timeout_alarm_handler);
73 /* set socket timeout */
74 alarm (socket_timeout);
76 time (&start_time);
77 result =
78 process_udp_request (server_address, server_port, server_send,
79 recv_buffer, MAX_INPUT_BUFFER - 1);
80 time (&end_time);
82 if (result != STATE_OK) {
83 printf ("No response from host on port %d\n", server_port);
84 result = STATE_CRITICAL;
85 }
87 else {
89 /* check to see if we got the response we wanted */
90 if (server_expect) {
91 if (!strstr (recv_buffer, server_expect)) {
92 printf ("Invalid response received from host on port %d\n",
93 server_port);
94 result = STATE_CRITICAL;
95 }
96 }
97 }
99 /* we connected, so close connection before exiting */
100 if (result == STATE_OK) {
102 if (check_critical_time == TRUE
103 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
104 else if (check_warning_time == TRUE
105 && (end_time - start_time) > warning_time) result =
106 STATE_WARNING;
108 printf ("Connection %s on port %d - %d second response time\n",
109 (result == STATE_OK) ? "accepted" : "problem", server_port,
110 (int) (end_time - start_time));
111 }
113 /* reset the alarm */
114 alarm (0);
116 return result;
117 }
122 /* process command-line arguments */
123 int
124 process_arguments (int argc, char **argv)
125 {
126 int c;
128 #ifdef HAVE_GETOPT_H
129 int option_index = 0;
130 static struct option long_options[] = {
131 {"hostname", required_argument, 0, 'H'},
132 {"critical", required_argument, 0, 'c'},
133 {"warning", required_argument, 0, 'w'},
134 {"timeout", required_argument, 0, 't'},
135 {"port", required_argument, 0, 'p'},
136 {"expect", required_argument, 0, 'e'},
137 {"send", required_argument, 0, 's'},
138 {"verbose", no_argument, 0, 'v'},
139 {"version", no_argument, 0, 'V'},
140 {"help", no_argument, 0, 'h'},
141 {0, 0, 0, 0}
142 };
143 #endif
145 if (argc < 2)
146 usage ("\n");
148 for (c = 1; c < argc; c++) {
149 if (strcmp ("-to", argv[c]) == 0)
150 strcpy (argv[c], "-t");
151 else if (strcmp ("-wt", argv[c]) == 0)
152 strcpy (argv[c], "-w");
153 else if (strcmp ("-ct", argv[c]) == 0)
154 strcpy (argv[c], "-c");
155 }
157 while (1) {
158 #ifdef HAVE_GETOPT_H
159 c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
160 #else
161 c = getopt (argc, argv, "+hVvH:e:s:c:w:t:p:");
162 #endif
164 switch (c) {
165 case '?': /* print short usage statement if args not parsable */
166 printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
167 print_usage ();
168 exit (STATE_UNKNOWN);
169 case 'h': /* help */
170 print_help ();
171 exit (STATE_OK);
172 case 'V': /* version */
173 print_revision (my_basename (argv[0]), "$Revision$");
174 exit (STATE_OK);
175 case 'v': /* verbose mode */
176 verbose = TRUE;
177 break;
178 case 'H': /* hostname */
179 if (is_host (optarg) == FALSE)
180 usage ("Invalid host name/address\n");
181 server_address = optarg;
182 break;
183 case 'c': /* critical */
184 if (!is_intnonneg (optarg))
185 usage ("Critical threshold must be a nonnegative integer\n");
186 critical_time = atoi (optarg);
187 check_critical_time = TRUE;
188 break;
189 case 'w': /* warning */
190 if (!is_intnonneg (optarg))
191 usage ("Warning threshold must be a nonnegative integer\n");
192 warning_time = atoi (optarg);
193 check_warning_time = TRUE;
194 break;
195 case 't': /* timeout */
196 if (!is_intnonneg (optarg))
197 usage ("Timeout interval must be a nonnegative integer\n");
198 socket_timeout = atoi (optarg);
199 break;
200 case 'p': /* port */
201 if (!is_intnonneg (optarg))
202 usage ("Serevr port must be a nonnegative integer\n");
203 server_port = atoi (optarg);
204 break;
205 case 'e': /* expect */
206 server_expect = optarg;
207 break;
208 case 's': /* send */
209 server_send = optarg;
210 break;
211 }
212 }
214 c = optind;
215 if (server_address == NULL && argv[c]) {
216 if (is_host (argv[c]) == FALSE)
217 usage ("Invalid host name/address\n");
218 server_address = argv[c++];
219 }
220 else {
221 usage ("Host name was not supplied\n");
222 }
224 return c;
225 }
231 void
232 print_usage (void)
233 {
234 printf
235 ("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n"
236 " [-e expect] [-s send] [-t to_sec] [-v]\n", PROGNAME);
237 }
243 void
244 print_help (void)
245 {
246 print_revision (PROGNAME, "$Revision$");
247 printf
248 ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
249 "This plugin tests an UDP connection with the specified host.\n\n");
250 print_usage ();
251 printf
252 ("Options:\n"
253 " -H, --hostname=ADDRESS\n"
254 " Host name argument for servers using host headers (use numeric\n"
255 " address if possible to bypass DNS lookup).\n"
256 " -p, --port=INTEGER\n"
257 " Port number\n"
258 " -e, --expect=STRING <optional>\n"
259 " String to expect in first line of server response\n"
260 " -s, --send=STRING <optional>\n"
261 " String to send to the server when initiating the connection\n"
262 " -w, --warning=INTEGER <optional>\n"
263 " Response time to result in warning status (seconds)\n"
264 " -c, --critical=INTEGER <optional>\n"
265 " Response time to result in critical status (seconds)\n"
266 " -t, --timeout=INTEGER <optional>\n"
267 " Seconds before connection times out (default: %d)\n"
268 " -v, --verbose <optional>\n"
269 " Show details for command-line debugging (do not use with nagios server)\n"
270 " -h, --help\n"
271 " Print detailed help screen and exit\n"
272 " -V, --version\n"
273 " Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT);
274 }