1
/******************************************************************************
5
* Program: UDP port plugin for Nagios
7
* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
9
* Last Modified: $Date: 2003/03/07 06:40:33 $
11
* Command line: CHECK_UDP <host_address> [-p port] [-s send] [-e expect] \
12
* [-wt warn_time] [-ct crit_time] [-to to_sec]
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
21
* License Information:
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.
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.
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.
37
*****************************************************************************/
44
const char *progname = "check_udp";
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);
57
char *server_address = NULL;
58
char *server_expect = NULL;
59
char *server_send = "";
62
main (int argc, char **argv)
65
char recv_buffer[MAX_INPUT_BUFFER];
67
if (process_arguments (argc, argv) == ERROR)
70
/* initialize alarm signal handling */
71
signal (SIGALRM, socket_timeout_alarm_handler);
73
/* set socket timeout */
74
alarm (socket_timeout);
78
process_udp_request (server_address, server_port, server_send,
79
recv_buffer, MAX_INPUT_BUFFER - 1);
82
if (result != STATE_OK) {
83
printf ("No response from host on port %d\n", server_port);
84
result = STATE_CRITICAL;
89
/* check to see if we got the response we wanted */
91
if (!strstr (recv_buffer, server_expect)) {
92
printf ("Invalid response received from host on port %d\n",
94
result = STATE_CRITICAL;
99
/* we connected, so close connection before exiting */
100
if (result == STATE_OK) {
102
if (check_critical_time == TRUE
103
&& (end_time - start_time) > critical_time) result = STATE_CRITICAL;
104
else if (check_warning_time == TRUE
105
&& (end_time - start_time) > warning_time) result =
108
printf ("Connection %s on port %d - %d second response time\n",
109
(result == STATE_OK) ? "accepted" : "problem", server_port,
110
(int) (end_time - start_time));
113
/* reset the alarm */
122
/* process command-line arguments */
124
process_arguments (int argc, char **argv)
129
int option_index = 0;
130
static struct option long_options[] = {
131
{"hostname", required_argument, 0, 'H'},
132
{"critical", required_argument, 0, 'c'},
133
{"warning", required_argument, 0, 'w'},
134
{"timeout", required_argument, 0, 't'},
135
{"port", required_argument, 0, 'p'},
136
{"expect", required_argument, 0, 'e'},
137
{"send", required_argument, 0, 's'},
138
{"verbose", no_argument, 0, 'v'},
139
{"version", no_argument, 0, 'V'},
140
{"help", no_argument, 0, 'h'},
148
for (c = 1; c < argc; c++) {
149
if (strcmp ("-to", argv[c]) == 0)
150
strcpy (argv[c], "-t");
151
else if (strcmp ("-wt", argv[c]) == 0)
152
strcpy (argv[c], "-w");
153
else if (strcmp ("-ct", argv[c]) == 0)
154
strcpy (argv[c], "-c");
159
c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, &option_index);
161
c = getopt (argc, argv, "+hVvH:e:s:c:w:t:p:");
164
if (c == -1 || c == EOF || c == 1)
168
case '?': /* print short usage statement if args not parsable */
169
printf ("%s: Unknown argument: %s\n\n", progname, optarg);
171
exit (STATE_UNKNOWN);
175
case 'V': /* version */
176
print_revision (progname, "$Revision: 1.4.2.1 $");
178
case 'v': /* verbose mode */
181
case 'H': /* hostname */
182
if (is_host (optarg) == FALSE)
183
usage ("Invalid host name/address\n");
184
server_address = optarg;
186
case 'c': /* critical */
187
if (!is_intnonneg (optarg))
188
usage ("Critical threshold must be a nonnegative integer\n");
189
critical_time = atoi (optarg);
190
check_critical_time = TRUE;
192
case 'w': /* warning */
193
if (!is_intnonneg (optarg))
194
usage ("Warning threshold must be a nonnegative integer\n");
195
warning_time = atoi (optarg);
196
check_warning_time = TRUE;
198
case 't': /* timeout */
199
if (!is_intnonneg (optarg))
200
usage ("Timeout interval must be a nonnegative integer\n");
201
socket_timeout = atoi (optarg);
204
if (!is_intnonneg (optarg))
205
usage ("Serevr port must be a nonnegative integer\n");
206
server_port = atoi (optarg);
208
case 'e': /* expect */
209
server_expect = optarg;
212
server_send = optarg;
218
if (server_address == NULL && c < argc && argv[c]) {
219
if (is_host (argv[c]) == FALSE)
220
usage ("Invalid host name/address\n");
221
server_address = argv[c++];
224
if (server_address == NULL)
225
usage ("Host name was not supplied\n");
238
("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n"
239
" [-e expect] [-s send] [-t to_sec] [-v]\n", progname);
249
print_revision (progname, "$Revision: 1.4.2.1 $");
251
("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"
252
"This plugin tests an UDP connection with the specified host.\n\n");
256
" -H, --hostname=ADDRESS\n"
257
" Host name argument for servers using host headers (use numeric\n"
258
" address if possible to bypass DNS lookup).\n"
259
" -p, --port=INTEGER\n"
261
" -e, --expect=STRING <optional>\n"
262
" String to expect in first line of server response\n"
263
" -s, --send=STRING <optional>\n"
264
" String to send to the server when initiating the connection\n"
265
" -w, --warning=INTEGER <optional>\n"
266
" Response time to result in warning status (seconds)\n"
267
" -c, --critical=INTEGER <optional>\n"
268
" Response time to result in critical status (seconds)\n"
269
" -t, --timeout=INTEGER <optional>\n"
270
" Seconds before connection times out (default: %d)\n"
271
" -v, --verbose <optional>\n"
272
" Show details for command-line debugging (do not use with nagios server)\n"
274
" Print detailed help screen and exit\n"
276
" Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT);