#!/usr/bin/perl -w #By Neil Watson on 08/18/01 (watson-wilson.ca) #Rev 1.3 11/07/01 #Usage filehog #options: # -p = report top 10 pst files use strict; use warnings; use File::Find; use CGI qw(:standard); #change this for the locale of final report: my $repdir = "/var/log"; my (@field, @dupes); my $dir = $ARGV[0]; my $pst = ""; my $args = join "", @ARGV; if ($args =~ m/-p/) { $pst = "yes" } my $host = `hostname`; chomp($host); my @date = split(/\s+/, `date`); chomp($date[5]); #input open(RAW, ">/tmp/filehog") || die "Could not open /tmp/filehog"; #calls sub wanted find(\&wanted, $dir); close(RAW); #output open(HTML, ">$repdir/hogs_$host.html") || die "Could not open $repdir/hogs_$host.html"; print HTML start_html("Space Hogs on server $host for $date[0], $date[1] $date[2] $date[5]"); print HTML h1("Space Hogs on server $host for $date[0], $date[1] $date[2] $date[5]")."\n\n"; print HTML h3("These people are shamefully using more than their fair share of disk space. Hopefully, under this public scrutiny, they will change their greedy ways."); print HTML "\n
"; print HTML "\n\n"; print HTML "\n"; print HTML "\t\n"; #sort RAW data if ($pst eq "yes"){ system "sort -nr /tmp/filehog|grep -iv .pst|head>/tmp/filehog_sort"; } else{ system "sort -nr /tmp/filehog|head>/tmp/filehog_sort"; } #write final report open(RAW, "/tmp/filehog_sort") || die "Could not open /tmp/filehog_sort"; while () { chomp; s/^\s*//; @field = split(/,/, $_); $field[2] = `grep x:$field[2] /etc/passwd|cut -d: -f1`; chomp($field[2]); print HTML "\n"; print HTML "\t\n"; } close(RAW); print HTML "
Size (MB)\n"; print HTML "\tFile Name and Location\n"; print HTML "\tFile Owner\n"; print HTML "
$field[0]\n"; print HTML "\t$field[1]\n"; print HTML "\t$field[2]\n"; print HTML "
"; #call pst report if ($pst eq "yes") { pst() } print HTML end_html(); close(HTML); system "rm -f /tmp/filehog /tmp/filehog_sort"; #remove scratch files #subroutines: sub wanted { # collects size, name and uid of all files my $uid = (lstat($_))[4]; my $size = (lstat($_))[7]; $size = $size/1000000; printf RAW "%5.0f", $size; print RAW ",$File::Find::name,$uid\n"; } sub pst { #generates report on top 10 pst files system "egrep -i \.PST, /tmp/filehog|sort -nr|head>/tmp/psthog_sort"; print HTML "

\n


\n

\n"; print HTML h3("Another space issue is caused by the proliferation of PST files. If any of these files are yours and you are not actively using them please contact Support to have them burned onto a CDROM"); print HTML "\n


"; print HTML "\n\n"; print HTML "\n"; print HTML "\t\n"; #write pst file report open(RAW, "/tmp/psthog_sort") or die "Could not open /tmp/psthog_sort"; while () { chomp; s/^\s*//; @field = split(/,/, $_); $field[2] = `grep x:$field[2] /etc/passwd|cut -d: -f1`; chomp($field[2]); print HTML "\n"; print HTML "\t\n"; } close(RAW); print HTML "
Size (MB)\n"; print HTML "\tFile Name and Location\n"; print HTML "\tFile Owner\n"; print HTML "
$field[0]\n"; print HTML "\t$field[1]\n"; print HTML "\t$field[2]\n"; print HTML "
"; }