use a smarter glob to pick up events that were Deep, when the storage area is now Medium.

This commit is contained in:
Isaac Connor 2018-01-22 11:10:31 -05:00
parent b317748796
commit 94fc8013c6
1 changed files with 33 additions and 27 deletions

View File

@ -72,13 +72,13 @@ my $storage_id = undef;
logInit();
GetOptions(
continuous =>\$continuous,
force =>\$force,
interactive =>\$interactive,
monitor_id =>\$monitor_id,
report =>\$report,
storage_id =>\$storage_id,
version =>\$version
continuous =>\$continuous,
force =>\$force,
interactive =>\$interactive,
'monitor_id=i' =>\$monitor_id,
report =>\$report,
'storage_id=i' =>\$storage_id,
version =>\$version
) or pod2usage(-exitstatus => -1);
if ( $version ) {
@ -167,6 +167,21 @@ MAIN: while( $loop ) {
} # end if
} # end while can't connect to the db
my @Storage_Areas;
if ( defined $storage_id ) {
@Storage_Areas = ZoneMinder::Storage->find( Id=>$storage_id );
if ( !@Storage_Areas ) {
Fatal("No Storage Area found with Id $storage_id");
}
Info("Auditing Storage Area $Storage_Areas[0]{Id} $Storage_Areas[0]{Name} at $Storage_Areas[0]{Path}");
} elsif ( $Config{ZM_SERVER_ID} ) {
@Storage_Areas = ZoneMinder::Storage->find( ServerId => $Config{ZM_SERVER_ID} );
Info("Auditing All Storage Areas on Server " . $Storage_Areas[0]->Server()->Name());
} else {
@Storage_Areas = ZoneMinder::Storage->find();
Info("Auditing All Storage Areas");
}
my %Monitors;
my $db_monitors;
my $monitorSelectSql = $monitor_id ? 'SELECT * FROM Monitors WHERE Id=?' : 'SELECT * FROM Monitors ORDER BY Id';
@ -184,28 +199,18 @@ MAIN: while( $loop ) {
while( my $monitor = $monitorSelectSth->fetchrow_hashref() ) {
$Monitors{$$monitor{Id}} = $monitor;
Debug( "Found database monitor '$monitor->{Id}'" );
my $db_events = $db_monitors->{$monitor->{Id}} = {};
my $res = $eventSelectSth->execute( $monitor->{Id} )
or Fatal( "Can't execute: ".$eventSelectSth->errstr() );
while ( my $event = $eventSelectSth->fetchrow_hashref() ) {
$db_events->{$event->{Id}} = $event->{Age};
}
Debug( 'Got '.int(keys(%$db_events))." events\n" );
Debug( 'Got '.int(keys(%$db_events))." events for monitor $monitor->{Id}" );
}
my $fs_monitors;
my @Storage_Areas;
if ( defined $storage_id ) {
@Storage_Areas = ZoneMinder::Storage->find( Id=>$storage_id );
} elsif ( $Config{ZM_SERVER_ID} ) {
@Storage_Areas = ZoneMinder::Storage->find( ServerId => $Config{ZM_SERVER_ID} );
} else {
@Storage_Areas = ZoneMinder::Storage->find();
}
foreach my $Storage ( @Storage_Areas ) {
Debug('Checking events in ' . $Storage->Path() );
if ( ! chdir( $Storage->Path() ) ) {
@ -224,8 +229,8 @@ MAIN: while( $loop ) {
# De-taint
( my $monitor_dir ) = ( $monitor =~ /^(.*)$/ );
if ( $$Storage{Scheme} eq 'Deep' ) {
foreach my $day_dir ( glob("$monitor_dir/*/*/*") ) {
#if ( $$Storage{Scheme} eq 'Deep' ) {
foreach my $day_dir ( glob("$monitor_dir/[0-9][0-9]/[0-9][0-9]/[0-9][0-9]") ) {
Debug( "Checking day dir $day_dir" );
( $day_dir ) = ( $day_dir =~ /^(.*)$/ ); # De-taint
if ( ! chdir( $day_dir ) ) {
@ -262,6 +267,7 @@ MAIN: while( $loop ) {
$$Event{Id} = $event;
$$Event{Path} = join('/', $Storage->Path(), $day_dir,$event_path);
$$Event{RelativePath} = join('/', $day_dir,$event_path);
$$Event{Scheme} = 'Deep';
$Event->MonitorId( $monitor_dir );
$Event->StorageId( $Storage->Id() );
$Event->DiskSpace( undef );
@ -269,8 +275,8 @@ MAIN: while( $loop ) {
} # end foreach event_link
chdir( $Storage->Path() );
} # end foreach day dir
} elsif ( $$Storage{Scheme} eq 'Medium' ) {
foreach my $event_dir ( glob("$monitor_dir/*/*") ) {
foreach my $event_dir ( glob("$monitor_dir/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/*") ) {
next if ! -d $event_dir;
my ( $date, $event_id ) = $event_dir =~ /^$monitor_dir\/(\d{4}\-\d{2}\-\d{2})\/(\d+)$/;
if ( ! $event_id ) {
@ -280,11 +286,13 @@ MAIN: while( $loop ) {
my $Event = $fs_events->{$event_id} = new ZoneMinder::Event();
$$Event{Id} = $event_id;
$$Event{Path} = join('/', $Storage->Path(), $event_dir );
$$Event{Scheme} = 'Medium';
$$Event{RelativePath} = $event_dir;
$Event->MonitorId( $monitor_dir );
$Event->StorageId( $Storage->Id() );
} # end foreach event
} else {
if ( ! $$Storage{Scheme} ) {
if ( ! chdir( $monitor_dir ) ) {
Error( "Can't chdir directory '$$Storage{Path}/$monitor_dir': $!" );
next;
@ -320,7 +328,6 @@ MAIN: while( $loop ) {
my $Event = $fs_events->{$fs_event_id};
if ( ! defined( $db_events->{$fs_event_id} ) ) {
my $age = $Event->age();
@ -417,9 +424,7 @@ MAIN: while( $loop ) {
$cleaned = 1;
}
} else {
my $Storage = $Event->Storage();
aud_print( "Database event '".$Event->getPath()." monitor:$db_monitor event:$db_event' does not exist in filesystem but too young to delete age: $age > MIN $Config{ZM_AUDIT_MIN_AGE}.\n" );
aud_print( "Database event '".$Event->Path()." monitor:$db_monitor event:$db_event' does not exist in filesystem but too young to delete age: $age > MIN $Config{ZM_AUDIT_MIN_AGE}.\n" );
}
} # end if exists in filesystem
} # end if ! in fs_events
@ -776,6 +781,7 @@ yet.
-f, --force - Run even if pid file exists
-i, --interactive - Ask before applying any changes
-r, --report - Just report don't actually do anything
-s, --storage_id - Specify a storage area to audit instead of all
-v, --version - Print the installed version of ZoneMinder
=cut