diff --git a/scripts/zmaudit.pl.in b/scripts/zmaudit.pl.in index b0fd7697b..f3db7da70 100644 --- a/scripts/zmaudit.pl.in +++ b/scripts/zmaudit.pl.in @@ -159,14 +159,14 @@ MAIN: while( $loop ) { while ( ! ( $dbh and $dbh->ping() ) ) { $dbh = zmDbConnect(); if ( ! $dbh ) { + Error('Unable to connect to database'); if ( $continuous ) { - Error('Unable to connect to database'); # if we are running continuously, then just skip to the next # interval, otherwise we are a one off run, so wait a second and # retry until someone kills us. sleep( $Config{ZM_AUDIT_CHECK_INTERVAL} ); } else { - Fatal('Unable to connect to database'); + Term(); } # end if } # end if } # end while can't connect to the db @@ -175,13 +175,15 @@ MAIN: while( $loop ) { if ( defined $storage_id ) { @Storage_Areas = ZoneMinder::Storage->find( Id=>$storage_id ); 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}"); } elsif ( $Config{ZM_SERVER_ID} ) { @Storage_Areas = ZoneMinder::Storage->find( ServerId => $Config{ZM_SERVER_ID} ); 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()); } else { @@ -486,37 +488,45 @@ MAIN: while( $loop ) { FROM Events LEFT JOIN Monitors ON (Events.MonitorId = Monitors.Id) WHERE isnull(Monitors.Id)'; 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() - or Fatal( "Can't execute: ".$selectOrphanedEventsSth->errstr() ); + or Error( "Can't execute: ".$selectOrphanedEventsSth->errstr() ); while( my $event = $selectOrphanedEventsSth->fetchrow_hashref() ) { aud_print( "Found orphaned event with no monitor '$event->{Id}'" ); if ( confirm() ) { - $res = $deleteEventSth->execute( $event->{Id} ) - or Fatal( "Can't execute: ".$deleteEventSth->errstr() ); - $cleaned = 1; + if ( $res = $deleteEventSth->execute( $event->{Id} ) ) { + $cleaned = 1; + } else { + Error( "Can't execute: ".$deleteEventSth->errstr() ); + } } } - redo MAIN if ( $cleaned ); + redo MAIN if $cleaned; # Remove empty events (with no frames) $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) WHERE isnull(F.EventId) AND now() - interval '.$Config{ZM_AUDIT_MIN_AGE}.' second > E.StartTime'; - my $selectEmptyEventsSth = $dbh->prepare_cached( $selectEmptyEventsSql ) - or Fatal( "Can't prepare '$selectEmptyEventsSql': ".$dbh->errstr() ); - $res = $selectEmptyEventsSth->execute() - or Fatal( "Can't execute: ".$selectEmptyEventsSth->errstr() ); - while( my $event = $selectEmptyEventsSth->fetchrow_hashref() ) { - aud_print( "Found empty event with no frame records '$event->{Id}' at $$event{StartTime}" ); - if ( confirm() ) { - $res = $deleteEventSth->execute( $event->{Id} ) - or Fatal( "Can't execute: ".$deleteEventSth->errstr() ); - $cleaned = 1; + if ( my $selectEmptyEventsSth = $dbh->prepare_cached( $selectEmptyEventsSql ) ) { + if ( $res = $selectEmptyEventsSth->execute() ) { + while( my $event = $selectEmptyEventsSth->fetchrow_hashref() ) { + aud_print( "Found empty event with no frame records '$event->{Id}' at $$event{StartTime}" ); + if ( confirm() ) { + if ( $res = $deleteEventSth->execute( $event->{Id} ) ) { + $cleaned = 1; + } else { + Error( "Can't execute: ".$deleteEventSth->errstr() ); + } + } + } # 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 $cleaned = 0; @@ -534,7 +544,7 @@ MAIN: while( $loop ) { $cleaned = 1; } } - redo MAIN if ( $cleaned ); + redo MAIN if $cleaned; # Remove orphaned stats records $cleaned = 0; @@ -664,6 +674,11 @@ MAIN: while( $loop ) { } } else { # 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 = 'DELETE low_priority FROM Logs WHERE TimeKey < unix_timestamp(now() - interval '.$Config{ZM_LOG_DATABASE_LIMIT}.')';