#!/usr/bin/perl -w # Copyright (c) 2009 Tobias Richter # use strict; use warnings; use Socket; use Sys::Hostname; use DBI; use DBD::mysql; sub ncpConnections { my $ncpcon = `/sbin/ncpcon connections | grep "Licensed Connections"`; $ncpcon =~ /Licensed Connections\s+(.*)/; my $connections = $1; return ( $connections ); } sub currentTime { # expects no inputs # returns Month, Day, Year, Hour, and Min # variables my $sec; my $min; my $hour; my $mday; my $mon; my $year; my $wday; my $yday; my $isdst; # get the current system date/time for later use ( $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst ) = localtime(time); # adjust mon $mon += 1; # adjust year $year += 1900; # ensure all values have leading zeros for proper comparisons $mon = "0" . $mon if $mon < 10; # month 1-12 $mday = "0" . $mday if $mday < 10; # day 1-31 $hour = "0" . $hour if $hour < 10; # hour 0-23 $min = "0" . $min if $min < 10; # min 0-59 $sec = "0" . $sec if $sec < 10; # sec 0-59 # return values return ( $mon, $mday, $year, $hour, $min, $sec, $wday ); } # get current system date/time my $month = ""; my $day = ""; my $year = ""; my $hour = ""; my $minute = ""; my $second = ""; my $weekDay; my $dateStamp = ""; my $timeStamp = ""; my $connections = ""; ( $month , $day , $year , $hour , $minute, $second, undef ) = currentTime; # format the date/time stamp $dateStamp = "$year$month$day"; $timeStamp = "$hour$minute$second"; # get connections ( $connections ) = ncpConnections; # get hostname ( $hostname ) = hostname(); # do the mysql connection my $dbserver = "nda04170i"; my $dbname = "statistics"; my $dbuser = "statistics"; my $dbpassword = "dachser"; my $dsn = "DBI:mysql:$dbname:$dbserver"; my $dbh = DBI->connect($dsn,$dbuser,$dbpassword,{RaiseError=>1}) || die ("Could not open $dsn! ");; # insert the statistics to the mysql db my $sth1 = $dbh->prepare ( q{INSERT INTO connections (DATE,TIME,SERVER,CONNECTION) VALUES (?,?,?,?)}); $sth1->execute($dateStamp,$timeStamp,$hostname,$connections); $sth1->finish; $dbh->disconnect(); #print "$timeStamp;$hostname;$connections\n";