File System reaches 90% or over
Here is the program for Linux machine.
GOAL:
----
User will get notify via email when any File System reaches 90% or over 90% of its used space.
User will notify as below:
Filesystem: /bkup
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/backup-BKUP
14G 12G 1.3G 91% /bkup
Large Files:
/bkup/prtlprd6_bk20100312_aft2ndjcr.xml
/bkup/prtlprd6_bk20100312_aft2ndjcr_b4toq.xml
New Files:
/bkup
/bkup/prtlprd6_bk20100312_aft2ndjcr.xml
/bkup/prtlprd6_bk20100312_aft2ndjcr_b4toq.xml
/bkup/lost+found
The user will receive information of effected file system only as above.
This part its working through script.
But here is the issue.
We have 30 servers.
Only file system of two servers exceeded the limits.
We received the notification from effected servers. That' fine.
But we also received blank notification from all other servers which are not exceeds limits.
We want to receive email from only effected servers.
CODE:
#!/usr/bin/perl
use strict;
use File::Find;
our ( @fs, @lines, @new_files, @big_files, %info );
my $threshold = 90; # disk usage %
my $age = 86400; # seconds in 1 day
my $size = 100; # in MB
my $when = time() - $age;
my $log_file = "/home/dn/FSwatch.log";
my $output = "";
@fs = ( '/', '/dev', '/bkup', '/opt', '/tmp', '/var', '/usr' );
foreach my $fs ( @fs ) {
undef @new_files;
undef @big_files;
@lines = `df -k $fs`;
$lines[ -1 ] =~ /(\d+)\%/;
if ( $1 > $threshold ) {
$output .= "Filesystem: $fs\n";
my $df = `df -h $fs`;
$output .= $df;
find( \&wanted, $fs );
if ( scalar @big_files ) {
$output .= " Large Files:\n";
foreach my $file ( @big_files ) {
$output .= " $file";
if ( defined $info{ $file } ) {
$output .= " - $info{ $file }"
}
$output .= "\n";
}
}
if ( scalar @new_files ) {
$output .= " New Files:\n";
foreach my $file ( @new_files ) {
$output .= " $file";
if ( defined $info{ $file } ) {
$output .= " - $info{ $file }";
}
$output .= "\n";
}
}
$output .= "\n";
}
}
open my $OUT, ">", $log_file;
print $OUT $output, "\n";
close $OUT;
if ( -e $log_file ) {
system(
qq~mail -s "FS reaches 90% in prtltst6" DN\@email.com< $log_file~
);
unlink $log_file; # cleanup after run
}
sub wanted {
my ( $filesize, $mtime ) = ( stat( $File::Find::name ) )[ 7, 9 ];
my $temp = sprintf( "%.2f", $filesize / 1024 / 1024 ); # convert bytes to MB
my $when = localtime( $mtime );
if ( $temp >= $size ) {
push( @big_files, $File::Find::name );
}
if ( $mtime > $when ) {
push( @new_files, $File::Find::name );
}
$info{ "$File::Fine::name" } = "$temp MB / $when";
}
|