2003-01-06 22:55:50 +08:00
|
|
|
#!/usr/bin/perl -wT
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Zone Minder WatchDog Script, $Date$, $Revision$
|
|
|
|
# Copyright (C) 2002 Philip Coombes
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# These are the elements you need to edit to suit your installation
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
|
|
|
use constant DB_NAME => "zm";
|
|
|
|
use constant DB_USER => "zmadmin";
|
|
|
|
use constant DB_PASS => "zmadminzm";
|
|
|
|
use constant COMMAND_PATH => '@prefix@/bin/';
|
|
|
|
use constant WATCH_LOG_FILE => '/tmp/zmwatch.log';
|
2003-01-07 18:44:25 +08:00
|
|
|
use constant CHECK_INTERVAL => 10; # How often to check the FPS
|
|
|
|
use constant NUM_BAD_CHECKS => 3; # How many bad checks before we restart
|
|
|
|
use constant MIN_FPS => 0.5; # FPS at or below this are bad
|
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 strict;
|
|
|
|
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" );
|
|
|
|
|
|
|
|
my $dbh = DBI->connect( "DBI:mysql:".DB_NAME, DB_USER, DB_PASS );
|
|
|
|
my $sql = "select * from Monitors";
|
|
|
|
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
|
2003-01-07 18:44:25 +08:00
|
|
|
my %fps_hash;
|
2003-01-06 22:55:50 +08:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
|
|
|
|
while( my $monitor = $sth->fetchrow_hashref() )
|
|
|
|
{
|
|
|
|
if ( $monitor->{Function} ne 'None' )
|
|
|
|
{
|
2003-01-07 18:44:25 +08:00
|
|
|
my $fps_list = $fps_hash{$monitor->{Id}};
|
|
|
|
if ( !$fps_list )
|
2003-01-06 22:55:50 +08:00
|
|
|
{
|
2003-01-07 18:44:25 +08:00
|
|
|
$fps_list = $fps_hash{$monitor->{Id}} = [];
|
2003-01-06 22:55:50 +08:00
|
|
|
}
|
2003-01-07 18:44:25 +08:00
|
|
|
# Check we are getting a decent FPS, if not then kill it
|
2003-01-06 22:55:50 +08:00
|
|
|
my $command = COMMAND_PATH."zmu -m ".$monitor->{Id}." -f";
|
2003-01-07 20:26:34 +08:00
|
|
|
print( "Getting FPS for monitor $monitor->{Id} ('$command')\n" ) if ( VERBOSE );
|
2003-01-07 18:44:25 +08:00
|
|
|
my $fps = qx( $command );
|
|
|
|
chomp($fps);
|
2003-01-07 20:26:34 +08:00
|
|
|
print( "Monitor $monitor->{Id} capturing at $fps FPS\n" ) if ( VERBOSE );
|
2003-01-07 18:44:25 +08:00
|
|
|
push( @$fps_list, $fps );
|
|
|
|
if ( @$fps_list > NUM_BAD_CHECKS )
|
|
|
|
{
|
|
|
|
pop( @$fps_list );
|
2003-01-07 19:32:12 +08:00
|
|
|
# See if we can find any good FPS readings
|
|
|
|
my @good_fps = grep { $_ > MIN_FPS } @$fps_list;
|
|
|
|
if ( @good_fps < NUM_BAD_CHECKS )
|
|
|
|
{
|
|
|
|
print( "Monitor $monitor->{Id} has ".(NUM_BAD_CHECKS-int(@good_fps))." samples below ".MIN_FPS." FPS\n" );
|
|
|
|
}
|
|
|
|
if ( @good_fps )
|
|
|
|
{
|
|
|
|
# Yes, so continue
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
# If we are here then something bad has happened
|
|
|
|
$command = COMMAND_PATH."zmdc.pl restart zmc -d ".$monitor->{Device};
|
|
|
|
print( "Restarting capture daemon ('$command')\n" );
|
|
|
|
print( qx( $command ) );
|
2003-01-07 18:44:25 +08:00
|
|
|
}
|
2003-01-06 22:55:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep( CHECK_INTERVAL );
|
|
|
|
}
|
|
|
|
print( "Watchdog exiting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
|
|
|
exit();
|