diff options
author | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 (GMT) |
---|---|---|
committer | Ethan Galstad <egalstad@users.sourceforge.net> | 2002-02-28 06:42:51 (GMT) |
commit | 44a321cb8a42d6c0ea2d96a1086a17f2134c89cc (patch) | |
tree | a1a4d9f7b92412a17ab08f34f04eec45433048b7 /plugins/check_udp.c | |
parent | 54fd5d7022ff2d6a59bc52b8869182f3fc77a058 (diff) | |
download | monitoring-plugins-44a321cb8a42d6c0ea2d96a1086a17f2134c89cc.tar.gz |
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_udp.c')
-rw-r--r-- | plugins/check_udp.c | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/plugins/check_udp.c b/plugins/check_udp.c new file mode 100644 index 0000000..d00ce9c --- /dev/null +++ b/plugins/check_udp.c | |||
@@ -0,0 +1,315 @@ | |||
1 | /****************************************************************************** | ||
2 | * | ||
3 | * CHECK_UDP.C | ||
4 | * | ||
5 | * Program: UDP port plugin for Nagios | ||
6 | * License: GPL | ||
7 | * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org) | ||
8 | * | ||
9 | * Last Modified: $Date$ | ||
10 | * | ||
11 | * Command line: CHECK_UDP <host_address> [-p port] [-s send] [-e expect] \ | ||
12 | * [-wt warn_time] [-ct crit_time] [-to to_sec] | ||
13 | * | ||
14 | * Description: | ||
15 | * | ||
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 | ||
19 | * STATE_UNKNOWN. | ||
20 | * | ||
21 | * License Information: | ||
22 | * | ||
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. | ||
27 | * | ||
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. | ||
32 | * | ||
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. | ||
36 | * | ||
37 | *****************************************************************************/ | ||
38 | |||
39 | #include "config.h" | ||
40 | #include "common.h" | ||
41 | #include "netutils.h" | ||
42 | #include "utils.h" | ||
43 | |||
44 | #define PROGNAME "check_udp" | ||
45 | |||
46 | int warning_time = 0; | ||
47 | int check_warning_time = FALSE; | ||
48 | int critical_time = 0; | ||
49 | int check_critical_time = FALSE; | ||
50 | |||
51 | int process_arguments (int, char **); | ||
52 | int call_getopt (int, char **); | ||
53 | void print_usage (void); | ||
54 | void print_help (void); | ||
55 | |||
56 | int verbose = FALSE; | ||
57 | int server_port = 0; | ||
58 | char *server_address = NULL; | ||
59 | char *server_expect = NULL; | ||
60 | char *server_send = NULL; | ||
61 | |||
62 | int | ||
63 | main (int argc, char **argv) | ||
64 | { | ||
65 | int result; | ||
66 | char recv_buffer[MAX_INPUT_BUFFER]; | ||
67 | |||
68 | if (process_arguments (argc, argv) == ERROR) | ||
69 | usage ("\n"); | ||
70 | |||
71 | /* initialize alarm signal handling */ | ||
72 | signal (SIGALRM, socket_timeout_alarm_handler); | ||
73 | |||
74 | /* set socket timeout */ | ||
75 | alarm (socket_timeout); | ||
76 | |||
77 | time (&start_time); | ||
78 | result = | ||
79 | process_udp_request (server_address, server_port, server_send, | ||
80 | recv_buffer, MAX_INPUT_BUFFER - 1); | ||
81 | time (&end_time); | ||
82 | |||
83 | if (result != STATE_OK) { | ||
84 | printf ("No response from host on port %d\n", server_port); | ||
85 | result = STATE_CRITICAL; | ||
86 | } | ||
87 | |||
88 | else { | ||
89 | |||
90 | /* check to see if we got the response we wanted */ | ||
91 | if (server_expect) { | ||
92 | if (!strstr (recv_buffer, server_expect)) { | ||
93 | printf ("Invalid response received from host on port %d\n", | ||
94 | server_port); | ||
95 | result = STATE_CRITICAL; | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | |||
100 | /* we connected, so close connection before exiting */ | ||
101 | if (result == STATE_OK) { | ||
102 | |||
103 | if (check_critical_time == TRUE | ||
104 | && (end_time - start_time) > critical_time) result = STATE_CRITICAL; | ||
105 | else if (check_warning_time == TRUE | ||
106 | && (end_time - start_time) > warning_time) result = | ||
107 | STATE_WARNING; | ||
108 | |||
109 | printf ("Connection %s on port %d - %d second response time\n", | ||
110 | (result == STATE_OK) ? "accepted" : "problem", server_port, | ||
111 | (int) (end_time - start_time)); | ||
112 | } | ||
113 | |||
114 | /* reset the alarm */ | ||
115 | alarm (0); | ||
116 | |||
117 | return result; | ||
118 | } | ||
119 | |||
120 | |||
121 | |||
122 | |||
123 | /* process command-line arguments */ | ||
124 | int | ||
125 | process_arguments (int argc, char **argv) | ||
126 | { | ||
127 | int c; | ||
128 | |||
129 | if (argc < 2) | ||
130 | usage ("\n"); | ||
131 | |||
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 | } | ||
140 | |||
141 | c = 0; | ||
142 | while ((c += call_getopt (argc - c, &argv[c])) < argc) { | ||
143 | |||
144 | if (is_option (argv[c])) | ||
145 | continue; | ||
146 | |||
147 | if (server_address == NULL) { | ||
148 | if (argc > c) { | ||
149 | if (is_host (argv[c]) == FALSE) | ||
150 | usage ("Invalid host name/address\n"); | ||
151 | server_address = argv[c]; | ||
152 | } | ||
153 | else { | ||
154 | usage ("Host name was not supplied\n"); | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | |||
159 | if (server_send == NULL) | ||
160 | server_send = strscpy (server_send, ""); | ||
161 | |||
162 | return OK; | ||
163 | } | ||
164 | |||
165 | |||
166 | |||
167 | |||
168 | |||
169 | int | ||
170 | call_getopt (int argc, char **argv) | ||
171 | { | ||
172 | int c, i = 0; | ||
173 | |||
174 | #ifdef HAVE_GETOPT_H | ||
175 | int option_index = 0; | ||
176 | static struct option long_options[] = { | ||
177 | {"hostname", required_argument, 0, 'H'}, | ||
178 | {"critical", required_argument, 0, 'c'}, | ||
179 | {"warning", required_argument, 0, 'w'}, | ||
180 | {"timeout", required_argument, 0, 't'}, | ||
181 | {"port", required_argument, 0, 'p'}, | ||
182 | {"expect", required_argument, 0, 'e'}, | ||
183 | {"send", required_argument, 0, 's'}, | ||
184 | {"verbose", no_argument, 0, 'v'}, | ||
185 | {"version", no_argument, 0, 'V'}, | ||
186 | {"help", no_argument, 0, 'h'}, | ||
187 | {0, 0, 0, 0} | ||
188 | }; | ||
189 | #endif | ||
190 | |||
191 | while (1) { | ||
192 | #ifdef HAVE_GETOPT_H | ||
193 | c = | ||
194 | getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", long_options, | ||
195 | &option_index); | ||
196 | #else | ||
197 | c = getopt (argc, argv, "+hVvH:e:s:c:w:t:p:"); | ||
198 | #endif | ||
199 | |||
200 | i++; | ||
201 | |||
202 | if (c == -1 || c == EOF || c == 1) | ||
203 | break; | ||
204 | |||
205 | switch (c) { | ||
206 | case 'H': | ||
207 | case 'c': | ||
208 | case 'w': | ||
209 | case 't': | ||
210 | case 'p': | ||
211 | case 'e': | ||
212 | case 's': | ||
213 | i++; | ||
214 | } | ||
215 | |||
216 | switch (c) { | ||
217 | case '?': /* print short usage statement if args not parsable */ | ||
218 | printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); | ||
219 | print_usage (); | ||
220 | exit (STATE_UNKNOWN); | ||
221 | case 'h': /* help */ | ||
222 | print_help (); | ||
223 | exit (STATE_OK); | ||
224 | case 'V': /* version */ | ||
225 | print_revision (my_basename (argv[0]), "$Revision$"); | ||
226 | exit (STATE_OK); | ||
227 | case 'v': /* verbose mode */ | ||
228 | verbose = TRUE; | ||
229 | break; | ||
230 | case 'H': /* hostname */ | ||
231 | if (is_host (optarg) == FALSE) | ||
232 | usage ("Invalid host name/address\n"); | ||
233 | server_address = optarg; | ||
234 | break; | ||
235 | case 'c': /* critical */ | ||
236 | if (!is_intnonneg (optarg)) | ||
237 | usage ("Critical threshold must be a nonnegative integer\n"); | ||
238 | critical_time = atoi (optarg); | ||
239 | check_critical_time = TRUE; | ||
240 | break; | ||
241 | case 'w': /* warning */ | ||
242 | if (!is_intnonneg (optarg)) | ||
243 | usage ("Warning threshold must be a nonnegative integer\n"); | ||
244 | warning_time = atoi (optarg); | ||
245 | check_warning_time = TRUE; | ||
246 | break; | ||
247 | case 't': /* timeout */ | ||
248 | if (!is_intnonneg (optarg)) | ||
249 | usage ("Timeout interval must be a nonnegative integer\n"); | ||
250 | socket_timeout = atoi (optarg); | ||
251 | break; | ||
252 | case 'p': /* port */ | ||
253 | if (!is_intnonneg (optarg)) | ||
254 | usage ("Serevr port must be a nonnegative integer\n"); | ||
255 | server_port = atoi (optarg); | ||
256 | break; | ||
257 | case 'e': /* expect */ | ||
258 | server_expect = optarg; | ||
259 | break; | ||
260 | case 's': /* send */ | ||
261 | server_send = optarg; | ||
262 | break; | ||
263 | } | ||
264 | } | ||
265 | return i; | ||
266 | } | ||
267 | |||
268 | |||
269 | |||
270 | |||
271 | |||
272 | void | ||
273 | print_usage (void) | ||
274 | { | ||
275 | printf | ||
276 | ("Usage: %s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n" | ||
277 | " [-e expect] [-s send] [-t to_sec] [-v]\n", PROGNAME); | ||
278 | } | ||
279 | |||
280 | |||
281 | |||
282 | |||
283 | |||
284 | void | ||
285 | print_help (void) | ||
286 | { | ||
287 | print_revision (PROGNAME, "$Revision$"); | ||
288 | printf | ||
289 | ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n" | ||
290 | "This plugin tests an UDP connection with the specified host.\n\n"); | ||
291 | print_usage (); | ||
292 | printf | ||
293 | ("Options:\n" | ||
294 | " -H, --hostname=ADDRESS\n" | ||
295 | " Host name argument for servers using host headers (use numeric\n" | ||
296 | " address if possible to bypass DNS lookup).\n" | ||
297 | " -p, --port=INTEGER\n" | ||
298 | " Port number\n" | ||
299 | " -e, --expect=STRING <optional>\n" | ||
300 | " String to expect in first line of server response\n" | ||
301 | " -s, --send=STRING <optional>\n" | ||
302 | " String to send to the server when initiating the connection\n" | ||
303 | " -w, --warning=INTEGER <optional>\n" | ||
304 | " Response time to result in warning status (seconds)\n" | ||
305 | " -c, --critical=INTEGER <optional>\n" | ||
306 | " Response time to result in critical status (seconds)\n" | ||
307 | " -t, --timeout=INTEGER <optional>\n" | ||
308 | " Seconds before connection times out (default: %d)\n" | ||
309 | " -v, --verbose <optional>\n" | ||
310 | " Show details for command-line debugging (do not use with nagios server)\n" | ||
311 | " -h, --help\n" | ||
312 | " Print detailed help screen and exit\n" | ||
313 | " -V, --version\n" | ||
314 | " Print version information and exit\n", DEFAULT_SOCKET_TIMEOUT); | ||
315 | } | ||