diff --git a/scripts/zmfilter.pl b/scripts/zmfilter.pl index 0b7833fde..da8961ded 100644 --- a/scripts/zmfilter.pl +++ b/scripts/zmfilter.pl @@ -213,8 +213,8 @@ sub getFilters { my @filters; my $sql = "select * from Filters where (AutoArchive = 1 or AutoVideo = 1 or AutoUpload = 1 or AutoEmail = 1 or AutoMessage = 1 or AutoExecute = 1 or AutoDelete = 1) order by Name"; - 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 $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute() or Fatal( "Can't execute '$sql': ".$sth->errstr() ); FILTER: while( my $filter_data = $sth->fetchrow_hashref() ) { Debug( "Found filter '$filter_data->{Name}'\n" ); @@ -509,8 +509,13 @@ sub checkFilter $sql =~ s/zmDiskBlocks/$disk_blocks/g; } - 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 $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute(); + if ( !$res ) + { + Error( "Can't execute filter '$sql', ignoring: ".$sth->errstr() ); + return; + } while( my $event = $sth->fetchrow_hashref() ) { @@ -521,8 +526,8 @@ sub checkFilter Info( "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 '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); } if ( ZM_OPT_MPEG ne "no" && $filter->{AutoVideo} ) { @@ -566,18 +571,18 @@ sub checkFilter Info( "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 '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); if ( !ZM_OPT_FAST_DELETE ) { my $sql = "delete from Frames where EventId = ?"; - 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() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); $sql = "delete from Stats where EventId = ?"; - $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() ); - $res = $sth->execute( $event->{Id} ) or die( "Can't execute '$sql': ".$sth->errstr() ); + $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); my $command = "rm -rf ".ZM_DIR_EVENTS."/*/".sprintf( "%d", $event->{Id} ); my $output = qx($command); @@ -638,10 +643,10 @@ sub generateVideo my $command = ZM_PATH_BIN."/zmvideo.pl -e ".$event->{Id}." -r ".$rate." -s ".$scale." -f ".$format; my $output = qx($command); + chomp( $output ); my $status = $? >> 8; if ( $status || DBG_LEVEL > 0 ) { - chomp( $output ); Debug( "Output: $output\n" ); } if ( $status ) @@ -729,8 +734,8 @@ sub uploadArchFile $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 '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); } return( 1 ); } @@ -752,8 +757,8 @@ sub substituteTags { my $db_now = strftime( "%Y-%m-%d %H:%M:%S", localtime() ); my $sql = "select M.Id, 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( $event->{MonitorId} ) or die( "Can't execute '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{MonitorId} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); $monitor = $sth->fetchrow_hashref(); $sth->finish(); return() if ( !$monitor ); @@ -767,8 +772,8 @@ sub substituteTags if ( $need_images ) { my $sql = "select * from Frames where EventId = ? and Type = 'Alarm' 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 '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); while( my $frame = $sth->fetchrow_hashref() ) { if ( !$first_alarm_frame ) @@ -943,8 +948,8 @@ sub sendEmail Info( "Notification email sent\n" ); } 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 '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); return( 1 ); } @@ -1037,8 +1042,8 @@ sub sendMessage Info( "Notification message sent\n" ); } 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 '$sql': ".$sth->errstr() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); return( 1 ); } @@ -1069,8 +1074,8 @@ sub executeCommand 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() ); + my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute( $event->{Id} ) or Fatal( "Can't execute '$sql': ".$sth->errstr() ); } return( 1 ); } diff --git a/scripts/zmvideo.pl b/scripts/zmvideo.pl index 57f9da501..b073af8dd 100644 --- a/scripts/zmvideo.pl +++ b/scripts/zmvideo.pl @@ -161,11 +161,12 @@ my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM my @filters; my $sql = "select max(F.Delta)-min(F.Delta) as FullLength, E.*, M.Name as MonitorName, M.Width as MonitorWidth, M.Height as MonitorHeight, M.Palette from Frames as F inner join Events as E on F.EventId = E.Id inner join Monitors as M on E.MonitorId = M.Id where EventId = '$event_id' group by F.EventId"; -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 $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); +my $res = $sth->execute() or Fatal( "Can't execute: ".$sth->errstr() ); my $event = $sth->fetchrow_hashref(); $sth->finish(); -chdir( ZM_PATH_WEB.'/'.ZM_DIR_EVENTS.'/'.$event->{MonitorId}.'/'.$event->{Id} ); +my $event_path = ZM_PATH_WEB.'/'.ZM_DIR_EVENTS.'/'.$event->{MonitorId}.'/'.$event->{Id}; +chdir( $event_path ); ( my $video_name = $event->{Name} ) =~ s/\s/_/g; my @file_parts; @@ -211,7 +212,7 @@ if ( $overwrite || !-s $video_file ) if ( ZM_OPT_MPEG eq "mpeg_encode" ) { my $param_file = "$video_name.mpe"; - open( PARAMS, ">$param_file" ) or die( "Can't open '$param_file': $!" ); + open( PARAMS, ">$param_file" ) or Fatal( "Can't open '$param_file': $!" ); print( PARAMS "PATTERN IBBPBBPBBPBBPBB\n" ); print( PARAMS "FORCE_ENCODE_LAST_FRAME\n" ); @@ -279,6 +280,11 @@ if ( $overwrite || !-s $video_file ) { Info( $output."\n" ); } + my $status = $? >> 8; + if ( $status ) + { + Fatal( "Error: $status" ); + } } elsif ( ZM_OPT_MPEG eq "ffmpeg" ) { @@ -314,24 +320,22 @@ if ( $overwrite || !-s $video_file ) } - my $command = ZM_PATH_FFMPEG." -y -r $frame_rate ".ZM_FFMPEG_INPUT_OPTIONS." -i %0".ZM_EVENT_IMAGE_DIGITS."d-capture.jpg -s $video_size ".ZM_FFMPEG_OUTPUT_OPTIONS." '$video_file' > ffmpeg.log"; + my $command = ZM_PATH_FFMPEG." -y -r $frame_rate ".ZM_FFMPEG_INPUT_OPTIONS." -i %0".ZM_EVENT_IMAGE_DIGITS."d-capture.jpg -s $video_size ".ZM_FFMPEG_OUTPUT_OPTIONS." '$video_file' >& ffmpeg.log"; Info( $command."\n" ); - if ( my $output = qx($command) ) + my $output = qx($command); + + my $status = $? >> 8; + if ( $status ) { - Info( $output."\n" ); + Error( "Unable to generate video, check ".$event_path."/ffmpeg.log for details" ); + exit( -1 ); } } else { - die( "Bogus mpeg option ".ZM_OPT_MPEG."\n" ); + Fatal( "Bogus mpeg option ".ZM_OPT_MPEG."\n" ); } - my $status = $? >> 8; - if ( $status ) - { - die( "Error: $status" ); - } - Info( "Finished $video_file\n" ); } else