2002-12-15 22:08:13 +08:00
#!/usr/bin/perl -wT
2002-11-29 19:01:44 +08:00
#
2002-12-10 21:17:16 +08:00
# ==========================================================================
#
2003-04-14 01:29:12 +08:00
# ZoneMinder Audit Script, $Date$, $Revision$
2003-01-12 02:22:27 +08:00
# Copyright (C) 2003 Philip Coombes
2002-12-10 21:17:16 +08:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ==========================================================================
#
2002-11-29 19:01:44 +08:00
# This script checks for consistency between the event filesystem and
# the database. If events are found in one and not the other they are
# deleted (optionally). Additionally any monitor event directories that
# do not correspond to a database monitor are similarly disposed of.
# However monitors in the database that don't have a directory are left
# alone as this is valid if they are newly created and have no events
# yet.
#
2002-12-10 21:33:18 +08:00
# ==========================================================================
#
# These are the elements you need to edit to suit your installation
#
# ==========================================================================
2003-01-15 23:06:15 +08:00
use constant ZM_PATH_BIN => "<from zmconfig>";
use constant ZM_PATH_WEB => "<from zmconfig>";
2003-01-11 08:15:29 +08:00
use constant ZM_DB_SERVER => "<from zmconfig>";
use constant ZM_DB_NAME => "<from zmconfig>";
use constant ZM_DB_USERA => "<from zmconfig>";
use constant ZM_DB_PASSA => "<from zmconfig>";
2003-07-04 20:31:36 +08:00
# Load the config from the database into the symbol table
BEGIN
{
use DBI;
no strict 'refs';
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA );
my $sql = "select * from Config";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
while( my $config = $sth->fetchrow_hashref() )
{
*{$config->{Name}} = sub { $config->{Value} };
}
$sth->finish();
$dbh->disconnect();
}
2003-01-11 08:15:29 +08:00
2003-01-15 23:06:15 +08:00
use constant IMAGE_PATH => ZM_PATH_WEB.'/'.ZM_DIR_IMAGES;
use constant EVENT_PATH => ZM_PATH_WEB.'/'.ZM_DIR_EVENTS;
2003-01-11 08:15:29 +08:00
use constant LOG_FILE => ZM_PATH_LOGS.'/zmaudit.log';
2003-01-07 20:26:34 +08:00
use constant VERBOSE => 0; # Whether to output more verbose debug
2002-12-10 21:33:18 +08:00
# ==========================================================================
#
# You shouldn't need to change anything from here downwards
#
# ==========================================================================
2002-11-29 19:01:44 +08:00
use strict;
use DBI;
use Getopt::Long;
$| = 1;
2003-01-07 19:32:37 +08:00
$ENV{PATH} = '/bin:/usr/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
2002-11-29 19:01:44 +08:00
my $report = 0;
my $yes = 0;
my $delay = 0;
sub usage
{
print( "
2003-11-08 19:44:01 +08:00
Usage: zmaudit.pl [-r,-report|-y,-yes] [-d <seconds>,-delay=<seconds>]
2002-11-29 19:01:44 +08:00
Parameters are :-
-r, --report - Just report don't actually do anything
-y, --yes - Just do all actions without confirmation
2002-12-14 07:23:24 +08:00
-d <seconds>, --delay=<seconds> - how long to delay between each pass, the default of 0 means run once only.
2002-11-29 19:01:44 +08:00
");
exit( -1 );
}
sub confirm
{
2004-05-05 17:10:22 +08:00
my $prompt = shift || "delete";
my $action = shift || "deleting";
2002-11-29 19:01:44 +08:00
my $yesno = $yes?1:0;
if ( $report )
{
print( "\n" );
}
elsif ( $yes )
{
2004-05-05 17:10:22 +08:00
print( ", $action\n" );
2002-11-29 19:01:44 +08:00
}
else
{
2004-05-05 17:10:22 +08:00
print( ", $prompt y/n: " );
2003-07-04 20:37:58 +08:00
my $char = <>;
chomp( $char );
2002-11-29 19:01:44 +08:00
if ( $char eq 'q' )
{
exit( 0 );
}
2003-07-04 20:37:58 +08:00
if ( !$char )
2002-11-29 19:01:44 +08:00
{
$char = 'y';
}
if ( $char eq "a" )
{
$yes = 1;
return( 1 );
}
$yesno = ( $char =~ /[yY]/ );
}
return( $yesno );
}
if ( !GetOptions( 'report'=>\$report, 'yes'=>\$yes, 'delay=i'=>\$delay ) )
{
usage();
}
if ( $report && $yes )
{
print( STDERR "Error, only one of --report and --yes may be specified\n" );
usage();
}
2003-01-11 17:08:57 +08:00
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA );
2003-01-11 06:20:23 +08:00
2002-12-10 21:33:18 +08:00
chdir( EVENT_PATH );
2002-12-24 20:43:38 +08:00
if ( $delay ) # Background mode
2002-12-15 22:08:13 +08:00
{
2002-12-24 20:43:38 +08:00
open( LOG, ">>".LOG_FILE ) or die( "Can't open log file: $!" );
2002-12-15 22:08:13 +08:00
open( STDOUT, ">&LOG" ) || die( "Can't dup stdout: $!" );
select( STDOUT ); $| = 1;
open( STDERR, ">&LOG" ) || die( "Can't dup stderr: $!" );
select( STDERR ); $| = 1;
select( LOG ); $| = 1;
}
2003-01-03 22:41:04 +08:00
my $max_image_age = 15/(24*60); # 15 Minutes
my $image_path = IMAGE_PATH;
2002-12-14 07:23:24 +08:00
do
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
my $fs_monitors;
foreach my $monitor ( <*> )
2002-11-29 19:01:44 +08:00
{
2004-01-15 19:36:56 +08:00
next if ( $monitor =~ /lost\+found/ ); # Ignore mount points
2003-01-07 20:26:34 +08:00
print( "Found filesystem monitor '$monitor'" ) if ( VERBOSE );
2002-12-14 07:23:24 +08:00
my $fs_events = $fs_monitors->{$monitor} = {};
2002-12-15 22:08:13 +08:00
( my $monitor_dir ) = ( $monitor =~ /^(.*)$/ ); # De-taint
chdir( $monitor_dir );
2002-12-14 07:23:24 +08:00
foreach my $event ( <*> )
{
$fs_events->{$event} = !undef;
}
2004-01-15 19:36:56 +08:00
chdir( EVENT_PATH );
2003-01-07 20:26:34 +08:00
print( ", got ".int(keys(%$fs_events))." events\n" ) if ( VERBOSE );
2002-11-29 19:01:44 +08:00
}
2002-12-14 07:23:24 +08:00
my $db_monitors;
my $sql = "select * from Monitors order by Name";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $sql2 = "select * from Events where MonitorId = ? order by Id";
my $sth2 = $dbh->prepare_cached( $sql2 ) or die( "Can't prepare '$sql2': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() )
2002-11-29 19:01:44 +08:00
{
2003-01-07 20:26:34 +08:00
print( "Found database monitor '$monitor->{Name}'" ) if ( VERBOSE );
2002-12-14 07:23:24 +08:00
my $db_events = $db_monitors->{$monitor->{Name}} = {};
my $res = $sth2->execute( $monitor->{Id} ) or die( "Can't execute: ".$sth2->errstr() );
while ( my $event = $sth2->fetchrow_hashref() )
{
$db_events->{$event->{Id}} = !undef;
}
2003-01-07 20:26:34 +08:00
print( ", got ".int(keys(%$db_events))." events\n" ) if ( VERBOSE );
2002-12-14 07:23:24 +08:00
$sth2->finish();
2002-11-29 19:01:44 +08:00
}
2002-12-14 07:23:24 +08:00
$sth->finish();
2002-11-29 19:01:44 +08:00
2002-12-14 07:23:24 +08:00
while ( my ( $fs_monitor, $fs_events ) = each(%$fs_monitors) )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
if ( my $db_events = $db_monitors->{$fs_monitor} )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
if ( $fs_events )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
while ( my ( $fs_event, $val ) = each(%$fs_events ) )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
if ( !$db_events->{$fs_event} )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
print( "Filesystem event '$fs_monitor/$fs_event' does not exist in database" );
if ( confirm() )
{
2002-12-24 19:50:43 +08:00
my $command = "/bin/rm -rf ".EVENT_PATH."/$fs_monitor/$fs_event";
2002-12-14 07:23:24 +08:00
qx( $command );
}
2002-11-29 19:01:44 +08:00
}
}
}
}
2002-12-14 07:23:24 +08:00
else
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
print( "Filesystem monitor '$fs_monitor' does not exist in database" );
if ( confirm() )
{
my $command = "rm -rf ".EVENT_PATH."/$fs_monitor";
qx( $command );
}
2002-11-29 19:01:44 +08:00
}
}
2002-12-14 07:23:24 +08:00
my $sql3 = "delete from Monitors where Id = ?";
my $sth3 = $dbh->prepare_cached( $sql3 ) or die( "Can't prepare '$sql3': ".$dbh->errstr() );
my $sql4 = "delete from Events where Id = ?";
my $sth4 = $dbh->prepare_cached( $sql4 ) or die( "Can't prepare '$sql4': ".$dbh->errstr() );
my $sql5 = "delete from Frames where EventId = ?";
my $sth5 = $dbh->prepare_cached( $sql5 ) or die( "Can't prepare '$sql5': ".$dbh->errstr() );
my $sql6 = "delete from Stats where EventId = ?";
my $sth6 = $dbh->prepare_cached( $sql6 ) or die( "Can't prepare '$sql6': ".$dbh->errstr() );
while ( my ( $db_monitor, $db_events ) = each(%$db_monitors) )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
if ( my $fs_events = $fs_monitors->{$db_monitor} )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
if ( $db_events )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
while ( my ( $db_event, $val ) = each(%$db_events ) )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
if ( !$fs_events->{$db_event} )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
print( "Database event '$db_monitor/$db_event' does not exist in filesystem" );
if ( confirm() )
{
my $res = $sth4->execute( $db_event ) or die( "Can't execute: ".$sth4->errstr() );
$res = $sth5->execute( $db_event ) or die( "Can't execute: ".$sth5->errstr() );
2004-05-05 17:10:22 +08:00
$res = $sth6->execute( $db_event ) or die( "Can't execute: ".$sth6->errstr() );
2002-12-14 07:23:24 +08:00
}
2002-11-29 19:01:44 +08:00
}
}
}
}
2002-12-14 07:23:24 +08:00
else
2002-11-29 19:01:44 +08:00
{
2004-02-26 19:10:42 +08:00
#print( "Database monitor '$db_monitor' does not exist in filesystem" );
2004-03-22 17:10:04 +08:00
#if ( confirm() )
#{
2002-12-14 07:23:24 +08:00
# We don't actually do this in case it's new
#my $res = $sth3->execute( $db_monitor ) or die( "Can't execute: ".$sth3->errstr() );
2004-03-22 17:10:04 +08:00
#}
2002-11-29 19:01:44 +08:00
}
}
2002-12-14 07:23:24 +08:00
my $sql7 = "select distinct EventId from Frames left join Events on Frames.EventId = Events.Id where isnull(Events.Id)";
my $sth7 = $dbh->prepare_cached( $sql7 ) or die( "Can't prepare '$sql7': ".$dbh->errstr() );
$res = $sth7->execute() or die( "Can't execute: ".$sth7->errstr() );
while( my $frame = $sth7->fetchrow_hashref() )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
print( "Found orphaned frame records for event '$frame->{EventId}'" );
if ( confirm() )
{
2004-05-05 17:10:22 +08:00
$res = $sth5->execute( $frame->{EventId} ) or die( "Can't execute: ".$sth6->errstr() );
2002-12-14 07:23:24 +08:00
}
2002-11-29 19:01:44 +08:00
}
2002-12-14 07:23:24 +08:00
my $sql8 = "select distinct EventId from Stats left join Events on Stats.EventId = Events.Id where isnull(Events.Id)";
my $sth8 = $dbh->prepare_cached( $sql8 ) or die( "Can't prepare '$sql8': ".$dbh->errstr() );
$res = $sth8->execute() or die( "Can't execute: ".$sth8->errstr() );
while( my $stat = $sth8->fetchrow_hashref() )
2002-11-29 19:01:44 +08:00
{
2002-12-14 07:23:24 +08:00
print( "Found orphaned statistic records for event '$stat->{EventId}'" );
if ( confirm() )
{
2004-05-05 17:10:22 +08:00
$res = $sth6->execute( $stat->{EventId} ) or die( "Can't execute: ".$sth6->errstr() );
}
}
# New audit to close any events that were left open for longer than 5 minutes
my $sql9 = "select E.Id, max(F.TimeStamp) as EndTime, unix_timestamp(max(F.TimeStamp)) - unix_timestamp(E.StartTime) as Length, count(F.Id) as Frames, count(if(F.Score>0,1,NULL)) as AlarmFrames, sum(F.Score) as TotScore, max(F.Score) as MaxScore from Events as E inner join Frames as F on E.Id = F.EventId where isnull(E.Frames) group by E.Id having EndTime < (now() - interval 5 minute)";
my $sth9 = $dbh->prepare_cached( $sql9 ) or die( "Can't prepare '$sql9': ".$dbh->errstr() );
my $sql10 = "update Events set Name = ?, EndTime = ?, Length = ?, Frames = ?, AlarmFrames = ?, TotScore = ?, AvgScore = ?, MaxScore = ? where Id = ?";
my $sth10 = $dbh->prepare_cached( $sql10 ) or die( "Can't prepare '$sql10': ".$dbh->errstr() );
$res = $sth9->execute() or die( "Can't execute: ".$sth9->errstr() );
while( my $event = $sth9->fetchrow_hashref() )
{
print( "Found open event '$event->{Id}'" );
if ( confirm( 'close', 'closing' ) )
{
$res = $sth10->execute( sprintf( "Event-%d(r)", $event->{Id}), $event->{EndTime}, $event->{Length}, $event->{Frames}, $event->{AlarmFrames}, $event->{TotScore}, $event->{AlarmFrames}?int($event->{TotScore}/$event->{AlarmFrames}):0, $event->{MaxScore}, $event->{Id} ) or die( "Can't execute: ".$sth10->errstr() );
2002-12-14 07:23:24 +08:00
}
2002-11-29 19:01:44 +08:00
}
2003-01-03 22:41:04 +08:00
2004-05-05 17:08:47 +08:00
# Now delete any old image files
if ( my @old_files = grep { -M > $max_image_age } <$image_path/*.{jpg,gif,wbmp}> )
2003-01-03 22:41:04 +08:00
{
2004-05-05 17:08:47 +08:00
print( "Deleting ".int(@old_files)." old images\n" );
2003-01-03 22:41:04 +08:00
my $untainted_old_files = join( ";", @old_files );
( $untainted_old_files ) = ( $untainted_old_files =~ /^(.*)$/ );
unlink( split( ";", $untainted_old_files ) );
}
2002-12-15 22:08:13 +08:00
sleep( $delay ) if ( $delay );
2002-12-14 07:23:24 +08:00
} while( $delay );