diff options
Diffstat (limited to 'plugins/check_udp.c')
-rw-r--r-- | plugins/check_udp.c | 276 |
1 files changed, 0 insertions, 276 deletions
diff --git a/plugins/check_udp.c b/plugins/check_udp.c deleted file mode 100644 index a712871..0000000 --- a/plugins/check_udp.c +++ /dev/null | |||
@@ -1,276 +0,0 @@ | |||
1 | /****************************************************************************** | ||
2 | * | ||
3 | * Nagios check_udp plugin | ||
4 | * | ||
5 | * License: GPL | ||
6 | * Copyright (c) 1999-2006 nagios-plugins team | ||
7 | * | ||
8 | * Last Modified: $Date$ | ||
9 | * | ||
10 | * Description: | ||
11 | * | ||
12 | * This file contains the check_udp plugin | ||
13 | * | ||
14 | * License Information: | ||
15 | * | ||
16 | * This program is free software; you can redistribute it and/or modify | ||
17 | * it under the terms of the GNU General Public License as published by | ||
18 | * the Free Software Foundation; either version 2 of the License, or | ||
19 | * (at your option) any later version. | ||
20 | * | ||
21 | * This program is distributed in the hope that it will be useful, | ||
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
24 | * GNU General Public License for more details. | ||
25 | * | ||
26 | * You should have received a copy of the GNU General Public License | ||
27 | * along with this program; if not, write to the Free Software | ||
28 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
29 | * | ||
30 | * $Id$ | ||
31 | * | ||
32 | *****************************************************************************/ | ||
33 | |||
34 | const char *progname = "check_udp"; | ||
35 | const char *revision = "$Revision$"; | ||
36 | const char *copyright = "1999-2006"; | ||
37 | const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
38 | |||
39 | #include "common.h" | ||
40 | #include "netutils.h" | ||
41 | #include "utils.h" | ||
42 | |||
43 | int process_arguments (int, char **); | ||
44 | void print_help (void); | ||
45 | void print_usage (void); | ||
46 | |||
47 | int warning_time = 0; | ||
48 | int check_warning_time = FALSE; | ||
49 | int critical_time = 0; | ||
50 | int check_critical_time = FALSE; | ||
51 | int verbose = FALSE; | ||
52 | int server_port = 0; | ||
53 | char *server_address = NULL; | ||
54 | char *server_expect = NULL; | ||
55 | char *server_send; | ||
56 | |||
57 | int | ||
58 | main (int argc, char **argv) | ||
59 | { | ||
60 | int result = STATE_UNKNOWN; | ||
61 | char recv_buffer[MAX_INPUT_BUFFER]; | ||
62 | |||
63 | setlocale (LC_ALL, ""); | ||
64 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
65 | textdomain (PACKAGE); | ||
66 | |||
67 | if (process_arguments (argc, argv) == ERROR) | ||
68 | usage4 (_("Could not parse arguments")); | ||
69 | |||
70 | /* initialize alarm signal handling */ | ||
71 | signal (SIGALRM, socket_timeout_alarm_handler); | ||
72 | |||
73 | /* set socket timeout */ | ||
74 | alarm (socket_timeout); | ||
75 | |||
76 | time (&start_time); | ||
77 | result = process_udp_request (server_address, server_port, server_send, | ||
78 | recv_buffer, MAX_INPUT_BUFFER - 1); | ||
79 | |||
80 | time (&end_time); | ||
81 | |||
82 | if (result != STATE_OK) { | ||
83 | printf (_("No response from host on port %d\n"), server_port); | ||
84 | result = STATE_CRITICAL; | ||
85 | } | ||
86 | |||
87 | else { | ||
88 | |||
89 | /* check to see if we got the response we wanted */ | ||
90 | if (server_expect) { | ||
91 | if (!strstr (recv_buffer, server_expect)) { | ||
92 | printf (_("Invalid response received from host on port %d\n"), | ||
93 | server_port); | ||
94 | result = STATE_CRITICAL; | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | |||
99 | /* we connected, so close connection before exiting */ | ||
100 | if (result == STATE_OK) { | ||
101 | |||
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 = | ||
106 | STATE_WARNING; | ||
107 | |||
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)); | ||
111 | } | ||
112 | |||
113 | /* reset the alarm */ | ||
114 | alarm (0); | ||
115 | |||
116 | return result; | ||
117 | } | ||
118 | |||
119 | |||
120 | |||
121 | /* process command-line arguments */ | ||
122 | int | ||
123 | process_arguments (int argc, char **argv) | ||
124 | { | ||
125 | int c; | ||
126 | |||
127 | int option = 0; | ||
128 | static struct option longopts[] = { | ||
129 | {"hostname", required_argument, 0, 'H'}, | ||
130 | {"critical", required_argument, 0, 'c'}, | ||
131 | {"warning", required_argument, 0, 'w'}, | ||
132 | {"timeout", required_argument, 0, 't'}, | ||
133 | {"port", required_argument, 0, 'p'}, | ||
134 | {"expect", required_argument, 0, 'e'}, | ||
135 | {"send", required_argument, 0, 's'}, | ||
136 | {"verbose", no_argument, 0, 'v'}, | ||
137 | {"version", no_argument, 0, 'V'}, | ||
138 | {"help", no_argument, 0, 'h'}, | ||
139 | {0, 0, 0, 0} | ||
140 | }; | ||
141 | |||
142 | if (argc < 2) | ||
143 | usage ("\n"); | ||
144 | |||
145 | for (c = 1; c < argc; c++) { | ||
146 | if (strcmp ("-to", argv[c]) == 0) | ||
147 | strcpy (argv[c], "-t"); | ||
148 | else if (strcmp ("-wt", argv[c]) == 0) | ||
149 | strcpy (argv[c], "-w"); | ||
150 | else if (strcmp ("-ct", argv[c]) == 0) | ||
151 | strcpy (argv[c], "-c"); | ||
152 | } | ||
153 | |||
154 | while (1) { | ||
155 | c = getopt_long (argc, argv, "+hVvH:e:s:c:w:t:p:", longopts, &option); | ||
156 | |||
157 | if (c == -1 || c == EOF || c == 1) | ||
158 | break; | ||
159 | |||
160 | switch (c) { | ||
161 | case '?': /* print short usage statement if args not parsable */ | ||
162 | usage2 (_("Unknown argument"), optarg); | ||
163 | case 'h': /* help */ | ||
164 | print_help (); | ||
165 | exit (STATE_OK); | ||
166 | case 'V': /* version */ | ||
167 | print_revision (progname, revision); | ||
168 | exit (STATE_OK); | ||
169 | case 'v': /* verbose mode */ | ||
170 | verbose = TRUE; | ||
171 | break; | ||
172 | case 'H': /* hostname */ | ||
173 | if (is_host (optarg) == FALSE) | ||
174 | usage2 (_("Invalid hostname/address"), optarg); | ||
175 | server_address = optarg; | ||
176 | break; | ||
177 | case 'c': /* critical */ | ||
178 | if (!is_intnonneg (optarg)) | ||
179 | usage4 (_("Critical threshold must be a positive integer")); | ||
180 | else | ||
181 | critical_time = atoi (optarg); | ||
182 | check_critical_time = TRUE; | ||
183 | break; | ||
184 | case 'w': /* warning */ | ||
185 | if (!is_intnonneg (optarg)) | ||
186 | usage4 (_("Warning threshold must be a positive integer")); | ||
187 | else | ||
188 | warning_time = atoi (optarg); | ||
189 | check_warning_time = TRUE; | ||
190 | break; | ||
191 | case 't': /* timeout */ | ||
192 | if (!is_intnonneg (optarg)) | ||
193 | usage2 (_("Timeout interval must be a positive integer"), optarg); | ||
194 | else | ||
195 | socket_timeout = atoi (optarg); | ||
196 | break; | ||
197 | case 'p': /* port */ | ||
198 | if (!is_intnonneg (optarg)) | ||
199 | usage4 (_("Port must be a positive integer")); | ||
200 | else | ||
201 | server_port = atoi (optarg); | ||
202 | break; | ||
203 | case 'e': /* expect */ | ||
204 | server_expect = optarg; | ||
205 | break; | ||
206 | case 's': /* send */ | ||
207 | server_send = optarg; | ||
208 | break; | ||
209 | } | ||
210 | } | ||
211 | |||
212 | c = optind; | ||
213 | if (server_address == NULL && c < argc && argv[c]) { | ||
214 | if (is_host (argv[c]) == FALSE) | ||
215 | usage2 (_("Invalid hostname/address"), optarg); | ||
216 | server_address = argv[c++]; | ||
217 | } | ||
218 | |||
219 | if (server_address == NULL) | ||
220 | usage4 (_("Hostname was not supplied")); | ||
221 | |||
222 | if (server_send == NULL) | ||
223 | server_send = strdup(""); | ||
224 | |||
225 | return c; | ||
226 | } | ||
227 | |||
228 | |||
229 | |||
230 | void | ||
231 | print_help (void) | ||
232 | { | ||
233 | print_revision (progname, revision); | ||
234 | |||
235 | printf ("Copyright (c) 1999 Ethan Galstad\n"); | ||
236 | printf (COPYRIGHT, copyright, email); | ||
237 | |||
238 | printf ("%s\n", _("This plugin tests an UDP connection with the specified host.")); | ||
239 | |||
240 | printf ("\n\n"); | ||
241 | |||
242 | print_usage (); | ||
243 | |||
244 | printf (_(UT_HELP_VRSN)); | ||
245 | |||
246 | printf (_(UT_HOST_PORT), 'p', "none"); | ||
247 | |||
248 | printf (" %s\n", "-e, --expect=STRING <optional>"); | ||
249 | printf (" %s\n", _("String to expect in first line of server response")); | ||
250 | printf (" %s\n", "-s, --send=STRING <optional>"); | ||
251 | printf (" %s\n", _("String to send to the server when initiating the connection")); | ||
252 | |||
253 | printf (_(UT_WARN_CRIT)); | ||
254 | |||
255 | printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); | ||
256 | |||
257 | printf (_(UT_VERBOSE)); | ||
258 | |||
259 | printf ("%s\n", _("This plugin will attempt to connect to the specified port on the host.")); | ||
260 | printf (" %s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); | ||
261 | printf (" %s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN.")); | ||
262 | |||
263 | printf(_(UT_SUPPORT)); | ||
264 | } | ||
265 | |||
266 | |||
267 | /* Original Command line: | ||
268 | check_udp <host_address> [-p port] [-s send] [-e expect] \ | ||
269 | [-wt warn_time] [-ct crit_time] [-to to_sec] */ | ||
270 | void | ||
271 | print_usage (void) | ||
272 | { | ||
273 | printf (_("Usage:")); | ||
274 | printf ("%s -H <host_address> [-p port] [-w warn_time] [-c crit_time]\n", progname); | ||
275 | printf (" [-e expect] [-s send] [-t to_sec] [-v]\n"); | ||
276 | } | ||