Added disk checks to filters and removed last event id dependency.

git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@1020 e3e1d417-86f3-4887-817a-d78f3d33393f
This commit is contained in:
stan 2004-04-21 09:19:23 +00:00
parent fa483725d9
commit 59e728e201
2 changed files with 145 additions and 65 deletions

View File

@ -122,9 +122,8 @@ my $event_id = 0;
sub Usage sub Usage
{ {
print( " print( "
Usage: zmfilter.pl [-e <event_id>,--event=<event_id>] [-d <seconds>,--delay=<seconds>] Usage: zmfilter.pl [-d <seconds>,--delay=<seconds>]
Parameters are :- Parameters are :-
-e<event_id>, --event=<event_id> - What event to start scanning from
-d<seconds>, --delay=<seconds> - How long to delay between each check, default 60 -d<seconds>, --delay=<seconds> - How long to delay between each check, default 60
"); ");
exit( -1 ); exit( -1 );
@ -162,7 +161,7 @@ sub DateTimeToSQL
return( strftime( "%Y-%m-%d %H:%M:%S", localtime( $dt_val ) ) ); return( strftime( "%Y-%m-%d %H:%M:%S", localtime( $dt_val ) ) );
} }
if ( !GetOptions( 'event=i'=>\$event_id, 'delay=i'=>\$delay ) ) if ( !GetOptions( 'delay=i'=>\$delay ) )
{ {
Usage(); Usage();
} }
@ -179,24 +178,11 @@ chdir( EVENT_PATH );
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA ); my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA );
my $last_event_id; print( "Scanning for events\n" );
if ( !$event_id )
{
my $sql = "select max(Id) as MaxEventId from Events";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
my $row = $sth->fetchrow_hashref();
$event_id = $row->{MaxEventId};
$sth->finish();
}
print( "Scanning for events since id $event_id\n" );
sleep( START_DELAY ); sleep( START_DELAY );
my $filters = GetFilters(); my $filters = getFilters();
my @id_filters;
my @date_filters;
my $last_action = 0; my $last_action = 0;
while( 1 ) while( 1 )
@ -205,41 +191,43 @@ while( 1 )
{ {
print( "Reloading filters\n" ) if ( VERBOSE ); print( "Reloading filters\n" ) if ( VERBOSE );
$last_action = time(); $last_action = time();
$filters = GetFilters(); $filters = getFilters();
@id_filters = grep { !$_->{has_dates} } @$filters;
@date_filters = grep { $_->{has_dates} } @$filters;
} }
foreach my $filter ( @date_filters ) foreach my $filter ( @$filters )
{ {
CheckFilter( $filter ); checkFilter( $filter );
} }
my $sql = "select max(Id) as LastEventId from Events";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
my $row = $sth->fetchrow_hashref();
$last_event_id = $row->{LastEventId};
$sth->finish();
if ( $last_event_id && $last_event_id > $event_id )
{
print( "Last event generated is $last_event_id\n" ) if ( VERBOSE );
# Got new event to check
foreach my $filter ( @id_filters )
{
CheckFilter( $filter );
}
$event_id = $last_event_id;
}
else
{
print( "Sleeping for $delay seconds\n" ) if ( VERBOSE ); print( "Sleeping for $delay seconds\n" ) if ( VERBOSE );
sleep( $delay ); sleep( $delay );
}
} }
sub GetFilters sub getDiskPercent
{
my $command = "df .";
my $df = qx( $command );
my $space = -1;
if ( $df =~ /\s(\d+)%/ms )
{
$space = $1;
}
return( $space );
}
sub getDiskBlocks
{
my $command = "df .";
my $df = qx( $command );
my $space = -1;
if ( $df =~ /\s(\d+)\s+\d+\s+\d+%/ms )
{
$space = $1;
}
return( $space );
}
sub getFilters
{ {
my @filters; my @filters;
my $sql = "select * from Filters where (AutoArchive = 1 or AutoDelete = 1 or AutoUpload = 1 or AutoEmail = 1 or AutoMessage = 1 or AutoExecute != '') order by Name"; my $sql = "select * from Filters where (AutoArchive = 1 or AutoDelete = 1 or AutoUpload = 1 or AutoEmail = 1 or AutoMessage = 1 or AutoExecute != '') order by Name";
@ -288,14 +276,10 @@ sub GetFilters
elsif ( $filter_terms{$attr_name} eq 'DateTime' ) elsif ( $filter_terms{$attr_name} eq 'DateTime' )
{ {
$filter_sql .= "E.StartTime"; $filter_sql .= "E.StartTime";
# Indicate that this filter uses dates and so should not be dependant on the event id
$filter_data->{has_dates} = !undef;
} }
elsif ( $filter_terms{$attr_name} eq 'Date' ) elsif ( $filter_terms{$attr_name} eq 'Date' )
{ {
$filter_sql .= "to_days( E.StartTime )"; $filter_sql .= "to_days( E.StartTime )";
# Indicate that this filter uses dates and so should not be dependant on the event id
$filter_data->{has_dates} = !undef;
} }
elsif ( $filter_terms{$attr_name} eq 'Time' ) elsif ( $filter_terms{$attr_name} eq 'Time' )
{ {
@ -309,6 +293,16 @@ sub GetFilters
{ {
$filter_sql .= "E.Archived = ".$filter_terms{$value_name}; $filter_sql .= "E.Archived = ".$filter_terms{$value_name};
} }
elsif ( $filter_terms{$attr_name} eq 'DiskPercent' )
{
$filter_sql .= "zmDiskPercent";
$filter_data->{HasDiskPercent} = !undef;
}
elsif ( $filter_terms{$attr_name} eq 'DiskBlocks' )
{
$filter_sql .= "zmDiskBlocks";
$filter_data->{HasDiskBlocks} = !undef;
}
else else
{ {
$filter_sql .= "E.".$filter_terms{$attr_name}; $filter_sql .= "E.".$filter_terms{$attr_name};
@ -400,12 +394,88 @@ sub GetFilters
{ {
$sql .= " and ( $filter_sql )"; $sql .= " and ( $filter_sql )";
} }
if ( !$filter_data->{has_dates} ) my @auto_terms;
if ( $filter_data->{AutoArchive} )
{ {
$sql .= " and E.Id > ?"; push( @auto_terms, "E.Archived = 0" )
} }
$sql .= " order by E.Id"; if ( $filter_data->{AutoUpload} )
#print $sql."\n"; {
push( @auto_terms, "E.Uploaded = 0" )
}
if ( $filter_data->{AutoEmail} )
{
push( @auto_terms, "E.Emailed = 0" )
}
if ( $filter_data->{AutoMessage} )
{
push( @auto_terms, "E.Messaged = 0" )
}
if ( $filter_data->{AutoExecute} )
{
push( @auto_terms, "E.Executed = 0" )
}
if ( @auto_terms )
{
$sql .= " and ( ".join( " or ", @auto_terms )." )";
}
if ( !$filter_terms{sort_field} )
{
$filter_terms{sort_field} = 'StartTime';
$filter_terms{sort_asc} = 0;
}
my $sort_column = '';
if ( $filter_terms{sort_field} eq 'Id' )
{
$sort_column = "E.Id";
}
elsif ( $filter_terms{sort_field} eq 'MonitorName' )
{
$sort_column = "M.Name";
}
elsif ( $filter_terms{sort_field} eq 'Name' )
{
$sort_column = "E.Name";
}
elsif ( $filter_terms{sort_field} eq 'StartTime' )
{
$sort_column = "E.StartTime";
}
elsif ( $filter_terms{sort_field} eq 'Secs' )
{
$sort_column = "E.Length";
}
elsif ( $filter_terms{sort_field} eq 'Frames' )
{
$sort_column = "E.Frames";
}
elsif ( $filter_terms{sort_field} eq 'AlarmFrames' )
{
$sort_column = "E.AlarmFrames";
}
elsif ( $filter_terms{sort_field} eq 'TotScore' )
{
$sort_column = "E.TotScore";
}
elsif ( $filter_terms{sort_field} eq 'AvgScore' )
{
$sort_column = "E.AvgScore";
}
elsif ( $filter_terms{sort_field} eq 'MaxScore' )
{
$sort_column = "E.MaxScore";
}
else
{
$sort_column = "E.StartTime";
}
my $sort_order = $filter_terms{sort_asc}?"asc":"desc";
$sql .= " order by ".$sort_column." ".$sort_order;
if ( $filter_terms{limit} )
{
$sql .= " limit 0, ".$filter_terms{limit};
}
print "SQL:$sql\n" if ( VERBOSE );
$filter_data->{Sql} = $sql; $filter_data->{Sql} = $sql;
if ( $filter_data->{AutoExecute} ) if ( $filter_data->{AutoExecute} )
{ {
@ -429,7 +499,7 @@ sub GetFilters
return( \@filters ); return( \@filters );
} }
sub CheckFilter sub checkFilter
{ {
my $filter = shift; my $filter = shift;
@ -443,16 +513,20 @@ sub CheckFilter
"\n" "\n"
) if ( VERBOSE ); ) if ( VERBOSE );
my $sql = $filter->{Sql}; my $sql = $filter->{Sql};
if ( $filter->{HasDiskPercent} )
{
my $disk_percent = getDiskPercent();
$sql =~ s/zmDiskPercent/$disk_percent/g;
}
if ( $filter->{HasDiskBlocks} )
{
my $disk_blocks = getDiskBlocks();
$sql =~ s/zmDiskBlocks/$disk_blocks/g;
}
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res; my $res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
if ( $filter->{has_dates} )
{
$res = $sth->execute() or die( "Can't execute '$sql': ".$sth->errstr() );
}
else
{
$res = $sth->execute( $event_id ) or die( "Can't execute '$sql': ".$sth->errstr() );
}
while( my $event = $sth->fetchrow_hashref() ) while( my $event = $sth->fetchrow_hashref() )
{ {
@ -530,6 +604,12 @@ sub executeCommand
{ {
print( "Command '$command' exited with status: $status\n" ); print( "Command '$command' exited with status: $status\n" );
} }
else
{
my $sql = "update Events set Executed = 1 where Id = ?";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $event->{Id} ) or die( "Can't execute '$sql': ".$sth->errstr() );
}
} }
sub uploadArchFile sub uploadArchFile

View File

@ -253,7 +253,7 @@ if ( $command =~ /^(?:start|restart)$/ )
$sth->finish(); $sth->finish();
# This is now started unconditionally # This is now started unconditionally
execute( ZM_PATH_BIN."/zmdc.pl start zmfilter.pl -e -1" ); execute( ZM_PATH_BIN."/zmdc.pl start zmfilter.pl" );
execute( ZM_PATH_BIN."/zmdc.pl start zmaudit.pl -d 900 -y" ); execute( ZM_PATH_BIN."/zmdc.pl start zmaudit.pl -d 900 -y" );
if ( ZM_OPT_X10 ) if ( ZM_OPT_X10 )