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 = process_udp_request (server_address, server_port, server_send,
78 recv_buffer, MAX_INPUT_BUFFER - 1);
79 time (&end_time);
81 if (result != STATE_OK) {
82 printf ("No response from host on port %d\n", server_port);
83 result = STATE_CRITICAL;
84 }
86 else {
88 /* check to see if we got the response we wanted */
89 if (server_expect) {
90 if (!strstr (recv_buffer, server_expect)) {
91 printf ("Invalid response received from host on port %d\n",
92 server_port);
93 result = STATE_CRITICAL;
94 }
95 }
96 }
98 /* we connected, so close connection before exiting */
99 if (result == STATE_OK) {
101 if (check_critical_time == TRUE
102 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
103 else if (check_warning_time == TRUE
104 && (end_time - start_time) > warning_time) result =
105 STATE_WARNING;
107 printf ("Connection %s on port %d - %d second response time\n",
108 (result == STATE_OK) ? "accepted" : "problem", server_port,
109 (int) (end_time - start_time));
110 }
112 /* reset the alarm */
113 alarm (0);
115 return result;
116 }
121 /* process command-line arguments */
122 int
123 process_arguments (int argc, char **argv)
124 {
125 int c;
127 int option_index = 0;
128 static struct option long_options[] = {
129 {"hostname", required_argument, 0, 'H'},
130 {"critical", required_argument, 0, 'c'},
131 {"warning", required_argument, 0, 'w'},
132 {"timeout", required_argument, 0, 't'},
133 {"port", required_argument, 0, 'p'},
134 {"expect", required_argument, 0, 'e'},
135 {"send", required_argument, 0, 's'},
136 {"verbose", no_argument, 0, 'v'},
137 {"version", no_argument, 0, 'V'},
138 {"help", no_argument, 0, 'h'},
139 {0, 0, 0, 0}
140 };
142 if (argc < 2)
143 usage ("\n");
145 for (c = 1; c < argc; c++) {
146 if (strcmp ("-to", argv[c]) == 0)
147 strcpy (argv[c], "-t");
148 else if (strcmp ("-wt", argv[c]) == 0)
149 strcpy (argv[c], "-w");
150 else if (strcmp ("-ct", argv[c]) == 0)
151 strcpy (argv[c], "-c");
152 }
154 while (1) {
155 c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
157 if (c == -1 || c == EOF || c == 1)
158 break;
160 switch (c) {
161 case '?': /* print short usage statement if args not parsable */
162 printf ("%s: Unknown argument: %s\n\n", progname, optarg);
163 print_usage ();
164 exit (STATE_UNKNOWN);
165 case 'h': /* help */
166 print_help ();
167 exit (STATE_OK);
168 case 'V': /* version */
169 print_revision (progname, "$Revision$");
170 exit (STATE_OK);
171 case 'v': /* verbose mode */
172 verbose = TRUE;
173 break;
174 case 'H': /* hostname */
175 if (is_host (optarg) == FALSE)
176 usage ("Invalid host name/address\n");
177 server_address = optarg;
178 break;
179 case 'c': /* critical */
180 if (!is_intnonneg (optarg))
181 usage ("Critical threshold must be a nonnegative integer\n");
182 critical_time = atoi (optarg);
183 check_critical_time = TRUE;
184 break;
185 case 'w': /* warning */
186 if (!is_intnonneg (optarg))
187 usage ("Warning threshold must be a nonnegative integer\n");
188 warning_time = atoi (optarg);
189 check_warning_time = TRUE;
190 break;
191 case 't': /* timeout */
192 if (!is_intnonneg (optarg))
193 usage ("Timeout interval must be a nonnegative integer\n");
194 socket_timeout = atoi (optarg);
195 break;
196 case 'p': /* port */
197 if (!is_intnonneg (optarg))
198 usage ("Serevr port must be a nonnegative integer\n");
199 server_port = atoi (optarg);
200 break;
201 case 'e': /* expect */
202 server_expect = optarg;
203 break;
204 case 's': /* send */
205 server_send = optarg;
206 break;
207 }
208 }
210 c = optind;
211 if (server_address == NULL && c < argc && argv[c]) {
212 if (is_host (argv[c]) == FALSE)
213 usage ("Invalid host name/address\n");
214 server_address = argv[c++];
215 }
217 if (server_address == NULL)
218 usage ("Host name was not supplied\n");
220 return c;
221 }
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 }
239 void
240 print_help (void)
241 {
242 print_revision (progname, "$Revision$");
243 printf
244 ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
245 "This plugin tests an UDP connection with the specified host.\n\n");
246 print_usage ();
247 printf
248 ("Options:\n"
249 " -H, --hostname=ADDRESS\n"
250 " Host name argument for servers using host headers (use numeric\n"
251 " address if possible to bypass DNS lookup).\n"
252 " -p, --port=INTEGER\n"
253 " Port number\n"
254 " -e, --expect=STRING <optional>\n"
255 " String to expect in first line of server response\n"
256 " -s, --send=STRING <optional>\n"
257 " String to send to the server when initiating the connection\n"
258 " -w, --warning=INTEGER <optional>\n"
259 " Response time to result in warning status (seconds)\n"
260 " -c, --critical=INTEGER <optional>\n"
261 " Response time to result in critical status (seconds)\n"
262 " -t, --timeout=INTEGER <optional>\n"
263 " Seconds before connection times out (default: %d)\n"
264 " -v, --verbose <optional>\n"
265 " Show details for command-line debugging (do not use with nagios server)\n"
266 " -h, --help\n"
267 " Print detailed help screen and exit\n"
268 " -V, --version\n"
269 " Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT);
270 }