2003-01-06 22:55:50 +08:00
|
|
|
#!/usr/bin/perl -wT
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
2003-04-14 01:29:12 +08:00
|
|
|
# ZoneMinder WatchDog Script, $Date$, $Revision$
|
2005-02-24 22:40:14 +08:00
|
|
|
# Copyright (C) 2003, 2004, 2005 Philip Coombes
|
2003-01-06 22:55:50 +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
|
|
|
|
#
|
2005-02-22 05:51:02 +08:00
|
|
|
use strict;
|
|
|
|
use bytes;
|
2003-01-06 22:55:50 +08:00
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# These are the elements you need to edit to suit your installation
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2004-12-29 00:46:48 +08:00
|
|
|
use constant ZM_CONFIG => "<from zmconfig>";
|
2003-01-15 23:06:15 +08:00
|
|
|
use constant ZM_PATH_BIN => "<from zmconfig>";
|
2003-07-04 20:31:36 +08:00
|
|
|
|
|
|
|
# Load the config from the database into the symbol table
|
|
|
|
BEGIN
|
|
|
|
{
|
2004-12-29 03:11:08 +08:00
|
|
|
no strict 'refs';
|
|
|
|
|
2004-12-29 00:46:48 +08:00
|
|
|
open( CONFIG, "<".ZM_CONFIG ) or die( "Can't open config file: $!" );
|
|
|
|
foreach my $str ( <CONFIG> )
|
|
|
|
{
|
2004-12-29 01:01:25 +08:00
|
|
|
next if ( $str =~ /^\s*$/ );
|
|
|
|
next if ( $str =~ /^\s*#/ );
|
2005-02-22 05:52:14 +08:00
|
|
|
my ( $name, $value ) = $str =~ /^\s*([^=\\s]+)\s*=\s*(\S+)\s*$/;
|
2004-12-29 00:46:48 +08:00
|
|
|
$name =~ tr/a-z/A-Z/;
|
|
|
|
if (( $name eq 'ZM_DB_SERVER' ) ||
|
|
|
|
( $name eq 'ZM_DB_NAME' ) ||
|
2004-12-29 01:01:25 +08:00
|
|
|
( $name eq 'ZM_DB_USER' ) ||
|
|
|
|
( $name eq 'ZM_DB_PASS' ))
|
2004-12-29 00:46:48 +08:00
|
|
|
{
|
|
|
|
*{$name} = sub { $value };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close( CONFIG );
|
|
|
|
|
2003-07-04 20:31:36 +08:00
|
|
|
use DBI;
|
2004-12-29 01:01:25 +08:00
|
|
|
my $dbh = DBI->connect( "DBI:mysql:database=".&ZM_DB_NAME.";host=".&ZM_DB_SERVER, &ZM_DB_USER, &ZM_DB_PASS );
|
2003-07-04 20:31:36 +08:00
|
|
|
my $sql = "select * from Config";
|
|
|
|
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() );
|
|
|
|
while( my $config = $sth->fetchrow_hashref() )
|
|
|
|
{
|
|
|
|
*{$config->{Name}} = sub { $config->{Value} };
|
|
|
|
}
|
|
|
|
$sth->finish();
|
|
|
|
$dbh->disconnect();
|
|
|
|
}
|
2003-01-11 08:15:29 +08:00
|
|
|
|
|
|
|
use constant WATCH_LOG_FILE => ZM_PATH_LOGS.'/zmwatch.log';
|
2003-01-15 23:06:15 +08:00
|
|
|
use constant START_DELAY => 30; # To give everything else time to start
|
2003-01-07 20:26:34 +08:00
|
|
|
use constant VERBOSE => 0; # Whether to output more verbose debug
|
2003-01-06 22:55:50 +08:00
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Don't change anything below here
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
|
|
|
use POSIX;
|
|
|
|
use DBI;
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
2003-01-07 19:32:12 +08:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin';
|
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
2003-01-06 22:55:50 +08:00
|
|
|
sub Usage
|
|
|
|
{
|
|
|
|
print( "
|
|
|
|
Usage: zmwatch.pl
|
|
|
|
");
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
open( LOG, '>>'.WATCH_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;
|
|
|
|
print( "Watchdog starting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
2003-01-13 19:49:40 +08:00
|
|
|
print( "Watchdog pausing for ".START_DELAY." seconds\n" );
|
|
|
|
sleep( START_DELAY );
|
2003-01-06 22:55:50 +08:00
|
|
|
|
2004-12-29 01:01:25 +08:00
|
|
|
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USER, ZM_DB_PASS );
|
2003-01-11 06:20:23 +08:00
|
|
|
|
2003-01-06 22:55:50 +08:00
|
|
|
my $sql = "select * from Monitors";
|
|
|
|
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
2003-01-13 19:49:40 +08:00
|
|
|
my $now = time();
|
2003-01-06 22:55:50 +08:00
|
|
|
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
|
2003-07-06 04:39:36 +08:00
|
|
|
my $shm_size = 24; # We only need the first 24 bytes really for the last event time
|
2003-01-06 22:55:50 +08:00
|
|
|
while( my $monitor = $sth->fetchrow_hashref() )
|
|
|
|
{
|
|
|
|
if ( $monitor->{Function} ne 'None' )
|
|
|
|
{
|
2003-01-13 19:49:40 +08:00
|
|
|
# Check we have got an image recently
|
2003-07-06 04:39:36 +08:00
|
|
|
$monitor->{ShmKey} = hex(ZM_SHM_KEY)|$monitor->{Id};
|
2003-11-05 22:53:11 +08:00
|
|
|
$monitor->{ShmId} = shmget( $monitor->{ShmKey}, $shm_size, 0 );
|
|
|
|
if ( !defined($monitor->{ShmId}) )
|
2003-07-06 04:39:36 +08:00
|
|
|
{
|
2004-01-30 17:56:34 +08:00
|
|
|
print( "Can't get shared memory id '$monitor->{ShmKey}': $!\n" );
|
2003-07-06 04:39:36 +08:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
my $image_time;
|
|
|
|
if ( !shmread( $monitor->{ShmId}, $image_time, 20, 4 ) )
|
|
|
|
{
|
2004-01-28 00:58:59 +08:00
|
|
|
print( "Can't read from shared memory '$monitor->{ShmKey}/$monitor->{ShmId}': $!\n" );
|
2003-07-06 04:39:36 +08:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
$image_time = unpack( "l", $image_time );
|
|
|
|
|
|
|
|
#my $command = ZM_PATH_BIN."/zmu -m ".$monitor->{Id}." -t";
|
|
|
|
#print( "Getting last image time for monitor $monitor->{Id} ('$command')\n" ) if ( VERBOSE );
|
|
|
|
#my $image_time = qx( $command );
|
|
|
|
#chomp($image_time);
|
2003-06-12 22:24:38 +08:00
|
|
|
|
|
|
|
if ( !$image_time )
|
|
|
|
{
|
|
|
|
# We can't get the last capture time so can't be sure it's died.
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
2005-01-16 01:26:07 +08:00
|
|
|
my $max_image_delay = (($monitor->{MaxFPS}>0)&&($monitor->{MaxFPS}<1))?(3/$monitor->{MaxFPS}):ZM_WATCH_MAX_DELAY;
|
2003-01-13 19:49:40 +08:00
|
|
|
my $image_delay = $now-$image_time;
|
2003-09-23 17:50:25 +08:00
|
|
|
print( "Monitor $monitor->{Id} last captured $image_delay seconds ago, max is $max_image_delay\n" ) if ( VERBOSE );
|
|
|
|
if ( $image_delay <= $max_image_delay )
|
2003-01-06 22:55:50 +08:00
|
|
|
{
|
2003-01-13 19:49:40 +08:00
|
|
|
# Yes, so continue
|
|
|
|
next;
|
2003-01-07 18:44:25 +08:00
|
|
|
}
|
2003-06-12 22:24:38 +08:00
|
|
|
|
2003-07-06 04:39:36 +08:00
|
|
|
my $command;
|
2003-01-13 19:49:40 +08:00
|
|
|
# If we are here then something bad has happened
|
2003-03-26 19:41:22 +08:00
|
|
|
if ( $monitor->{Type} eq 'Local' )
|
|
|
|
{
|
|
|
|
$command = ZM_PATH_BIN."/zmdc.pl restart zmc -d $monitor->{Device}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-02-16 03:17:38 +08:00
|
|
|
$command = ZM_PATH_BIN."/zmdc.pl restart zmc -m $monitor->{Id}";
|
2003-03-26 19:41:22 +08:00
|
|
|
}
|
2003-07-06 04:39:36 +08:00
|
|
|
print( "Restarting capture daemon ('$command'), time since last capture $image_delay seconds ($now-$image_time)\n" );
|
2003-01-13 19:49:40 +08:00
|
|
|
print( qx( $command ) );
|
2003-01-06 22:55:50 +08:00
|
|
|
}
|
|
|
|
}
|
2003-01-11 08:15:29 +08:00
|
|
|
sleep( ZM_WATCH_CHECK_INTERVAL );
|
2003-01-06 22:55:50 +08:00
|
|
|
}
|
|
|
|
print( "Watchdog exiting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
|
|
|
exit();
|