Softpanorama

Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
May the source be with you, but remember the KISS principle ;-)

Filesystem free space monitoring

News Unix System Monitoring Recommended Links Perl Unix df Command df (Unix) - Wikipedia

Oracle Redo Logs Backup

Sample Monitoring scripts Perl Admin Tools and Scripts Perl Backup Scripts and Systems Humor Etc

Filesystem free space monitoring is probably one of the most widely used types of monitors in existence. It is deployed in some form in most organizations that use monitoring. We will discuss this topic using Perl as an implementation language.

First of all despite looking tretty trivial this is actually a pretty complex task. This is because there are many factors that you need to take into account -- thresholds for each filesystem can be different, Action requred if "fatal" threshold is breached are individual for each server (or application).

There are several important issues in writing such a script:

Tips:


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News

[Nov 14, 2010] Perl script to monitor disk space and send an email by Vivek Gite

This approach is wrong. Filesys::DiskSpace is not maintained, primitive and better should not be used

How to write a perl script that can monitor my disk space under UNIX or Linux and send me an email alert?

There is a nice perl system routine called Perl df or Filesys::DiskSpace. This routine displays information on a file system such as its type, the amount of disk space occupied, the total disk space and the number of inodes etc. This is not a standard module and you need to install it Filesys::DiskSpace

First you need to install this perl module using apt-get or from cpan (Comprehensive Perl Archive Network).

$ sudo apt-get install libfilesys-diskspace-perl

Perl script code to monitor disk space

Now write a perl script called df.pl:
$ vi df.pl
Append following code:

#!/usr/bin/perl
use strict;
use warnings;
use Filesys::DiskSpace;

# file system /home or /dev/sda5
my $dir = "/home";

# get data for /home fs
my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;

# calculate free space in %
my $df_free = (($avail) / ($avail+$used)) * 100.0;

# display message
my $out = sprintf("Disk space on $dir == %0.2f\n",$df_free);
print $out;

Save and close the file. Run this script as follows:
$ chmod +x df.pl
$ ./df.pl
Output:

Disk space on /home == 75.35

So /home has 75.35% free disk space. Next logical step is to compare this number to limit so that you can send an email if only 10% free disk space is left on /home file system. Here is the code with

#!/usr/bin/perl
use strict;
use warnings;
use Filesys::DiskSpace;

my $dir = "/home";

# warning level 10%
my $warning_level=10;

my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
my $df_free = (($avail) / ($avail+$used)) * 100.0;

# compare free disk space with warning level
if ($df_free %0.2f%% (WARNING Low Disk Space)\n",$df_free);
print $out;
}
else
{
my $out = sprintf("Disk space on $dir => %0.2f%% (OK)\n",$df_free);
print $out;
}

Run script as follows:
$ ./df.pl
Output:

Send an Email – Disk space on /home => 3.99% (WARNING Low Disk Space)

Here is final code that send an email alert ( download):

#!/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
use strict;
use warnings;
use Filesys::DiskSpace;

# file system to monitor
my $dir = "/home";

# warning level
my $warning_level=10;

# email setup
my $to='admin@yourdomain.com';
my $from='webmaster@YOURDOMAIN.COM';
my $subject='Low Disk Space';

# 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 Read man page of this module by typing following command:
$ man filesys::diskspace

scripthat.com • View topic - Perl script to monitor free disk space and send email alert

  1. #!/usr/bin/perl -w
  2. # Script to check free diskspace and email notifications. Change the email and alert levels and you should be good to go.
  3. # created by lb
  4. use strict;
  5. # Alert levels Warning and Critical - Below what percent level of free disk space do you want an alert?
  6. my $alert1 = 30; #Warning level free space below 30%
  7. my $alert2 = 10; #Critical level free space below 10%
  8. # Put the email address to notify here
  9. my $email = 'you@foo.com';
  10. my ($size,$used,$avail,$use,$mounted);
  11. my $message;
  12. my @list;
  13. my $sysname = `/bin/uname -n`;
  14. chomp $sysname;
  15. my @df = `/bin/df`;
  16. my $df;
  17. foreach $df (@df) {
  18. if ($df =~ /\/\n/) {
  19. @list = split(/\s+/, $df);
  20. }
  21. else {next;}
  22. }
  23. # Check the usage
  24. my $diskfree = (($list[3]) / ($list[2]+$list[3])) * 100.00;
  25. # Round the number off to 2 decimals
  26. $diskfree = sprintf("%.2f", $diskfree);
  27. # See if free disk space is below any of our levels
  28. if ( ($diskfree < $alert1) && ($diskfree > $alert2) ) {
  29. $message = "Warning Diskspace threshold reached...free space below $alert1% at $diskfree%\n";
  30. &mailer;
  31. }
  32. elsif ( ($diskfree < $alert1) && ($diskfree < $alert2) ) {
  33. $message = "Critical Diskspace threshold reached...free space below $alert2% at $diskfree%\n";
  34. &mailer;
  35. }
  36. else {
  37. $message = "Free diskspace is good at $diskfree%\n";
  38. }
  39. #Output to terminal (comment out if you wish)
  40. print $message;
  41. print "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname\n";
  42. #Subroutine for Mail, notifies on warning and critical levels.
  43. sub mailer {
  44. open(MAIL, "|/usr/sbin/sendmail -t") or die "Cannot open sendmail!: $!";
  45. print MAIL "To: $email\n";
  46. print MAIL "From: $sysname\n";
  47. print MAIL "Subject: $message\n\n";
  48. print MAIL "$message";
  49. print MAIL "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname";
  50. close(MAIL);
  51. }

use FilesysDiskspace - Perl answers

I want to monitor disk space and send a email if it exceeds the limit.
I m using "use Filesys:: Diskspace" but there is an error in compling.

Expand|Select|Wrap|Line Numbers

  1. "Can't locate Filesys/Diskspace.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.4 /usr/local/share/perl/5.8.4 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at disk.pl line 4.
  2. BEGIN failed--compilation aborted at disk.pl line 4."

Thanks
Vasu

That is not part of the core modules of perl:
http://perldoc.perl.org/index-modules-F.html

So as Kevin states, you'll have to install it:
http://cpan.perl.org/misc/cpan-faq.h...l_Perl_modules

I'm not certain this'll be a module you want to use though. By browing the cpan description, you'll notice that the module has not been updated since 1999. This might be a problem, but I would consider looking through the other Filesys modules to determine if there is one that might be better, as in maintained more frequently and therefore supports more file systems.
http://search.cpan.org/search?query=Filesys

Ximinez

Ximinez is a disk usage analyzer and comparator. It enables you to take snapshots of disk usage for a given folder, to browse through the snapshots, and to view differences between snapshots. It is aimed towards solving problems like: "Why is my disk suddenly so full when it was OK just recently?" Besides the GUI, the package also includes an optional command line tool, which can be used, for example, in conjunction with a scheduler to take snapshots on a regular basis.
Tags Systems Administration Filesystems
Licenses BSD Revised
Operating Systems OS Independent
Implementation Python
Translations Slovenian English

System monitoring tools

On Tue, 18 Aug 1998, chuck wrote:

First off, I really enjoyed your email.

> Frankly, a good automatic monitoring tool should be able to do
> what you do by hand but better.  If an important machine goes
> down or is heading for trouble, I want to know ahead of time.
> Watcher (or SNMP) can alter you when a disk reaches 95%.
> Watcher can note that a partition is filling fast or that a
> process is running away.  Yeah, you should know that machines,
> but you can automate a lot of this (see bottom for a rebuttal of
> this:)

As I mentioned before I just got done implementing a monitoring system
(using Tivoli, arg, which basically just calls shell scripts that I
wrote ;). One 'big picture' thing i formalized in my implementation
was 'reactions'.  I mean, it's great that now I *know* the drive is
filling up, but what now?  For all of our 'events' I automated reactions
to deal with them.  In the example of disk space, a script gets run which
does a find on the filesystem for all files mod in the last 24 hours and
sorts them by size and emails it to the admin on duty.

I am very pleased with the 'reactions' i have put in place, because now,
not only am i aware of things going wrong, but the first steps i would
take are already taken, and I can just analyze instead of doing grunt
work. 

> #################### ALERTING: ####################

I established three different levels that did different things.  Warning,
Severe and Critical.  Warning just an email, severe email and popup,
critical, page email and popup.  This made it very easy to 'classify'
events.  My environment is not very large (OK, it's small), but it has
been a timesaver for me.  

> but it got us bonus points from auditors and data security.

heh, that's cool.

> Can Tivoli(/SNM/Openview) do this?  Well, kinda sorta, not really.
> You have a nice GUI. that makes some people feel good.  You have
> an API (and every SA has time to learn an API to write their
> scripts in C to talk to these tools); you have support
> (generically).  You have someone else to shoulder responsibility;
> vaguely.

bingo.  in Tivoli's defense the underlying architecture (CORBA) is pretty
solid, and you are given the tools to customize easily.  But out of the
box (for Distributed Monitoring) it's useless.  It seems as though these
products are being developed by ppl that are marketing types vs. admins
who have been in the trenches.

> I want something adaptive ("the machines are usually quiet now, but
> today they aren't, please note")  Nothing replaces ME watching the
> machines and even the graphs (oh sendmail's running hotter than
> usual for a sunday).  I want that.  Must I relearn prolog?

I think that is *very* key.  In my documentation of my implementation I
state, "No software product in the world is going to be able to provide
the root of what needs to happen.  Competent system/network architecture
and operations.  Experience is the only thing I have seen which can really
provide that."

For some reason it seems as though IT managers think software can fix all
of their problems when odds are it's not the software or hardware that
needs fixing, it's the ppl.

> Perhaps a SAGe group to cover SA tools like this might be of
> interest (even just a web page as a resource).  Perhaps I volunteer
> (as long as someone else does the pretty pictures); I'll poke at it.

I would be into that.  Email me directly if you would like to brainstorm.
I have seen a lot of details on how to do this and that with monitoring,
but have yet to see a resource that covers the 'big picture' thinking (ie.
create a loghost for your network, use email address roles <-you really
don't want to have to change email addresses is mucho scripts when bob
leaves do you?, etc).

--
Scott Walters
-PacketPusher

"The world speaks IP"

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites

Internal

External:



Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least


Copyright © 1996-2018 by Dr. Nikolai Bezroukov. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) in the author free time and without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

 

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to make a contribution, supporting development of this site and speed up access. In case softpanorama.org is down you can use the at softpanorama.info

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: September 12, 2017