#! /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 : Patrick Krogel, Fri Feb 24 11:51:35 EST 2006 (perl version) # Last modified : Patrick Krogel, Fri Feb 24 11:56:40 EST 2006 # 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;