Page 1 of 1

[Nagios-devel] Patch to properly poll() on the command pipe

Posted: Sun Jan 13, 2008 10:20 pm
by Guest
This is a multi-part message in MIME format.
--------------060102020105030403080307
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Ethan, hi list,

There's one thing that trickle me with nagios since I came across that
code... In nagios v2, base/utils.c, there's the
service_result_worker_thread function that uses poll calls to read from
an ipc pipe, and there's the command_file_worker_thread (still present
in Nagios v3) that seems to be a cut and paste of the previous function
with polling replaced by a sleep timer because poll didn't work.

The reason poll didn't work is simply because to poll a named pipe, you
must open it RDWR, so this patch takes back the code from nagios v2
service_result_worker_thread to use polling on the command pipe.

I did it just for fun, but I'm wondering if you'd be interested in using
is for Nagios v3 (this is against CVS HEAD btw). It worked well on my
Ubuntu box with the following tests:

a. Running Nagios with 1500 passive checks accross 100 hosts getting
their result from NSCA every minute at the exact same time (1)

b. Same as [a] while trying to keep the pipe full by cat'ing a file
containing millions of check results for the same host/service (2)

(1) A perl script (attached too) forking 100 childs syncronized with a
semaphore. every 60 seconds each child opens a pipe to send_nsca and
prints 15 service results. nsca was configured not to aggregate writes.

(2) Running the following just before the semaphore fires off the childs
(results.txt is big enough so that the command finishes after nagios is
done processing the 1500 passive results):
$ `cat results.txt >>var/rw/nagios.cmd`

I guess this patch would help keeping the command pipe empty on huge
distributed monitoring systems. As I said it was for fun, do whatever
you want with it ;)


Thomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHiv8u6dZ+Kt5BchYRAkCGAKCA1muzYCQo31cA1ZI/RPQQyQBidwCgv6K6
2Ft31txzgmay0kCnF/EQ1so=
=d5/z
-----END PGP SIGNATURE-----

--------------060102020105030403080307
Content-Type: text/x-diff;
name="use_poll_on_cmd_pipe.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="use_poll_on_cmd_pipe.patch"

? base/perlxsi.c
Index: base/utils.c
===================================================================
RCS file: /cvsroot/nagios/nagios/base/utils.c,v
retrieving revision 1.216
diff -u -r1.216 utils.c
--- base/utils.c 13 Dec 2007 17:14:28 -0000 1.216
+++ base/utils.c 14 Jan 2008 05:15:46 -0000
@@ -2813,7 +2813,7 @@
}

/* open the command file for reading (non-blocked) - O_TRUNC flag cannot be used due to errors on some systems */
- if((command_file_fd=open(command_file,O_RDONLY | O_NONBLOCK)) %s\n",errno,strerror(errno));

@@ -3718,6 +3718,8 @@
/* worker thread - artificially increases buffer of named pipe */
void * command_file_worker_thread(void *arg){
char input_buffer[MAX_EXTERNAL_COMMAND_LENGTH];
+ struct pollfd pfd;
+ int pollval;
struct timeval tv;
int buffer_items=0;
int result=0;
@@ -3734,14 +3736,45 @@
/* should we shutdown? */
pthread_testcancel();

- /**** POLL() AND SELECT() DON'T SEEM TO WORK ****/
- /* wait a bit */
- tv.tv_sec=0;
- tv.tv_usec=500000;
- select(0,NULL,NULL,NULL,&tv);
-
- /* should we shutdown? */
- pthread_testcancel();
+ /* wait for data to arrive */
+ /* select seems to not work, so we have to use poll instead */
+ pfd.fd=command_file_fd;
+ pfd.events=POLLIN;
+ pollval=poll(&pfd,1,500);
+
+ /* loop if no data */
+ if(pollval==0)
+ continue;
+
+ /* check for errors */
+ if(pollval==-1){
+
+ switch(errno){
+ case EBADF:
+ write_to_log("command_file_worker_thread(): poll(): EBADF",logging_options,NULL);
+ break;
+ case ENOMEM:
+ write_to_log("command_file_worker_thre

...[email truncated]...


This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]