2016-02-07 04:08:28 +08:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# ZoneMinder Update 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
zmtelemetry.pl - Send usage information to the ZoneMinder development team
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
zmtelemetry.pl
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This script collects usage information of the local system and sends it to the
|
|
|
|
ZoneMinder development team. This data will be used to determine things like
|
|
|
|
who and where our customers are, how big their systems are, the underlying
|
|
|
|
hardware and operating system, etc. This is being done for the sole purpoase of
|
|
|
|
creating a better product for our target audience. This script is intended to
|
|
|
|
be completely transparent to the end user, and can be disabled from the web
|
|
|
|
console under Options.
|
|
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
|
|
|
|
none currently
|
|
|
|
|
|
|
|
=cut
|
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
|
|
|
@EXTRA_PERL_LIB@
|
2016-02-12 22:06:51 +08:00
|
|
|
use ZoneMinder;
|
2016-02-07 04:08:28 +08:00
|
|
|
use DBI;
|
|
|
|
use Getopt::Long;
|
|
|
|
use autouse 'Pod::Usage'=>qw(pod2usage);
|
2016-02-11 10:46:05 +08:00
|
|
|
use LWP::UserAgent;
|
2016-02-13 22:54:40 +08:00
|
|
|
use Sys::MemInfo qw(totalmem);
|
|
|
|
use Sys::CPU qw(cpu_count);
|
2016-02-15 22:43:34 +08:00
|
|
|
use POSIX qw(strftime uname);
|
2016-02-07 04:08:28 +08:00
|
|
|
|
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
|
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
|
|
|
use constant CHECK_INTERVAL => (14*24*60*60); # Interval between version checks
|
|
|
|
|
|
|
|
# Setting these as contants for now.
|
|
|
|
# Alternatively, we can put these in the dB and then retrieve using the Config hash.
|
2016-02-16 02:38:13 +08:00
|
|
|
use constant ZM_TELEMETRY_SERVER_ENDPOINT => 'https://zmanon:2b2d0b4skps@telemetry.zoneminder.com/zmtelemetry/testing5';
|
2016-02-07 04:08:28 +08:00
|
|
|
|
|
|
|
if ( $Config{ZM_TELEMETRY_DATA} )
|
|
|
|
{
|
|
|
|
print( "Update agent starting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
|
|
|
|
|
|
|
my $lastCheck = $Config{ZM_TELEMETRY_LAST_CHECK};
|
|
|
|
|
2016-02-13 22:54:40 +08:00
|
|
|
while( 1 ) {
|
2016-02-07 04:08:28 +08:00
|
|
|
my $now = time();
|
2016-02-13 22:54:40 +08:00
|
|
|
if ( ($now-$lastCheck) > CHECK_INTERVAL ) {
|
2016-02-11 10:46:05 +08:00
|
|
|
Info( "Colleting data to send to ZoneMinder Telemetry server." );
|
2016-02-12 22:06:51 +08:00
|
|
|
my $dbh = zmDbConnect();
|
2016-02-11 10:46:05 +08:00
|
|
|
# Build the telemetry hash
|
2016-02-07 04:08:28 +08:00
|
|
|
# We should keep *BSD systems in mind when calling system commands
|
2016-02-11 10:46:05 +08:00
|
|
|
my %telemetry;
|
2016-02-13 22:54:40 +08:00
|
|
|
$telemetry{uuid} = getUUID($dbh);
|
2016-02-11 10:46:05 +08:00
|
|
|
$telemetry{ip} = getIP();
|
2016-02-16 04:11:58 +08:00
|
|
|
$telemetry{timestamp} = strftime( "%Y-%m-%dT%H:%M:%S%z", localtime() );
|
2016-02-13 22:54:40 +08:00
|
|
|
$telemetry{monitor_count} = countQuery($dbh,"Monitors");
|
|
|
|
$telemetry{event_count} = countQuery($dbh,"Events");
|
2016-02-13 00:47:12 +08:00
|
|
|
$telemetry{architecture} = runSysCmd("uname -p");
|
2016-02-13 05:18:45 +08:00
|
|
|
($telemetry{kernel}, $telemetry{distro}, $telemetry{version}) = getDistro();
|
2016-02-11 10:46:05 +08:00
|
|
|
$telemetry{zm_version} = ZoneMinder::Base::ZM_VERSION;
|
2016-02-14 05:01:22 +08:00
|
|
|
$telemetry{system_memory} = totalmem();
|
2016-02-11 10:46:05 +08:00
|
|
|
$telemetry{processor_count} = cpu_count();
|
2016-02-13 22:54:40 +08:00
|
|
|
$telemetry{monitors} = getMonitorRef($dbh);
|
2016-02-11 10:46:05 +08:00
|
|
|
|
|
|
|
Info( "Sending data to ZoneMinder Telemetry server." );
|
|
|
|
|
|
|
|
my $result = jsonEncode( \%telemetry );
|
2016-02-07 04:08:28 +08:00
|
|
|
|
|
|
|
if ( sendData($result) ) {
|
|
|
|
$lastCheck = $now;
|
|
|
|
|
2016-02-13 22:54:40 +08:00
|
|
|
my $sql = "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( "$lastCheck" ) or die( "Can't execute: ".$sth->errstr() );
|
|
|
|
$sth->finish();
|
2016-02-07 04:08:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep( 3600 );
|
|
|
|
}
|
|
|
|
print( "Update agent exiting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
|
|
|
}
|
|
|
|
|
2016-02-13 22:54:40 +08:00
|
|
|
###############
|
|
|
|
# SUBROUTINES #
|
|
|
|
###############
|
|
|
|
|
2016-02-07 04:08:28 +08:00
|
|
|
# Find, verify, then run the supplied system command
|
|
|
|
sub runSysCmd {
|
|
|
|
my $msg = shift;
|
|
|
|
my @arguments = split(/ /,$msg);
|
|
|
|
chomp($arguments[0]);
|
|
|
|
my $path = qx( which $arguments[0] );
|
|
|
|
|
|
|
|
my $status = $? >> 8;
|
|
|
|
my $result = "";
|
|
|
|
if ( !$path || $status ) {
|
|
|
|
Warning( "Cannot find the $arguments[0] executable." );
|
|
|
|
} else {
|
|
|
|
chomp($path);
|
|
|
|
$arguments[0] = $path;
|
|
|
|
my $cmd = join(" ",@arguments);
|
2016-02-13 05:18:45 +08:00
|
|
|
$result = qx( $cmd );
|
2016-02-14 04:53:22 +08:00
|
|
|
chomp($result);
|
2016-02-07 04:08:28 +08:00
|
|
|
}
|
2016-02-14 04:53:22 +08:00
|
|
|
|
|
|
|
return $result;
|
2016-02-07 04:08:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Upload message data to ZoneMinder telemetry server
|
|
|
|
sub sendData {
|
|
|
|
my $msg = shift;
|
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new;
|
2016-02-15 22:50:11 +08:00
|
|
|
my $server_endpoint = ZM_TELEMETRY_SERVER_ENDPOINT;
|
2016-02-07 04:08:28 +08:00
|
|
|
|
|
|
|
if ( $Config{ZM_UPDATE_CHECK_PROXY} ) {
|
2016-02-10 04:35:45 +08:00
|
|
|
$ua->proxy( "https", $Config{ZM_UPDATE_CHECK_PROXY} );
|
2016-02-07 04:08:28 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 04:53:22 +08:00
|
|
|
Debug("Posting telemetry data to: $server_endpoint");
|
|
|
|
|
2016-02-07 04:08:28 +08:00
|
|
|
# set custom HTTP request header fields
|
|
|
|
my $req = HTTP::Request->new(POST => $server_endpoint);
|
2016-02-10 04:35:45 +08:00
|
|
|
$req->header('content-type' => 'application/x-www-form-urlencoded');
|
2016-02-07 04:08:28 +08:00
|
|
|
$req->header('content-length' => length($msg));
|
|
|
|
$req->header('connection' => 'Close');
|
|
|
|
|
|
|
|
$req->content($msg);
|
|
|
|
|
|
|
|
my $resp = $ua->request($req);
|
2016-02-14 04:53:22 +08:00
|
|
|
my $resp_msg = $resp->decoded_content;
|
|
|
|
my $resp_code = $resp->code;
|
|
|
|
if ($resp->is_success) {
|
|
|
|
Info("Telemetry data uploaded successfully.");
|
|
|
|
Debug("Telemetry server upload success response message: $resp_msg");
|
2016-02-07 04:08:28 +08:00
|
|
|
} else {
|
2016-02-14 04:53:22 +08:00
|
|
|
Warning("Telemetry server returned HTTP POST error code: $resp_code");
|
|
|
|
Debug("Telemetry server upload failure response message: $resp_msg");
|
2016-02-07 04:08:28 +08:00
|
|
|
}
|
2016-02-14 04:53:22 +08:00
|
|
|
return $resp->is_success;
|
2016-02-07 04:08:28 +08:00
|
|
|
}
|
|
|
|
|
2016-02-11 03:04:14 +08:00
|
|
|
# Retrieves the UUID from the database. Creates a new UUID if one does not exist.
|
|
|
|
sub getUUID {
|
2016-02-13 22:54:40 +08:00
|
|
|
my $dbh = shift;
|
2016-02-11 03:04:14 +08:00
|
|
|
my $uuid= "";
|
|
|
|
|
2016-02-14 04:53:22 +08:00
|
|
|
# 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" )) {
|
2016-02-11 03:04:14 +08:00
|
|
|
$uuid = $Config{ZM_TELEMETRY_UUID};
|
|
|
|
} else {
|
2016-02-13 22:54:40 +08:00
|
|
|
my $sql = "SELECT uuid()";
|
2016-02-11 03:04:14 +08:00
|
|
|
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
2016-02-12 23:18:15 +08:00
|
|
|
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
|
|
|
|
$uuid = $Config{ZM_TELEMETRY_UUID} = $sth->fetchrow_array();
|
|
|
|
$sth->finish();
|
|
|
|
|
2016-02-13 22:54:40 +08:00
|
|
|
$sql = "UPDATE Config set Value = ? WHERE Name = 'ZM_TELEMETRY_UUID'";
|
2016-02-12 23:18:15 +08:00
|
|
|
$sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
2016-02-13 22:54:40 +08:00
|
|
|
$res = $sth->execute( "$uuid" ) or die( "Can't execute: ".$sth->errstr() );
|
|
|
|
$sth->finish();
|
2016-02-11 03:04:14 +08:00
|
|
|
}
|
2016-02-14 04:53:22 +08:00
|
|
|
Debug("Using UUID of: $uuid");
|
2016-02-11 03:04:14 +08:00
|
|
|
|
|
|
|
return $uuid;
|
|
|
|
}
|
|
|
|
|
2016-02-11 04:05:34 +08:00
|
|
|
# Retrieves the local server's external IP address
|
|
|
|
sub getIP {
|
|
|
|
my $ipaddr = "0.0.0.0";
|
|
|
|
my $ua = LWP::UserAgent->new;
|
|
|
|
my $server_endpoint = "https://wiki.zoneminder.com/ip.php";
|
|
|
|
|
|
|
|
my $req = HTTP::Request->new(GET => $server_endpoint);
|
|
|
|
my $resp = $ua->request($req);
|
|
|
|
|
|
|
|
if ($resp->is_success) {
|
|
|
|
$ipaddr = $resp->decoded_content;
|
|
|
|
}
|
|
|
|
|
2016-02-16 04:03:16 +08:00
|
|
|
Debug("Found external ip address of: $ipaddr");
|
2016-02-14 04:53:22 +08:00
|
|
|
|
2016-02-11 04:05:34 +08:00
|
|
|
return $ipaddr;
|
|
|
|
}
|
2016-02-11 03:04:14 +08:00
|
|
|
|
2016-02-11 21:22:31 +08:00
|
|
|
# As the name implies, just your average mysql count query
|
|
|
|
sub countQuery {
|
2016-02-13 22:54:40 +08:00
|
|
|
my $dbh = shift;
|
2016-02-11 21:22:31 +08:00
|
|
|
my $table = shift;
|
|
|
|
|
2016-02-13 22:54:40 +08:00
|
|
|
my $sql = "SELECT count(*) FROM $table";
|
2016-02-11 21:22:31 +08:00
|
|
|
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
2016-02-13 22:54:40 +08:00
|
|
|
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
|
2016-02-12 23:18:15 +08:00
|
|
|
my $count = $sth->fetchrow_array();
|
2016-02-13 22:54:40 +08:00
|
|
|
$sth->finish();
|
2016-02-11 21:22:31 +08:00
|
|
|
|
|
|
|
return $count
|
|
|
|
}
|
|
|
|
|
2016-02-12 22:59:16 +08:00
|
|
|
# Returns a reference to an array of hashes containing data from all monitors
|
|
|
|
sub getMonitorRef {
|
2016-02-13 22:54:40 +08:00
|
|
|
my $dbh = shift;
|
2016-02-12 22:59:16 +08:00
|
|
|
|
|
|
|
my $sql = "SELECT Id,Name,Type,Function,Width,Height,Colours,MaxFPS,AlarmMaxFPS FROM Monitors";
|
|
|
|
my $sth = $dbh->prepare_cached( $sql ) or die( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() );
|
2016-02-13 22:54:40 +08:00
|
|
|
my $arrayref = $sth->fetchall_arrayref({});
|
2016-02-12 22:59:16 +08:00
|
|
|
|
|
|
|
return $arrayref;
|
|
|
|
}
|
|
|
|
|
2016-02-13 05:18:45 +08:00
|
|
|
sub getDistro {
|
|
|
|
my $kernel = "";
|
|
|
|
my $distro = "";
|
|
|
|
my $version = "";
|
2016-02-15 22:43:34 +08:00
|
|
|
my @uname = uname();
|
2016-02-13 05:18:45 +08:00
|
|
|
|
2016-02-15 22:43:34 +08:00
|
|
|
if ( $uname[0] =~ /Linux/ ) {
|
2016-02-13 07:15:04 +08:00
|
|
|
Debug("Linux distro detected.");
|
2016-02-13 05:18:45 +08:00
|
|
|
($kernel, $distro, $version) = linuxDistro();
|
2016-02-15 22:43:34 +08:00
|
|
|
} elsif ( $uname[0] =~ /.*BSD/ ) {
|
2016-02-13 07:15:04 +08:00
|
|
|
Debug("BSD distro detected.");
|
2016-02-15 22:43:34 +08:00
|
|
|
$kernel = $uname[3];
|
|
|
|
$distro = $uname[0];
|
|
|
|
$version = $uname[2];
|
|
|
|
} elsif ( $uname[0] =~ /Darwin/ ) {
|
2016-02-13 07:15:04 +08:00
|
|
|
Debug("Mac OS distro detected.");
|
2016-02-15 22:43:34 +08:00
|
|
|
$kernel = $uname[3];
|
|
|
|
$distro = runSysCmd("sw_vers -productName");
|
|
|
|
$version = runSysCmd("sw_vers -productVersion");
|
|
|
|
} elsif ( $uname[0] =~ /SunOS|Solaris/ ) {
|
2016-02-13 07:15:04 +08:00
|
|
|
Debug("Sun Solaris detected.");
|
2016-02-15 22:43:34 +08:00
|
|
|
$kernel = $uname[3];
|
|
|
|
$distro = $uname[1];
|
|
|
|
$version = $uname[2];
|
2016-02-13 05:18:45 +08:00
|
|
|
} else {
|
2016-02-13 07:45:08 +08:00
|
|
|
Warning("ZoneMinder was unable to determine the host system. Please report.");
|
2016-02-13 05:18:45 +08:00
|
|
|
$kernel = "Unknown";
|
|
|
|
$distro = "Unknown";
|
|
|
|
$version = "Unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($kernel, $distro, $version);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub linuxDistro {
|
2016-02-15 22:43:34 +08:00
|
|
|
my @uname = uname();
|
|
|
|
my $kernel = $uname[2];
|
2016-02-13 05:18:45 +08:00
|
|
|
my $distro = "Unknown Linux Distro";
|
|
|
|
my $version = "Unknown Linux Version";
|
|
|
|
my $found = 0;
|
|
|
|
|
|
|
|
# os-release is the standard for many new distros based on systemd
|
|
|
|
if ( -f "/etc/os-release" ) {
|
|
|
|
open(my $RELFILE,"<","/etc/os-release") or die( "Can't Open file: $!\n" );
|
|
|
|
while (<$RELFILE>) {
|
2016-02-13 07:15:04 +08:00
|
|
|
if ( /^NAME=(")?(.*)(?(1)\1|).*$/ ) {
|
|
|
|
$distro = $2;
|
2016-02-13 05:18:45 +08:00
|
|
|
$found = 1;
|
|
|
|
}
|
2016-02-13 07:15:04 +08:00
|
|
|
if ( /^VERSION_ID=(")?(.*)(?(1)\1|).*$/ ) {
|
|
|
|
$version = $2;
|
2016-02-13 05:18:45 +08:00
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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" );
|
|
|
|
while (<$RELFILE>) {
|
2016-02-13 07:15:04 +08:00
|
|
|
if ( /^DISTRIB_DESCRIPTION=(")?(.*)(?(1)\1|).*$/ ) {
|
|
|
|
$distro = $2;
|
2016-02-13 05:18:45 +08:00
|
|
|
$found = 1;
|
|
|
|
}
|
2016-02-13 07:15:04 +08:00
|
|
|
if ( /^DISTRIB_RELEASE=(")?(.*)(?(1)\1|).*$/ ) {
|
|
|
|
$version = $2;
|
2016-02-13 05:18:45 +08:00
|
|
|
$found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $RELFILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
# If all else fails, search through a list of known release files until we find one
|
|
|
|
if ( !$found ) {
|
2016-02-13 22:54:40 +08:00
|
|
|
my @releasefile = ("/etc/SuSE-release", "/etc/redhat-release", "/etc/redhat_version",
|
2016-02-13 05:18:45 +08:00
|
|
|
"/etc/fedora-release", "/etc/slackware-release", "/etc/slackware-version",
|
|
|
|
"/etc/debian_release", "/etc/debian_version", "/etc/mandrake-release",
|
|
|
|
"/etc/yellowdog-release", "/etc/gentoo-release");
|
|
|
|
foreach (@releasefile) {
|
|
|
|
if ( -f $_ ) {
|
|
|
|
open(my $RELFILE,"<",$_) or die( "Can't Open file: $!\n" );
|
|
|
|
while (<$RELFILE>) {
|
|
|
|
if ( /(.*).* (\d+\.?\d*) .*/ ) {
|
|
|
|
$distro = $1;
|
|
|
|
$version = $2;
|
2016-02-13 07:45:08 +08:00
|
|
|
$found = 1;
|
2016-02-13 05:18:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close $RELFILE;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-13 07:45:08 +08:00
|
|
|
|
2016-02-13 22:54:40 +08:00
|
|
|
if ( !$found ) {
|
2016-02-13 07:45:08 +08:00
|
|
|
Warning("ZoneMinder was unable to determine Linux distro. Please report.");
|
|
|
|
}
|
|
|
|
|
2016-02-13 05:18:45 +08:00
|
|
|
return ($kernel, $distro, $version);
|
|
|
|
}
|
|
|
|
|
|
|
|
|