zoneminder/scripts/zmwatch.pl.z

118 lines
4.0 KiB
Perl
Executable File

#!/usr/bin/perl -wT
#
# ==========================================================================
#
# Zone Minder WatchDog Script, $Date$, $Revision$
# Copyright (C) 2003 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 ZM_PATH_BIN => "<from zmconfig>";
use constant ZM_PATH_LOGS => "<from zmconfig>";
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_WATCH_CHECK_INTERVAL => <from zmconfig>;
use constant ZM_WATCH_MAX_DELAY => <from zmconfig>;
use constant WATCH_LOG_FILE => ZM_PATH_LOGS.'/zmwatch.log';
use constant START_DELAY => 30; # To give everything else time to start
use constant VERBOSE => 0; # Whether to output more verbose debug
# ==========================================================================
#
# Don't change anything below here
#
# ==========================================================================
use strict;
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 );
}
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" );
print( "Watchdog pausing for ".START_DELAY." seconds\n" );
sleep( START_DELAY );
my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERA, ZM_DB_PASSA );
my $sql = "select * from Monitors";
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
while( 1 )
{
my $now = time();
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
while( my $monitor = $sth->fetchrow_hashref() )
{
if ( $monitor->{Function} ne 'None' )
{
# Check we have got an image recently
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);
my $image_delay = $now-$image_time;
print( "Monitor $monitor->{Id} last captured $image_delay seconds ago\n" ) if ( VERBOSE );
if ( $image_delay <= ZM_WATCH_MAX_DELAY )
{
# Yes, so continue
next;
}
# If we are here then something bad has happened
$command = ZM_PATH_BIN."/zmdc.pl restart zmc -d ".$monitor->{Device};
print( "Restarting capture daemon ('$command')\n" );
print( qx( $command ) );
}
}
sleep( ZM_WATCH_CHECK_INTERVAL );
}
print( "Watchdog exiting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
exit();