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 $Id$
19 *****************************************************************************/
21 const char *progname = "check_udp";
22 const char *revision = "$Revision$";
23 const char *copyright = "1999-2002";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "netutils.h"
28 #include "utils.h"
30 int process_arguments (int, char **);
31 void print_help (void);
32 void print_usage (void);
34 int warning_time = 0;
35 int check_warning_time = FALSE;
36 int critical_time = 0;
37 int check_critical_time = FALSE;
38 int verbose = FALSE;
39 int server_port = 0;
40 char *server_address = NULL;
41 char *server_expect = NULL;
42 char *server_send;
44 int
45 main (int argc, char **argv)
46 {
47 int result;
48 char recv_buffer[MAX_INPUT_BUFFER];
50 setlocale (LC_ALL, "");
51 bindtextdomain (PACKAGE, LOCALEDIR);
52 textdomain (PACKAGE);
54 if (process_arguments (argc, argv) != OK)
55 usage (_("check_udp: could not parse arguments\n"));
57 /* initialize alarm signal handling */
58 signal (SIGALRM, socket_timeout_alarm_handler);
60 /* set socket timeout */
61 alarm (socket_timeout);
63 time (&start_time);
64 result = process_udp_request (server_address, server_port, server_send,
65 recv_buffer, MAX_INPUT_BUFFER - 1);
66 time (&end_time);
68 if (result != STATE_OK) {
69 printf ("No response from host on port %d\n", server_port);
70 result = STATE_CRITICAL;
71 }
73 else {
75 /* check to see if we got the response we wanted */
76 if (server_expect) {
77 if (!strstr (recv_buffer, server_expect)) {
78 printf ("Invalid response received from host on port %d\n",
79 server_port);
80 result = STATE_CRITICAL;
81 }
82 }
83 }
85 /* we connected, so close connection before exiting */
86 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;
94 printf (_("Connection %s on port %d - %d second response time\n"),
95 (result == STATE_OK) ? _("accepted") : _("problem"), server_port,
96 (int) (end_time - start_time));
97 }
99 /* reset the alarm */
100 alarm (0);
102 return result;
103 }
107 /* process command-line arguments */
108 int
109 process_arguments (int argc, char **argv)
110 {
111 int c;
113 int option = 0;
114 static struct option longopts[] = {
115 {"hostname", required_argument, 0, 'H'},
116 {"critical", required_argument, 0, 'c'},
117 {"warning", required_argument, 0, 'w'},
118 {"timeout", required_argument, 0, 't'},
119 {"port", required_argument, 0, 'p'},
120 {"expect", required_argument, 0, 'e'},
121 {"send", required_argument, 0, 's'},
122 {"verbose", no_argument, 0, 'v'},
123 {"version", no_argument, 0, 'V'},
124 {"help", no_argument, 0, 'h'},
125 {0, 0, 0, 0}
126 };
128 if (argc < 2)
129 usage ("\n");
131 for (c = 1; c < argc; c++) {
132 if (strcmp ("-to", argv[c]) == 0)
133 strcpy (argv[c], "-t");
134 else if (strcmp ("-wt", argv[c]) == 0)
135 strcpy (argv[c], "-w");
136 else if (strcmp ("-ct", argv[c]) == 0)
137 strcpy (argv[c], "-c");
138 }
140 while (1) {
141 c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", longopts, &option);
143 if (c == -1 || c == EOF || c == 1)
144 break;
146 switch (c) {
147 case '?': /* print short usage statement if args not parsable */
148 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
149 print_usage ();
150 exit (STATE_UNKNOWN);
151 case 'h': /* help */
152 print_help ();
153 exit (STATE_OK);
154 case 'V': /* version */
155 print_revision (progname, revision);
156 exit (STATE_OK);
157 case 'v': /* verbose mode */
158 verbose = TRUE;
159 break;
160 case 'H': /* hostname */
161 if (is_host (optarg) == FALSE)
162 usage2 (_("Invalid hostname/address"), optarg);
163 server_address = optarg;
164 break;
165 case 'c': /* critical */
166 if (!is_intnonneg (optarg))
167 usage (_("Critical threshold must be a positive integer\n"));
168 else
169 critical_time = atoi (optarg);
170 check_critical_time = TRUE;
171 break;
172 case 'w': /* warning */
173 if (!is_intnonneg (optarg))
174 usage (_("Warning threshold must be a positive integer\n"));
175 else
176 warning_time = atoi (optarg);
177 check_warning_time = TRUE;
178 break;
179 case 't': /* timeout */
180 if (!is_intnonneg (optarg))
181 usage2 (_("Timeout interval must be a positive integer"), optarg);
182 else
183 socket_timeout = atoi (optarg);
184 break;
185 case 'p': /* port */
186 if (!is_intnonneg (optarg))
187 usage (_("Port must be a positive integer\n"));
188 else
189 server_port = atoi (optarg);
190 break;
191 case 'e': /* expect */
192 server_expect = optarg;
193 break;
194 case 's': /* send */
195 server_send = optarg;
196 break;
197 }
198 }
200 c = optind;
201 if (server_address == NULL && c < argc && argv[c]) {
202 if (is_host (argv[c]) == FALSE)
203 usage2 (_("Invalid hostname/address"), optarg);
204 server_address = argv[c++];
205 }
207 if (server_address == NULL)
208 usage (_("Host name was not supplied\n"));
210 if (server_send == NULL)
211 server_send = strdup("");
213 return c;
214 }
218 void
219 print_help (void)
220 {
221 print_revision (progname, revision);
223 printf ("Copyright (c) 1999 Ethan Galstad\n");
224 printf (COPYRIGHT, copyright, email);
226 printf (_("\
227 This plugin tests an UDP connection with the specified host.\n\n"));
229 print_usage ();
231 printf (_(UT_HELP_VRSN));
233 printf (_(UT_HOST_PORT), 'p', "none");
235 printf (_("\
236 -e, --expect=STRING <optional>\n\
237 String to expect in first line of server response\n\
238 -s, --send=STRING <optional>\n\
239 String to send to the server when initiating the connection\n"));
241 printf (_(UT_WARN_CRIT));
243 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
245 printf (_(UT_VERBOSE));
247 printf (_("\
248 This plugin will attempt to connect to the specified port on the host.\n\
249 Successful connects return STATE_OK, refusals and timeouts return\n\
250 STATE_CRITICAL, other errors return STATE_UNKNOWN.\n\n"));
252 printf(_(UT_SUPPORT));
253 }
258 /* Original Command line:
259 check_udp <host_address> [-p port] [-s send] [-e expect] \
260 [-wt warn_time] [-ct crit_time] [-to to_sec] */
261 void
262 print_usage (void)
263 {
264 printf (_("\
265 Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n\
266 [-e expect] [-s send] [-t to_sec] [-v]\n"), progname);
267 printf (_(UT_HLP_VRS), progname, progname);
268 }