<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Perl script to monitor disk space and send an email</title> <atom:link href="http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html/feed" rel="self" type="application/rss+xml" /><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html</link> <description>This is a Linux sys admin journal by Vivek about sys admin work, Linux tips &#38; tricks, hacks, news and more.</description> <lastBuildDate>Fri, 10 Feb 2012 20:37:43 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: paulcano</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-175977</link> <dc:creator>paulcano</dc:creator> <pubDate>Thu, 24 Nov 2011 01:02:17 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-175977</guid> <description>I am getting this issue on 64-bit RHEL and CentOS:
Illegal division by zero at ./df.pl line 23.
Line 23 is
my $df_free = (($avail) / ($avail+$used)) * 100.0;</description> <content:encoded><![CDATA[<p>I am getting this issue on 64-bit RHEL and CentOS:</p><p>Illegal division by zero at ./df.pl line 23.</p><p>Line 23 is<br
/> my $df_free = (($avail) / ($avail+$used)) * 100.0;</p> ]]></content:encoded> </item> <item><title>By: Andre</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-175500</link> <dc:creator>Andre</dc:creator> <pubDate>Tue, 08 Nov 2011 00:47:22 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-175500</guid> <description>Thanks for the script.  I tweaked it so that it would monitor multiple folders.  I also added a &quot;--test&quot; command line option to send out a test email.  Here it is:
&lt;pre&gt;#!/usr/bin/perl
# Available under BSD License. See url for more info:
# http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html
# Pass in command line parameter &quot;--test&quot; to perform an email test.
use strict;
use warnings;
use Filesys::DiskSpace;
# ---------------------------
# Configuration section
# ---------------------------
# default warning level
my $default_warning_level=10;
# email setup
my $to=&#039;admin@yourdomain.com&#039;;
my $from=&#039;webmaster@YOURDOMAIN.COM&#039;;
my $subject=&#039;Low Disk Space&#039;;
# folders to check
sub perform_checks {
&amp;check_free_space(&quot;/&quot;, 30);
&amp;check_free_space(&quot;/mnt/data&quot;);
&amp;check_free_space(&quot;/mnt/storage&quot;);
&amp;check_free_space(&quot;/mnt/backup&quot;, 20);
}
# ---------------------------
# Main script
# ---------------------------
my $out = &quot;&quot;;
# Check free space for folder and add to email message if it is below warning level.
# Parameters:
#   folder_to_check - The path of the filesystem to check.
#   warning_level - If the free disk space percentage is below this level,
#                   an email is sent.  If this parameter is omitted, the
#                   default level will be used.
sub check_free_space {
my ($dir, $warning_level) = @_;
# set warning level to default if not specified
if(!defined($warning_level)) {
$warning_level = $default_warning_level;
}
# get df
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
# calculate
my $df_free = ($avail / ($avail + $used)) * 100.0;
# compare
if (($df_free &lt; $warning_level) &#124;&#124; (defined($ARGV[0]) &amp;&amp; ($ARGV[0] eq &quot;--test&quot;))) {
# append to email
$out .= sprintf(&quot;WARNING Low Disk Space on $dir : %0.2f%% ()\n&quot;,$df_free);
}
}
&amp;perform_checks;
# check if there are warnings to email
if($out ne &quot;&quot;) {
# send email using UNIX/Linux sendmail
open(MAIL, &quot;&#124;/usr/sbin/sendmail -t&quot;);
## Mail Header
print MAIL &quot;To: $to\n&quot;;
print MAIL &quot;From: $from\n&quot;;
print MAIL &quot;Subject: $subject\n&quot;;
## Mail Body
print MAIL $out;
}
close(MAIL);&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>Thanks for the script.  I tweaked it so that it would monitor multiple folders.  I also added a &#8220;&#8211;test&#8221; command line option to send out a test email.  Here it is:</p><pre>#!/usr/bin/perl
# Available under BSD License. See url for more info:
# <a href="http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html" rel="nofollow">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html</a>
# Pass in command line parameter "--test" to perform an email test.
use strict;
use warnings;
use Filesys::DiskSpace;
# ---------------------------
# Configuration section
# ---------------------------
# default warning level
my $default_warning_level=10;
# email setup
my $to='admin@yourdomain.com';
my $from='webmaster@YOURDOMAIN.COM';
my $subject='Low Disk Space';
# folders to check
sub perform_checks {
    &amp;check_free_space("/", 30);
    &amp;check_free_space("/mnt/data");
    &amp;check_free_space("/mnt/storage");
    &amp;check_free_space("/mnt/backup", 20);
}
# ---------------------------
# Main script
# ---------------------------
my $out = "";
# Check free space for folder and add to email message if it is below warning level.
# Parameters:
#   folder_to_check - The path of the filesystem to check.
#   warning_level - If the free disk space percentage is below this level,
#                   an email is sent.  If this parameter is omitted, the
#                   default level will be used.
sub check_free_space {
    my ($dir, $warning_level) = @_;
    # set warning level to default if not specified
    if(!defined($warning_level)) {
        $warning_level = $default_warning_level;
    }
    # get df
    my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
    # calculate
    my $df_free = ($avail / ($avail + $used)) * 100.0;
    # compare
    if (($df_free &lt; $warning_level) || (defined($ARGV[0]) &amp;&amp; ($ARGV[0] eq &quot;--test&quot;))) {
        # append to email
        $out .= sprintf(&quot;WARNING Low Disk Space on $dir : %0.2f%% ()\n&quot;,$df_free);
    }
}
&amp;perform_checks;
# check if there are warnings to email
if($out ne &quot;&quot;) {
    # send email using UNIX/Linux sendmail
    open(MAIL, &quot;|/usr/sbin/sendmail -t&quot;);
    ## Mail Header
    print MAIL &quot;To: $to\n&quot;;
    print MAIL &quot;From: $from\n&quot;;
    print MAIL &quot;Subject: $subject\n&quot;;
    ## Mail Body
    print MAIL $out;
}
close(MAIL);</pre>]]></content:encoded> </item> <item><title>By: jawg02</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-169149</link> <dc:creator>jawg02</dc:creator> <pubDate>Thu, 24 Feb 2011 20:04:32 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-169149</guid> <description>resolved using different arguments:
h2ph * sys/*</description> <content:encoded><![CDATA[<p>resolved using different arguments:</p><p>h2ph * sys/*</p> ]]></content:encoded> </item> <item><title>By: jawg02</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-169143</link> <dc:creator>jawg02</dc:creator> <pubDate>Thu, 24 Feb 2011 18:13:39 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-169143</guid> <description>Cant get this script to run. Can anyone help?
running on Solaris 10
Makefile.PL  and make    complete successfully
make test spits out errors
# make test
PERL_DL_NONLAZY=1 /usr/bin/perl &quot;-MExtUtils::Command::MM&quot; &quot;-e&quot; &quot;test_harness(0, &#039;blib/lib&#039;, &#039;blib/arch&#039;)&quot; t/*.t
t/1st............ok
t/freebsd-ufs....skipped
all skipped: no reason given
t/linux-ext2.....skipped
all skipped: no reason given
t/linux-vfat.....skipped
all skipped: no reason given
t/solaris-ufs....An error occured. statvfs failed. Did you run h2ph?
FAILED before any test output arrived
Failed Test     Stat Wstat Total Fail  Failed  List of Failed
-----------------------------------------------------------------
t/solaris-ufs.t               ??   ??       %  ??
3 tests skipped.
Failed 1/5 test scripts, 80.00% okay. 8/9 subtests failed, 11.11% okay.
*** Error code 29
make: Fatal error: Command failed for target `test_dynamic&#039;
running h2ph hangs after &#039;redefine&#039; message
# h2ph -r -l
require &#039;_h2ph_pre.ph&#039;;
no warnings &#039;redefine&#039;;            // hangs here
I added require &#039;_h2ph_pre.ph&#039;; to the h2ph and re-ran with the same result.
What&#039;s up with this?
Solaris 10</description> <content:encoded><![CDATA[<p>Cant get this script to run. Can anyone help?</p><p>running on Solaris 10</p><p>Makefile.PL  and make    complete successfully<br
/> make test spits out errors</p><p># make test<br
/> PERL_DL_NONLAZY=1 /usr/bin/perl &#8220;-MExtUtils::Command::MM&#8221; &#8220;-e&#8221; &#8220;test_harness(0, &#8216;blib/lib&#8217;, &#8216;blib/arch&#8217;)&#8221; t/*.t<br
/> t/1st&#8230;&#8230;&#8230;&#8230;ok<br
/> t/freebsd-ufs&#8230;.skipped<br
/> all skipped: no reason given<br
/> t/linux-ext2&#8230;..skipped<br
/> all skipped: no reason given<br
/> t/linux-vfat&#8230;..skipped<br
/> all skipped: no reason given<br
/> t/solaris-ufs&#8230;.An error occured. statvfs failed. Did you run h2ph?<br
/> FAILED before any test output arrived<br
/> Failed Test     Stat Wstat Total Fail  Failed  List of Failed<br
/> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br
/> t/solaris-ufs.t               ??   ??       %  ??<br
/> 3 tests skipped.<br
/> Failed 1/5 test scripts, 80.00% okay. 8/9 subtests failed, 11.11% okay.<br
/> *** Error code 29<br
/> make: Fatal error: Command failed for target `test_dynamic&#8217;</p><p>running h2ph hangs after &#8216;redefine&#8217; message<br
/> # h2ph -r -l<br
/> require &#8216;_h2ph_pre.ph&#8217;;<br
/> no warnings &#8216;redefine&#8217;;            // hangs here</p><p>I added require &#8216;_h2ph_pre.ph&#8217;; to the h2ph and re-ran with the same result.</p><p>What&#8217;s up with this?<br
/> Solaris 10</p> ]]></content:encoded> </item> <item><title>By: Marek</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-168814</link> <dc:creator>Marek</dc:creator> <pubDate>Thu, 17 Feb 2011 15:24:17 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-168814</guid> <description>I got this on CentOS 5.5 64-bit:
./df.pl
Illegal division by zero at ./df.pl line 23.</description> <content:encoded><![CDATA[<p>I got this on CentOS 5.5 64-bit:</p><p>./df.pl<br
/> Illegal division by zero at ./df.pl line 23.</p> ]]></content:encoded> </item> <item><title>By: Satya</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-160062</link> <dc:creator>Satya</dc:creator> <pubDate>Wed, 06 Oct 2010 02:09:01 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-160062</guid> <description>Hi RC,
I have just basic knowledge of UNIX Cmd and now want to write Unix PErl Script.
.which book I can refer to go with.
Thanks,</description> <content:encoded><![CDATA[<p>Hi RC,<br
/> I have just basic knowledge of UNIX Cmd and now want to write Unix PErl Script.<br
/> .which book I can refer to go with.</p><p>Thanks,</p> ]]></content:encoded> </item> <item><title>By: Satya</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-160061</link> <dc:creator>Satya</dc:creator> <pubDate>Wed, 06 Oct 2010 02:05:46 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-160061</guid> <description>Hi All,
I am very new to Unix. Just got a new assignment where I need to write Unix Perl Script.
Can some one segues me which book can help to know Perl script for beginner.
Please help..with link also.
Thanks</description> <content:encoded><![CDATA[<p>Hi All,<br
/> I am very new to Unix. Just got a new assignment where I need to write Unix Perl Script.<br
/> Can some one segues me which book can help to know Perl script for beginner.<br
/> Please help..with link also.</p><p>Thanks</p> ]]></content:encoded> </item> <item><title>By: Fitri</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-156788</link> <dc:creator>Fitri</dc:creator> <pubDate>Wed, 23 Jun 2010 09:07:10 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-156788</guid> <description>sorry I&#039;m new in hardware programing.
can I use vb6 / .net programing to  monitor disk space and send an email under Linux ?</description> <content:encoded><![CDATA[<p>sorry I&#8217;m new in hardware programing.<br
/> can I use vb6 / .net programing to  monitor disk space and send an email under Linux ?</p> ]]></content:encoded> </item> <item><title>By: Shan</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-154662</link> <dc:creator>Shan</dc:creator> <pubDate>Thu, 01 Apr 2010 04:35:44 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-154662</guid> <description>Hi ,
I am a newbie in perl....can any body tell how to use the same script when i am using  windows -XP?
Thanks in advance</description> <content:encoded><![CDATA[<p>Hi ,</p><p>I am a newbie in perl&#8230;.can any body tell how to use the same script when i am using  windows -XP?</p><p>Thanks in advance</p> ]]></content:encoded> </item> <item><title>By: chinnu</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-154011</link> <dc:creator>chinnu</dc:creator> <pubDate>Mon, 01 Mar 2010 10:39:16 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-154011</guid> <description>HI
I am newnie in perl programming..
I am getting this error &quot;An error occured. statvfs failed. Did you run h2ph?&quot;
Could anyone help what is this error mean? and how to solve it?
Thanks</description> <content:encoded><![CDATA[<p>HI<br
/> I am newnie in perl programming..<br
/> I am getting this error &#8220;An error occured. statvfs failed. Did you run h2ph?&#8221;<br
/> Could anyone help what is this error mean? and how to solve it?</p><p>Thanks</p> ]]></content:encoded> </item> <item><title>By: DiskTracy</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-153545</link> <dc:creator>DiskTracy</dc:creator> <pubDate>Mon, 15 Feb 2010 21:03:03 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-153545</guid> <description>Dear Peter,
&gt;    Although your perl script is nice, you could do it with a standard shell script as well:
for fs in `df -k&#124;awk &#039;{print $1}&#039;&#124;sed -n &quot;2,14 p&quot;`; do
x=&quot;`df -kl &#124; grep $fs &#124; awk &#039;{ print $5 }&#039;&#124; sed -e &#039;s/%//&#039;`&quot;
...
done
Why do You use pipe snakes, like df -k&#124;awk &#039;{print $1}&#039;&#124;sed -n &quot;2,14 p&quot; and df -kl &#124; grep $fs &#124; awk &#039;{ print $5 }&#039;&#124; sed -e &#039;s/%//&#039;??? If You start awk why do You need a grep and sed? On the other hand if You have n disks You call df (n+1) times instead of once.
Good byte!</description> <content:encoded><![CDATA[<p>Dear Peter,</p><p>&gt;    Although your perl script is nice, you could do it with a standard shell script as well:</p><p> for fs in `df -k|awk &#8216;{print $1}&#8217;|sed -n &#8220;2,14 p&#8221;`; do<br
/> x=&#8221;`df -kl | grep $fs | awk &#8216;{ print $5 }&#8217;| sed -e &#8216;s/%//&#8217;`&#8221;<br
/> &#8230;<br
/> done</p><p>Why do You use pipe snakes, like df -k|awk &#8216;{print $1}&#8217;|sed -n &#8220;2,14 p&#8221; and df -kl | grep $fs | awk &#8216;{ print $5 }&#8217;| sed -e &#8216;s/%//&#8217;??? If You start awk why do You need a grep and sed? On the other hand if You have n disks You call df (n+1) times instead of once.</p><p>Good byte!</p> ]]></content:encoded> </item> <item><title>By: domaniqs</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-151895</link> <dc:creator>domaniqs</dc:creator> <pubDate>Fri, 27 Nov 2009 10:06:54 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-151895</guid> <description>Hello,
would enyone tell me why do I get such an error during execution of Kens script:
-----------------------------------------
Use of uninitialized value in concatenation (.) or string at ./new_df.pl line 17.
tail: cannot open `+2&#039; for reading: No such file or directory
Use of uninitialized value in addition (+) at ./new_df.pl line 20.
Use of uninitialized value in addition (+) at ./new_df.pl line 20.
Use of uninitialized value in division (/) at ./new_df.pl line 20.
Illegal division by zero at ./new_df.pl line 20.
------------------------------------------
It happens also during execution of original script from article above:
------------------------------------------
Illegal division by zero at ./df.pl line 26.
------------------------------------------
both errors are about the same line:
my $df_free = (($avail) / ($avail+$used)) * 100.0;
I have perl-Filesys-DiskSpace installed in my system.
I do not understand what it is complaining about
Any ideas?</description> <content:encoded><![CDATA[<p>Hello,<br
/> would enyone tell me why do I get such an error during execution of Kens script:<br
/> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br
/> Use of uninitialized value in concatenation (.) or string at ./new_df.pl line 17.<br
/> tail: cannot open `+2&#8242; for reading: No such file or directory<br
/> Use of uninitialized value in addition (+) at ./new_df.pl line 20.<br
/> Use of uninitialized value in addition (+) at ./new_df.pl line 20.<br
/> Use of uninitialized value in division (/) at ./new_df.pl line 20.<br
/> Illegal division by zero at ./new_df.pl line 20.<br
/> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br
/> It happens also during execution of original script from article above:<br
/> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br
/> Illegal division by zero at ./df.pl line 26.<br
/> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br
/> both errors are about the same line:<br
/> my $df_free = (($avail) / ($avail+$used)) * 100.0;<br
/> I have perl-Filesys-DiskSpace installed in my system.<br
/> I do not understand what it is complaining about<br
/> Any ideas?</p> ]]></content:encoded> </item> <item><title>By: Neru</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-148076</link> <dc:creator>Neru</dc:creator> <pubDate>Fri, 10 Apr 2009 14:00:26 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-148076</guid> <description>Guys, my script is slightly different, basically i&#039;m wanna count files in a directory and after certain amount of files, the script should send me alert.
I&#039;ve two problems with the current.
1. I&#039;m recieving emails but without body message
2. I&#039;m getting following error while execute this script:
Useless use of private variable in void context at ./VMCount.pl line 38.
However there is nothing wrong with line# 38, as i hve &quot;}&quot; on this line
any comments?</description> <content:encoded><![CDATA[<p>Guys, my script is slightly different, basically i&#8217;m wanna count files in a directory and after certain amount of files, the script should send me alert.</p><p>I&#8217;ve two problems with the current.</p><p>1. I&#8217;m recieving emails but without body message<br
/> 2. I&#8217;m getting following error while execute this script:</p><p>Useless use of private variable in void context at ./VMCount.pl line 38.</p><p>However there is nothing wrong with line# 38, as i hve &#8220;}&#8221; on this line</p><p>any comments?</p> ]]></content:encoded> </item> <item><title>By: Ken</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-147282</link> <dc:creator>Ken</dc:creator> <pubDate>Tue, 17 Feb 2009 20:29:19 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-147282</guid> <description>the df command there is part of the perl library FileSys::DiskSpace and not the normal df command available from the OS. This script is short and sweet but in my opinion, overly dependant on external libraries. The whole thing could have been written thusly..
&lt;pre lang=&quot;perl&quot;&gt;#!/usr/bin/perl
use strict;
use warnings;
# file system to monitor
my $target = shift;
# warning level
my $warning_level=10;
# email setup
my $to=&#039;whomever@whatever.com&#039;;
my $from=&#039;root@whereever.com&#039;;
my $subject=&#039;Low Disk Space Warning&#039;;
# get df
my ($fs_dev, $fs_type, $size, $used, $avail, $use, $mount) = split(&quot; &quot;, `df -PT $target &#124; tail +2`);
# calculate
my $df_free = (($avail) / ($avail+$used)) * 100.0;
# compare
if ($df_free &lt; $warning_level) {
my $out = sprintf(&quot;WARNING Low Disk Space on $target : %0.2f%% ()\n&quot;,$df_free);
# send email using UNIX/Linux sendmail
open(MAIL, &quot;&#124;/usr/sbin/sendmail -t&quot;);
## Mail Header
print MAIL &quot;To: $to\n&quot;;
print MAIL &quot;From: $from\n&quot;;
print MAIL &quot;Subject: $subject\n&quot;;
## Mail Body
print MAIL $out;
close(MAIL);
}&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>the df command there is part of the perl library FileSys::DiskSpace and not the normal df command available from the OS. This script is short and sweet but in my opinion, overly dependant on external libraries. The whole thing could have been written thusly..</p><pre lang="perl">#!/usr/bin/perl
use strict;
use warnings;
# file system to monitor
my $target = shift;
# warning level
my $warning_level=10;
# email setup
my $to='whomever@whatever.com';
my $from='root@whereever.com';
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 &lt; $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);
}</pre>]]></content:encoded> </item> <item><title>By: RC</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-146670</link> <dc:creator>RC</dc:creator> <pubDate>Mon, 12 Jan 2009 22:04:12 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-146670</guid> <description>Hi
I am a newbie to perl programming. Can somebody explain the following line? I understood rest of the program except  for this bit. How are we getting this array out of the df command?
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df  $dir;
Thanks for your help!</description> <content:encoded><![CDATA[<p>Hi</p><p>I am a newbie to perl programming. Can somebody explain the following line? I understood rest of the program except  for this bit. How are we getting this array out of the df command?</p><p>my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df  $dir;</p><p>Thanks for your help!</p> ]]></content:encoded> </item> <item><title>By: David</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-146374</link> <dc:creator>David</dc:creator> <pubDate>Mon, 22 Dec 2008 02:21:23 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-146374</guid> <description>For RPM based Linux systems like CentOS and Fedora, install the &lt;i&gt;perl-Filesys-DiskSpace&lt;/i&gt; package with:
&lt;b&gt;# yum install perl-Filesys-DiskSpace&lt;/b&gt;</description> <content:encoded><![CDATA[<p>For RPM based Linux systems like CentOS and Fedora, install the <i>perl-Filesys-DiskSpace</i> package with:<br
/> <b># yum install perl-Filesys-DiskSpace</b></p> ]]></content:encoded> </item> <item><title>By: Renante</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-144177</link> <dc:creator>Renante</dc:creator> <pubDate>Thu, 26 Jun 2008 10:04:43 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-144177</guid> <description>Hi!
I tried running the script on my mac os x leopard 10.5.3 machine and it says &quot;Cannot use df on this machine (untested or unsupported).&quot;
What should I do? Sorry I&#039;m quite a newbie into this thing.</description> <content:encoded><![CDATA[<p>Hi!</p><p>I tried running the script on my mac os x leopard 10.5.3 machine and it says &#8220;Cannot use df on this machine (untested or unsupported).&#8221;</p><p>What should I do? Sorry I&#8217;m quite a newbie into this thing.</p> ]]></content:encoded> </item> <item><title>By: Icapan</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-141157</link> <dc:creator>Icapan</dc:creator> <pubDate>Fri, 21 Sep 2007 14:46:02 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-141157</guid> <description>Well, for multiple e-mails, here it is:
&lt;code&gt;
## Mail Body
if (-e &quot;/tmp/sms_sent&quot;)
{
print &quot;already sent&quot;;
}
else
{
print MAIL $out;
system (&quot;touch /tmp/sms_sent&quot;);
}
&lt;/code&gt;</description> <content:encoded><![CDATA[<p>Well, for multiple e-mails, here it is:<br
/> <code><br
/> ## Mail Body<br
/> if (-e "/tmp/sms_sent")<br
/> {<br
/> print "already sent";<br
/> }<br
/> else<br
/> {<br
/> print MAIL $out;<br
/> system ("touch /tmp/sms_sent");<br
/> }<br
/> </code></p> ]]></content:encoded> </item> <item><title>By: Icapan</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-141149</link> <dc:creator>Icapan</dc:creator> <pubDate>Thu, 20 Sep 2007 12:23:00 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-141149</guid> <description>How we can make tests that we don&#039;t send multiple e-mails? I&#039;ve done that in shell scripts by setting up /tmp/mail_sent file and checking if it exists. After I logged onto a machine and free up some disk, I would remove the lock file.
My Perl is a bit rusty, so can anyone write the enhancement?</description> <content:encoded><![CDATA[<p>How we can make tests that we don&#8217;t send multiple e-mails? I&#8217;ve done that in shell scripts by setting up /tmp/mail_sent file and checking if it exists. After I logged onto a machine and free up some disk, I would remove the lock file.<br
/> My Perl is a bit rusty, so can anyone write the enhancement?</p> ]]></content:encoded> </item> <item><title>By: John</title><link>http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-141036</link> <dc:creator>John</dc:creator> <pubDate>Fri, 07 Sep 2007 16:31:51 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html#comment-141036</guid> <description>I am getting an error &quot;An error occured. statvfs failed. Did you run h2ph?&quot; Any idea?</description> <content:encoded><![CDATA[<p>I am getting an error &#8220;An error occured. statvfs failed. Did you run h2ph?&#8221; Any idea?</p> ]]></content:encoded> </item> </channel> </rss>
