zoneminder/scripts/zmwatch.pl.in

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

208 lines
6.8 KiB
Perl
Raw Permalink Normal View History

#!@PERL_EXECUTABLE@ -wT
#
# ==========================================================================
#
# ZoneMinder WatchDog Script, $Date$, $Revision$
# Copyright (C) 2001-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# ==========================================================================
=head1 NAME
zmwatch.pl - ZoneMinder WatchDog Script
=head1 SYNOPSIS
zmwatch.pl
=head1 DESCRIPTION
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
=cut
use strict;
use bytes;
# ==========================================================================
#
# These are the elements you can edit to suit your installation
#
# ==========================================================================
use constant START_DELAY => 30; # To give everything else time to start
# ==========================================================================
#
# Don't change anything below here
#
# ==========================================================================
@EXTRA_PERL_LIB@
use ZoneMinder;
2017-11-15 00:55:00 +08:00
use ZoneMinder::Storage;
use POSIX;
use DBI;
2015-04-16 13:42:56 +08:00
use autouse 'Data::Dumper'=>qw(Dumper);
$| = 1;
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
logInit();
logSetSignal();
my $zm_terminate = 0;
sub TermHandler {
Info('Received TERM, exiting');
$zm_terminate = 1;
}
$SIG{TERM} = \&TermHandler;
$SIG{INT} = \&TermHandler;
Info('Watchdog starting, pausing for '.START_DELAY.' seconds');
sleep(START_DELAY);
my $dbh = zmDbConnect();
2015-07-16 04:24:01 +08:00
my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'SELECT * FROM Monitors';
my $sth = $dbh->prepare_cached($sql)
or Fatal("Can't prepare '$sql': ".$dbh->errstr());
while (!$zm_terminate) {
2018-09-08 04:29:23 +08:00
while ( ! ( $dbh and $dbh->ping() ) ) {
if ( ! ( $dbh = zmDbConnect() ) ) {
sleep($Config{ZM_WATCH_CHECK_INTERVAL});
}
}
2016-08-22 23:41:26 +08:00
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
or Fatal('Can\'t execute: '.$sth->errstr());
2016-08-22 23:41:26 +08:00
while( my $monitor = $sth->fetchrow_hashref() ) {
next if $monitor->{Function} eq 'None';
next if $monitor->{Type} eq 'WebSite';
my $now = time();
2016-08-22 23:41:26 +08:00
my $restart = 0;
2021-08-31 01:32:43 +08:00
if (zmMemVerify($monitor)) {
2016-08-22 23:41:26 +08:00
# Check we have got an image recently
my $capture_time = zmGetLastWriteTime($monitor);
2021-08-31 01:32:43 +08:00
if (!defined($capture_time)) {
2016-08-22 23:41:26 +08:00
# Can't read from shared data
Debug('LastWriteTime is not defined.');
zmMemInvalidate($monitor);
2016-08-22 23:41:26 +08:00
next;
}
Debug("Monitor $$monitor{Id} LastWriteTime is $capture_time.");
2021-08-31 01:32:43 +08:00
if (!$capture_time) {
my $startup_time = zmGetStartupTime($monitor);
2021-08-31 01:32:43 +08:00
if (($now - $startup_time) > $Config{ZM_WATCH_MAX_DELAY}) {
Warning(
"Restarting capture daemon for $$monitor{Name}, no image since startup. ".
"Startup time was $startup_time - now $now > $Config{ZM_WATCH_MAX_DELAY}"
);
$restart = 1;
} else {
# We can't get the last capture time so can't be sure it's died, it might just be starting up.
zmMemInvalidate($monitor);
2017-11-25 04:37:50 +08:00
next;
}
2016-08-22 23:41:26 +08:00
}
2021-08-31 01:32:43 +08:00
if (!$restart) {
2018-09-08 04:29:23 +08:00
my $max_image_delay = (
$monitor->{MaxFPS}
&&($monitor->{MaxFPS}>0)
&&($monitor->{MaxFPS}<1)
) ? (3/$monitor->{MaxFPS})
: $Config{ZM_WATCH_MAX_DELAY}
;
2018-12-27 07:18:22 +08:00
my $image_delay = $now - $capture_time;
Debug("Monitor $monitor->{Id} last captured $image_delay seconds ago, max is $max_image_delay");
if ( $image_delay > $max_image_delay ) {
Warning("Restarting capture daemon for "
.$monitor->{Name}.", time since last capture $image_delay seconds ($now-$capture_time)"
);
$restart = 1;
}
} # end if ! restart
2016-08-22 23:41:26 +08:00
} else {
Info("Restarting capture daemon for $monitor->{Name}, shared data not valid");
2016-08-22 23:41:26 +08:00
$restart = 1;
}
2021-08-31 01:32:43 +08:00
if ($restart) {
2016-08-22 23:41:26 +08:00
my $command;
2021-08-31 01:32:43 +08:00
if ($monitor->{Type} eq 'Local') {
$command = 'zmdc.pl restart zmc -d '.$monitor->{Device};
2016-08-22 23:41:26 +08:00
} else {
2021-08-31 01:32:43 +08:00
$command = 'zmdc.pl restart zmc -m '.$monitor->{Id};
2016-08-22 23:41:26 +08:00
}
runCommand($command);
2021-08-31 01:32:43 +08:00
} elsif ($monitor->{Function} ne 'Monitor') {
2016-08-22 23:41:26 +08:00
# Now check analysis daemon
$restart = 0;
# Check we have got an image recently
my $image_time = zmGetLastReadTime($monitor);
2021-08-31 01:32:43 +08:00
if (!defined($image_time)) {
2016-08-22 23:41:26 +08:00
# Can't read from shared data
$restart = 1;
Error("Error reading shared data for $$monitor{Id} $$monitor{Name}");
2021-08-31 01:32:43 +08:00
} elsif (!$image_time) {
2016-08-22 23:41:26 +08:00
# We can't get the last capture time so can't be sure it's died.
#$restart = 1;
Error("Last analyse time for $$monitor{Id} $$monitor{Name} was zero.");
2016-08-22 23:41:26 +08:00
} else {
my $max_image_delay = ( $monitor->{MaxFPS}
&&($monitor->{MaxFPS}>0)
&&($monitor->{MaxFPS}<1)
) ? (3/$monitor->{MaxFPS})
: $Config{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");
2021-08-31 01:32:43 +08:00
if ($image_delay > $max_image_delay) {
Warning("Analysis daemon for $$monitor{Id} $$monitor{Name} needs restarting,"
." time since last analysis $image_delay seconds ($now-$image_time)"
2016-08-22 23:41:26 +08:00
);
$restart = 1;
}
2016-08-22 23:41:26 +08:00
}
2021-08-31 01:32:43 +08:00
if ($restart) {
Info("Restarting analysis daemon for $$monitor{Id} $$monitor{Name}");
my $command;
if ( $monitor->{Type} eq 'Local' ) {
2021-08-31 01:32:43 +08:00
$command = 'zmdc.pl restart zmc -d '.$monitor->{Device};
} else {
2021-08-31 01:32:43 +08:00
$command = 'zmdc.pl restart zmc -m '.$monitor->{Id};
}
runCommand($command);
2016-08-22 23:41:26 +08:00
} # end if restart
} # end if check analysis daemon
# Prevent open handles building up if we have connect to shared memory
zmMemInvalidate($monitor); # Close our file handle to the zmc process we are about to end
2016-08-22 23:41:26 +08:00
} # end foreach monitor
sleep($Config{ZM_WATCH_CHECK_INTERVAL});
} # end while (!$zm_terminate)
2016-08-22 23:41:26 +08:00
2021-08-31 01:32:43 +08:00
Info('Watchdog exiting');
exit();
2016-08-22 23:41:26 +08:00
1;
__END__