Make some errors just errors instead of fatal. Add a check for plural days in log cleaning

This commit is contained in:
Isaac Connor 2018-02-11 14:18:10 -05:00
parent 956f9ee193
commit c0eb0011f0
1 changed files with 37 additions and 22 deletions

View File

@ -159,14 +159,14 @@ MAIN: while( $loop ) {
while ( ! ( $dbh and $dbh->ping() ) ) { while ( ! ( $dbh and $dbh->ping() ) ) {
$dbh = zmDbConnect(); $dbh = zmDbConnect();
if ( ! $dbh ) { if ( ! $dbh ) {
Error('Unable to connect to database');
if ( $continuous ) { if ( $continuous ) {
Error('Unable to connect to database');
# if we are running continuously, then just skip to the next # if we are running continuously, then just skip to the next
# interval, otherwise we are a one off run, so wait a second and # interval, otherwise we are a one off run, so wait a second and
# retry until someone kills us. # retry until someone kills us.
sleep( $Config{ZM_AUDIT_CHECK_INTERVAL} ); sleep( $Config{ZM_AUDIT_CHECK_INTERVAL} );
} else { } else {
Fatal('Unable to connect to database'); Term();
} # end if } # end if
} # end if } # end if
} # end while can't connect to the db } # end while can't connect to the db
@ -175,13 +175,15 @@ MAIN: while( $loop ) {
if ( defined $storage_id ) { if ( defined $storage_id ) {
@Storage_Areas = ZoneMinder::Storage->find( Id=>$storage_id ); @Storage_Areas = ZoneMinder::Storage->find( Id=>$storage_id );
if ( !@Storage_Areas ) { if ( !@Storage_Areas ) {
Fatal("No Storage Area found with Id $storage_id"); Error("No Storage Area found with Id $storage_id");
Term();
} }
Info("Auditing Storage Area $Storage_Areas[0]{Id} $Storage_Areas[0]{Name} at $Storage_Areas[0]{Path}"); Info("Auditing Storage Area $Storage_Areas[0]{Id} $Storage_Areas[0]{Name} at $Storage_Areas[0]{Path}");
} elsif ( $Config{ZM_SERVER_ID} ) { } elsif ( $Config{ZM_SERVER_ID} ) {
@Storage_Areas = ZoneMinder::Storage->find( ServerId => $Config{ZM_SERVER_ID} ); @Storage_Areas = ZoneMinder::Storage->find( ServerId => $Config{ZM_SERVER_ID} );
if ( ! @Storage_Areas ) { if ( ! @Storage_Areas ) {
Fatal("No Storage Area found with ServerId =" . $Config{ZM_SERVER_ID}); Error("No Storage Area found with ServerId =" . $Config{ZM_SERVER_ID});
Term();
} }
Info("Auditing All Storage Areas on Server " . $Storage_Areas[0]->Server()->Name()); Info("Auditing All Storage Areas on Server " . $Storage_Areas[0]->Server()->Name());
} else { } else {
@ -486,37 +488,45 @@ MAIN: while( $loop ) {
FROM Events LEFT JOIN Monitors ON (Events.MonitorId = Monitors.Id) FROM Events LEFT JOIN Monitors ON (Events.MonitorId = Monitors.Id)
WHERE isnull(Monitors.Id)'; WHERE isnull(Monitors.Id)';
my $selectOrphanedEventsSth = $dbh->prepare_cached( $selectOrphanedEventsSql ) my $selectOrphanedEventsSth = $dbh->prepare_cached( $selectOrphanedEventsSql )
or Fatal( "Can't prepare '$selectOrphanedEventsSql': ".$dbh->errstr() ); or Error( "Can't prepare '$selectOrphanedEventsSql': ".$dbh->errstr() );
$res = $selectOrphanedEventsSth->execute() $res = $selectOrphanedEventsSth->execute()
or Fatal( "Can't execute: ".$selectOrphanedEventsSth->errstr() ); or Error( "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} ) if ( $res = $deleteEventSth->execute( $event->{Id} ) ) {
or Fatal( "Can't execute: ".$deleteEventSth->errstr() ); $cleaned = 1;
$cleaned = 1; } else {
Error( "Can't execute: ".$deleteEventSth->errstr() );
}
} }
} }
redo MAIN if ( $cleaned ); redo MAIN if $cleaned;
# Remove empty events (with no frames) # Remove empty events (with no frames)
$cleaned = 0; $cleaned = 0;
my $selectEmptyEventsSql = 'SELECT E.Id AS Id, E.StartTime, F.EventId FROM Events as E LEFT JOIN Frames as F ON (E.Id = F.EventId) my $selectEmptyEventsSql = 'SELECT E.Id AS Id, E.StartTime, F.EventId FROM Events as E LEFT JOIN Frames as F ON (E.Id = F.EventId)
WHERE isnull(F.EventId) AND now() - interval '.$Config{ZM_AUDIT_MIN_AGE}.' second > E.StartTime'; WHERE isnull(F.EventId) AND now() - interval '.$Config{ZM_AUDIT_MIN_AGE}.' second > E.StartTime';
my $selectEmptyEventsSth = $dbh->prepare_cached( $selectEmptyEventsSql ) if ( my $selectEmptyEventsSth = $dbh->prepare_cached( $selectEmptyEventsSql ) ) {
or Fatal( "Can't prepare '$selectEmptyEventsSql': ".$dbh->errstr() ); if ( $res = $selectEmptyEventsSth->execute() ) {
$res = $selectEmptyEventsSth->execute() while( my $event = $selectEmptyEventsSth->fetchrow_hashref() ) {
or Fatal( "Can't execute: ".$selectEmptyEventsSth->errstr() ); aud_print( "Found empty event with no frame records '$event->{Id}' at $$event{StartTime}" );
while( my $event = $selectEmptyEventsSth->fetchrow_hashref() ) { if ( confirm() ) {
aud_print( "Found empty event with no frame records '$event->{Id}' at $$event{StartTime}" ); if ( $res = $deleteEventSth->execute( $event->{Id} ) ) {
if ( confirm() ) { $cleaned = 1;
$res = $deleteEventSth->execute( $event->{Id} ) } else {
or Fatal( "Can't execute: ".$deleteEventSth->errstr() ); Error( "Can't execute: ".$deleteEventSth->errstr() );
$cleaned = 1; }
}
} # end foreach row
} else {
Error( "Can't execute: ".$selectEmptyEventsSth->errstr() );
} }
} else {
Error( "Can't prepare '$selectEmptyEventsSql': ".$dbh->errstr() );
} }
redo MAIN if ( $cleaned ); redo MAIN if $cleaned;
# Remove orphaned frame records # Remove orphaned frame records
$cleaned = 0; $cleaned = 0;
@ -534,7 +544,7 @@ MAIN: while( $loop ) {
$cleaned = 1; $cleaned = 1;
} }
} }
redo MAIN if ( $cleaned ); redo MAIN if $cleaned;
# Remove orphaned stats records # Remove orphaned stats records
$cleaned = 0; $cleaned = 0;
@ -664,6 +674,11 @@ MAIN: while( $loop ) {
} }
} else { } else {
# Time of record # Time of record
# 7 days is invalid. We need to remove the s
if ( $Config{ZM_LOG_DATABASE_LIMIT} =~ /^(.*)s$/ ) {
$Config{ZM_LOG_DATABASE_LIMIT} = $1;
}
my $deleteLogByTimeSql = my $deleteLogByTimeSql =
'DELETE low_priority FROM Logs 'DELETE low_priority FROM Logs
WHERE TimeKey < unix_timestamp(now() - interval '.$Config{ZM_LOG_DATABASE_LIMIT}.')'; WHERE TimeKey < unix_timestamp(now() - interval '.$Config{ZM_LOG_DATABASE_LIMIT}.')';