#!/usr/bin/perl #(@) v1.00 3/14/99 $|=1; $DEBUG=1; if ($ARGV[0] eq "-v") { shift @ARGV; $verbose = true; } if ($ARGV[0] eq "--rerun") { shift @ARGV; $rerun = true; } ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time); ($mday == 0) ? ($total_months = 2) : ($total_months = 1); $total_months = $ARGV[1] if $ARGV[1]; $starttime = time; %months = qw(Jan 0 Feb 1 Mar 2 Apr 3 May 4 Jun 5 Jul 6 Aug 7 Sep 8 Oct 9 Nov 10 Dec 11); $runtype = $ARGV[0]; if (!(($runtype eq "monthly") || ($runtype eq "weekly") || ($runtype eq "daily"))) { $user_to_run = " AND u.userid='$ARGV[0]'"; $runtype="daily"; } require "/usr/home/wsh/public_html/admin/admin-bin/wshdbapi.pl"; exit unless &OpenDB(*dbh); open(CONF,"/usr/home/wsh/public_html/admin/admin-bin/webalizer.conf.template") || die "Cannot open conf tempate ($!)\n"; while() { $conf_template .= $_; } close(CONF); if ( &OpenRS(*dbh, <<__SQL__, *fields, *data, *iRecs) ) { SELECT ua.*, u.* FROM user_attributes ua, users u WHERE ua.lumberjack_$runtype = 1 AND ua.userid = u.userid AND u.status <> 'Suspended' $user_to_run __SQL__ for ($idx=0; $idx<$iRecs; ++$idx) { $userid = $data[$idx]->{userid}; $homedir = $data[$idx]->{homedir}; $password = $data[$idx]->{password}; $crypted_password = crypt($password, substr(time,-2)); $conf_file = "$homedir/webalizer.conf"; print "\nRunning webalizer for user $userid for past $total_months month(s)...\n" if ($verbose); if (! -t $homedir."/public_html/webalizer") { mkdir $homedir."/public_html/webalizer", 0751; open(HTACCESS, ">$homedir/public_html/webalizer/.htaccess"); print HTACCESS qq( AuthType Basic AuthName "Authorized Access ONLY" AuthUserFile $homedir/public_html/webalizer/.htpasswd order deny,allow require valid-user order deny,allow deny from all ); close(HTACCESS); } print "Updating .htpasswd for user\n" if ($verbose); open(HTPASSWD, ">$homedir/public_html/webalizer/.htpasswd"); print HTPASSWD "$userid:$crypted_password\n"; close(HTPASSWD); if ($rerun eq true) { print "Removing old history for rerun mode\n" if ($verbose); system "/bin/rm $homedir/public_html/webalizer/webalizer.hist" if (-f "$homedir/public_html/webalizer/webalizer.hist"); system "/bin/rm $homedir/public_html/webalizer/webalizer.current" if (-f "$homedir/public_html/webalizer/webalizer.current"); } $conf = $conf_template; foreach $field (@fields) { $conf =~s/\{$field\}/$data[$idx]->{$field}/g; } open(outCONF,">$conf_file"); print outCONF $conf; close(outCONF); # Get Files to run in order of date opendir(DIR,$homedir) || "$!\n"; @files = grep /access_log./,readdir(DIR); closedir(DIR); @sorted_files = sort filebydate @files; undef @files; for($fidx=0; $fidx<$total_months; ++$fidx) { if ($#sorted_files >= 0) { push @files, pop @sorted_files; } } print "\n" if ($verbose); foreach $log_file (reverse @files) { $cmd = "/usr/local/bin/webalizer -c $conf_file $homedir/$log_file"; $cmd .= "> /dev/null 2>&1" unless ($verbose); print "$cmd\n" if ($verbose); system $cmd; # sleep 10; } print "-"x60 if ($verbose); print "\n" if ($verbose); system "/usr/sbin/chown -R $userid:$userid $homedir/public_html/webalizer"; unlink $conf_file; } }else{ die "Error! $errdescription\n"; } &CloseDB(*dbh); ($user,$system,$cuser,$csystem) = times; print "\n\nTimes:\n" if ($verbose); printf "%0.2fs User %0.2fs System %0.2fs Real %0.2f%% CPU\n", ($user+$cuser), ($system+$csystem), (time-$starttime), (($system+$csystem)/(time-$starttime))*100 if ($verbose); sub filebydate { $a =~ m/\.(\w+)\.(\d+)/; $a_date = $2.sprintf "%02d", $months{$1}; $b =~ m/\.(\w+)\.(\d+)/; $b_date = $2.sprintf "%02d", $months{$1}; $a_date <=> $b_date; }