command pipe concatenated messages

Jim Mozley jim.mozley at exponential-e.com
Mon Dec 29 15:32:04 CET 2003


Jim Mozley wrote:

> I have been seeing a problem with submitting passive checks using 
> version 1.0 on Solaris 8. I had some discusions on this originally on 
> the nagios-users list, asked some questions on comp.lang.misc.perl and 
> finally ended up discussing this with Al Tobey as he I had some 
> questions on how he implemented command submission via in his perl module.
> 
> The issue I see is with multiple passive checks becoming concatenated, 
> so I end up with events like this:
> 
> My service is Up Some description[1065056739]
> PROCESS_SERVICE_CHECK_RESULT:my-host:my-service:0:Some description.
> 
> Where another passive check becomes part of the description field of the 
> previous check.
> 

The sample code below illustrates the problem. I have tried variations 
of print, syswrite and using select to avoid I/O buffering. If the sleep 
statement is commented out I see the concatenated commands. My checking 
interval is set as command_check_interval=1s, I haven't tried setting 
this to -1 yet!


#!/usr/local/bin/perl

# pass a nagios status code number as an argument

use strict;
use warnings;

# Modules
use Carp;
use Fcntl qw| :flock |;

# file private lexicals
my $DEBUG = 1;
my $host = "slugs";
my $service = "testing";
my $status = $ARGV[0];

die "No status number\n" unless defined $status; # status can 
legitimately be 0
print "Status code: $status\n" if $DEBUG;
my $epoch = time;

for ( 1 .. 10 ) {
     $status = 0 if $status > 2;
     my $cmd = "[$epoch] 
PROCESS_SERVICE_CHECK_RESULT;$host;$service;$status;Test $_ - status 
$status";
     print "$cmd\n" if $DEBUG;
     nagios_cmd( $cmd );
     $status++;
}

sub nagios_cmd {
     my $cmd = shift;
     my $pipe = '/usr/local/nagios/var/rw/nagios.cmd';

     croak "$pipe does not exist!" unless ( -e $pipe );
     croak "$pipe is not a pipe!"  unless ( -p $pipe );

     open(PIPE, "> $pipe") or croak "Cannot open pipe $!";
     flock(PIPE, LOCK_EX);
     #select((select(PIPE), $| = 1)[0]);
     #print PIPE $cmd;
     syswrite PIPE, "$cmd", length $cmd;
     # wait before releasing lock so we don't overlap cmds to Nagios
     sleep 2;
     close PIPE or croak "Cannot close pipe $!"; # also releases lock
}


-------------------------------------------------------
This SF.net email is sponsored by: IBM Linux Tutorials.
Become an expert in LINUX or just sharpen your skills.  Sign up for IBM's
Free Linux Tutorials.  Learn everything from the bash shell to sys admin.
Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click




More information about the Developers mailing list