#!/usr/bin/perl -w ################################################################ # $Id$ # vim: ts=2:et # # (c) 2012 Copyright: Martin Fischer (m_fischer at gmx dot de) # All rights reserved # # This script 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 # any later version. # # The GNU General Public License can be found at # http://www.gnu.org/copyleft/gpl.html. # A copy is found in the textfile GPL.txt and important notices to the license # from the author is found in LICENSE.txt distributed with these scripts. # # This script 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. # ################################################################ use CGI qw(:standard Vars); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use DBI; #requires libdbd-sqlite3-perl use lib "./lib"; use Geo::IP; use strict; use warnings; no warnings 'uninitialized'; sub createDB(); sub insertDB(); sub checkColumn($$); sub getLocation($); sub googleVisualizationLib($); sub drawMarkersMap(@); sub drawPieChart(@); sub drawRegionsMap(@); sub drawTable(@); sub drawColumnChartTop10Modules(@); sub drawColumnChartTop10ModDef(@); sub drawBarChartModules(@); sub viewStatistics(); # cascading style sheet my $css = "http://fhem.de/../css/style.css"; # exclude modules from top 10 graph my $excludeFromTop10modules = "at autocreate notify telnet weblink FileLog FHEMWEB Global SUNRISE_EL"; my $excludeFromTop10definitions = "at autocreate notify telnet weblink FileLog FHEMWEB Global SUNRISE_EL"; # directory cointains databases my $datadir = "./data"; # geo ip database file from http://www.maxmind.com/download/geoip/database/ # should be updated once per month my $geoIPDat = "$datadir/GeoLiteCity.dat"; # database my $dbf = "$datadir/fhem_statistics_db.sqlite"; my $dsn = "dbi:SQLite:dbname=$dbf"; my $sth; # fhem node my $ua = $ENV{HTTP_USER_AGENT}; my $ip = $ENV{REMOTE_ADDR}; my %data = Vars(); # create database if not exists createDB() if (! -e $dbf); my $dbh = DBI->connect($dsn,"","", { RaiseError => 1, ShowErrorStatement => 1 }) || die "Cannot connect: $DBI::errstr"; if(index($ua,"Fhem") > -1) { print header("application/x-www-form-urlencoded"); insertDB(); print "==> ok"; } else { viewStatistics(); } sub viewStatistics() { my $visLib = googleVisualizationLib("'corechart','geochart','table'"); my $cOS = drawPieChart("nodes","os","Operating System",390,300,"chart_os"); my $cArch = drawPieChart("nodes","arch","Architecture",390,300,"chart_arch"); my $cRelease = drawPieChart("nodes","release","FHEM Release",390,300,"chart_release"); my $cPerl = drawPieChart("nodes","perl","Perl Version",390,300,"chart_perl"); my $cModulesTop10 = drawColumnChartTop10Modules("modules","modulestop10",,"Used",800,300,"chart_modulestop10"); my $cModDefTop10 = drawColumnChartTop10ModDef("modules","definitions","Definitions",800,300,"chart_moddeftop10"); #my $cModules = drawBarChartModules("modules","modules","Used","Definitions",800,600,"chart_modules"); my $mWorld = drawRegionsMap("locations","countryname","world","map_world"); my $mEU = drawRegionsMap("locations","countryname","150","map_europe"); my $mWesternEU = drawMarkersMap("locations","city","155","map_germany"); my $tModules = drawTable3cols("modules","total_modules","string","Module","number","Used","number","Definitions","table_modules"); #my $tModDef = drawTable("modules","total_moddef","string","Module","number","Definitions","table_moddef"); my $tModels = drawTable2cols("models","total_models","string","Model","boolean","defined","table_models"); my @res = $dbh->selectrow_array("SELECT created FROM db"); my $since = "@res"; print header; print start_html( -title => 'fhem.de - Statistics', -author => 'm_fischer@gmx.de', -base => 'true', -style => {-src => $css}, -meta => {'keywords' => 'fhem houseautomation statistics'}, -script => [ { -type => 'text/javascript', -src => 'https://www.google.com/jsapi', }, $visLib, $cOS, $cArch, $cRelease, $cPerl, $cModulesTop10, $cModDefTop10, #$cModules, $mWorld, $mEU, $mWesternEU, $tModules, $tModels, ], ); my ($nodes) = $dbh->selectrow_array("SELECT COUNT(uniqueID) FROM nodes"); print <