2019-09-24 00:54:27 +08:00
|
|
|
#!@PERL_EXECUTABLE@ -wT
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# 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
|
2016-12-26 23:23:16 +08:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
2015-04-16 13:23:40 +08:00
|
|
|
|
|
|
|
=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
|
2005-12-16 18:05:29 +08:00
|
|
|
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 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;
|
2017-11-15 00:55:00 +08:00
|
|
|
use ZoneMinder::Storage;
|
2021-12-15 22:11:08 +08:00
|
|
|
use ZoneMinder::Monitor;
|
2005-12-16 18:05:29 +08:00
|
|
|
use POSIX;
|
|
|
|
use DBI;
|
2015-04-16 13:42:56 +08:00
|
|
|
use autouse 'Data::Dumper'=>qw(Dumper);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
2016-03-12 05:28:16 +08:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
|
2005-12-16 18:05:29 +08:00
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
2022-01-06 05:49:20 +08:00
|
|
|
my $log = logInit();
|
2011-06-21 17:19:10 +08:00
|
|
|
logSetSignal();
|
2021-03-18 01:09:54 +08:00
|
|
|
my $zm_terminate = 0;
|
|
|
|
sub TermHandler {
|
|
|
|
Info('Received TERM, exiting');
|
|
|
|
$zm_terminate = 1;
|
|
|
|
}
|
|
|
|
$SIG{TERM} = \&TermHandler;
|
|
|
|
$SIG{INT} = \&TermHandler;
|
2005-12-21 01:03:33 +08:00
|
|
|
|
2018-05-24 22:49:20 +08:00
|
|
|
Info('Watchdog starting, pausing for '.START_DELAY.' seconds');
|
|
|
|
sleep(START_DELAY);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2007-09-07 23:39:44 +08:00
|
|
|
my $dbh = zmDbConnect();
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2021-03-18 01:09:54 +08:00
|
|
|
while (!$zm_terminate) {
|
2021-12-15 22:11:08 +08:00
|
|
|
while (!($dbh and $dbh->ping())) {
|
|
|
|
if (!($dbh = zmDbConnect())) {
|
2018-09-08 04:29:23 +08:00
|
|
|
sleep($Config{ZM_WATCH_CHECK_INTERVAL});
|
|
|
|
}
|
|
|
|
}
|
2018-01-08 03:20:17 +08:00
|
|
|
|
2021-12-15 22:11:08 +08:00
|
|
|
foreach my $monitor (ZoneMinder::Monitor->find($Config{ZM_SERVER_ID} ? (ServerId=>$Config{ZM_SERVER_ID}) : ())) {
|
2016-08-22 23:41:26 +08:00
|
|
|
next if $monitor->{Function} eq 'None';
|
2018-04-27 05:18:36 +08:00
|
|
|
next if $monitor->{Type} eq 'WebSite';
|
2018-05-24 22:49:20 +08:00
|
|
|
my $now = time();
|
2016-08-22 23:41:26 +08:00
|
|
|
my $restart = 0;
|
2021-12-15 22:11:08 +08:00
|
|
|
|
|
|
|
zmMemInvalidate($monitor);
|
|
|
|
if (!zmMemVerify($monitor)) {
|
2018-05-24 22:49:20 +08:00
|
|
|
Info("Restarting capture daemon for $monitor->{Name}, shared data not valid");
|
2021-12-15 22:11:08 +08:00
|
|
|
$monitor->control('restart');
|
|
|
|
next;
|
2016-08-22 23:41:26 +08:00
|
|
|
}
|
|
|
|
|
2021-12-15 22:11:08 +08:00
|
|
|
# Check we have got an image recently
|
|
|
|
my $capture_time = zmGetLastWriteTime($monitor);
|
|
|
|
if (!defined($capture_time)) {
|
|
|
|
# Can't read from shared data
|
|
|
|
Warning('LastWriteTime is not defined.');
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
Debug("Monitor $$monitor{Id} LastWriteTime is $capture_time.");
|
|
|
|
if (!$capture_time) {
|
|
|
|
# We can't get the last capture time so can't be sure it's died, it might just be starting up.
|
|
|
|
my $startup_time = zmGetStartupTime($monitor);
|
|
|
|
if (($now - $startup_time) > $Config{ZM_WATCH_MAX_DELAY}) {
|
2022-01-06 05:49:20 +08:00
|
|
|
$log->logPrint(ZoneMinder::Logger::WARNING+$monitor->Importance(),
|
2021-12-15 22:11:08 +08:00
|
|
|
"Restarting capture daemon for $$monitor{Name}, no image since startup. ".
|
|
|
|
"Startup time was $startup_time - now $now > $Config{ZM_WATCH_MAX_DELAY}"
|
|
|
|
);
|
|
|
|
$monitor->control('restart');
|
2016-08-22 23:41:26 +08:00
|
|
|
}
|
2021-12-15 22:11:08 +08:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $max_image_delay = (
|
|
|
|
$monitor->{MaxFPS}
|
|
|
|
&&($monitor->{MaxFPS}>0)
|
|
|
|
&&($monitor->{MaxFPS}<1)
|
|
|
|
) ? (3/$monitor->{MaxFPS})
|
|
|
|
: $Config{ZM_WATCH_MAX_DELAY}
|
|
|
|
;
|
|
|
|
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) {
|
2022-01-06 05:49:20 +08:00
|
|
|
$log->logPrint(ZoneMinder::Logger::WARNING+$monitor->Importance(),
|
|
|
|
'Restarting capture daemon for '.$monitor->{Name}.
|
|
|
|
", time since last capture $image_delay seconds ($now-$capture_time)");
|
2021-12-15 22:11:08 +08:00
|
|
|
$monitor->control('restart');
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($monitor->{Function} ne 'Monitor') {
|
|
|
|
# Now check analysis thread
|
2016-08-22 23:41:26 +08:00
|
|
|
# Check we have got an image recently
|
2018-05-24 22:49:20 +08:00
|
|
|
my $image_time = zmGetLastReadTime($monitor);
|
2021-08-31 01:32:43 +08:00
|
|
|
if (!defined($image_time)) {
|
2021-12-15 22:11:08 +08:00
|
|
|
# Can't read from shared data
|
2018-05-24 22:49:20 +08:00
|
|
|
Error("Error reading shared data for $$monitor{Id} $$monitor{Name}");
|
2021-12-15 22:11:08 +08:00
|
|
|
$monitor->control('restart');
|
|
|
|
next;
|
2021-08-31 01:32:43 +08:00
|
|
|
} elsif (!$image_time) {
|
2021-12-15 22:11:08 +08:00
|
|
|
Debug("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;
|
2018-05-24 22:49:20 +08:00
|
|
|
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) {
|
2022-01-06 05:49:20 +08:00
|
|
|
$log->logPrint(ZoneMinder::Logger::WARNING+$monitor->Importance(),
|
|
|
|
"daemon for $$monitor{Id} $$monitor{Name} needs restarting,"
|
|
|
|
." time since last analysis $image_delay seconds ($now-$image_time)");
|
|
|
|
$monitor->control('restart');
|
|
|
|
next;
|
2015-06-19 20:59:49 +08:00
|
|
|
}
|
2016-08-22 23:41:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} # end if check analysis daemon
|
2021-12-15 22:11:08 +08:00
|
|
|
|
2016-08-22 23:41:26 +08:00
|
|
|
} # end foreach monitor
|
2017-11-23 09:03:37 +08:00
|
|
|
|
2018-05-24 22:49:20 +08:00
|
|
|
sleep($Config{ZM_WATCH_CHECK_INTERVAL});
|
2021-03-18 01:09:54 +08:00
|
|
|
} # end while (!$zm_terminate)
|
2016-08-22 23:41:26 +08:00
|
|
|
|
2021-08-31 01:32:43 +08:00
|
|
|
Info('Watchdog exiting');
|
2005-12-16 18:05:29 +08:00
|
|
|
exit();
|
2018-01-11 01:58:04 +08:00
|
|
|
|
2016-08-22 23:41:26 +08:00
|
|
|
1;
|
|
|
|
__END__
|