I wrote this little utility because I run a process on several servers that create a ton of data. I unfortunately need to share my partition with another group, who also runs process that write data to the disk. So, as part 1 of my "process management scheme", I want to be notified when disk space reaches 50%, at which point I can transfer some data off of that server. I am planning on running this as a cron job, on an hourly basis.
My question is, how can I do this without opening and closing my output file so many times?
The (cough) code:
#!/usr/local/bin/perl -w
use strict;
use Sys::Hostname;
my ($fs, $kbytes, $used, $avail, $cap, $mount);
my $line;
my $host = hostname();
my $date = `date +%Y%m%d%H`;
my $outfile = "/export/home/steve/disk_log.$host.$date";
my $sendmail ='/usr/lib/sendmail -t ';
my $mailhdr = "To: steve\@foo.com\n".
"Subject: Disk usage alarm - $host - $date\n======================
+================================\n\n";
open (OUTFILE, ">$outfile") || die "Can't open $outfile: $!\n";
print OUTFILE $mailhdr;
close (OUTFILE);
open (OUTFILE, ">>$outfile") || die "Can't open $outfile: $!\n";
my $cmd = `df -k /usr >> $outfile`;
close (OUTFILE);
open (OUTFILE, "$outfile") || die "Can't open $outfile: $!\n";
while ($line = <OUTFILE>) {
chomp $line;
next unless ($line =~ /^\//);
($fs, $kbytes, $used, $avail, $cap, $mount) = split(' ', $line);
$cap =~ s/\%//;
}
close (OUTFILE);
if ($cap >= 50) {
system( "$sendmail < $outfile") == 0 or die "Can't Mail: $!\n";
}
else {
exit;
}
May the laughter commence.......
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.