#!/usr/bin/perl -w # $Id$ # # Listing 1: # SCRIPT: diskHog # AUTHOR: Ivan Griffin (ivan.griffin@ul.ie) # DATE: 14 April 1996 # # REVISION HISTORY: # 06 Mar 1996 Original version (written using Bourne shell and Awk) # 14 Apr 1996 Perl rewrite # 01 Aug 1996 Found piggie image on the web, added second red ball # 02 Aug 1996 Added third red ball # 20 Feb 1997 Moved piggie image :-) # # outlaw barewords and set up the paranoid stuff # use strict 'subs'; use English; # $ENV{'PATH'} = '/bin:/usr/bin:/usr/ucb'; # ucb for Solaris dudes $ENV{'IFS'} = ''; # # some initial values and script defines # $NumUsers = 0; $Total = 0; $Position = 0; $RED_ZONE3 = 15000000; $RED_ZONE2 = 10000000; $RED_ZONE = 1000000; $ORANGE_ZONE = 500000; $CRITICAL = 90000000; $DANGER = 75000000; $TmpFile = "/var/tmp/fac_foo$$"; $HtmlFile = '>/home/faculty/rshore/public_html/fac_diskHog.html'; $PerlWebHome = "fac_hog.txt"; $HtmlDir = "~rshore"; $HtmlIndexFile = "$HtmlDir/index.shtml"; $Login = " "; $HomeDir=" "; $Gcos = "A user"; @AccountDirs = ( "/home/faculty" ); @KeyList = (); @TmpList = (); chop ($Machine = `/bin/hostname`); # chop ($Machine = `/usr/ucb/hostname`); # for Solaris # # Explicit sort subroutine # sub by_disk_usage { $Foo{$b} <=> $Foo{$a}; # sort integers in numerically descending order } # # get disk usage for each user and total usage # sub get_disk_usage { foreach $Directory (@AccountDirs) { chdir $Directory or die "Could not cd to $Directory\n"; # system "du -k -s * >> $TmpFile"; # for Solaris system "du -s * >> $TmpFile"; } open(FILEIN, "<$TmpFile") or die "Could not open $TmpFile\n"; while () { chop; ($DiskUsage, $Key) = split(' ', $_); if (defined($Foo{$Key})) { $Foo{Key} += $DiskUsage; } else { $Foo{$Key} = $DiskUsage; @TmpList = (@KeyList, $Key); @KeyList = @TmpList; }; $NumUsers ++; $Total += $DiskUsage; }; close(FILEIN); unlink $TmpFile; } # # for each user with a public_html directory, ensure that it is # executable (and a directory) and that the index.html file is readable # sub user_and_homepage { $User = $_[0]; ($Login, $_, $_, $_, $_, $_, $Gcos, $HomeDir, $_) = getpwnam($User) or return "$User"; if ( -r "$HomeDir/$HtmlIndexFile" ) { return "$Gcos ($User)"; } else { return "$Gcos ($User)"; }; } # # generate HTML code for the disk usage file # sub html_preamble { $CurrentDate = localtime; open(HTMLOUT, $HtmlFile) or die "Could not open $HtmlFile\n"; printf HTMLOUT <<"EOF"; Disk Hog Top $NumUsers on $Machine

Disk Hog Top $NumUsers on $Machine

[PIGGIE!] This is a Perl script which runs
automatically every night

Last run started: $StartDate
Last run finished: $CurrentDate

Status EOF if ($Total > $CRITICAL) { print HTMLOUT "CRITICAL!!! - Reduce Disk Usage NOW!"; } elsif (($Total <= $CRITICAL) && ($Total > $DANGER)) { print HTMLOUT "Danger - Delete unnecessary Files"; } else { print HTMLOUT "Safe"; } printf HTMLOUT <<"EOF";


EOF } # # # sub html_note_time { $StartDate = localtime; } # # for each user, categorize and display their usage statistics # sub dump_user_stats { foreach $Key (sort by_disk_usage @KeyList) { $Position ++; print HTMLOUT <<"EOF"; \n \n"; } elsif (($Foo{$Key} <= $RED_ZONE) && ($Foo{$Key} > $ORANGE_ZONE)) { print HTMLOUT " \n"; } else { print HTMLOUT " \n"; } print HTMLOUT <<"EOF"; EOF print HTMLOUT " \n"; print HTMLOUT <<"EOF"; EOF }; } # # end HTML code # sub html_postamble { print HTMLOUT <<"EOF";
Chart Posn. Username Disk Usage
EOF # # colour code disk usage # if ($Foo{$Key} > $RED_ZONE) { if ($Foo{$Key} > $RED_ZONE3) { print HTMLOUT " \n"; } if ($Foo{$Key} > $RED_ZONE2) { print HTMLOUT " \n"; } print HTMLOUT " $Position"; print HTMLOUT &user_and_homepage($Key); print HTMLOUT "$Foo{$Key} KB
Total: $Total

[$Machine Home Page] EOF close HTMLOUT ; # # ownership hack # $Uid = getpwnam("rshore"); $Gid = getgrnam("rshore"); chown $Uid, $Gid, $HtmlFile; } # # main() # &html_note_time; &get_disk_usage; &html_preamble; &dump_user_stats; &html_postamble; # all done!