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