add code to update the statuses in the Server record every second. This code also reduces the timeout from .1 seconds to 1 second. Should reduce load by zmdc on low power devices. I see no reason to poll that heavily...

This commit is contained in:
Isaac Connor 2017-10-24 12:31:23 -07:00
parent 12fd1c151e
commit 6c03ae32c2
2 changed files with 46 additions and 6 deletions

View File

@ -157,6 +157,16 @@ sub zmDbGetMonitors {
return( \@monitors ); return( \@monitors );
} }
sub zmSQLExecute {
my $sql = shift;
my $sth = $dbh->prepare_cached( $sql )
or croak( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute( @_ )
or croak( "Can't execute '$sql': ".$sth->errstr() );
return 1;
}
sub zmDbGetMonitor { sub zmDbGetMonitor {
zmDbConnect(); zmDbConnect();

View File

@ -55,7 +55,7 @@ use bytes;
# ========================================================================== # ==========================================================================
# in useconds, not seconds. # in useconds, not seconds.
use constant MAX_CONNECT_DELAY => 10*1000*1000; use constant MAX_CONNECT_DELAY => 40;
# ========================================================================== # ==========================================================================
# #
@ -69,6 +69,8 @@ use POSIX;
use Socket; use Socket;
use IO::Handle; use IO::Handle;
use Time::HiRes qw(usleep); use Time::HiRes qw(usleep);
use Sys::MemInfo qw(totalmem freemem totalswap freeswap);
use autouse 'Pod::Usage'=>qw(pod2usage); use autouse 'Pod::Usage'=>qw(pod2usage);
#use Data::Dumper; #use Data::Dumper;
@ -140,12 +142,20 @@ foreach my $arg ( @ARGV ) {
} }
} }
my $dbh = zmDbConnect();
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" ); socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
my $saddr = sockaddr_un( SOCK_FILE ); my $saddr = sockaddr_un( SOCK_FILE );
my $server_up = connect( CLIENT, $saddr ); my $server_up = connect( CLIENT, $saddr );
if ( ! $server_up ) { if ( ! $server_up ) {
# Server is not up. Some commands can still be handled if ( $Config{ZM_SERVER_ID} ) {
if ( ! defined $dbh->do(q{UPDATE Servers SET Status=?,TotalMem=?,FreeMem=?,TotalSwap=?,FreeSwap=? WHERE Id=?}, undef,
'NotRunning', &totalmem, &freemem, &totalswap, &freeswap, $Config{ZM_SERVER_ID} ) ) {
Error("Failed Updating status of Server record to Not RUnning for Id=$Config{ZM_SERVER_ID}" . $dbh->errstr());
}
}
# Server is not up. Some commands can still be handled
if ( $command eq 'logrot' ) { if ( $command eq 'logrot' ) {
# If server is not running, then logrotate doesn't need to do anything. # If server is not running, then logrotate doesn't need to do anything.
exit(); exit();
@ -172,7 +182,7 @@ if ( ! $server_up ) {
my $attempts = 0; my $attempts = 0;
while( !connect( CLIENT, $saddr ) ) { while( !connect( CLIENT, $saddr ) ) {
$attempts++; $attempts++;
Error("Waiting for zmdc.pl server process, attempt $attempts" ); Error("Waiting for zmdc.pl server process at " . SOCK_FILE.", attempt $attempts" );
Fatal( "Can't connect: $!" ) if ($attempts > MAX_CONNECT_DELAY); Fatal( "Can't connect: $!" ) if ($attempts > MAX_CONNECT_DELAY);
usleep(200000); usleep(200000);
} # end while } # end while
@ -219,6 +229,7 @@ use POSIX;
use Socket; use Socket;
use IO::Handle; use IO::Handle;
use Time::HiRes qw(usleep); use Time::HiRes qw(usleep);
use Sys::MemInfo qw(totalmem freemem totalswap freeswap);
#use Data::Dumper; #use Data::Dumper;
use constant KILL_DELAY => 100*1000; # 1/10th of a second use constant KILL_DELAY => 100*1000; # 1/10th of a second
@ -250,11 +261,15 @@ sub run {
killAll( 1 ); killAll( 1 );
dPrint( ZoneMinder::Logger::INFO, 'Socket should be open at ' .main::SOCK_FILE );
my $dbh = zmDbConnect(1);
dPrint( ZoneMinder::Logger::INFO, 'Connected to db' );
socket( SERVER, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" ); socket( SERVER, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
unlink( main::SOCK_FILE ) or Error( 'Unable to unlink ' . main::SOCK_FILE .". Error message was: $!" ) if ( -e main::SOCK_FILE ); unlink( main::SOCK_FILE ) or Error( 'Unable to unlink ' . main::SOCK_FILE .". Error message was: $!" ) if -e main::SOCK_FILE;
bind( SERVER, $saddr ) or Fatal( "Can't bind to " . main::SOCK_FILE . ": $!" ); bind( SERVER, $saddr ) or Fatal( "Can't bind to " . main::SOCK_FILE . ": $!" );
listen( SERVER, SOMAXCONN ) or Fatal( "Can't listen: $!" ); listen( SERVER, SOMAXCONN ) or Fatal( "Can't listen: $!" );
$SIG{CHLD} = \&reaper; $SIG{CHLD} = \&reaper;
$SIG{INT} = \&shutdownAll; $SIG{INT} = \&shutdownAll;
$SIG{TERM} = \&shutdownAll; $SIG{TERM} = \&shutdownAll;
@ -265,8 +280,17 @@ sub run {
vec( $rin, fileno(SERVER), 1 ) = 1; vec( $rin, fileno(SERVER), 1 ) = 1;
my $win = $rin; my $win = $rin;
my $ein = $win; my $ein = $win;
my $timeout = 0.1; my $timeout = 1;
while( 1 ) { while( 1 ) {
if ( $Config{ZM_SERVER_ID} ) {
$dbh = zmDbConnect() if ! $dbh->ping();
if ( ! defined $dbh->do(q{UPDATE Servers SET Status=?,TotalMem=?,FreeMem=?,TotalSwap=?,FreeSwap=? WHERE Id=?}, undef,
'Running', &totalmem, &freemem, &totalswap, &freeswap, $Config{ZM_SERVER_ID} ) ) {
Error("Failed Updating status of Server record for Id=$Config{ZM_SERVER_ID}".$dbh->errstr());
} else {
Debug("Updated status of Server record for Id=$Config{ZM_SERVER_ID}".$dbh->errstr());
}
}
my $nfound = select( my $rout = $rin, undef, undef, $timeout ); my $nfound = select( my $rout = $rin, undef, undef, $timeout );
if ( $nfound > 0 ) { if ( $nfound > 0 ) {
if ( vec( $rout, fileno(SERVER), 1 ) ) { if ( vec( $rout, fileno(SERVER), 1 ) ) {
@ -327,6 +351,12 @@ sub run {
.strftime( '%y/%m/%d %H:%M:%S', localtime() ) .strftime( '%y/%m/%d %H:%M:%S', localtime() )
."\n" ."\n"
); );
if ( $Config{ZM_SERVER_ID} ) {
$dbh = zmDbConnect() if ! $dbh->ping();
if ( ! defined $dbh->do(q{UPDATE Servers SET Status='NotRunning' WHERE Id=?}, undef, $Config{ZM_SERVER_ID} ) ) {
Error("Failed Updating status of Server record for Id=$Config{ZM_SERVER_ID}".$dbh->errstr());
}
}
unlink( main::SOCK_FILE ) or Error( 'Unable to unlink ' . main::SOCK_FILE .". Error message was: $!" ) if ( -e main::SOCK_FILE ); unlink( main::SOCK_FILE ) or Error( 'Unable to unlink ' . main::SOCK_FILE .". Error message was: $!" ) if ( -e main::SOCK_FILE );
unlink( ZM_PID ) or Error( 'Unable to unlink ' . ZM_PID .". Error message was: $!" ) if ( -e ZM_PID ); unlink( ZM_PID ) or Error( 'Unable to unlink ' . ZM_PID .". Error message was: $!" ) if ( -e ZM_PID );
exit(); exit();
@ -393,7 +423,7 @@ sub start {
sigprocmask( SIG_SETMASK, $sigset ) or Fatal( "Can't restore SIGCHLD: $!" ); sigprocmask( SIG_SETMASK, $sigset ) or Fatal( "Can't restore SIGCHLD: $!" );
} elsif ( defined($cpid ) ) { } elsif ( defined($cpid ) ) {
# Force reconnection to the db. # Force reconnection to the db.
zmDbConnect(1); $dbh = zmDbConnect(1);
logReinit(); logReinit();
dPrint( ZoneMinder::Logger::INFO, "'".join( ' ', ( $daemon, @args ) ) dPrint( ZoneMinder::Logger::INFO, "'".join( ' ', ( $daemon, @args ) )