#!/usr/bin/perl -Tw # # use strict; use DBI; use Config::IniFiles; use Getopt::Long; use vars qw($logfile $configfile $quiet $help $version @nodes); use POSIX qw(strftime); my $_version = '1.0'; my %options = ( "--config-file|C=s" => \$configfile, "--quiet|q" => \$quiet, "--help|h" => \$help, "--version|v" => \$version, "--node=s" => \@nodes, ); Getopt::Long::config("no_ignore_case"); GetOptions(%options); @nodes=split(/,/,join(',',@nodes)); if($help) { print STDERR < $configfile; my $dbtype = $ini->val('database', 'type') || 'mysql'; my $dbhost = $ini->val('database', 'host') || 'localhost'; my $dbuser = $ini->val('database', 'user') || 'root'; my $dbpasswd = $ini->val('database', 'password') || ''; my $dbname = $ini->val('database', 'database') || 'lms'; my $dbase; if($dbtype eq "mysql") { $dbase = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 }); } elsif($dbtype eq "postgres") { $dbase = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 }); } else { print STDERR "Fatal error: unsupported database type: $dbtype, exiting.\n"; exit 1; } if (@nodes) { my $nodelist="nodeid="; $nodelist.=pop(@nodes); foreach my $nodeid (@nodes) { $nodelist="$nodelist OR nodeid=$nodeid"; }; my $dbq=$dbase->prepare("SELECT SUM(download) AS download,SUM(upload) AS upload FROM stats WHERE ($nodelist) AND dt>UNIX_TIMESTAMP()-300"); $dbq->execute(); my $row = $dbq->fetchrow_hashref(); if ($row->{'upload'}) { print "$row->{'upload'}\n" } else { print "0\n"; } if ($row->{'download'}) { print "$row->{'download'}\n"; } else { print "0\n"; } print "gateway.perfect.net.pl\n"; print "-\n"; $dbq->finish; $dbase->disconnect(); }