|
Home | Switchboard | Unix Administration | Red Hat | TCP/IP Networks | Neoliberalism | Toxic Managers |
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and bastardization of classic Unix |
News | Unix System Monitoring | Recommended Links | Perl | Unix df Command | df (Unix) - Wikipedia |
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 pretty 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 required if "fatal" threshold is breached are individual for each server (or application).
|
There are several important issues in writing such a script:
You can also get some relevant information by analyzing output of mount command (In case of SAS harddrives, local filesystems generally start with /dev/sda
root@unlab12:/home/bezroun # mount /dev/sda2 on / type ext3 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/sda5 on /tmp type ext3 (rw) /dev/sda7 on /var type ext3 (rw) /dev/sda6 on /home type ext3 (rw) /dev/sda1 on /boot type ext3 (rw) /dev/sdb1 on /u01 type ext3 (rw) /dev/sdb2 on /backup type ext3 (rw) tmpfs on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) /backup/ISO/TB248-15000.iso on /media type iso9660 (rw,loop=/dev/loop0)
Tips:
You can install GNU coreutils
Depot file for each of common versions of HP is available from http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-8.20/
|
Switchboard | ||||
Latest | |||||
Past week | |||||
Past month |
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-perlPerl 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='[email protected]';
my $from='[email protected]';
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
- #!/usr/bin/perl -w
- # Script to check free diskspace and email notifications. Change the email and alert levels and you should be good to go.
- # created by lb
- use strict;
- # Alert levels Warning and Critical - Below what percent level of free disk space do you want an alert?
- my $alert1 = 30; #Warning level free space below 30%
- my $alert2 = 10; #Critical level free space below 10%
- # Put the email address to notify here
- my $email = '[email protected]';
- my ($size,$used,$avail,$use,$mounted);
- my $message;
- my @list;
- my $sysname = `/bin/uname -n`;
- chomp $sysname;
- my @df = `/bin/df`;
- my $df;
- foreach $df (@df) {
- if ($df =~ /\/\n/) {
- @list = split(/\s+/, $df);
- }
- else {next;}
- }
- # Check the usage
- my $diskfree = (($list[3]) / ($list[2]+$list[3])) * 100.00;
- # Round the number off to 2 decimals
- $diskfree = sprintf("%.2f", $diskfree);
- # See if free disk space is below any of our levels
- if ( ($diskfree < $alert1) && ($diskfree > $alert2) ) {
- $message = "Warning Diskspace threshold reached...free space below $alert1% at $diskfree%\n";
- &mailer;
- }
- elsif ( ($diskfree < $alert1) && ($diskfree < $alert2) ) {
- $message = "Critical Diskspace threshold reached...free space below $alert2% at $diskfree%\n";
- &mailer;
- }
- else {
- $message = "Free diskspace is good at $diskfree%\n";
- }
- #Output to terminal (comment out if you wish)
- print $message;
- print "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname\n";
- #Subroutine for Mail, notifies on warning and critical levels.
- sub mailer {
- open(MAIL, "|/usr/sbin/sendmail -t") or die "Cannot open sendmail!: $!";
- print MAIL "To: $email\n";
- print MAIL "From: $sysname\n";
- print MAIL "Subject: $message\n\n";
- print MAIL "$message";
- print MAIL "~" x 75, "\n@df","~" x 75,"\n","From system: $sysname";
- close(MAIL);
- }
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
- "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.
- BEGIN failed--compilation aborted at disk.pl line 4."
Thanks
VasuThat 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_modulesI'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.
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
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"
Google matched content |
Internal
External:
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 quotes : Somerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose Bierce : Bernard 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 DOS : Programming Languages History : PL/1 : Simula 67 : C : History of GCC development : Scripting 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-Month : How 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 Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D
Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) 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 to buy a cup of coffee for authors of this site |
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 Softpanorama society. 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: November 16, 2020