Here is the current version of the source
If you want the other version, it used Win32:Console to make it pretty. I also have a CGI script to read the log file and convert it into a into a graph using OLE, and saves a PNG/GIF etc. and for web display.
As always any comment and suggestions welcome.
# SPACEd
# Daemon to check free Space on DAT01
my $ver = "v2.0.0beta1";
use warnings;
use MySubs;
use Win32::AdminMisc;
use Win32::OLE;
use Win32::Daemon;
use Win32::EventLog;
START_DAEMON();
my $MB = 1024 ** 2;
my $hour = 60*60;
my $SERVERPATH = uc(shift) || "\\\\SERVER\\SHARE\\";
my $ALERT = (shift) * $MB || 60000 * $MB; # for testing email
#my $ALERT = (shift) * $MB || 2000 * $MB;
if ($SERVERPATH !~ /\\$/)
{
$SERVERPATH=$SERVERPATH.'\\';
};
while (1)
{
my $sleep_time = ($hour*2);
if ( GET_DRIVES($SERVERPATH) )
{
$sleep_time = ($hour/2);
}
for (1 .. int($sleep_time/60))
{
if ( Win32::Daemon::State() eq SERVICE_STOP_PENDING )
{
Win32::Daemon::StopService();
die;
}
sleep 60;
}
}
sub GET_DRIVES
{
my $OK;
my $drive = shift;
my $TIME = localtime;
my ($sectors_per_cluster, $bytes_per_cluster, $free_clusters, $tot
+al_clusters) = Win32::AdminMisc::GetDriveGeometry($drive);
my $free = $sectors_per_cluster * $bytes_per_cluster * $free_clust
+ers;
my $total = $sectors_per_cluster * $bytes_per_cluster * $total_clu
+sters;
LOG_CSV($free);
print $drive,"\n";
print $TIME,"\n";
if ($free)
{
my $align = 42;
if ($free < $ALERT)
{
undef $OK;
email($drive,$free,$total);
} else {
$OK = 1;
}
} else {
undef $OK;
}
return $OK;
}
sub email
{
my $path = shift;
my $free = shift;
my $total = shift;
my $TIME = localtime;
my @TOLIST = ('bowersm'); # for testing
# my @TOLIST =('Aust ISD Hardware/Software Support','Aust ISD Syste
+ms Admin','toglej');
my $message = "<HTML><FONT SIZE=6><B><I><FONT COLOR=NAVY>LOW SPACE
+ on ".$path."<\/FONT COLOR><\/B><\/I><HR color=red><FONT SIZE=5>There
+ is currently <FONT COLOR=RED>".&COMMA(int($free/1048576))."<\/FONT C
+OLOR>Mb free of <FONT COLOR=RED>".&COMMA(int($total/1048576))."<\/FON
+T COLOR>Mb total on <FONT COLOR=NAVY>".$path.".<\/FONT COLOR><BR>This
+ is below the alert mark of <FONT COLOR=RED>".&COMMA(int($ALERT/10485
+76))."<\/FONT COLOR>Mb.<BR>CSC may want to look at this!!!<HR color=r
+ed>".$TIME."\n";
eval {$OUTLOOK = Win32::OLE->GetActiveObject('Outlook.Application'
+)};
die "Outlook not installed" if $@;
unless (defined $OUTLOOK)
{
$OUTLOOK = Win32::OLE->new('Outlook.Application', sub {$_[0]->
+Quit;})
or die "Oops, cannot start Outlook";
}
$mailitem = $OUTLOOK->CreateItem(0);
map {$mailitem->Recipients->Add($_)} @TOLIST;
$mailitem->{"Subject"} = "LOW SPACE on ".$path." \@ ".$TIME;
$mailitem->{"Categories"} = 'LOW SPACE!!!' ;
$mailitem->{"HTMLBody"} = $message;
$mailitem->Send();
undef $OUTLOOK,@TOLIST, $message, $TIME, $path, $free, $total;
}
sub LOG_CSV
{
my $NEW;
my $NOW = localtime;
my $NOW_CONV = CONV_LOCALTIME($NOW);
my $FREE = shift;
my %LOG->{$NOW_CONV } = $FREE;
my @days = qw/MON TUE WED THU FRI SAT SUN/;
if (open (CSV, "<d:/LOG_v2.CSV"))
{
foreach (<CSV>)
{
next if /Date/;
chomp;
my ($DATE, $SIZE) = split /,/, $_;
%LOG->{$DATE} = $SIZE;
}
close CSV;
}
open (CSV, ">d:/LOG_v2.CSV");
print CSV "Date,Bytes,Megabytes\n";
foreach $key (sort keys %LOG)
{
print CSV "$key,".%LOG->{$key}.",".(%LOG->{$key}/1048576)."\n"
+;
}
close CSV;
}
sub CONV_LOCALTIME
{
my %MONTHS = (
"Jan" => '01',
"Feb" => '02',
"Mar" => '03',
"Apr" => '04',
"May" => '05',
"Jun" => '06',
"Jul" => '07',
"Aug" => '08',
"Sep" => '09',
"Oct" => '10',
"Nov" => '11',
"Dec" => '12'
);
my ($DAY, $MON, $DATE, $TIME, $YEAR) = split /\s+/, shift;
my $CONV = join(" ",$YEAR, %MONTHS->{$MON}, sprintf("%02d",$DATE),
+ $TIME);
return $CONV;
}
sub START_DAEMON
{
# Tell the OS to start processing the service...
Win32::Daemon::StartService();
# Wait until the service manager is ready for us to continue...
while( SERVICE_START_PENDING != Win32::Daemon::State() )
{
sleep( 1 );
}
# Now let the service manager know that we are running...
Win32::Daemon::State( SERVICE_RUNNING );
# EVENT();
}
sub EVENT
{
#### Adding Event log entries
#### Not implemented
%EVENT = (
Computer => '',
Source => '',
EventType => EVENTLOG_INFORMATION_TYPE,
Catergory => '1048576',
# EventID => '',
Strings => 'TEST'
);
$handle=Win32::EventLog->new("Application");
$handle->Report(\%EVENT);
}
|