ode style and some more debug

This commit is contained in:
Isaac Connor 2018-09-28 11:50:34 -04:00
parent 7768b4eeef
commit d45406bce7
3 changed files with 97 additions and 101 deletions

View File

@ -247,8 +247,9 @@ sub zmMemInit {
sub zmMemVerify {
my $monitor = shift;
if ( !zmMemAttach($monitor, $mem_size) ) {
return( undef );
return undef;
}
my $sd_size = zmMemRead($monitor, 'shared_data:size', 1);
@ -269,7 +270,7 @@ sub zmMemVerify {
.", got ".$sd_size
);
}
return( undef );
return undef;
}
my $td_size = zmMemRead($monitor, 'trigger_data:size', 1);
if ( $td_size != $mem_data->{trigger_data}->{size} ) {
@ -290,14 +291,17 @@ sub zmMemVerify {
.$td_size
);
}
return( undef );
return undef;
}
if ( !zmMemRead($monitor, 'shared_data:valid',1) ) {
my $valid = zmMemRead($monitor, 'shared_data:valid',1);
if ( !$valid ) {
Error("Shared data not valid for monitor $$monitor{Id}");
return( undef );
return undef;
} else {
Debug("Shared data appears vaild for monitor $$monitor{Id}: $valid");
}
return( !undef );
return !undef;
}
sub zmMemRead {

View File

@ -75,49 +75,44 @@ sub zmMemKey {
sub zmMemAttach {
my ( $monitor, $size ) = @_;
if ( !$size ) {
Error( "No size passed to zmMemAttach for monitor $$monitor{Id}\n" );
Error("No size passed to zmMemAttach for monitor $$monitor{Id}");
return undef;
}
if ( !defined($monitor->{MMapAddr}) ) {
if ( defined($monitor->{MMapAddr}) ) {
Debug("zmMemAttach already attached at $monitor->{MMapAddr}");
return !undef;
}
my $mmap_file = $Config{ZM_PATH_MAP}.'/zm.mmap.'.$monitor->{Id};
if ( ! -e $mmap_file ) {
Error( sprintf( "Memory map file '%s' does not exist. zmc might not be running."
, $mmap_file
)
);
Error("Memory map file '$mmap_file' does not exist. zmc might not be running.");
return undef;
}
my $mmap_file_size = -s $mmap_file;
if ( $mmap_file_size < $size ) {
Error( sprintf( "Memory map file '%s' should have been %d but was instead %d"
, $mmap_file
, $size
, $mmap_file_size
)
);
Error("Memory map file '$mmap_file' should have been $size but was instead $mmap_file_size");
return undef;
}
my $MMAP;
if ( !open($MMAP, '+<', $mmap_file) ) {
Error( sprintf( "Can't open memory map file '%s': $!", $mmap_file ) );
Error("Can't open memory map file '$mmap_file': $!");
return undef;
}
my $mmap = undef;
my $mmap_addr = mmap($mmap, $size, PROT_READ|PROT_WRITE, MAP_SHARED, $MMAP);
if ( !$mmap_addr || !$mmap ) {
Error( sprintf( "Can't mmap to file '%s': $!\n", $mmap_file ) );
Error("Can't mmap to file '$mmap_file': $!");
close($MMAP);
return undef;
}
$monitor->{MMapHandle} = $MMAP;
$monitor->{MMapAddr} = $mmap_addr;
$monitor->{MMap} = \$mmap;
}
return !undef;
}
} # end sub zmMemAttach
sub zmMemDetach {
my $monitor = shift;
@ -144,10 +139,7 @@ sub zmMemGet {
my $mmap = $monitor->{MMap};
if ( !$mmap || !$$mmap ) {
Error( sprintf( "Can't read from mapped memory for monitor '%d', gone away?"
, $monitor->{Id}
)
);
Error("Can't read from mapped memory for monitor '$$monitor{Id}', gone away?");
return undef;
}
my $data = substr($$mmap, $offset, $size);
@ -162,22 +154,19 @@ sub zmMemPut {
my $mmap = $monitor->{MMap};
if ( !$mmap || !$$mmap ) {
Error( sprintf( "Can't write mapped memory for monitor '%d', gone away?"
, $monitor->{Id}
)
);
return( undef );
Error("Can't write mapped memory for monitor '$$monitor{Id}', gone away?");
return undef;
}
substr($$mmap, $offset, $size) = $data;
return( !undef );
return !undef;
}
sub zmMemClean {
Debug( "Removing memory map files\n" );
Debug("Removing memory map files");
my $mapPath = $Config{ZM_PATH_MAP}.'/zm.mmap.*';
foreach my $mapFile( glob( $mapPath ) ) {
( $mapFile ) = $mapFile =~ /^(.+)$/;
Debug( "Removing memory map file '$mapFile'\n" );
Debug("Removing memory map file '$mapFile'");
unlink($mapFile);
}
}

View File

@ -305,11 +305,11 @@ while( 1 ) {
# zmDbConnect will ping and reconnect if neccessary
$dbh = zmDbConnect();
} # end while ( 1 )
Info( "Trigger daemon exiting\n" );
Info("Trigger daemon exiting");
exit;
sub loadMonitors {
Debug( "Loading monitors\n" );
Debug("Loading monitors");
$monitor_reload_time = time();
my %new_monitors = ();
@ -324,6 +324,7 @@ sub loadMonitors {
or Fatal( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() ) {
# Check shared memory ok
if ( 0 ) {
if ( !zmMemVerify( $monitor ) ) {
zmMemInvalidate( $monitor );
next;
@ -331,6 +332,7 @@ sub loadMonitors {
$monitor->{LastState} = zmGetMonitorState($monitor);
$monitor->{LastEvent} = zmGetLastEvent($monitor);
}
$new_monitors{$monitor->{Id}} = $monitor;
} # end while fetchrow
%monitors = %new_monitors;
@ -348,14 +350,14 @@ sub handleMessage {
my $monitor = $monitors{$id};
if ( !$monitor ) {
Warning( "Can't find monitor '$id' for message '$message'\n" );
Warning("Can't find monitor '$id' for message '$message'");
return;
}
Debug( "Found monitor for id '$id'\n" );
Debug("Found monitor for id '$id'");
next if ( !zmMemVerify($monitor) );
Debug( "Handling action '$action'\n" );
Debug("Handling action '$action'");
if ( $action =~ /^(enable|disable)(?:\+(\d+))?$/ ) {
my $state = $1;
my $delay = $2;
@ -366,7 +368,7 @@ sub handleMessage {
}
# Force a reload
$monitor_reload_time = 0;
Info( "Set monitor to $state\n" );
Info("Set monitor to $state");
if ( $delay ) {
my $action_text = $id.'|'.( ($state eq 'enable')
? 'disable'
@ -383,7 +385,7 @@ sub handleMessage {
if ( $trigger eq 'on' ) {
zmTriggerEventOn($monitor, $score, $cause, $text);
zmTriggerShowtext($monitor, $showtext) if defined($showtext);
Info( "Trigger '$trigger' '$cause'\n" );
Info("Trigger '$trigger' '$cause'");
if ( $delay ) {
my $action_text = $id.'|cancel';
handleDelay($delay, $connection, $action_text);
@ -396,7 +398,7 @@ sub handleMessage {
my $last_event = zmGetLastEvent($monitor);
zmTriggerEventOff($monitor);
zmTriggerShowtext($monitor, $showtext) if defined($showtext);
Info( "Trigger '$trigger'\n" );
Info("Trigger '$trigger'");
# Wait til it's finished
while( zmInAlarm($monitor)
&& ($last_event == zmGetLastEvent($monitor))
@ -410,12 +412,12 @@ sub handleMessage {
} elsif( $action eq 'cancel' ) {
zmTriggerEventCancel($monitor);
zmTriggerShowtext($monitor, $showtext) if defined($showtext);
Info( "Cancelled event\n" );
Info("Cancelled event");
} elsif( $action eq 'show' ) {
zmTriggerShowtext( $monitor, $showtext );
Info( "Updated show text to '$showtext'\n" );
Info("Updated show text to '$showtext'");
} else {
Error( "Unrecognised action '$action' in message '$message'\n" );
Error("Unrecognised action '$action' in message '$message'");
}
} # end sub handleMessage
@ -430,8 +432,9 @@ sub handleDelay {
$action_array = $actions{$action_time} = [];
}
push( @$action_array, { connection=>$connection, message=>$action_text } );
Debug( "Added timed event '$action_text', expires at $action_time (+$delay secs)\n" );
Debug("Added timed event '$action_text', expires at $action_time (+$delay secs)");
}
1;
__END__