2005-12-16 18:05:29 +08:00
|
|
|
#!/usr/bin/perl -wT
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# ZoneMinder WatchDog Script, $Date$, $Revision$
|
2008-07-25 17:48:16 +08:00
|
|
|
# Copyright (C) 2001-2008 Philip Coombes
|
2005-12-16 18:05:29 +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.
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# This does some basic setup for ZoneMinder to run and then periodically
|
|
|
|
# checks the fps output of the active daemons to check they haven't
|
|
|
|
# locked up. If they have then they are killed and restarted
|
|
|
|
#
|
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# These are the elements you can edit to suit your installation
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2005-12-16 21:16:37 +08:00
|
|
|
use constant DBG_ID => "zmwatch"; # Tag that appears in debug to identify source
|
2005-12-16 20:17:10 +08:00
|
|
|
use constant DBG_LEVEL => 0; # 0 is errors, warnings and info only, > 0 for debug
|
|
|
|
|
2005-12-16 21:16:37 +08:00
|
|
|
use constant START_DELAY => 30; # To give everything else time to start
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Don't change anything below here
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2009-06-08 17:11:56 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2005-12-16 18:05:29 +08:00
|
|
|
use ZoneMinder;
|
|
|
|
use POSIX;
|
|
|
|
use DBI;
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
|
|
|
$ENV{PATH} = '/bin:/usr/bin';
|
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
|
|
|
sub Usage
|
|
|
|
{
|
|
|
|
print( "
|
|
|
|
Usage: zmwatch.pl
|
|
|
|
");
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
2006-01-12 07:57:24 +08:00
|
|
|
zmDbgInit( DBG_ID, level=>DBG_LEVEL );
|
2006-07-04 18:34:21 +08:00
|
|
|
zmDbgSetSignal();
|
2005-12-21 01:03:33 +08:00
|
|
|
|
2005-12-16 21:36:24 +08:00
|
|
|
Info( "Watchdog starting\n" );
|
2005-12-16 20:17:10 +08:00
|
|
|
Info( "Watchdog pausing for ".START_DELAY." seconds\n" );
|
2005-12-16 18:05:29 +08:00
|
|
|
sleep( START_DELAY );
|
|
|
|
|
2007-09-07 23:39:44 +08:00
|
|
|
my $dbh = zmDbConnect();
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
my $sql = "select * from Monitors";
|
2006-07-04 18:31:14 +08:00
|
|
|
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
my $now = time();
|
2006-07-04 18:31:14 +08:00
|
|
|
my $res = $sth->execute() or Fatal( "Can't execute: ".$sth->errstr() );
|
2005-12-16 18:05:29 +08:00
|
|
|
while( my $monitor = $sth->fetchrow_hashref() )
|
|
|
|
{
|
|
|
|
if ( $monitor->{Function} ne 'None' )
|
|
|
|
{
|
2008-07-21 22:45:48 +08:00
|
|
|
my $restart = 0;
|
2008-07-14 23:21:16 +08:00
|
|
|
if ( zmMemVerify( $monitor ) && zmMemRead( $monitor, "shared_data:valid" ) )
|
2006-01-12 07:57:24 +08:00
|
|
|
{
|
|
|
|
# Check we have got an image recently
|
2008-07-21 22:45:48 +08:00
|
|
|
my $image_time = zmGetLastWriteTime( $monitor );
|
2008-07-14 23:21:16 +08:00
|
|
|
next if ( !defined($image_time) ); # Can't read from shared data
|
2006-01-12 07:57:24 +08:00
|
|
|
next if ( !$image_time ); # We can't get the last capture time so can't be sure it's died.
|
|
|
|
|
2006-10-18 23:08:07 +08:00
|
|
|
my $max_image_delay = ($monitor->{MaxFPS}&&($monitor->{MaxFPS}>0)&&($monitor->{MaxFPS}<1))?(3/$monitor->{MaxFPS}):ZM_WATCH_MAX_DELAY;
|
2006-01-12 07:57:24 +08:00
|
|
|
my $image_delay = $now-$image_time;
|
|
|
|
Debug( "Monitor $monitor->{Id} last captured $image_delay seconds ago, max is $max_image_delay\n" );
|
2008-07-21 22:45:48 +08:00
|
|
|
if ( $image_delay > $max_image_delay )
|
2006-01-12 07:57:24 +08:00
|
|
|
{
|
2008-07-21 22:45:48 +08:00
|
|
|
Info( "Restarting capture daemon for ".$monitor->{Name}.", time since last capture $image_delay seconds ($now-$image_time)\n" );
|
|
|
|
$restart = 1;
|
|
|
|
}
|
2006-01-12 07:57:24 +08:00
|
|
|
}
|
|
|
|
else
|
2005-12-16 18:05:29 +08:00
|
|
|
{
|
2008-10-28 20:52:39 +08:00
|
|
|
#Info( "Restarting capture daemon for ".$monitor->{Name}.", shared data not valid\n" );
|
|
|
|
#$restart = 1;
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2008-07-21 22:45:48 +08:00
|
|
|
if ( $restart )
|
|
|
|
{
|
|
|
|
my $command;
|
|
|
|
if ( $monitor->{Type} eq 'Local' )
|
|
|
|
{
|
2011-04-19 17:32:50 +08:00
|
|
|
$command = "zmdc.pl restart zmc -d $monitor->{Device}";
|
2008-07-21 22:45:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-19 17:32:50 +08:00
|
|
|
$command = "zmdc.pl restart zmc -m $monitor->{Id}";
|
2008-07-21 22:45:48 +08:00
|
|
|
}
|
2011-04-19 17:32:50 +08:00
|
|
|
runCommand( $command );
|
2008-07-21 22:45:48 +08:00
|
|
|
}
|
|
|
|
elsif ( $monitor->{Function} ne 'Monitor' )
|
|
|
|
{
|
|
|
|
if ( zmMemVerify( $monitor ) && zmMemRead( $monitor, "shared_data:valid" ) )
|
|
|
|
{
|
|
|
|
# Check we have got an image recently
|
|
|
|
my $image_time = zmGetLastReadTime( $monitor );
|
|
|
|
next if ( !defined($image_time) ); # Can't read from shared data
|
|
|
|
next if ( !$image_time ); # We can't get the last capture time so can't be sure it's died.
|
|
|
|
|
|
|
|
my $max_image_delay = ($monitor->{MaxFPS}&&($monitor->{MaxFPS}>0)&&($monitor->{MaxFPS}<1))?(3/$monitor->{MaxFPS}):ZM_WATCH_MAX_DELAY;
|
|
|
|
my $image_delay = $now-$image_time;
|
|
|
|
Debug( "Monitor $monitor->{Id} last analysed $image_delay seconds ago, max is $max_image_delay\n" );
|
|
|
|
if ( $image_delay > $max_image_delay )
|
|
|
|
{
|
|
|
|
Info( "Restarting analysis daemon for ".$monitor->{Name}.", time since last analysis $image_delay seconds ($now-$image_time)\n" );
|
2011-04-19 17:32:50 +08:00
|
|
|
my $command = "zmdc.pl restart zma -m ".$monitor->{Id};
|
|
|
|
runCommand( $command );
|
2008-07-21 22:45:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 16:27:46 +08:00
|
|
|
# Prevent open handles building up if we have connect to shared memory
|
|
|
|
zmMemInvalidate( $monitor );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
sleep( ZM_WATCH_CHECK_INTERVAL );
|
|
|
|
}
|
2005-12-16 21:36:24 +08:00
|
|
|
Info( "Watchdog exiting\n" );
|
2005-12-16 18:05:29 +08:00
|
|
|
exit();
|