#!/usr/bin/perl use strict; use Win32::DriveInfo; use Net::MySQL; use Sys::Hostname; my @drives; my ($TotalNumberOfFreeBytes, $TotalNumberOfBytes, $totalMB, $totalFreeMB, $UsedMB); my ($t, $date, $record_set, $record, $volume, $SQL, $host, $drive); my $mysql = Net::MySQL->new( hostname => 'host', # Default use UNIX socket database => 'hdd_capacity', user => 'user', password => 'pass' ); #Calculate the date for logging into database my ($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; $date="$year-$month-$mday"; $host = hostname; print "hostname = $host\n"; @drives = Win32::DriveInfo::DrivesInUse(); foreach $drive (@drives) { if (Win32::DriveInfo::DriveType($drive) == 3 ) #If the disk is HDD { $TotalNumberOfFreeBytes = (Win32::DriveInfo::DriveSpace($drive ))[6]; $TotalNumberOfBytes = (Win32::DriveInfo::DriveSpace($drive ))[5]; $totalMB = int($TotalNumberOfBytes / 1048576); $totalFreeMB = int($TotalNumberOfFreeBytes / 1048576); $UsedMB = $totalMB - $totalFreeMB; print "$drive, $totalMB, $totalFreeMB\n"; #SELECT volume_id for current volume $SQL = "SELECT volume_id FROM hdd_info WHERE server_name = \'$host\' AND volume_name = \'$drive\'"; $mysql->query($SQL); $record_set = $mysql->create_record_iterator; $record = $record_set->each; $volume = $record->[0]; print "Volume = $volume\n"; #Create SQL statement $SQL = "insert into hdd_usage values (\'$date\', \'$volume\', \'$UsedMB\')"; $mysql->query($SQL); # Execute SQL statement - log the data die $mysql->get_error_message if $mysql->is_error; } } $mysql->close; #-----------------------------------------------------------------