ea91f417a25223dde3738763211fa8e78e9bdda6
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-2004";
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 = STATE_UNKNOWN;
48 char recv_buffer[MAX_INPUT_BUFFER];
50 setlocale (LC_ALL, "");
51 bindtextdomain (PACKAGE, LOCALEDIR);
52 textdomain (PACKAGE);
54 if (process_arguments (argc, argv) != TRUE)
55 usage4 (_("Could not parse arguments"));
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);
67 time (&end_time);
69 if (result != STATE_OK) {
70 printf ("No response from host on port %d\n", server_port);
71 result = STATE_CRITICAL;
72 }
74 else {
76 /* check to see if we got the response we wanted */
77 if (server_expect) {
78 if (!strstr (recv_buffer, server_expect)) {
79 printf ("Invalid response received from host on port %d\n",
80 server_port);
81 result = STATE_CRITICAL;
82 }
83 }
84 }
86 /* we connected, so close connection before exiting */
87 if (result == STATE_OK) {
89 if (check_critical_time == TRUE
90 && (end_time - start_time) > critical_time) result = STATE_CRITICAL;
91 else if (check_warning_time == TRUE
92 && (end_time - start_time) > warning_time) result =
93 STATE_WARNING;
95 printf (_("Connection %s on port %d - %d second response time\n"),
96 (result == STATE_OK) ? _("accepted") : _("problem"), server_port,
97 (int) (end_time - start_time));
98 }
100 /* reset the alarm */
101 alarm (0);
103 return result;
104 }
108 /* process command-line arguments */
109 int
110 process_arguments (int argc, char **argv)
111 {
112 int c;
114 int option = 0;
115 static struct option longopts[] = {
116 {"hostname", required_argument, 0, 'H'},
117 {"critical", required_argument, 0, 'c'},
118 {"warning", required_argument, 0, 'w'},
119 {"timeout", required_argument, 0, 't'},
120 {"port", required_argument, 0, 'p'},
121 {"expect", required_argument, 0, 'e'},
122 {"send", required_argument, 0, 's'},
123 {"verbose", no_argument, 0, 'v'},
124 {"version", no_argument, 0, 'V'},
125 {"help", no_argument, 0, 'h'},
126 {0, 0, 0, 0}
127 };
129 if (argc < 2)
130 usage ("\n");
132 for (c = 1; c < argc; c++) {
133 if (strcmp ("-to", argv[c]) == 0)
134 strcpy (argv[c], "-t");
135 else if (strcmp ("-wt", argv[c]) == 0)
136 strcpy (argv[c], "-w");
137 else if (strcmp ("-ct", argv[c]) == 0)
138 strcpy (argv[c], "-c");
139 }
141 while (1) {
142 c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", longopts, &option);
144 if (c == -1 || c == EOF || c == 1)
145 break;
147 switch (c) {
148 case '?': /* print short usage statement if args not parsable */
149 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
150 print_usage ();
151 exit (STATE_UNKNOWN);
152 case 'h': /* help */
153 print_help ();
154 exit (STATE_OK);
155 case 'V': /* version */
156 print_revision (progname, revision);
157 exit (STATE_OK);
158 case 'v': /* verbose mode */
159 verbose = TRUE;
160 break;
161 case 'H': /* hostname */
162 if (is_host (optarg) == FALSE)
163 usage2 (_("Invalid hostname/address"), optarg);
164 server_address = optarg;
165 break;
166 case 'c': /* critical */
167 if (!is_intnonneg (optarg))
168 usage (_("Critical threshold must be a positive integer\n"));
169 else
170 critical_time = atoi (optarg);
171 check_critical_time = TRUE;
172 break;
173 case 'w': /* warning */
174 if (!is_intnonneg (optarg))
175 usage (_("Warning threshold must be a positive integer\n"));
176 else
177 warning_time = atoi (optarg);
178 check_warning_time = TRUE;
179 break;
180 case 't': /* timeout */
181 if (!is_intnonneg (optarg))
182 usage2 (_("Timeout interval must be a positive integer"), optarg);
183 else
184 socket_timeout = atoi (optarg);
185 break;
186 case 'p': /* port */
187 if (!is_intnonneg (optarg))
188 usage (_("Port must be a positive integer\n"));
189 else
190 server_port = atoi (optarg);
191 break;
192 case 'e': /* expect */
193 server_expect = optarg;
194 break;
195 case 's': /* send */
196 server_send = optarg;
197 break;
198 }
199 }
201 c = optind;
202 if (server_address == NULL && c < argc && argv[c]) {
203 if (is_host (argv[c]) == FALSE)
204 usage2 (_("Invalid hostname/address"), optarg);
205 server_address = argv[c++];
206 }
208 if (server_address == NULL)
209 usage4 (_("Hostname was not supplied"));
211 if (server_send == NULL)
212 server_send = strdup("");
214 return c;
215 }
219 void
220 print_help (void)
221 {
222 print_revision (progname, revision);
224 printf ("Copyright (c) 1999 Ethan Galstad\n");
225 printf (COPYRIGHT, copyright, email);
227 printf (_("\
228 This plugin tests an UDP connection with the specified host.\n\n"));
230 print_usage ();
232 printf (_(UT_HELP_VRSN));
234 printf (_(UT_HOST_PORT), 'p', "none");
236 printf (_("\
237 -e, --expect=STRING <optional>\n\
238 String to expect in first line of server response\n\
239 -s, --send=STRING <optional>\n\
240 String to send to the server when initiating the connection\n"));
242 printf (_(UT_WARN_CRIT));
244 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
246 printf (_(UT_VERBOSE));
248 printf (_("\
249 This plugin will attempt to connect to the specified port on the host.\n\
250 Successful connects return STATE_OK, refusals and timeouts return\n\
251 STATE_CRITICAL, other errors return STATE_UNKNOWN.\n\n"));
253 printf(_(UT_SUPPORT));
254 }
259 /* Original Command line:
260 check_udp <host_address> [-p port] [-s send] [-e expect] \
261 [-wt warn_time] [-ct crit_time] [-to to_sec] */
262 void
263 print_usage (void)
264 {
265 printf ("\
266 Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n\
267 [-e expect] [-s send] [-t to_sec] [-v]\n", progname);
268 }