diff options
author | Karl DeBisschop <kdebisschop@users.sourceforge.net> | 2004-02-29 04:09:34 +0000 |
---|---|---|
committer | Karl DeBisschop <kdebisschop@users.sourceforge.net> | 2004-02-29 04:09:34 +0000 |
commit | dc8f5c0f658b5e060fc17a4beb4a0a2b195ba470 (patch) | |
tree | d8239d5c49ddce04fbc661046e2c5670b65feb60 /plugins/netutils.c | |
parent | 5316da0b8d35823ce52bed41487292bcfff1a0b2 (diff) | |
download | monitoring-plugins-dc8f5c0f658b5e060fc17a4beb4a0a2b195ba470.tar.gz |
was making up to 34 separate tcp connections - now we open one and reuse
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@831 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/netutils.c')
-rw-r--r-- | plugins/netutils.c | 103 |
1 files changed, 64 insertions, 39 deletions
diff --git a/plugins/netutils.c b/plugins/netutils.c index 55f02cad..5017eb9e 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c | |||
@@ -168,11 +168,7 @@ process_request (const char *server_address, int server_port, int proto, | |||
168 | const char *send_buffer, char *recv_buffer, int recv_size) | 168 | const char *send_buffer, char *recv_buffer, int recv_size) |
169 | { | 169 | { |
170 | int result; | 170 | int result; |
171 | int send_result; | ||
172 | int recv_result; | ||
173 | int sd; | 171 | int sd; |
174 | struct timeval tv; | ||
175 | fd_set readfds; | ||
176 | 172 | ||
177 | result = STATE_OK; | 173 | result = STATE_OK; |
178 | 174 | ||
@@ -180,41 +176,7 @@ process_request (const char *server_address, int server_port, int proto, | |||
180 | if (result != STATE_OK) | 176 | if (result != STATE_OK) |
181 | return STATE_CRITICAL; | 177 | return STATE_CRITICAL; |
182 | 178 | ||
183 | send_result = send (sd, send_buffer, strlen (send_buffer), 0); | 179 | result = send_request (sd, proto, send_buffer, recv_buffer, recv_size); |
184 | if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) { | ||
185 | printf ("send() failed\n"); | ||
186 | result = STATE_WARNING; | ||
187 | } | ||
188 | |||
189 | /* wait up to the number of seconds for socket timeout minus one | ||
190 | for data from the host */ | ||
191 | tv.tv_sec = socket_timeout - 1; | ||
192 | tv.tv_usec = 0; | ||
193 | FD_ZERO (&readfds); | ||
194 | FD_SET (sd, &readfds); | ||
195 | select (sd + 1, &readfds, NULL, NULL, &tv); | ||
196 | |||
197 | /* make sure some data has arrived */ | ||
198 | if (!FD_ISSET (sd, &readfds)) { | ||
199 | strcpy (recv_buffer, ""); | ||
200 | printf ("No data was received from host!\n"); | ||
201 | result = STATE_WARNING; | ||
202 | } | ||
203 | |||
204 | else { | ||
205 | recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0); | ||
206 | if (recv_result == -1) { | ||
207 | strcpy (recv_buffer, ""); | ||
208 | if (proto != IPPROTO_TCP) | ||
209 | printf ("recv() failed\n"); | ||
210 | result = STATE_WARNING; | ||
211 | } | ||
212 | else | ||
213 | recv_buffer[recv_result] = 0; | ||
214 | |||
215 | /* die returned string */ | ||
216 | recv_buffer[recv_size - 1] = 0; | ||
217 | } | ||
218 | 180 | ||
219 | close (sd); | 181 | close (sd); |
220 | 182 | ||
@@ -315,6 +277,69 @@ my_connect (const char *host_name, int port, int *sd, int proto) | |||
315 | } | 277 | } |
316 | } | 278 | } |
317 | 279 | ||
280 | |||
281 | int | ||
282 | send_tcp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size) | ||
283 | { | ||
284 | return send_request (sd, IPPROTO_TCP, send_buffer, recv_buffer, recv_size); | ||
285 | } | ||
286 | |||
287 | |||
288 | int | ||
289 | send_udp_request (int sd, const char *send_buffer, char *recv_buffer, int recv_size) | ||
290 | { | ||
291 | return send_request (sd, IPPROTO_UDP, send_buffer, recv_buffer, recv_size); | ||
292 | } | ||
293 | |||
294 | |||
295 | int | ||
296 | send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size) | ||
297 | { | ||
298 | int result; | ||
299 | int send_result; | ||
300 | int recv_result; | ||
301 | struct timeval tv; | ||
302 | fd_set readfds; | ||
303 | |||
304 | send_result = send (sd, send_buffer, strlen (send_buffer), 0); | ||
305 | if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) { | ||
306 | printf ("send() failed\n"); | ||
307 | result = STATE_WARNING; | ||
308 | } | ||
309 | |||
310 | /* wait up to the number of seconds for socket timeout minus one | ||
311 | for data from the host */ | ||
312 | tv.tv_sec = socket_timeout - 1; | ||
313 | tv.tv_usec = 0; | ||
314 | FD_ZERO (&readfds); | ||
315 | FD_SET (sd, &readfds); | ||
316 | select (sd + 1, &readfds, NULL, NULL, &tv); | ||
317 | |||
318 | /* make sure some data has arrived */ | ||
319 | if (!FD_ISSET (sd, &readfds)) { | ||
320 | strcpy (recv_buffer, ""); | ||
321 | printf ("No data was received from host!\n"); | ||
322 | result = STATE_WARNING; | ||
323 | } | ||
324 | |||
325 | else { | ||
326 | recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0); | ||
327 | if (recv_result == -1) { | ||
328 | strcpy (recv_buffer, ""); | ||
329 | if (proto != IPPROTO_TCP) | ||
330 | printf ("recv() failed\n"); | ||
331 | result = STATE_WARNING; | ||
332 | } | ||
333 | else | ||
334 | recv_buffer[recv_result] = 0; | ||
335 | |||
336 | /* die returned string */ | ||
337 | recv_buffer[recv_size - 1] = 0; | ||
338 | } | ||
339 | return result; | ||
340 | } | ||
341 | |||
342 | |||
318 | int | 343 | int |
319 | is_host (const char *address) | 344 | is_host (const char *address) |
320 | { | 345 | { |