PERL – Disk Usage Reminder

Disclaimer

The instructions/steps/scripts/methods given below worked for me running CentOS. It may very well work for you on other linux distributions, Red Hat-like or otherwise. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.com, nor its author is responsible for any/all damage – intellectual or otherwise.

What is PERL?

In computer programming, PERL is a high-level, general-purpose, interpreted, dynamic programming language. PERL was originally developed by Larry Wall, a linguist working as a systems administrator for NASA, in 1987, as a general purpose UNIX scripting language to make report processing easier. Since then, it has undergone many changes and revisions and became widely popular among programmers. Larry Wall continues to oversee development of the core language. PERL borrows features from other programming languages including C, shell scripting (sh), AWK, sed and Lisp. The language provides powerful text processing facilities without the arbitrary data length limits of many contemporary UNIX tools, making it the ideal language for manipulating text files. It is also used for graphics programming, system administration, network programming, applications that require database access and CGI programming on the Web. PERL is nicknamed the Swiss Army knife of programming languages due to its flexibility and adaptability.

The Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#! /usr/bin/perl -wT
 
# Perl script to check disk usage and send emails to users hogging more than a specified limit. 
# Needs to be run as root and a good way to do it is via a cron job.
# 
# First written : Pat Krogel, Fri Feb 24 11:51:35 EST 2006
# Last modified : Gowtham, Fri Feb 24 15:37:40 EST 2006
 
# Variable initializations
$date    = localtime;
$dulimit = 20;   # Disk Usage Limit, in GB
 
# Get the disk usage, in GB, and separate out space and uids 
open PIPE, "du -s -B 1024M /home/* |" or die "Can't get disk usage.\n";
while ()
{
   chomp;
   my ($space, $user) = split /\s+/;
   $user =~ s/^.*\/(\w+)$/\1/;
   if ($space > $dulimit)
   {
      # Debugging purposes only! 
      # print "$user is taking up $space GB\n";
      open MAIL, "| mail -s 'Disk Usage Notification' $user\@domain.name"
        or die "Can't send email.\n";
      print MAIL "$user,\n\n";
      print MAIL "Your home folder,\n\n";
      print MAIL "/home/$user ($space GB)\n\n";
      print MAIL "in 'host.domain.name' has exceeded the present limit.\n";
      print MAIL "Please back it up elsewhere (some other machine or\n";
      print MAIL "external hard drives) and clean up the home folder.\n\n";
      print MAIL "root\@host.domain.name\n";
      print MAIL "$date\n";
      close MAIL;
   }
}
close PIPE;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.