From c42179ffa75b671aedd4f59c29cd942161dd0c58 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 24 Nov 2024 12:57:56 -0500 Subject: [PATCH] Handle db reconnects, don't disconnect db because it rug pulls from under logging functions --- scripts/zmtelemetry.pl.in | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/scripts/zmtelemetry.pl.in b/scripts/zmtelemetry.pl.in index 2f589ff96..8158a79c6 100644 --- a/scripts/zmtelemetry.pl.in +++ b/scripts/zmtelemetry.pl.in @@ -67,9 +67,11 @@ if ($help) { pod2usage(-exitstatus => -1); } +logInit(); +my $dbh = zmDbConnect(); + if ($show) { my %telemetry; - my $dbh = zmDbConnect(); collectData($dbh, \%telemetry); my $result = jsonEncode(\%telemetry); print($result); @@ -90,6 +92,13 @@ print('ZoneMinder Telemetry Agent starting at '.strftime('%y/%m/%d %H:%M:%S', lo my $lastCheck = $Config{ZM_TELEMETRY_LAST_UPLOAD}; while (1) { + while ( ! ( $dbh and $dbh->ping() ) ) { + Info('Reconnecting to db'); + if ( !($dbh = zmDbConnect()) ) { + #What we do here is not that important, so just skip this interval + sleep($interval); + } + } my $now = time(); my $since_last_check = $now - $lastCheck; Debug("Last Check time (now($now) - lastCheck($lastCheck)) = $since_last_check > interval($interval) or force($force)"); @@ -100,23 +109,17 @@ while (1) { } if ((($since_last_check) > $interval) or $force) { print "Collecting data to send to ZoneMinder Telemetry server.\n"; - my $dbh = zmDbConnect(); - if ($dbh) { - # Build the telemetry hash - # We should keep *BSD systems in mind when calling system commands + # Build the telemetry hash + # We should keep *BSD systems in mind when calling system commands - my %telemetry; - collectData($dbh, \%telemetry); - my $result = jsonEncode(\%telemetry); + my %telemetry; + collectData($dbh, \%telemetry); + my $result = jsonEncode(\%telemetry); - if (sendData($result)) { - ZoneMinder::Database::zmDbDo('UPDATE Config SET Value=? WHERE Name=?', - $now, 'ZM_TELEMETRY_LAST_UPLOAD'); - $Config{ZM_TELEMETRY_LAST_UPLOAD} = $now; - } - zmDbDisconnect(); - } else { - Error('Failed to open database. Sleeping.'); + if (sendData($result)) { + ZoneMinder::Database::zmDbDo('UPDATE Config SET Value=? WHERE Name=?', + $now, 'ZM_TELEMETRY_LAST_UPLOAD'); + $Config{ZM_TELEMETRY_LAST_UPLOAD} = $now; } } elsif (-t STDIN) { print "ZoneMinder Telemetry Agent sleeping for $interval seconds because ($now-$lastCheck=$since_last_check > $interval\n";