|
|
|
@ -206,31 +206,26 @@ my $win = $rin;
|
|
|
|
|
my $ein = $win;
|
|
|
|
|
my $timeout = SELECT_TIMEOUT;
|
|
|
|
|
my %actions;
|
|
|
|
|
while( 1 )
|
|
|
|
|
{
|
|
|
|
|
while( 1 ) {
|
|
|
|
|
$rin = $base_rin;
|
|
|
|
|
# Add the file descriptors of any spawned connections
|
|
|
|
|
foreach my $fileno ( keys(%spawned_connections) )
|
|
|
|
|
{
|
|
|
|
|
foreach my $fileno ( keys(%spawned_connections) ) {
|
|
|
|
|
vec( $rin, $fileno, 1 ) = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $nfound = select( my $rout = $rin, undef, my $eout = $ein, $timeout );
|
|
|
|
|
if ( $nfound > 0 )
|
|
|
|
|
{
|
|
|
|
|
if ( $nfound > 0 ) {
|
|
|
|
|
Debug( "Got input from $nfound connections\n" );
|
|
|
|
|
foreach my $connection ( @in_select_connections ) {
|
|
|
|
|
|
|
|
|
|
if ( vec( $rout, $connection->fileno(), 1 ) )
|
|
|
|
|
{
|
|
|
|
|
if ( vec( $rout, $connection->fileno(), 1 ) ) {
|
|
|
|
|
Debug( "Got input from connection "
|
|
|
|
|
.$connection->name()
|
|
|
|
|
." ("
|
|
|
|
|
.$connection->fileno()
|
|
|
|
|
.")\n"
|
|
|
|
|
);
|
|
|
|
|
if ( $connection->spawns() )
|
|
|
|
|
{
|
|
|
|
|
if ( $connection->spawns() ) {
|
|
|
|
|
my $new_connection = $connection->accept();
|
|
|
|
|
$spawned_connections{$new_connection->fileno()} = $new_connection;
|
|
|
|
|
Debug( "Added new spawned connection ("
|
|
|
|
@ -239,24 +234,19 @@ while( 1 )
|
|
|
|
|
.int(keys(%spawned_connections))
|
|
|
|
|
." spawned connections\n"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
my $messages = $connection->getMessages();
|
|
|
|
|
if ( defined($messages) )
|
|
|
|
|
{
|
|
|
|
|
foreach my $message ( @$messages )
|
|
|
|
|
{
|
|
|
|
|
if ( defined($messages) ) {
|
|
|
|
|
foreach my $message ( @$messages ) {
|
|
|
|
|
handleMessage( $connection, $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach my $connection ( values(%spawned_connections) )
|
|
|
|
|
{
|
|
|
|
|
if ( vec( $rout, $connection->fileno(), 1 ) )
|
|
|
|
|
{
|
|
|
|
|
} # end foreach connection
|
|
|
|
|
|
|
|
|
|
foreach my $connection ( values(%spawned_connections) ) {
|
|
|
|
|
if ( vec( $rout, $connection->fileno(), 1 ) ) {
|
|
|
|
|
Debug( "Got input from spawned connection "
|
|
|
|
|
.$connection->name()
|
|
|
|
|
." ("
|
|
|
|
@ -264,15 +254,11 @@ while( 1 )
|
|
|
|
|
.")\n"
|
|
|
|
|
);
|
|
|
|
|
my $messages = $connection->getMessages();
|
|
|
|
|
if ( defined($messages) )
|
|
|
|
|
{
|
|
|
|
|
foreach my $message ( @$messages )
|
|
|
|
|
{
|
|
|
|
|
if ( defined($messages) ) {
|
|
|
|
|
foreach my $message ( @$messages ) {
|
|
|
|
|
handleMessage( $connection, $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
delete( $spawned_connections{$connection->fileno()} );
|
|
|
|
|
Debug( "Removed spawned connection ("
|
|
|
|
|
.$connection->fileno()
|
|
|
|
@ -283,28 +269,20 @@ while( 1 )
|
|
|
|
|
$connection->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elsif ( $nfound < 0 )
|
|
|
|
|
{
|
|
|
|
|
if ( $! == EINTR )
|
|
|
|
|
{
|
|
|
|
|
} # end foreach connection
|
|
|
|
|
} elsif ( $nfound < 0 ) {
|
|
|
|
|
if ( $! == EINTR ) {
|
|
|
|
|
# Do nothing
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
Fatal( "Can't select: $!" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} # end if select returned activitiy
|
|
|
|
|
|
|
|
|
|
# Check polled connections
|
|
|
|
|
foreach my $connection ( @in_poll_connections )
|
|
|
|
|
{
|
|
|
|
|
foreach my $connection ( @in_poll_connections ) {
|
|
|
|
|
my $messages = $connection->getMessages();
|
|
|
|
|
if ( defined($messages) )
|
|
|
|
|
{
|
|
|
|
|
foreach my $message ( @$messages )
|
|
|
|
|
{
|
|
|
|
|
if ( defined($messages) ) {
|
|
|
|
|
foreach my $message ( @$messages ) {
|
|
|
|
|
handleMessage( $connection, $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -314,13 +292,12 @@ while( 1 )
|
|
|
|
|
my @out_messages;
|
|
|
|
|
foreach my $monitor ( values(%monitors) ) {
|
|
|
|
|
|
|
|
|
|
my $memVerified = 1;
|
|
|
|
|
if ( !zmMemRead($monitor, "shared_data:valid") ) {
|
|
|
|
|
zmMemInvalidate($monitor);
|
|
|
|
|
$memVerified = zmMemVerify($monitor);
|
|
|
|
|
if ( ! zmMemVerify($monitor) ) {
|
|
|
|
|
# Our attempt to verify the memory handle failed. We should reload the monitors.
|
|
|
|
|
$needsReload = 1;
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($memVerified) {
|
|
|
|
|
my ( $state, $last_event )
|
|
|
|
|
= zmMemRead( $monitor,
|
|
|
|
|
[ "shared_data:state",
|
|
|
|
@ -357,22 +334,15 @@ while( 1 )
|
|
|
|
|
}
|
|
|
|
|
$monitor->{LastState} = $state;
|
|
|
|
|
$monitor->{LastEvent} = $last_event;
|
|
|
|
|
} else { # Our attempt to verify the memory handle failed. We should reload the monitors.
|
|
|
|
|
$needsReload = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} # end foreach monitor
|
|
|
|
|
|
|
|
|
|
foreach my $connection ( @out_connections )
|
|
|
|
|
{
|
|
|
|
|
if ( $connection->canWrite() )
|
|
|
|
|
{
|
|
|
|
|
foreach my $connection ( @out_connections ) {
|
|
|
|
|
if ( $connection->canWrite() ) {
|
|
|
|
|
$connection->putMessages( \@out_messages );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach my $connection ( values(%spawned_connections) )
|
|
|
|
|
{
|
|
|
|
|
if ( $connection->canWrite() )
|
|
|
|
|
{
|
|
|
|
|
foreach my $connection ( values(%spawned_connections) ) {
|
|
|
|
|
if ( $connection->canWrite() ) {
|
|
|
|
|
$connection->putMessages( \@out_messages );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -380,11 +350,9 @@ while( 1 )
|
|
|
|
|
Debug( "Checking for timed actions\n" )
|
|
|
|
|
if ( int(keys(%actions)) );
|
|
|
|
|
my $now = time();
|
|
|
|
|
foreach my $action_time ( sort( grep { $_ < $now } keys( %actions ) ) )
|
|
|
|
|
{
|
|
|
|
|
foreach my $action_time ( sort( grep { $_ < $now } keys( %actions ) ) ) {
|
|
|
|
|
Info( "Found actions expiring at $action_time\n" );
|
|
|
|
|
foreach my $action ( @{$actions{$action_time}} )
|
|
|
|
|
{
|
|
|
|
|
foreach my $action ( @{$actions{$action_time}} ) {
|
|
|
|
|
my $connection = $action->{connection};
|
|
|
|
|
my $message = $action->{message};
|
|
|
|
|
Info( "Found action '$message'\n" );
|
|
|
|
@ -394,34 +362,26 @@ while( 1 )
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Allow connections to do their own timed actions
|
|
|
|
|
foreach my $connection ( @connections )
|
|
|
|
|
{
|
|
|
|
|
foreach my $connection ( @connections ) {
|
|
|
|
|
my $messages = $connection->timedActions();
|
|
|
|
|
if ( defined($messages) )
|
|
|
|
|
{
|
|
|
|
|
foreach my $message ( @$messages )
|
|
|
|
|
{
|
|
|
|
|
if ( defined($messages) ) {
|
|
|
|
|
foreach my $message ( @$messages ) {
|
|
|
|
|
handleMessage( $connection, $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach my $connection ( values(%spawned_connections) )
|
|
|
|
|
{
|
|
|
|
|
foreach my $connection ( values(%spawned_connections) ) {
|
|
|
|
|
my $messages = $connection->timedActions();
|
|
|
|
|
if ( defined($messages) )
|
|
|
|
|
{
|
|
|
|
|
foreach my $message ( @$messages )
|
|
|
|
|
{
|
|
|
|
|
if ( defined($messages) ) {
|
|
|
|
|
foreach my $message ( @$messages ) {
|
|
|
|
|
handleMessage( $connection, $message );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# If necessary reload monitors
|
|
|
|
|
if ( $needsReload || ((time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL ))
|
|
|
|
|
{
|
|
|
|
|
foreach my $monitor ( values(%monitors) )
|
|
|
|
|
{
|
|
|
|
|
if ( $needsReload || ((time() - $monitor_reload_time) > MONITOR_RELOAD_INTERVAL )) {
|
|
|
|
|
foreach my $monitor ( values(%monitors) ) {
|
|
|
|
|
# Free up any used memory handle
|
|
|
|
|
zmMemInvalidate( $monitor );
|
|
|
|
|
}
|
|
|
|
@ -432,8 +392,7 @@ while( 1 )
|
|
|
|
|
Info( "Trigger daemon exiting\n" );
|
|
|
|
|
exit;
|
|
|
|
|
|
|
|
|
|
sub loadMonitors
|
|
|
|
|
{
|
|
|
|
|
sub loadMonitors {
|
|
|
|
|
Debug( "Loading monitors\n" );
|
|
|
|
|
$monitor_reload_time = time();
|
|
|
|
|
|
|
|
|
@ -447,33 +406,29 @@ sub loadMonitors
|
|
|
|
|
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
|
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
|
|
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
|
|
|
|
while( my $monitor = $sth->fetchrow_hashref() )
|
|
|
|
|
{
|
|
|
|
|
next if ( !zmMemVerify( $monitor ) ); # Check shared memory ok
|
|
|
|
|
|
|
|
|
|
if ( defined($monitors{$monitor->{Id}}->{LastState}) )
|
|
|
|
|
{
|
|
|
|
|
$monitor->{LastState} = $monitors{$monitor->{Id}}->{LastState};
|
|
|
|
|
while( my $monitor = $sth->fetchrow_hashref() ) {
|
|
|
|
|
# Check shared memory ok
|
|
|
|
|
if ( !zmMemVerify( $monitor ) ) {
|
|
|
|
|
zmMemInvalidate( $monitor );
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if ( defined($monitors{$monitor->{Id}}->{LastState}) ) {
|
|
|
|
|
$monitor->{LastState} = $monitors{$monitor->{Id}}->{LastState};
|
|
|
|
|
} else {
|
|
|
|
|
$monitor->{LastState} = zmGetMonitorState( $monitor );
|
|
|
|
|
}
|
|
|
|
|
if ( defined($monitors{$monitor->{Id}}->{LastEvent}) )
|
|
|
|
|
{
|
|
|
|
|
if ( defined($monitors{$monitor->{Id}}->{LastEvent}) ) {
|
|
|
|
|
$monitor->{LastEvent} = $monitors{$monitor->{Id}}->{LastEvent};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
$monitor->{LastEvent} = zmGetLastEvent( $monitor );
|
|
|
|
|
}
|
|
|
|
|
$new_monitors{$monitor->{Id}} = $monitor;
|
|
|
|
|
}
|
|
|
|
|
} # end while fetchrow
|
|
|
|
|
%monitors = %new_monitors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub handleMessage
|
|
|
|
|
{
|
|
|
|
|
sub handleMessage {
|
|
|
|
|
my $connection = shift;
|
|
|
|
|
my $message = shift;
|
|
|
|
|
|
|
|
|
@ -484,8 +439,7 @@ sub handleMessage
|
|
|
|
|
$text = "" if ( !defined($text) );
|
|
|
|
|
|
|
|
|
|
my $monitor = $monitors{$id};
|
|
|
|
|
if ( !$monitor )
|
|
|
|
|
{
|
|
|
|
|
if ( !$monitor ) {
|
|
|
|
|
Warning( "Can't find monitor '$id' for message '$message'\n" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -494,52 +448,40 @@ sub handleMessage
|
|
|
|
|
next if ( !zmMemVerify( $monitor ) );
|
|
|
|
|
|
|
|
|
|
Debug( "Handling action '$action'\n" );
|
|
|
|
|
if ( $action =~ /^(enable|disable)(?:\+(\d+))?$/ )
|
|
|
|
|
{
|
|
|
|
|
if ( $action =~ /^(enable|disable)(?:\+(\d+))?$/ ) {
|
|
|
|
|
my $state = $1;
|
|
|
|
|
my $delay = $2;
|
|
|
|
|
if ( $state eq "enable" )
|
|
|
|
|
{
|
|
|
|
|
if ( $state eq "enable" ) {
|
|
|
|
|
zmMonitorEnable( $monitor );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
zmMonitorDisable( $monitor );
|
|
|
|
|
}
|
|
|
|
|
# Force a reload
|
|
|
|
|
$monitor_reload_time = 0;
|
|
|
|
|
Info( "Set monitor to $state\n" );
|
|
|
|
|
if ( $delay )
|
|
|
|
|
{
|
|
|
|
|
if ( $delay ) {
|
|
|
|
|
my $action_text = $id."|".( ($state eq "enable")
|
|
|
|
|
? "disable"
|
|
|
|
|
: "enable"
|
|
|
|
|
);
|
|
|
|
|
handleDelay($delay, $connection, $action_text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elsif ( $action =~ /^(on|off)(?:[ \+](\d+))?$/ )
|
|
|
|
|
{
|
|
|
|
|
} elsif ( $action =~ /^(on|off)(?:[ \+](\d+))?$/ ) {
|
|
|
|
|
next if ( !$monitor->{Enabled} );
|
|
|
|
|
|
|
|
|
|
my $trigger = $1;
|
|
|
|
|
my $delay = $2;
|
|
|
|
|
my $trigger_data;
|
|
|
|
|
if ( $trigger eq "on" )
|
|
|
|
|
{
|
|
|
|
|
if ( $trigger eq "on" ) {
|
|
|
|
|
zmTriggerEventOn( $monitor, $score, $cause, $text );
|
|
|
|
|
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
|
|
|
|
|
Info( "Trigger '$trigger' '$cause'\n" );
|
|
|
|
|
if ( $delay )
|
|
|
|
|
{
|
|
|
|
|
if ( $delay ) {
|
|
|
|
|
my $action_text = $id."|cancel";
|
|
|
|
|
handleDelay($delay, $connection, $action_text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elsif ( $trigger eq "off" )
|
|
|
|
|
{
|
|
|
|
|
if ( $delay )
|
|
|
|
|
{
|
|
|
|
|
} elsif ( $trigger eq "off" ) {
|
|
|
|
|
if ( $delay ) {
|
|
|
|
|
my $action_text = $id."|off|0|".$cause."|".$text;
|
|
|
|
|
handleDelay($delay, $connection, $action_text);
|
|
|
|
|
} else {
|
|
|
|
@ -548,44 +490,33 @@ sub handleMessage
|
|
|
|
|
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
|
|
|
|
|
Info( "Trigger '$trigger'\n" );
|
|
|
|
|
# Wait til it's finished
|
|
|
|
|
while( zmInAlarm( $monitor )
|
|
|
|
|
&& ($last_event == zmGetLastEvent( $monitor ))
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
while( zmInAlarm( $monitor ) && ($last_event == zmGetLastEvent( $monitor ))) {
|
|
|
|
|
# Tenth of a second
|
|
|
|
|
usleep( 100000 );
|
|
|
|
|
}
|
|
|
|
|
zmTriggerEventCancel( $monitor );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elsif( $action eq "cancel" )
|
|
|
|
|
{
|
|
|
|
|
} # end if trigger is on or off
|
|
|
|
|
} elsif( $action eq "cancel" ) {
|
|
|
|
|
zmTriggerEventCancel( $monitor );
|
|
|
|
|
zmTriggerShowtext( $monitor, $showtext ) if defined($showtext);
|
|
|
|
|
Info( "Cancelled event\n" );
|
|
|
|
|
}
|
|
|
|
|
elsif( $action eq "show" )
|
|
|
|
|
{
|
|
|
|
|
} elsif( $action eq "show" ) {
|
|
|
|
|
zmTriggerShowtext( $monitor, $showtext );
|
|
|
|
|
Info( "Updated show text to '$showtext'\n" );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
} else {
|
|
|
|
|
Error( "Unrecognised action '$action' in message '$message'\n" );
|
|
|
|
|
}
|
|
|
|
|
} # end sub handleMessage
|
|
|
|
|
|
|
|
|
|
sub handleDelay
|
|
|
|
|
{
|
|
|
|
|
sub handleDelay {
|
|
|
|
|
my $delay = shift;
|
|
|
|
|
my $connection = shift;
|
|
|
|
|
my $action_text = shift;
|
|
|
|
|
|
|
|
|
|
my $action_time = time()+$delay;
|
|
|
|
|
my $action_array = $actions{$action_time};
|
|
|
|
|
if ( !$action_array )
|
|
|
|
|
{
|
|
|
|
|
if ( !$action_array ) {
|
|
|
|
|
$action_array = $actions{$action_time} = [];
|
|
|
|
|
}
|
|
|
|
|
push( @$action_array, { connection=>$connection,
|
|
|
|
|