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 /* Original Command line:
29 check_udp <host_address> [-p port] [-s send] [-e expect] \
30 [-wt warn_time] [-ct crit_time] [-to to_sec] */
31 void
32 print_usage (void)
33 {
34 printf (_("\
35 Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n\
36 [-e expect] [-s send] [-t to_sec] [-v]\n"), progname);
37 printf (_(UT_HLP_VRS), progname, progname);
38 }
40 void
41 print_help (void)
42 {
43 print_revision (progname, revision);
45 printf (_("Copyright (c) 1999 Ethan Galstad\n"));
46 printf (_(COPYRIGHT), copyright, email);
48 printf (_("\
49 This plugin tests an UDP connection with the specified host.\n\n"));
51 print_usage ();
53 printf (_(UT_HELP_VRSN));
55 printf (_(UT_HOST_PORT), 'p', "none");
57 printf (_("\
58 -e, --expect=STRING <optional>\n\
59 String to expect in first line of server response\n\
60 -s, --send=STRING <optional>\n\
61 String to send to the server when initiating the connection\n"));
63 printf (_(UT_WARN_CRIT));
65 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
67 printf (_(UT_VERBOSE));
69 printf (_("\
70 This plugin will attempt to connect to the specified port on the host.\n\
71 Successful connects return STATE_OK, refusals and timeouts return\n\
72 STATE_CRITICAL, other errors return STATE_UNKNOWN.\n\n"));
74 printf(_(UT_SUPPORT));
75 }
76 \f
77 int process_arguments (int, char **);
79 int warning_time = 0;
80 int check_warning_time = FALSE;
81 int critical_time = 0;
82 int check_critical_time = FALSE;
83 int verbose = FALSE;
84 int server_port = 0;
85 char *server_address = NULL;
86 char *server_expect = NULL;
87 char *server_send = "";
89 int
90 main (int argc, char **argv)
91 {
92 int result;
93 char recv_buffer[MAX_INPUT_BUFFER];
95 if (process_arguments (argc, argv) == ERROR)
96 usage ("\n");
98 /* initialize alarm signal handling */
99 signal (SIGALRM, socket_timeout_alarm_handler);
101 /* set socket timeout */
102 alarm (socket_timeout);
104 time (&start_time);
105 result = process_udp_request (server_address, server_port, server_send,
106 recv_buffer, MAX_INPUT_BUFFER - 1);
107 time (&end_time);
109 if (result != STATE_OK) {
110 printf ("No response from host on port %d\n", server_port);
111 result = STATE_CRITICAL;
112 }
114 else {
116 /* check to see if we got the response we wanted */
117 if (server_expect) {
118 if (!strstr (recv_buffer, server_expect)) {
119 printf ("Invalid response received from host on port %d\n",
120 server_port);
121 result = STATE_CRITICAL;
122 }
123 }
124 }
126 /* we connected, so close connection before exiting */
127 if (result == STATE_OK) {
129 if (check_critical_time == TRUE
130 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
131 else if (check_warning_time == TRUE
132 && (end_time - start_time) > warning_time) result =
133 STATE_WARNING;
135 printf (_("Connection %s on port %d - %d second response time\n"),
136 (result == STATE_OK) ? _("accepted") : _("problem"), server_port,
137 (int) (end_time - start_time));
138 }
140 /* reset the alarm */
141 alarm (0);
143 return result;
144 }
149 /* process command-line arguments */
150 int
151 process_arguments (int argc, char **argv)
152 {
153 int c;
155 int option_index = 0;
156 static struct option long_options[] = {
157 {"hostname", required_argument, 0, 'H'},
158 {"critical", required_argument, 0, 'c'},
159 {"warning", required_argument, 0, 'w'},
160 {"timeout", required_argument, 0, 't'},
161 {"port", required_argument, 0, 'p'},
162 {"expect", required_argument, 0, 'e'},
163 {"send", required_argument, 0, 's'},
164 {"verbose", no_argument, 0, 'v'},
165 {"version", no_argument, 0, 'V'},
166 {"help", no_argument, 0, 'h'},
167 {0, 0, 0, 0}
168 };
170 if (argc < 2)
171 usage ("\n");
173 for (c = 1; c < argc; c++) {
174 if (strcmp ("-to", argv[c]) == 0)
175 strcpy (argv[c], "-t");
176 else if (strcmp ("-wt", argv[c]) == 0)
177 strcpy (argv[c], "-w");
178 else if (strcmp ("-ct", argv[c]) == 0)
179 strcpy (argv[c], "-c");
180 }
182 while (1) {
183 c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
185 if (c == -1 || c == EOF || c == 1)
186 break;
188 switch (c) {
189 case '?': /* print short usage statement if args not parsable */
190 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
191 print_usage ();
192 exit (STATE_UNKNOWN);
193 case 'h': /* help */
194 print_help ();
195 exit (STATE_OK);
196 case 'V': /* version */
197 print_revision (progname, revision);
198 exit (STATE_OK);
199 case 'v': /* verbose mode */
200 verbose = TRUE;
201 break;
202 case 'H': /* hostname */
203 if (is_host (optarg) == FALSE)
204 usage (_("Invalid host name/address\n"));
205 server_address = optarg;
206 break;
207 case 'c': /* critical */
208 if (!is_intnonneg (optarg))
209 usage (_("Critical threshold must be a nonnegative integer\n"));
210 critical_time = atoi (optarg);
211 check_critical_time = TRUE;
212 break;
213 case 'w': /* warning */
214 if (!is_intnonneg (optarg))
215 usage (_("Warning threshold must be a nonnegative integer\n"));
216 warning_time = atoi (optarg);
217 check_warning_time = TRUE;
218 break;
219 case 't': /* timeout */
220 if (!is_intnonneg (optarg))
221 usage (_("Timeout interval must be a nonnegative integer\n"));
222 socket_timeout = atoi (optarg);
223 break;
224 case 'p': /* port */
225 if (!is_intnonneg (optarg))
226 usage (_("Server port must be a nonnegative integer\n"));
227 server_port = atoi (optarg);
228 break;
229 case 'e': /* expect */
230 server_expect = optarg;
231 break;
232 case 's': /* send */
233 server_send = optarg;
234 break;
235 }
236 }
238 c = optind;
239 if (server_address == NULL && c < argc && argv[c]) {
240 if (is_host (argv[c]) == FALSE)
241 usage (_("Invalid host name/address\n"));
242 server_address = argv[c++];
243 }
245 if (server_address == NULL)
246 usage (_("Host name was not supplied\n"));
248 return c;
249 }