use DBI; use DBD::mysql; use Win32::API; use Win32::PerfMon; use Win32::DriveInfo; use Sys::Hostname; my @drives; my %hdds; my ($sec,$min,$hour,$mday,$month,$year, $wday,$yday,$isdst); my ($bmem, $mem, $host, $dbh, $serverID, $usedMB, $logdate, $t, $cpuload); my ($TotalNumberOfBytes, $totalMB, $volume, $drive, $sth, $ref); my ($TotalNumberOfFreeBytes); my ($len, $load, $totalphys, $availphys, $totalpage, $availpage, $totalvirt, $availvirt); my $grabInterval = 60; my $hddcounter = 1440; my $strSQL = ''; my $logserver = '10.1.1.1'; #---------------------------- Get server_id ------------------------------------ $host = hostname; $dbh = DBI->connect("DBI:mysql:database=performance;host=$logserver", "username", "password", {'RaiseError' => 1}); $sth = $dbh->prepare("SELECT server_id FROM servers WHERE server_name = \'$host\'"); $sth->execute(); $ref = $sth->fetchrow_hashref(); $sth->finish(); $serverID = $ref->{'server_id'}; if (! defined $ref->{'server_id'}) #This server is not in database - update DB { $dbh->do("INSERT INTO servers (server_name) VALUES (" . $dbh->quote("$host") . ")"); my $sth = $dbh->prepare("SELECT server_id FROM servers WHERE server_name = \'$host\'"); $sth->execute(); my $ref = $sth->fetchrow_hashref(); $serverID = $ref->{'server_id'}; $sth->finish(); $bmem = " " x 32; $mem = new Win32::API('kernel32', 'GlobalMemoryStatus', 'P', 'N'); $mem->Call($bmem); ($len, $load, $totalphys, $availphys, $totalpage, $availpage, $totalvirt, $availvirt) = unpack('IIIIIIII', $bmem); $totalMB = int($totalphys/1024/1024); $dbh->do("INSERT INTO mem VALUES ($serverID, $totalMB, 80)"); @drives = Win32::DriveInfo::DrivesInUse(); foreach $drive (@drives) { if (Win32::DriveInfo::DriveType($drive) == 3 ) #If the disk is HDD { $TotalNumberOfBytes = (Win32::DriveInfo::DriveSpace($drive ))[5]; $totalMB = int($TotalNumberOfBytes / 1048576); $dbh->do("INSERT INTO hdd (server_id, volume_name, capacityMB, threshold) " . "VALUES ($serverID, ".$dbh->quote("$drive").", $totalMB, 70)"); } } } $sth = $dbh->prepare("SELECT volume_id, volume_name FROM hdd WHERE server_id = \'$serverID\'"); $sth->execute(); while ($ref = $sth->fetchrow_hashref()) { $hdds{$ref->{'volume_name'}} = $ref->{'volume_id'}; } $sth->finish(); #------------------------------------------------------------------------------- my $PerfObj = Win32::PerfMon->new("."); $PerfObj->AddCounter("Processor","% Processor Time", "_Total"); while (1) #Main loop, infinite... { #Calculate the CPU load for (1..$grabInterval) { #Calculate the date for logging into database ($sec,$min,$hour,$mday,$month,$year, $wday,$yday,$isdst) = localtime time; $month++; foreach $t ($mday, $month, $hour, $min, $sec) { if ($t < 10) {$t = '0' . $t;} } $year=substr($year,1); $year="20".$year if length($year)==2; $logdate="$year-$month-$mday $hour:$min:$sec"; # Populate the counters from perfmon $PerfObj->CollectData(); $cpuload = $PerfObj->GetCounterValue("Processor","% Processor Time", "_Total"); # Do something with $value - e.g. store it in a DB $strSQL .= " (\'$logdate\', $serverID, $cpuload),"; sleep 1; } #Dump the gathered CPU load data into the database chop $strSQL; $dbh->do("INSERT INTO cpu_usage VALUES $strSQL"); $strSQL = ''; #Clear the variable for the next cycle #Calculate the memory usage $bmem = " " x 32; $mem = new Win32::API('kernel32', 'GlobalMemoryStatus', 'P', 'N'); $mem->Call($bmem); ($len, $load, $totalphys, $availphys, $totalpage, $availpage, $totalvirt, $availvirt) = unpack('IIIIIIII', $bmem); $usedMB = int(($totalphys-$availphys)/1024/1024); #Store used memory in MB #Log the usage into the database $dbh->do("INSERT INTO mem_usage VALUES (".$dbh->quote("$logdate").", $serverID, $usedMB)"); #HDD usage logging if ($hddcounter == 1440) { $hddcounter = 0; foreach $drive (keys %hdds) { $TotalNumberOfFreeBytes = (Win32::DriveInfo::DriveSpace($drive ))[6]; $TotalNumberOfBytes = (Win32::DriveInfo::DriveSpace($drive ))[5]; $usedMB = int(($TotalNumberOfBytes - $TotalNumberOfFreeBytes)/1048576); #Create SQL statement $logdate="$year-$month-$mday"; $volume = $hdds{$drive}; $dbh->do("INSERT INTO hdd_usage VALUES (\'$logdate\', \'$volume\', $usedMB)"); } }else { $hddcounter++; } } $dbh->disconnect();