2002-12-24 20:44:11 +08:00
#!/usr/bin/perl -wT
2002-11-29 19:01:44 +08:00
#
2002-12-10 21:17:16 +08:00
# ==========================================================================
#
# Zone Minder Event Filter 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 continuously monitors the recorded events for the given
# monitor and applies any filters which would delete and/or upload
# matching events
#
use strict;
# ==========================================================================
#
# 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>";
use constant ZM_PATH_CGI => "<from zmconfig>";
use constant ZM_PATH_LOGS => "<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>";
use constant ZM_DIR_EVENTS => "<from zmconfig>";
use constant ZM_SHM_KEY => <from zmconfig>;
use constant ZM_FILTER_RELOAD_DELAY => <from zmconfig>;
use constant ZM_OPT_UPLOAD => <from zmconfig>;
use constant ZM_UPLOAD_ARCH_FORMAT => <from zmconfig>;
use constant ZM_UPLOAD_ARCH_COMPRESS => <from zmconfig>;
use constant ZM_UPLOAD_ARCH_ANALYSE => <from zmconfig>;
use constant ZM_UPLOAD_FTP_HOST => "<from zmconfig>";
use constant ZM_UPLOAD_FTP_USER => "<from zmconfig>";
use constant ZM_UPLOAD_FTP_PASS => "<from zmconfig>";
use constant ZM_UPLOAD_FTP_LOC_DIR => "<from zmconfig>";
use constant ZM_UPLOAD_FTP_REM_DIR => "<from zmconfig>";
use constant ZM_UPLOAD_FTP_TIMEOUT => <from zmconfig>;
use constant ZM_UPLOAD_FTP_PASSIVE => <from zmconfig>;
use constant ZM_UPLOAD_FTP_DEBUG => <from zmconfig>;
2003-01-15 21:49:16 +08:00
use constant ZM_OPT_EMAIL => <from zmconfig>;
use constant ZM_EMAIL_ADDRESS => <from zmconfig>;
use constant ZM_OPT_MESSAGE => <from zmconfig>;
use constant ZM_MESSAGE_ADDRESS => <from zmconfig>;
use constant ZM_URL => <from zmconfig>;
use constant ZM_FROM_EMAIL => <from zmconfig>;
2003-01-11 08:15:29 +08:00
2003-01-16 01:29:36 +08:00
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.'/zmfilter-%d.log';
2003-01-17 18:34:12 +08:00
use constant START_DELAY => 5; # How long to wait before starting
2003-01-07 20:26:34 +08:00
use constant VERBOSE => 0; # Whether to output more verbose debug
2002-11-29 19:01:44 +08:00
2003-01-11 08:15:29 +08:00
if ( ZM_OPT_UPLOAD )
{
# Comment these out if you don't have them and don't want to upload
# or don't want to use that format
if ( ZM_UPLOAD_ARCH_FORMAT eq "zip" )
{
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
}
else
{
use Archive::Tar;
}
use Net::FTP;
}
2002-11-29 19:01:44 +08:00
2003-01-15 21:49:16 +08:00
my $email_subject;
my $email_body;
if ( ZM_OPT_EMAIL )
{
use Mail::Mailer;
2003-01-18 23:24:01 +08:00
use constant ZM_EMAIL_FORMAT => "<from zmconfig>"; # File containing the contents of the email
2003-01-15 21:49:16 +08:00
2003-01-18 23:24:01 +08:00
( $email_subject, $email_body ) = ZM_EMAIL_FORMAT =~ /subject\s*=\s*"([^\n]*)".*body\s*=\s*"(.*)"/ms;
2003-01-15 21:49:16 +08:00
}
my $message_subject;
my $message_body;
if ( ZM_OPT_MESSAGE )
{
use Mail::Mailer;
2003-01-18 23:24:01 +08:00
use constant ZM_MESSAGE_FORMAT => "<from zmconfig>"; # File containing the contents of the message
2003-01-15 21:49:16 +08:00
2003-01-18 23:24:01 +08:00
( $message_subject, $message_body ) = ZM_MESSAGE_FORMAT =~ /subject\s*=\s*"([^\n]*)".*body\s*=\s*"(.*)"/ms;
2003-01-15 21:49:16 +08:00
}
2002-11-29 19:01:44 +08:00
# ==========================================================================
#
# You shouldn't need to change anything from here downwards
#
# ==========================================================================
use DBI;
2003-01-15 21:49:16 +08:00
use POSIX;
2002-11-29 19:01:44 +08:00
use Date::Manip;
use Data::Dumper;
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 $monitor = 0;
my $delay = 30;
my $event_id = 0;
sub Usage
{
print( "
2002-12-12 06:44:32 +08:00
Usage: zmfilter.pl (-m <monitor_id>,-monitor=<monitor_id>) [-e <event_id>,--event=<event_id>] [-d <seconds>,--delay=<seconds>]
2002-11-29 19:01:44 +08:00
Parameters are :-
-m <monitor_id>, --monitor=<monitor_id> - Indicate which monitor id to run for
-e<event_id>, --event=<event_id> - What event to start scanning from
-d<seconds>, --delay=<seconds> - How long to delay between each check, default 30
");
exit( -1 );
}
#
# More or less replicates the equivalent PHP function
#
sub strtotime
{
my $date_str = shift;
return( UnixDate( $date_str, '%s' ) );
}
#
# More or less replicates the equivalent PHP function
#
sub str_repeat
{
my $string = shift;
my $count = shift;
return( ${string}x${count} );
}
if ( !GetOptions( 'monitor=i'=>\$monitor, 'event=i'=>\$event_id, 'delay=i'=>\$delay ) )
{
Usage();
}
if ( !$monitor )
{
print( STDERR "Please give a valid monitor id\n" );
Usage();
}
2002-12-24 20:44:11 +08:00
( $monitor ) = ( $monitor =~ /^(\d+)$/ );
my $log_file = sprintf( LOG_FILE, $monitor );
open( LOG, ">>$log_file" ) or die( "Can't open log file: $!" );
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;
2002-11-29 19:01:44 +08:00
chdir( EVENT_PATH );
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 );
2002-11-29 19:01:44 +08:00
my $last_event_id;
if ( !$event_id )
{
my $sql = "select max(Id) as MaxEventId from Events where MonitorId = '$monitor'";
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() );
my $row = $sth->fetchrow_hashref();
$event_id = $row->{MaxEventId};
$sth->finish();
}
print( "Scanning for events since id $event_id\n" );
2003-01-17 18:34:12 +08:00
sleep( START_DELAY );
2002-11-29 19:01:44 +08:00
my $filters = GetFilters( $monitor );
my $last_action = 0;
my $size = 16; # We only need the first 16 bytes really for the last event count
2002-12-24 19:52:23 +08:00
my $key = ZM_SHM_KEY|$monitor;
2002-11-29 19:01:44 +08:00
my $shmid = shmget( $key, $size, 0 ) || die( "Can't get shared memory id: $!" );
while( 1 )
{
shmread( $shmid, $last_event_id, 12, 4 ) || die( "Can't read from shared memory: $!" );
$last_event_id = unpack( "l", $last_event_id );
2003-01-07 20:26:34 +08:00
print( "Last event generated is $last_event_id\n" ) if ( VERBOSE );
2002-11-29 19:01:44 +08:00
if ( $last_event_id > $event_id )
{
# Got new event to check
2003-01-11 08:15:29 +08:00
if ( (time() - $last_action) > ZM_FILTER_RELOAD_DELAY )
2002-11-29 19:01:44 +08:00
{
2003-01-07 20:26:34 +08:00
print( "Reloading filters\n" ) if ( VERBOSE );
2002-11-29 19:01:44 +08:00
$last_action = time();
$filters = GetFilters( $monitor );
foreach my $filter ( @$filters )
{
2003-01-15 21:49:16 +08:00
print( "Checking filter '$filter->{Name}'".
($filter->{AutoDelete}?", delete":"").
($filter->{AutoUpload}?", upload":"").
($filter->{AutoEmail}?", email":"").
($filter->{AutoMessage}?", message":"").
"\n" ) if ( VERBOSE );
2002-11-29 19:01:44 +08:00
my $sql = $filter->{Sql};
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
2003-01-07 18:43:02 +08:00
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() );
}
2002-11-29 19:01:44 +08:00
while( my $event = $sth->fetchrow_hashref() )
{
2003-01-07 20:26:34 +08:00
print( "Checking event $event->{Id}\n" ) if ( VERBOSE );
2003-01-15 21:49:16 +08:00
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 );
}
2003-01-11 08:15:29 +08:00
if ( ZM_OPT_UPLOAD && $filter->{AutoUpload} )
2002-11-29 19:01:44 +08:00
{
2003-01-07 21:08:28 +08:00
next if ( $event->{Uploaded} );
2003-01-15 21:49:16 +08:00
uploadArchFile( $filter, $event );
2002-11-29 19:01:44 +08:00
}
if ( $filter->{AutoDelete} )
{
2003-01-07 20:26:34 +08:00
print( "Deleting event $event->{Id}\n" );
2002-11-29 19:01:44 +08:00
# 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() );
2002-12-10 19:00:17 +08:00
my $res = $sth->execute( $event->{Id} ) or die( "Can't execute: ".$sth->errstr() );
2002-11-29 19:01:44 +08:00
# 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();
}
}
$event_id = $last_event_id;
}
else
{
2003-01-07 20:26:34 +08:00
print( "Sleeping for $delay seconds\n" ) if ( VERBOSE );
2002-11-29 19:01:44 +08:00
sleep( $delay );
}
}
sub GetFilters
{
my $monitor = shift;
my @filters;
2003-01-15 21:49:16 +08:00
my $sql = "select Filters.*,Monitors.Name as MonitorName from Filters inner join Monitors on Filters.MonitorId = Monitors.Id where Monitors.Id = '$monitor' and (AutoDelete = 1 or AutoUpload = 1 or AutoEmail = 1 or AutoMessage = 1) order by Name";
2002-11-29 19:01:44 +08:00
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() );
FILTER: while( my $filter_data = $sth->fetchrow_hashref() )
{
2003-01-07 20:26:34 +08:00
print( "Found filter '$filter_data->{Name}'\n" ) if ( VERBOSE );
2002-11-29 19:01:44 +08:00
my %filter_terms;
foreach my $filter_parm ( split( '&', $filter_data->{Query} ) )
{
my( $key, $value ) = split( '=', $filter_parm, 2 );
if ( $key )
{
$filter_terms{$key} = $value;
}
}
#print( Dumper( %filter_terms ) );
2003-01-15 21:49:16 +08:00
my $sql = "select E.Id, E.Name,E.StartTime,unix_timestamp(E.StartTime) as Time,E.Length,E.Frames,E.AlarmFrames,E.TotScore,E.AvgScore,E.MaxScore,E.Archived,E.Uploaded,E.Emailed,E.Messaged,E.LearnState from Events as E inner join Monitors as M on M.Id = E.MonitorId where M.Id = '$monitor'";
2002-11-29 19:01:44 +08:00
my $filter_sql = '';
for ( my $i = 1; $i <= $filter_terms{trms}; $i++ )
{
my $conjunction_name = "cnj$i";
my $obracket_name = "obr$i";
my $cbracket_name = "cbr$i";
my $attr_name = "attr$i";
my $op_name = "op$i";
my $value_name = "val$i";
if ( $filter_terms{$conjunction_name} )
{
$filter_sql .= " ".$filter_terms{$conjunction_name}." ";
}
if ( $filter_terms{$obracket_name} )
{
$filter_sql .= str_repeat( "(", $filter_terms{$obracket_name} );
}
if ( $filter_terms{$attr_name} )
{
if ( $filter_terms{$attr_name} eq 'DateTime' )
{
my $dt_val = strtotime( $filter_terms{$value_name} );
if ( !$dt_val )
{
printf( STDERR "Error, unable to parse date '$filter_terms{$value_name}', skipping\n" );
next FILTER;
}
$filter_sql .= "E.StartTime ".$filter_terms{$op_name}." from_unixtime( $dt_val )";
2003-01-07 18:43:02 +08:00
# Indicate that this filter uses dates and so should not be dependant on the event id
$filter_data->{has_dates} = !undef;
2002-11-29 19:01:44 +08:00
}
elsif ( $filter_terms{$attr_name} eq 'Date' )
{
my $dt_val = strtotime( $filter_terms{$value_name} );
$filter_sql .= "to_days( E.StartTime ) ".$filter_terms{$op_name}." to_days( from_unixtime( $dt_val ) )";
2003-01-07 18:43:02 +08:00
# Indicate that this filter uses dates and so should not be dependant on the event id
$filter_data->{has_dates} = !undef;
2002-11-29 19:01:44 +08:00
}
elsif ( $filter_terms{$attr_name} eq 'Time' )
{
my $dt_val = strtotime( $filter_terms{$value_name} );
$filter_sql .= "extract( hour_second from E.StartTime ) ".$filter_terms{$op_name}." extract( hour_second from from_unixtime( $dt_val ) )";
}
elsif ( $filter_terms{$attr_name} eq 'Weekday' )
{
my $dt_val = strtotime( $filter_terms{$value_name} );
$filter_sql .= "weekday( E.StartTime ) ".$filter_terms{$op_name}." weekday( from_unixtime( $dt_val ) )";
}
elsif ( $filter_terms{$attr_name} eq 'Archived' )
{
$filter_sql .= "E.Archived = ".$filter_terms{$value_name};
}
else
{
$filter_sql .= "E.".$filter_terms{$attr_name}." ".$filter_terms{$op_name}." ".$filter_terms{$value_name};
}
}
if ( $filter_terms{$cbracket_name} )
{
$filter_sql .= str_repeat( ")", $filter_terms{$cbracket_name} );
}
}
if ( $filter_sql )
{
$sql .= " and ( $filter_sql )";
}
2003-01-07 18:43:02 +08:00
if ( !$filter_data->{has_dates} )
{
$sql .= " and E.Id > ?";
}
2002-12-10 19:00:17 +08:00
$sql .= " order by E.Id";
2002-11-29 19:01:44 +08:00
#print $sql."\n";
$filter_data->{Sql} = $sql;
push( @filters, $filter_data );
}
$sth->finish();
return( \@filters );
}
2003-01-15 21:49:16 +08:00
sub uploadArchFile
{
my $filter = shift;
my $event = shift;
my $arch_file = ZM_UPLOAD_FTP_LOC_DIR.'/'.$filter->{MonitorName}.'-'.$event->{Id};
my $arch_image_path = "$filter->{MonitorName}/$event->{Id}/".(ZM_UPLOAD_ARCH_ANALYSE?'*':'capture*').".jpg";
my $arch_error;
if ( ZM_UPLOAD_ARCH_FORMAT eq "zip" )
{
$arch_file .= '.zip';
my $zip = Archive::Zip->new();
print( "Creating upload file '$arch_file'\n" );
my $status = AZ_OK;
foreach my $image_file ( <*$arch_image_path> )
{
print( "Adding $image_file\n" );
my $member = $zip->addFile( $image_file );
last unless ( $member );
$member->desiredCompressionMethod( ZM_UPLOAD_ARCH_COMPRESS?COMPRESSION_DEFLATED:COMPRESSION_STORED );
}
$status = $zip->writeToFileNamed( $arch_file );
if ( $arch_error = ($status != AZ_OK) )
{
print( "Zip error: $status\n " );
}
}
elsif ( ZM_UPLOAD_ARCH_FORMAT eq "tar" )
{
if ( ZM_UPLOAD_ARCH_COMPRESS )
{
$arch_file .= '.tar.gz';
}
else
{
$arch_file .= '.tar';
}
print( "Creating upload file '$arch_file'\n" );
if ( $arch_error = !Archive::Tar->create_archive( $arch_file, ZM_UPLOAD_ARCH_COMPRESS, <*$arch_image_path> ) )
{
print( "Tar error: ".Archive::Tar->error()."\n " );
}
}
if ( !$arch_error )
{
print( "Uploading to ".ZM_UPLOAD_FTP_HOST."\n" );
my $ftp = Net::FTP->new( ZM_UPLOAD_FTP_HOST, Timeout=>ZM_UPLOAD_FTP_TIMEOUT, Passive=>ZM_UPLOAD_FTP_PASSIVE, Debug=>ZM_UPLOAD_FTP_DEBUG );
if ( !$ftp )
{
warn( "Can't create ftp connection: $@" );
return();
}
$ftp->login( ZM_UPLOAD_FTP_USER, ZM_UPLOAD_FTP_PASS ) or warn( "FTP - Can't login" );
$ftp->binary() or warn( "FTP - Can't go binary" );
$ftp->cwd( ZM_UPLOAD_FTP_REM_DIR ) or warn( "FTP - Can't cwd" );
$ftp->put( $arch_file ) or warn( "FTP - Can't upload '$arch_file'" );
$ftp->quit() or warn( "FTP - Can't quit" );
unlink( $arch_file );
my $sql = "update Events set Uploaded = 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() );
}
}
sub sendEmail
{
my $filter = shift;
my $event = shift;
if ( !ZM_FROM_EMAIL )
{
warn( "No 'from' email address defined, not sending email" );
return;
}
if ( !ZM_EMAIL_ADDRESS )
{
warn( "No email address defined, not sending email" );
return;
}
print( "Creating notification email\n" );
my $subject = substituteTags( $email_subject, $filter, $event );
my $body = substituteTags( $email_body, $filter, $event );
print( "Sending notification email '$subject'\n" );
print( "$body\n" ) if ( VERBOSE );
eval
{
my $mailer = new Mail::Mailer;
$mailer->open( {
Subject=>$subject,
From=>ZM_FROM_EMAIL,
To=>ZM_EMAIL_ADDRESS
} );
print( $mailer $body );
$mailer->close();
};
if ( $@ )
{
warn( "Can't send email: $@" );
return();
}
my $sql = "update Events set Emailed = 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() );
}
sub sendMessage
{
my $filter = shift;
my $event = shift;
if ( !ZM_FROM_EMAIL )
{
warn( "No 'from' email address defined, not sending message" );
return;
}
if ( !ZM_MESSAGE_ADDRESS )
{
warn( "No message address defined, not sending message" );
return;
}
print( "Creating notification email\n" );
my $subject = substituteTags( $message_subject, $filter, $event );
my $body = substituteTags( $message_body, $filter, $event );
print( "Sending notification message '$subject'\n" );
print( "$body\n" ) if ( VERBOSE );
eval
{
my $mailer = new Mail::Mailer;
$mailer->open( {
Subject=>$subject,
From=>ZM_FROM_EMAIL,
To=>ZM_MESSAGE_ADDRESS
} );
print( $mailer $body );
$mailer->close();
};
if ( $@ )
{
warn( "Can't send email: $@" );
return();
}
my $sql = "update Events set Messaged = 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() );
}
sub substituteTags
{
my $text = shift;
my $filter = shift;
my $event = shift;
# First we'd better check what we need to get
# We have a filter and an event, do we need any more
# monitor information?
my $need_monitor = $text =~ /%(?:MET|MEH|MED|MEW|MEN|MEA)%/;
my $monitor = {};
if ( $need_monitor )
{
my $db_now = strftime( "%Y-%m-%d %H:%M:%S", localtime() );
my $sql = "select M.*, count(E.Id) as EventCount, count(if(E.Archived,1,NULL)) as ArchEventCount, count(if(E.StartTime>'$db_now' - INTERVAL 1 HOUR && E.Archived = 0,1,NULL)) as HourEventCount, count(if(E.StartTime>'$db_now' - INTERVAL 1 DAY && E.Archived = 0,1,NULL)) as DayEventCount, count(if(E.StartTime>'$db_now' - INTERVAL 7 DAY && E.Archived = 0,1,NULL)) as WeekEventCount, count(if(E.StartTime>'$db_now' - INTERVAL 1 MONTH && E.Archived = 0,1,NULL)) as MonthEventCount from Monitors as M left join Events as E on E.MonitorId = M.Id where MonitorId = ? group by E.MonitorId order by Id";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( $filter->{MonitorId} ) or die( "Can't execute: ".$sth->errstr() );
$monitor = $sth->fetchrow_hashref();
return() if ( !$monitor );
}
# Do we need the image information too?
my $need_images = $text =~ /%(?:EPI1|EPIM)%/;
my @frames = ();
my $first_alarm_frame;
my $max_alarm_frame;
my $max_alarm_score = 0;
if ( $need_images )
{
my $sql = "select * from Frames where EventId = ? order by FrameId";
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() );
while( my $frame = $sth->fetchrow_hashref() )
{
push( @frames, $frame );
if ( $frame->{AlarmFrame} )
{
if ( !$first_alarm_frame )
{
$first_alarm_frame = $frame;
}
if ( $frame->{Score} > $max_alarm_score )
{
$max_alarm_frame = $frame;
$max_alarm_score = $frame->{Score};
}
}
}
}
my $url = ZM_URL;
$text =~ s/%ZP%/$url/g;
$text =~ s/%MN%/$filter->{MonitorName}/g;
$text =~ s/%MET%/$monitor->{EventCount}/g;
$text =~ s/%MEH%/$monitor->{HourEventCount}/g;
$text =~ s/%MED%/$monitor->{DayEventCount}/g;
$text =~ s/%MEW%/$monitor->{WeekEventCount}/g;
$text =~ s/%MEM%/$monitor->{MonthEventCount}/g;
$text =~ s/%MEA%/$monitor->{ArchEventCount}/g;
2003-01-16 00:00:26 +08:00
$text =~ s/%MP%/$url?view=watch&mid=$filter->{MonitorId}/g;
2003-01-15 21:49:16 +08:00
$text =~ s/%MPS%/$url?view=watchfeed&mid=$filter->{MonitorId}&mode=stream/g;
$text =~ s/%MPI%/$url?view=watchfeed&mid=$filter->{MonitorId}&mode=still/g;
2003-01-16 00:00:26 +08:00
$text =~ s/%EP%/$url?view=event&mid=$filter->{MonitorId}&eid=$event->{Id}/g;
2003-01-15 21:49:16 +08:00
$text =~ s/%EPS%/$url?view=event&mode=stream&mid=$filter->{MonitorId}&eid=$event->{Id}/g;
$text =~ s/%EPI%/$url?view=event&mode=still&mid=$filter->{MonitorId}&eid=$event->{Id}/g;
$text =~ s/%EI%/$event->{Id}/g;
$text =~ s/%EN%/$event->{Name}/g;
$text =~ s/%ET%/$event->{StartTime}/g;
$text =~ s/%ED%/$event->{Length}/g;
$text =~ s/%EF%/$event->{Frames}/g;
$text =~ s/%EFA%/$event->{AlarmFrames}/g;
$text =~ s/%EST%/$event->{TotScore}/g;
$text =~ s/%ESA%/$event->{AvgScore}/g;
$text =~ s/%ESM%/$event->{MaxScore}/g;
$text =~ s/%EPI1%/$url?view=image&mid=$filter->{MonitorId}&eid=$event->{Id}&fid=$first_alarm_frame->{FrameId}/g;
$text =~ s/%EPIM%/$url?view=image&mid=$filter->{MonitorId}&eid=$event->{Id}&fid=$max_alarm_frame->{FrameId}/g;
$text =~ s/%FN%/$filter->{Name}/g;
( my $filter_name = $filter->{Name} ) =~ s/ /+/g;
$text =~ s/%FP%/$url?view=filter&mid=$filter->{MonitorId}&filter_name=$filter_name/g;
return( $text );
}