LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2010, 10:27 AM   #1
dnaqvi
Member
 
Registered: Oct 2009
Posts: 117

Rep: Reputation: 15
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";
}
 
Old 03-19-2010, 11:30 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
I would say you are creating an empty output file and not checking to see if it contains data.
Code:
my $output = "";

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
}
Suggested fix

Code:
if ($output ne ""){
open my $OUT, ">", $log_file;
print $OUT $output, "\n";
close $OUT;
}

Last edited by smoker; 03-19-2010 at 11:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
system boots very slow when reaches send mail service JavaWorks Linux - Newbie 2 02-18-2009 01:28 PM
Linpus Fedora after saving files to file system, CAN'T open FILE SYSTEM from desktop! remiwa Fedora 2 01-07-2009 07:28 AM
when does linux switch from realmode file system to protectedmode file system sitthar Linux - General 3 04-08-2006 08:05 AM
figuring out 'file system' and 'swap file system' types TrulyTessa Linux - Newbie 3 09-26-2005 06:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration