ac2210257d4f32d422fe2794b6a16063f89237f6
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 const char *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 int option_index = 0;
129 static struct option long_options[] = {
130 {"hostname", required_argument, 0, 'H'},
131 {"critical", required_argument, 0, 'c'},
132 {"warning", required_argument, 0, 'w'},
133 {"timeout", required_argument, 0, 't'},
134 {"port", required_argument, 0, 'p'},
135 {"expect", required_argument, 0, 'e'},
136 {"send", required_argument, 0, 's'},
137 {"verbose", no_argument, 0, 'v'},
138 {"version", no_argument, 0, 'V'},
139 {"help", no_argument, 0, 'h'},
140 {0, 0, 0, 0}
141 };
143 if (argc < 2)
144 usage ("\n");
146 for (c = 1; c < argc; c++) {
147 if (strcmp ("-to", argv[c]) == 0)
148 strcpy (argv[c], "-t");
149 else if (strcmp ("-wt", argv[c]) == 0)
150 strcpy (argv[c], "-w");
151 else if (strcmp ("-ct", argv[c]) == 0)
152 strcpy (argv[c], "-c");
153 }
155 while (1) {
156 c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
158 if (c == -1 || c == EOF || c == 1)
159 break;
161 switch (c) {
162 case '?': /* print short usage statement if args not parsable */
163 printf ("%s: Unknown argument: %s\n\n", progname, optarg);
164 print_usage ();
165 exit (STATE_UNKNOWN);
166 case 'h': /* help */
167 print_help ();
168 exit (STATE_OK);
169 case 'V': /* version */
170 print_revision (progname, "$Revision$");
171 exit (STATE_OK);
172 case 'v': /* verbose mode */
173 verbose = TRUE;
174 break;
175 case 'H': /* hostname */
176 if (is_host (optarg) == FALSE)
177 usage ("Invalid host name/address\n");
178 server_address = optarg;
179 break;
180 case 'c': /* critical */
181 if (!is_intnonneg (optarg))
182 usage ("Critical threshold must be a nonnegative integer\n");
183 critical_time = atoi (optarg);
184 check_critical_time = TRUE;
185 break;
186 case 'w': /* warning */
187 if (!is_intnonneg (optarg))
188 usage ("Warning threshold must be a nonnegative integer\n");
189 warning_time = atoi (optarg);
190 check_warning_time = TRUE;
191 break;
192 case 't': /* timeout */
193 if (!is_intnonneg (optarg))
194 usage ("Timeout interval must be a nonnegative integer\n");
195 socket_timeout = atoi (optarg);
196 break;
197 case 'p': /* port */
198 if (!is_intnonneg (optarg))
199 usage ("Serevr port must be a nonnegative integer\n");
200 server_port = atoi (optarg);
201 break;
202 case 'e': /* expect */
203 server_expect = optarg;
204 break;
205 case 's': /* send */
206 server_send = optarg;
207 break;
208 }
209 }
211 c = optind;
212 if (server_address == NULL && c < argc && argv[c]) {
213 if (is_host (argv[c]) == FALSE)
214 usage ("Invalid host name/address\n");
215 server_address = argv[c++];
216 }
218 if (server_address == NULL)
219 usage ("Host name was not supplied\n");
221 return c;
222 }
228 void
229 print_usage (void)
230 {
231 printf
232 ("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n"
233 " [-e expect] [-s send] [-t to_sec] [-v]\n", progname);
234 }
240 void
241 print_help (void)
242 {
243 print_revision (progname, "$Revision$");
244 printf
245 ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
246 "This plugin tests an UDP connection with the specified host.\n\n");
247 print_usage ();
248 printf
249 ("Options:\n"
250 " -H, --hostname=ADDRESS\n"
251 " Host name argument for servers using host headers (use numeric\n"
252 " address if possible to bypass DNS lookup).\n"
253 " -p, --port=INTEGER\n"
254 " Port number\n"
255 " -e, --expect=STRING <optional>\n"
256 " String to expect in first line of server response\n"
257 " -s, --send=STRING <optional>\n"
258 " String to send to the server when initiating the connection\n"
259 " -w, --warning=INTEGER <optional>\n"
260 " Response time to result in warning status (seconds)\n"
261 " -c, --critical=INTEGER <optional>\n"
262 " Response time to result in critical status (seconds)\n"
263 " -t, --timeout=INTEGER <optional>\n"
264 " Seconds before connection times out (default: %d)\n"
265 " -v, --verbose <optional>\n"
266 " Show details for command-line debugging (do not use with nagios server)\n"
267 " -h, --help\n"
268 " Print detailed help screen and exit\n"
269 " -V, --version\n"
270 " Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT);
271 }