Friday, 6 March 2009

Perl - how to read in a file

open(DF_FILE, "<$file");

my @DF_CHECK = ;
close(DF_FILE);
unlink $file;

print join("\n", @DF_CHECK) . "\n" if $DEBUG;

LINE: foreach (@DF_CHECK)
{

next LINE if (/^$/ || /^Filesystem/); # skip the headings or blank lines

print "line: " . $_ . "\n" if $DEBUG;

my @line = split();
my ($fs, $size, $user, $avail, $used_pct, $mount);


if (@line eq 6) {
($fs, $size, $user, $avail, $used_pct, $mount) = @line;
} elsif (@line eq 5) {
$fs = $last_line;
($size, $user, $avail, $used_pct, $mount) = @line;
} else {
$used_pct = 0;
}

$used_pct =~ s/\%//g;
if ($used_pct > $alert_limit ) {
print "alert flag set\n" if $DEBUG;
$alert_flag = "true";
$message = $message . "\t" . $fs . ", used: " . $used_pct . "%, mount: " . $mount . "\r\n";
}

$last_line = $_;
}

if ($alert_flag eq "true" ) {
return $message;
}

return undef;
}