code style.
This commit is contained in:
parent
914581bc57
commit
0bcb9d276f
|
@ -57,10 +57,16 @@ GetOptions(
|
|||
interval => \$interval,
|
||||
version => \$version
|
||||
);
|
||||
|
||||
if ($version) {
|
||||
print(ZoneMinder::Base::ZM_VERSION . "\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if ($help) {
|
||||
pod2usage(-exitstatus => -1);
|
||||
}
|
||||
|
||||
if ($show) {
|
||||
my %telemetry;
|
||||
my $dbh = zmDbConnect();
|
||||
|
@ -69,19 +75,17 @@ if ($show) {
|
|||
print($result);
|
||||
exit(0);
|
||||
}
|
||||
if ( $help ) {
|
||||
pod2usage(-exitstatus => -1);
|
||||
}
|
||||
|
||||
if (!defined $interval) {
|
||||
$interval = eval($Config{ZM_TELEMETRY_INTERVAL});
|
||||
}
|
||||
|
||||
if (!($Config{ZM_TELEMETRY_DATA} or $force)) {
|
||||
print "ZoneMinder Telemetry Agent not enabled. Exiting.\n";
|
||||
print("ZoneMinder Telemetry Agent not enabled. Exiting.\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
print 'ZoneMinder Telemetry Agent starting at '.strftime('%y/%m/%d %H:%M:%S', localtime())."\n";
|
||||
print('ZoneMinder Telemetry Agent starting at '.strftime('%y/%m/%d %H:%M:%S', localtime())."\n");
|
||||
|
||||
my $lastCheck = $Config{ZM_TELEMETRY_LAST_UPLOAD};
|
||||
|
||||
|
@ -97,6 +101,7 @@ 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
|
||||
|
||||
|
@ -105,20 +110,22 @@ while( 1 ) {
|
|||
my $result = jsonEncode(\%telemetry);
|
||||
|
||||
if (sendData($result)) {
|
||||
my $sql = q`UPDATE Config SET Value = ? WHERE Name = 'ZM_TELEMETRY_LAST_UPLOAD'`;
|
||||
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
||||
my $res = $sth->execute($now) or die( "Can't execute: ".$sth->errstr() );
|
||||
$sth->finish();
|
||||
ZoneMinder::Database::zmDbDo('UPDATE Config SET Value=? WHERE Name=?',
|
||||
$now, 'ZM_TELEMETRY_LAST_UPLOAD');
|
||||
$Config{ZM_TELEMETRY_LAST_UPLOAD} = $now;
|
||||
}
|
||||
zmDbDisconnect();
|
||||
} elsif ( -t STDIN ) {
|
||||
print "ZoneMinder Telemetry Agent sleeping for $interval seconds because ($now-$lastCheck=$since_last_check > $interval\n";
|
||||
}
|
||||
} else {
|
||||
Error("Failed to opem database. Sleeping.");
|
||||
}
|
||||
$lastCheck = $now;
|
||||
sleep($interval);
|
||||
} # end while
|
||||
print 'ZoneMinder Telemetry Agent exiting at '.strftime('%y/%m/%d %H:%M:%S', localtime())."\n";
|
||||
exit(0);
|
||||
|
||||
###############
|
||||
# SUBROUTINES #
|
||||
|
@ -129,12 +136,12 @@ sub collectData {
|
|||
my $dbh = shift;
|
||||
my $telemetry = shift;
|
||||
$telemetry->{uuid} = getUUID($dbh);
|
||||
($telemetry->{city},$telemetry->{region},$telemetry->{country},$telemetry->{latitude},$telemetry->{longitude})=getGeo();
|
||||
@$telemetry{qw(city region country latitude longitude)} = getGeo();
|
||||
$telemetry->{timestamp} = strftime('%Y-%m-%dT%H:%M:%S%z', localtime());
|
||||
$telemetry->{monitor_count} = countQuery($dbh, 'Monitors');
|
||||
$telemetry->{event_count} = countQuery($dbh, 'Events');
|
||||
$telemetry->{architecture} = runSysCmd('uname -p');
|
||||
($telemetry->{kernel}, $telemetry->{distro}, $telemetry->{version}) = getDistro();
|
||||
@$telemetry{qw(kernel distro version)} = getDistro();
|
||||
$telemetry->{zm_version} = ZoneMinder::Base::ZM_VERSION;
|
||||
$telemetry->{system_memory} = totalmem();
|
||||
$telemetry->{processor_count} = cpu_count();
|
||||
|
@ -153,14 +160,15 @@ sub runSysCmd {
|
|||
my $result = '';
|
||||
if (!$path || $status) {
|
||||
Warning("Cannot find the $arguments[0] executable.");
|
||||
} else {
|
||||
return $result;
|
||||
}
|
||||
|
||||
chomp($path);
|
||||
$arguments[0] = $path;
|
||||
my $cmd = join(' ', @arguments);
|
||||
($cmd) = $cmd =~ /(.*)/; # detaint
|
||||
$result = qx( $cmd );
|
||||
chomp($result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
@ -183,7 +191,6 @@ sub sendData {
|
|||
$req->header('content-type' => 'application/x-www-form-urlencoded');
|
||||
$req->header('content-length' => length($msg));
|
||||
$req->header('connection' => 'Close');
|
||||
|
||||
$req->content(encode('UTF-8', $msg));
|
||||
|
||||
my $resp = $ua->request($req);
|
||||
|
@ -205,7 +212,11 @@ sub getUUID {
|
|||
my $uuid = '';
|
||||
|
||||
# Verify the current UUID is valid and not nil
|
||||
if (( $Config{ZM_TELEMETRY_UUID} =~ /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i ) && ( $Config{ZM_TELEMETRY_UUID} ne '00000000-0000-0000-0000-000000000000' )) {
|
||||
if (
|
||||
($Config{ZM_TELEMETRY_UUID} =~ /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i)
|
||||
&&
|
||||
($Config{ZM_TELEMETRY_UUID} ne '00000000-0000-0000-0000-000000000000' )
|
||||
) {
|
||||
$uuid = $Config{ZM_TELEMETRY_UUID};
|
||||
} else {
|
||||
my $sql = 'SELECT uuid()';
|
||||
|
@ -214,13 +225,8 @@ sub getUUID {
|
|||
$uuid = $Config{ZM_TELEMETRY_UUID} = $sth->fetchrow_array();
|
||||
$sth->finish();
|
||||
|
||||
$sql = q`UPDATE Config SET Value = ? WHERE Name = 'ZM_TELEMETRY_UUID'`;
|
||||
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
||||
$res = $sth->execute( "$uuid" ) or die( "Can't execute: ".$sth->errstr() );
|
||||
$sth->finish();
|
||||
zmDbDo('UPDATE Config SET Value=? WHERE Name=?', $uuid, 'ZM_TELEMETRY_UUID');
|
||||
}
|
||||
Debug("Using UUID of: $uuid");
|
||||
|
||||
return $uuid;
|
||||
}
|
||||
|
||||
|
@ -331,7 +337,7 @@ sub linuxDistro {
|
|||
close $RELFILE;
|
||||
# exists on many distros but does not always contain useful information, such as redhat
|
||||
} elsif (-f '/etc/lsb-release') {
|
||||
open(my $RELFILE,'<','/etc/lsb-release') or die( "Can't Open file: $!\n" );
|
||||
if (open(my $RELFILE,'<','/etc/lsb-release')) {
|
||||
while (<$RELFILE>) {
|
||||
if (/^DISTRIB_DESCRIPTION=(")?(.*)(?(1)\1|).*$/) {
|
||||
$distro = $2;
|
||||
|
@ -342,7 +348,10 @@ sub linuxDistro {
|
|||
$found = 1;
|
||||
}
|
||||
}
|
||||
close $RELFILE;
|
||||
close($RELFILE);
|
||||
} else {
|
||||
Error("Can't Open file /etc/lsb-release: $!\n");
|
||||
}
|
||||
}
|
||||
|
||||
# If all else fails, search through a list of known release files until we find one
|
||||
|
|
Loading…
Reference in New Issue