From acc20eea28c313bd35012f46227f40ba4da7091e Mon Sep 17 00:00:00 2001 From: stan Date: Sat, 8 Nov 2003 11:45:17 +0000 Subject: [PATCH] Timed filters now do not depend on events being added. git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@686 e3e1d417-86f3-4887-817a-d78f3d33393f --- scripts/zmfilter.pl.z | 161 +++++++++++++++++++++++------------------- 1 file changed, 90 insertions(+), 71 deletions(-) diff --git a/scripts/zmfilter.pl.z b/scripts/zmfilter.pl.z index 9cd1e1ba9..d269b18d3 100755 --- a/scripts/zmfilter.pl.z +++ b/scripts/zmfilter.pl.z @@ -61,6 +61,7 @@ BEGIN use constant EVENT_PATH => ZM_PATH_WEB.'/'.ZM_DIR_EVENTS; use constant LOG_FILE => ZM_PATH_LOGS.'/zmfilter-%d.log'; use constant START_DELAY => 5; # How long to wait before starting +use constant CHECK_DELAY => 60; # How long between each event check use constant VERBOSE => 0; # Whether to output more verbose debug if ( ZM_OPT_UPLOAD ) @@ -116,7 +117,7 @@ $ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL}; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; my $monitor = 0; -my $delay = 30; +my $delay = CHECK_DELAY; my $event_id = 0; sub Usage @@ -126,7 +127,7 @@ Usage: zmfilter.pl (-m ,-monitor=) [-e ,--even Parameters are :- -m , --monitor= - Indicate which monitor id to run for -e, --event= - What event to start scanning from --d, --delay= - How long to delay between each check, default 30 +-d, --delay= - How long to delay between each check, default 60 "); exit( -1 ); } @@ -191,6 +192,8 @@ print( "Scanning for events since id $event_id\n" ); sleep( START_DELAY ); my $filters = GetFilters( $monitor ); +my @id_filters; +my @date_filters; my $last_action = 0; my $size = 28; # We only need the first 28 bytes really for the last event count @@ -205,79 +208,27 @@ while( 1 ) $last_event_id = unpack( "l", $last_event_id ); print( "Last event generated is $last_event_id\n" ) if ( VERBOSE ); + + if ( (time() - $last_action) > ZM_FILTER_RELOAD_DELAY ) + { + print( "Reloading filters\n" ) if ( VERBOSE ); + $last_action = time(); + $filters = GetFilters( $monitor ); + @id_filters = grep { !$_->{has_dates} } @$filters; + @date_filters = grep { $_->{has_dates} } @$filters; + } + + foreach my $filter ( @date_filters ) + { + CheckFilter( $filter ); + } + if ( $last_event_id > $event_id ) { # Got new event to check - if ( (time() - $last_action) > ZM_FILTER_RELOAD_DELAY ) + foreach my $filter ( @id_filters ) { - print( "Reloading filters\n" ) if ( VERBOSE ); - $last_action = time(); - $filters = GetFilters( $monitor ); - } - - foreach my $filter ( @$filters ) - { - print( "Checking filter '$filter->{Name}'". - ($filter->{AutoArchive}?", archive":""). - ($filter->{AutoDelete}?", delete":""). - ($filter->{AutoUpload}?", upload":""). - ($filter->{AutoEmail}?", email":""). - ($filter->{AutoMessage}?", message":""). - "\n" ) if ( VERBOSE ); - my $sql = $filter->{Sql}; - my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); - my $res; - if ( $filter->{has_dates} ) - { - $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() ); - } - else - { - $res = $sth->execute( $event_id ) or die( "Can't execute: ".$sth->errstr() ); - } - - while( my $event = $sth->fetchrow_hashref() ) - { - print( "Checking event $event->{Id}\n" ) if ( VERBOSE ); - if ( ZM_OPT_EMAIL && $filter->{AutoEmail} ) - { - next if ( $event->{Emailed} ); - - sendEmail( $filter, $event ); - } - if ( ZM_OPT_MESSAGE && $filter->{AutoMessage} ) - { - next if ( $event->{Messaged} ); - - sendMessage( $filter, $event ); - } - if ( ZM_OPT_UPLOAD && $filter->{AutoUpload} ) - { - next if ( $event->{Uploaded} ); - - uploadArchFile( $filter, $event ); - } - if ( $filter->{AutoArchive} ) - { - print( "Archiving event $event->{Id}\n" ); - # Do it individually to avoid locking up the table for new events - my $sql = "update Events set Archived = 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: ".$sth->errstr() ); - } - if ( $filter->{AutoDelete} ) - { - print( "Deleting event $event->{Id}\n" ); - # Do it individually to avoid locking up the table for new events - my $sql = "delete from Events 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: ".$sth->errstr() ); - - # We could now delete from the Frames and Stats table and the files themselves, - # but we can let the database checker sort that out for us instead. - } - } - $sth->finish(); + CheckFilter( $filter ); } $event_id = $last_event_id; } @@ -390,6 +341,74 @@ sub GetFilters return( \@filters ); } +sub CheckFilter +{ + my $filter = shift; + + print( "Checking filter '$filter->{Name}'". + ($filter->{AutoArchive}?", archive":""). + ($filter->{AutoDelete}?", delete":""). + ($filter->{AutoUpload}?", upload":""). + ($filter->{AutoEmail}?", email":""). + ($filter->{AutoMessage}?", message":""). + "\n" + ) if ( VERBOSE ); + my $sql = $filter->{Sql}; + my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res; + if ( $filter->{has_dates} ) + { + $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() ); + } + else + { + $res = $sth->execute( $event_id ) or die( "Can't execute: ".$sth->errstr() ); + } + + while( my $event = $sth->fetchrow_hashref() ) + { + print( "Checking event $event->{Id}\n" ) if ( VERBOSE ); + if ( ZM_OPT_EMAIL && $filter->{AutoEmail} ) + { + next if ( $event->{Emailed} ); + + sendEmail( $filter, $event ); + } + if ( ZM_OPT_MESSAGE && $filter->{AutoMessage} ) + { + next if ( $event->{Messaged} ); + + sendMessage( $filter, $event ); + } + if ( ZM_OPT_UPLOAD && $filter->{AutoUpload} ) + { + next if ( $event->{Uploaded} ); + + uploadArchFile( $filter, $event ); + } + if ( $filter->{AutoArchive} ) + { + print( "Archiving event $event->{Id}\n" ); + # Do it individually to avoid locking up the table for new events + my $sql = "update Events set Archived = 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: ".$sth->errstr() ); + } + if ( $filter->{AutoDelete} ) + { + print( "Deleting event $event->{Id}\n" ); + # Do it individually to avoid locking up the table for new events + my $sql = "delete from Events 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: ".$sth->errstr() ); + + # We could now delete from the Frames and Stats table and the files themselves, + # but we can let the database checker sort that out for us instead + } + } + $sth->finish(); +} + sub uploadArchFile { my $filter = shift;