LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   df script not working (https://www.linuxquestions.org/questions/linux-newbie-8/df-script-not-working-821389/)

kma07 07-21-2010 10:50 PM

df script not working
 
Hi guys, I have a script that checks disk usage and emails if it is low but it keeps returning this error:


Use of uninitialized value in concatenation (.) or string at ./df.pl line 17.
tail: cannot open `+2' for reading: No such file or directory
Use of uninitialized value in addition (+) at ./df.pl line 20.
Use of uninitialized value in addition (+) at ./df.pl line 20.
Use of uninitialized value in division (/) at ./df.pl line 20.
Illegal division by zero at ./df.pl line 20.


Here's the script:

Code:

#!/usr/bin/perl
use strict;
use warnings;

# file system to monitor
my $target = shift;

# warning level
my $warning_level=10;

# email setup
my $to='myemail@email.com';
my $from='email@myemail.com.au';
my $subject='Low Disk Space Warning';

# get df
my ($fs_dev, $fs_type, $size, $used, $avail, $use, $mount) = split(" ", `df -PT $target | tail +2`);

# calculate
my $df_free = (($avail) / ($avail+$used)) * 100.0;

# compare
if ($df_free < $warning_level) {
my $out = sprintf("WARNING Low Disk Space on $target : %0.2f%% ()\n",$df_free);

# send email using UNIX/Linux sendmail
open(MAIL, "|/usr/sbin/sendmail -t");

## Mail Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n";

## Mail Body
print MAIL $out;

close(MAIL);
}


Any ideas?

David the H. 07-21-2010 11:59 PM

Well, we can start with this for one thing:

"tail: cannot open `+2' for reading: No such file or directory"

Code:

man tail
...
-n, --lines=K    output  the  last  K  lines, instead of the last 10; or use -n +K to output lines starting with the Kth
...

So it's "tail -n +2", not "tail +2".

kma07 07-22-2010 12:07 AM

Works now!
 
Works now!

Thanks


All times are GMT -5. The time now is 08:17 AM.