fixup spacing/braces

This commit is contained in:
Isaac Connor 2017-05-17 11:52:25 -04:00
parent a933d4ae12
commit 6ffe8018ef
1 changed files with 410 additions and 484 deletions

View File

@ -106,8 +106,7 @@ if ( $version ) {
print( ZoneMinder::Base::ZM_VERSION . "\n"); print( ZoneMinder::Base::ZM_VERSION . "\n");
exit(0); exit(0);
} }
if ( ($report + $interactive + $continuous) > 1 ) if ( ($report + $interactive + $continuous) > 1 ) {
{
print( STDERR "Error, only one option may be specified\n" ); print( STDERR "Error, only one option may be specified\n" );
pod2usage(-exitstatus => -1); pod2usage(-exitstatus => -1);
} }
@ -162,32 +161,27 @@ MAIN: while( $loop ) {
$cleaned = 0; $cleaned = 0;
my $res = $monitorSelectSth->execute() my $res = $monitorSelectSth->execute()
or Fatal( "Can't execute: ".$monitorSelectSth->errstr() ); or Fatal( "Can't execute: ".$monitorSelectSth->errstr() );
while( my $monitor = $monitorSelectSth->fetchrow_hashref() ) while( my $monitor = $monitorSelectSth->fetchrow_hashref() ) {
{
Debug( "Found database monitor '$monitor->{Id}'" ); Debug( "Found database monitor '$monitor->{Id}'" );
my $db_events = $db_monitors->{$monitor->{Id}} = {}; my $db_events = $db_monitors->{$monitor->{Id}} = {};
my $res = $eventSelectSth->execute( $monitor->{Id} ) my $res = $eventSelectSth->execute( $monitor->{Id} )
or Fatal( "Can't execute: ".$eventSelectSth->errstr() ); or Fatal( "Can't execute: ".$eventSelectSth->errstr() );
while ( my $event = $eventSelectSth->fetchrow_hashref() ) while ( my $event = $eventSelectSth->fetchrow_hashref() ) {
{
$db_events->{$event->{Id}} = $event->{Age}; $db_events->{$event->{Id}} = $event->{Age};
} }
Debug( "Got ".int(keys(%$db_events))." events\n" ); Debug( "Got ".int(keys(%$db_events))." events\n" );
} }
my $fs_monitors; my $fs_monitors;
foreach my $monitor ( glob("[0-9]*") ) foreach my $monitor ( glob("[0-9]*") ) {
{
# Thie glob above gives all files starting with a digit. So a monitor with a name starting with a digit will be in this list. # Thie glob above gives all files starting with a digit. So a monitor with a name starting with a digit will be in this list.
next if $monitor =~ /\D/; next if $monitor =~ /\D/;
Debug( "Found filesystem monitor '$monitor'" ); Debug( "Found filesystem monitor '$monitor'" );
my $fs_events = $fs_monitors->{$monitor} = {}; my $fs_events = $fs_monitors->{$monitor} = {};
( my $monitor_dir ) = ( $monitor =~ /^(.*)$/ ); # De-taint ( my $monitor_dir ) = ( $monitor =~ /^(.*)$/ ); # De-taint
if ( $Config{ZM_USE_DEEP_STORAGE} ) if ( $Config{ZM_USE_DEEP_STORAGE} ) {
{ foreach my $day_dir ( glob("$monitor_dir/*/*/*") ) {
foreach my $day_dir ( glob("$monitor_dir/*/*/*") )
{
Debug( "Checking $day_dir" ); Debug( "Checking $day_dir" );
( $day_dir ) = ( $day_dir =~ /^(.*)$/ ); # De-taint ( $day_dir ) = ( $day_dir =~ /^(.*)$/ ); # De-taint
chdir( $day_dir ); chdir( $day_dir );
@ -196,86 +190,63 @@ MAIN: while( $loop ) {
my @event_links = sort { $b <=> $a } grep { -l $_ } readdir( DIR ); my @event_links = sort { $b <=> $a } grep { -l $_ } readdir( DIR );
closedir( DIR ); closedir( DIR );
my $count = 0; my $count = 0;
foreach my $event_link ( @event_links ) foreach my $event_link ( @event_links ) {
{
Debug( "Checking link $event_link" ); Debug( "Checking link $event_link" );
( my $event = $event_link ) =~ s/^.*\.//; ( my $event = $event_link ) =~ s/^.*\.//;
my $event_path = readlink( $event_link ); my $event_path = readlink( $event_link );
if ( $count++ > MAX_AGED_DIRS ) if ( $count++ > MAX_AGED_DIRS ) {
{
$fs_events->{$event} = -1; $fs_events->{$event} = -1;
} } else {
else if ( !-e $event_path ) {
{
if ( !-e $event_path )
{
aud_print( "Event link $day_dir/$event_link does not point to valid target" ); aud_print( "Event link $day_dir/$event_link does not point to valid target" );
if ( confirm() ) if ( confirm() ) {
{
( $event_link ) = ( $event_link =~ /^(.*)$/ ); # De-taint ( $event_link ) = ( $event_link =~ /^(.*)$/ ); # De-taint
unlink( $event_link ); unlink( $event_link );
$cleaned = 1; $cleaned = 1;
} }
} } else {
else
{
$fs_events->{$event} = (time() - ($^T - ((-M $event_path) * 24*60*60))); $fs_events->{$event} = (time() - ($^T - ((-M $event_path) * 24*60*60)));
} }
} }
} } # end foreach event_link
chdir( EVENT_PATH ); chdir( EVENT_PATH );
} } # end foreach day_dir
} } else {
else
{
chdir( $monitor_dir ); chdir( $monitor_dir );
opendir( DIR, "." ) or Fatal( "Can't open directory '$monitor_dir': $!" ); opendir( DIR, "." ) or Fatal( "Can't open directory '$monitor_dir': $!" );
my @temp_events = sort { $b <=> $a } grep { -d $_ && $_ =~ /^\d+$/ } readdir( DIR ); my @temp_events = sort { $b <=> $a } grep { -d $_ && $_ =~ /^\d+$/ } readdir( DIR );
closedir( DIR ); closedir( DIR );
my $count = 0; my $count = 0;
foreach my $event ( @temp_events ) foreach my $event ( @temp_events ) {
{ if ( $count++ > MAX_AGED_DIRS ) {
if ( $count++ > MAX_AGED_DIRS )
{
$fs_events->{$event} = -1; $fs_events->{$event} = -1;
} } else {
else
{
$fs_events->{$event} = (time() - ($^T - ((-M $event) * 24*60*60))); $fs_events->{$event} = (time() - ($^T - ((-M $event) * 24*60*60)));
} }
} }
chdir( EVENT_PATH ); chdir( EVENT_PATH );
} }
Debug( "Got ".int(keys(%$fs_events))." events\n" ); Debug( "Got ".int(keys(%$fs_events))." events\n" );
} } # end foreach monitor Id
redo MAIN if ( $cleaned ); redo MAIN if ( $cleaned );
$cleaned = 0; $cleaned = 0;
while ( my ( $fs_monitor, $fs_events ) = each(%$fs_monitors) ) while ( my ( $fs_monitor, $fs_events ) = each(%$fs_monitors) ) {
{ if ( my $db_events = $db_monitors->{$fs_monitor} ) {
if ( my $db_events = $db_monitors->{$fs_monitor} ) if ( $fs_events ) {
{ while ( my ( $fs_event, $age ) = each(%$fs_events ) ) {
if ( $fs_events ) if ( !defined($db_events->{$fs_event}) && ($age < 0 || ($age > $Config{ZM_AUDIT_MIN_AGE})) ) {
{
while ( my ( $fs_event, $age ) = each(%$fs_events ) )
{
if ( !defined($db_events->{$fs_event}) && ($age < 0 || ($age > $Config{ZM_AUDIT_MIN_AGE})) )
{
aud_print( "Filesystem event '$fs_monitor/$fs_event' does not exist in database" ); aud_print( "Filesystem event '$fs_monitor/$fs_event' does not exist in database" );
if ( confirm() ) if ( confirm() ) {
{
deleteEventFiles( $fs_event, $fs_monitor ); deleteEventFiles( $fs_event, $fs_monitor );
$cleaned = 1; $cleaned = 1;
} }
} }
} }
} }
} } else {
else
{
aud_print( "Filesystem monitor '$fs_monitor' does not exist in database" ); aud_print( "Filesystem monitor '$fs_monitor' does not exist in database" );
if ( confirm() ) if ( confirm() ) {
{
my $command = "rm -rf $fs_monitor"; my $command = "rm -rf $fs_monitor";
executeShellCommand( $command ); executeShellCommand( $command );
$cleaned = 1; $cleaned = 1;
@ -284,14 +255,12 @@ MAIN: while( $loop ) {
} }
my $monitor_links; my $monitor_links;
foreach my $link ( glob("*") ) foreach my $link ( glob("*") ) {
{
next if ( !-l $link ); next if ( !-l $link );
next if ( -e $link ); next if ( -e $link );
aud_print( "Filesystem monitor link '$link' does not point to valid monitor directory" ); aud_print( "Filesystem monitor link '$link' does not point to valid monitor directory" );
if ( confirm() ) if ( confirm() ) {
{
( $link ) = ( $link =~ /^(.*)$/ ); # De-taint ( $link ) = ( $link =~ /^(.*)$/ ); # De-taint
my $command = qq`rm "$link"`; my $command = qq`rm "$link"`;
executeShellCommand( $command ); executeShellCommand( $command );
@ -313,14 +282,10 @@ MAIN: while( $loop ) {
my $deleteStatsSql = "delete low_priority from Stats where EventId = ?"; my $deleteStatsSql = "delete low_priority from Stats where EventId = ?";
my $deleteStatsSth = $dbh->prepare_cached( $deleteStatsSql ) my $deleteStatsSth = $dbh->prepare_cached( $deleteStatsSql )
or Fatal( "Can't prepare '$deleteStatsSql': ".$dbh->errstr() ); or Fatal( "Can't prepare '$deleteStatsSql': ".$dbh->errstr() );
while ( my ( $db_monitor, $db_events ) = each(%$db_monitors) ) while ( my ( $db_monitor, $db_events ) = each(%$db_monitors) ) {
{ if ( my $fs_events = $fs_monitors->{$db_monitor} ) {
if ( my $fs_events = $fs_monitors->{$db_monitor} ) if ( $db_events ) {
{ while ( my ( $db_event, $age ) = each(%$db_events ) ) {
if ( $db_events )
{
while ( my ( $db_event, $age ) = each(%$db_events ) )
{
if ( !defined($fs_events->{$db_event}) ) { if ( !defined($fs_events->{$db_event}) ) {
if ( $age > $Config{ZM_AUDIT_MIN_AGE} ) { if ( $age > $Config{ZM_AUDIT_MIN_AGE} ) {
aud_print( "Database event '$db_monitor/$db_event' does not exist in filesystem" ); aud_print( "Database event '$db_monitor/$db_event' does not exist in filesystem" );
@ -338,10 +303,8 @@ MAIN: while( $loop ) {
} }
} }
} }
} } # end if db_events
} } else {
else
{
aud_print( "Database monitor '$db_monitor' does not exist in filesystem" ); aud_print( "Database monitor '$db_monitor' does not exist in filesystem" );
#if ( confirm() ) #if ( confirm() )
#{ #{
@ -351,7 +314,7 @@ MAIN: while( $loop ) {
#$cleaned = 1; #$cleaned = 1;
#} #}
} }
} } # end foreach db_monitor
redo MAIN if ( $cleaned ); redo MAIN if ( $cleaned );
# Remove orphaned events (with no monitor) # Remove orphaned events (with no monitor)
@ -363,11 +326,9 @@ MAIN: while( $loop ) {
or Fatal( "Can't prepare '$selectOrphanedEventsSql': ".$dbh->errstr() ); or Fatal( "Can't prepare '$selectOrphanedEventsSql': ".$dbh->errstr() );
$res = $selectOrphanedEventsSth->execute() $res = $selectOrphanedEventsSth->execute()
or Fatal( "Can't execute: ".$selectOrphanedEventsSth->errstr() ); or Fatal( "Can't execute: ".$selectOrphanedEventsSth->errstr() );
while( my $event = $selectOrphanedEventsSth->fetchrow_hashref() ) while( my $event = $selectOrphanedEventsSth->fetchrow_hashref() ) {
{
aud_print( "Found orphaned event with no monitor '$event->{Id}'" ); aud_print( "Found orphaned event with no monitor '$event->{Id}'" );
if ( confirm() ) if ( confirm() ) {
{
$res = $deleteEventSth->execute( $event->{Id} ) $res = $deleteEventSth->execute( $event->{Id} )
or Fatal( "Can't execute: ".$deleteEventSth->errstr() ); or Fatal( "Can't execute: ".$deleteEventSth->errstr() );
$cleaned = 1; $cleaned = 1;
@ -383,11 +344,9 @@ MAIN: while( $loop ) {
or Fatal( "Can't prepare '$selectEmptyEventsSql': ".$dbh->errstr() ); or Fatal( "Can't prepare '$selectEmptyEventsSql': ".$dbh->errstr() );
$res = $selectEmptyEventsSth->execute() $res = $selectEmptyEventsSth->execute()
or Fatal( "Can't execute: ".$selectEmptyEventsSth->errstr() ); or Fatal( "Can't execute: ".$selectEmptyEventsSth->errstr() );
while( my $event = $selectEmptyEventsSth->fetchrow_hashref() ) while( my $event = $selectEmptyEventsSth->fetchrow_hashref() ) {
{
aud_print( "Found empty event with no frame records '$event->{Id}'" ); aud_print( "Found empty event with no frame records '$event->{Id}'" );
if ( confirm() ) if ( confirm() ) {
{
$res = $deleteEventSth->execute( $event->{Id} ) $res = $deleteEventSth->execute( $event->{Id} )
or Fatal( "Can't execute: ".$deleteEventSth->errstr() ); or Fatal( "Can't execute: ".$deleteEventSth->errstr() );
$cleaned = 1; $cleaned = 1;
@ -403,11 +362,9 @@ MAIN: while( $loop ) {
or Fatal( "Can't prepare '$selectOrphanedFramesSql': ".$dbh->errstr() ); or Fatal( "Can't prepare '$selectOrphanedFramesSql': ".$dbh->errstr() );
$res = $selectOrphanedFramesSth->execute() $res = $selectOrphanedFramesSth->execute()
or Fatal( "Can't execute: ".$selectOrphanedFramesSth->errstr() ); or Fatal( "Can't execute: ".$selectOrphanedFramesSth->errstr() );
while( my $frame = $selectOrphanedFramesSth->fetchrow_hashref() ) while( my $frame = $selectOrphanedFramesSth->fetchrow_hashref() ) {
{
aud_print( "Found orphaned frame records for event '$frame->{EventId}'" ); aud_print( "Found orphaned frame records for event '$frame->{EventId}'" );
if ( confirm() ) if ( confirm() ) {
{
$res = $deleteFramesSth->execute( $frame->{EventId} ) $res = $deleteFramesSth->execute( $frame->{EventId} )
or Fatal( "Can't execute: ".$deleteFramesSth->errstr() ); or Fatal( "Can't execute: ".$deleteFramesSth->errstr() );
$cleaned = 1; $cleaned = 1;
@ -423,11 +380,9 @@ MAIN: while( $loop ) {
or Fatal( "Can't prepare '$selectOrphanedStatsSql': ".$dbh->errstr() ); or Fatal( "Can't prepare '$selectOrphanedStatsSql': ".$dbh->errstr() );
$res = $selectOrphanedStatsSth->execute() $res = $selectOrphanedStatsSth->execute()
or Fatal( "Can't execute: ".$selectOrphanedStatsSth->errstr() ); or Fatal( "Can't execute: ".$selectOrphanedStatsSth->errstr() );
while( my $stat = $selectOrphanedStatsSth->fetchrow_hashref() ) while( my $stat = $selectOrphanedStatsSth->fetchrow_hashref() ) {
{
aud_print( "Found orphaned statistic records for event '$stat->{EventId}'" ); aud_print( "Found orphaned statistic records for event '$stat->{EventId}'" );
if ( confirm() ) if ( confirm() ) {
{
$res = $deleteStatsSth->execute( $stat->{EventId} ) $res = $deleteStatsSth->execute( $stat->{EventId} )
or Fatal( "Can't execute: ".$deleteStatsSth->errstr() ); or Fatal( "Can't execute: ".$deleteStatsSth->errstr() );
$cleaned = 1; $cleaned = 1;
@ -470,13 +425,10 @@ MAIN: while( $loop ) {
or Fatal( "Can't prepare '$updateUnclosedEventsSql': ".$dbh->errstr() ); or Fatal( "Can't prepare '$updateUnclosedEventsSql': ".$dbh->errstr() );
$res = $selectUnclosedEventsSth->execute() $res = $selectUnclosedEventsSth->execute()
or Fatal( "Can't execute: ".$selectUnclosedEventsSth->errstr() ); or Fatal( "Can't execute: ".$selectUnclosedEventsSth->errstr() );
while( my $event = $selectUnclosedEventsSth->fetchrow_hashref() ) while( my $event = $selectUnclosedEventsSth->fetchrow_hashref() ) {
{
aud_print( "Found open event '$event->{Id}'" ); aud_print( "Found open event '$event->{Id}'" );
if ( confirm( 'close', 'closing' ) ) if ( confirm( 'close', 'closing' ) ) {
{ $res = $updateUnclosedEventsSth->execute(
$res = $updateUnclosedEventsSth->execute
(
sprintf("%s%d%s", sprintf("%s%d%s",
$event->{Prefix}, $event->{Prefix},
$event->{Id}, $event->{Id},
@ -499,8 +451,8 @@ MAIN: while( $loop ) {
} }
# Now delete any old image files # Now delete any old image files
if ( my @old_files = grep { -M > $max_image_age } <$image_path/*.{jpg,gif,wbmp}> ) my @old_files = grep { -M > $max_image_age } <$image_path/*.{jpg,gif,wbmp}>;
{ if ( @old_files ) {
aud_print( "Deleting ".int(@old_files)." old images\n" ); aud_print( "Deleting ".int(@old_files)." old images\n" );
my $untainted_old_files = join( ";", @old_files ); my $untainted_old_files = join( ";", @old_files );
( $untainted_old_files ) = ( $untainted_old_files =~ /^(.*)$/ ); ( $untainted_old_files ) = ( $untainted_old_files =~ /^(.*)$/ );
@ -512,10 +464,8 @@ MAIN: while( $loop ) {
File::Find::find( { wanted=>\&deleteSwapImage, untaint=>1 }, $swap_image_root ); File::Find::find( { wanted=>\&deleteSwapImage, untaint=>1 }, $swap_image_root );
# Prune the Logs table if required # Prune the Logs table if required
if ( $Config{ZM_LOG_DATABASE_LIMIT} ) if ( $Config{ZM_LOG_DATABASE_LIMIT} ) {
{ if ( $Config{ZM_LOG_DATABASE_LIMIT} =~ /^\d+$/ ) {
if ( $Config{ZM_LOG_DATABASE_LIMIT} =~ /^\d+$/ )
{
# Number of rows # Number of rows
my $selectLogRowCountSql = "SELECT count(*) as Rows from Logs"; my $selectLogRowCountSql = "SELECT count(*) as Rows from Logs";
my $selectLogRowCountSth = $dbh->prepare_cached( $selectLogRowCountSql ) my $selectLogRowCountSth = $dbh->prepare_cached( $selectLogRowCountSql )
@ -524,23 +474,17 @@ MAIN: while( $loop ) {
or Fatal( "Can't execute: ".$selectLogRowCountSth->errstr() ); or Fatal( "Can't execute: ".$selectLogRowCountSth->errstr() );
my $row = $selectLogRowCountSth->fetchrow_hashref(); my $row = $selectLogRowCountSth->fetchrow_hashref();
my $logRows = $row->{Rows}; my $logRows = $row->{Rows};
if ( $logRows > $Config{ZM_LOG_DATABASE_LIMIT} ) if ( $logRows > $Config{ZM_LOG_DATABASE_LIMIT} ) {
{
my $deleteLogByRowsSql = "DELETE low_priority FROM Logs ORDER BY TimeKey ASC LIMIT ?"; my $deleteLogByRowsSql = "DELETE low_priority FROM Logs ORDER BY TimeKey ASC LIMIT ?";
my $deleteLogByRowsSth = $dbh->prepare_cached( $deleteLogByRowsSql ) my $deleteLogByRowsSth = $dbh->prepare_cached( $deleteLogByRowsSql )
or Fatal( "Can't prepare '$deleteLogByRowsSql': ".$dbh->errstr() ); or Fatal( "Can't prepare '$deleteLogByRowsSql': ".$dbh->errstr() );
$res = $deleteLogByRowsSth->execute( $logRows - $Config{ZM_LOG_DATABASE_LIMIT} ) $res = $deleteLogByRowsSth->execute( $logRows - $Config{ZM_LOG_DATABASE_LIMIT} )
or Fatal( "Can't execute: ".$deleteLogByRowsSth->errstr() ); or Fatal( "Can't execute: ".$deleteLogByRowsSth->errstr() );
if ( $deleteLogByRowsSth->rows() ) if ( $deleteLogByRowsSth->rows() ) {
{ aud_print( "Deleted ".$deleteLogByRowsSth->rows() ." log table entries by count\n" );
aud_print( "Deleted ".$deleteLogByRowsSth->rows()
." log table entries by count\n" )
;
} }
} }
} } else {
else
{
# Time of record # Time of record
my $deleteLogByTimeSql = my $deleteLogByTimeSql =
"DELETE low_priority FROM Logs "DELETE low_priority FROM Logs
@ -550,12 +494,10 @@ MAIN: while( $loop ) {
$res = $deleteLogByTimeSth->execute() $res = $deleteLogByTimeSth->execute()
or Fatal( "Can't execute: ".$deleteLogByTimeSth->errstr() ); or Fatal( "Can't execute: ".$deleteLogByTimeSth->errstr() );
if ( $deleteLogByTimeSth->rows() ){ if ( $deleteLogByTimeSth->rows() ){
aud_print( "Deleted ".$deleteLogByTimeSth->rows() aud_print( "Deleted ".$deleteLogByTimeSth->rows() ." log table entries by time\n" );
." log table entries by time\n" )
;
}
} }
} }
} # end if ( $Config{ZM_LOG_DATABASE_LIMIT} )
$loop = $continuous; $loop = $continuous;
sleep( $Config{ZM_AUDIT_CHECK_INTERVAL} ) if $continuous; sleep( $Config{ZM_AUDIT_CHECK_INTERVAL} ) if $continuous;
@ -563,52 +505,37 @@ MAIN: while( $loop ) {
exit( 0 ); exit( 0 );
sub aud_print sub aud_print {
{
my $string = shift; my $string = shift;
if ( !$continuous ) if ( ! $continuous ) {
{
print( $string ); print( $string );
} } else {
else
{
Info( $string ); Info( $string );
} }
} }
sub confirm sub confirm {
{
my $prompt = shift || "delete"; my $prompt = shift || "delete";
my $action = shift || "deleting"; my $action = shift || "deleting";
my $yesno = 0; my $yesno = 0;
if ( $report ) if ( $report ) {
{
print( "\n" ); print( "\n" );
} } elsif ( $interactive ) {
elsif ( $interactive )
{
print( ", $prompt y/n: " ); print( ", $prompt y/n: " );
my $char = <>; my $char = <>;
chomp( $char ); chomp( $char );
if ( $char eq 'q' ) if ( $char eq 'q' ) {
{
exit( 0 ); exit( 0 );
} }
if ( !$char ) if ( !$char ) {
{
$char = 'y'; $char = 'y';
} }
$yesno = ( $char =~ /[yY]/ ); $yesno = ( $char =~ /[yY]/ );
} } else {
else if ( !$continuous ) {
{
if ( !$continuous )
{
print( ", $action\n" ); print( ", $action\n" );
} } else {
else
{
Info( $action ); Info( $action );
} }
$yesno = 1; $yesno = 1;
@ -616,24 +543,23 @@ sub confirm
return( $yesno ); return( $yesno );
} }
sub deleteSwapImage sub deleteSwapImage {
{
my $file = $_; my $file = $_;
if ( $file !~ /^zmswap-/ ) if ( $file !~ /^zmswap-/ ) {
{
return; return;
} }
# Ignore directories # Ignore directories
if ( -d $file ) if ( -d $file ) {
{
return; return;
} }
if ( -M $file > $max_swap_age ) if ( -M $file > $max_swap_age ) {
{
Debug( "Deleting $file" ); Debug( "Deleting $file" );
#unlink( $file ); #unlink( $file );
} }
} }
1;
__END__