#!/usr/bin/perl # weekly-set-date.pl # runs every Saturday to update the current weekfile # creates new subdiretories for daily logs on each host if ($#ARGV > -1) { die "usage: weekly-set-date.pl\n"; } umask(027); # output files are not world readable chop($CHGRP=`which chgrp`); die("can't find chgrp\n") if $CHGRP =~ /^no /; chop($RM=`which rm`); die("can't find rm\n") if $RM =~ /^no /; $PRG='/www/data/httpd/pl'; $LOG='/www/data/httpd/log/fu'; $SEC='/www/data/httpd/log/sec'; $weekfile="$PRG/thisweek"; # the week that ends today $nextfile="$PRG/nextweek"; # the week that ends next Saturday if (open(DATEFILE, "<$weekfile")) { chop($lastwk=); chop($lastrep=); close(DATEFILE); print "last week was $lastwk ($lastrep)\n"; } # today's date in the form "Sep 13, 1997" # today's date in the form "19970913" ($thiswk, $thisrep) = &makdate(time); die "weekly-set-date has already run this week\n" if $thisrep eq $lastrep; print "this week is $thiswk ($thisrep)\n"; if (!open(DATEFILE, ">$weekfile")) {die "can't overwrite $weekfile: $!"} print DATEFILE "$thiswk\n$thisrep\n"; close(DATEFILE); ($nextwk, $nextrep) = &makdate(time + 7*24*3600); # 7 days from now print "next week will be $nextwk ($nextrep)\n"; if (!open(DATEFILE, ">$nextfile")) {die "can't overwrite $nextfile: $!"} print DATEFILE "$nextwk\n$nextrep\n"; close(DATEFILE); for $host ('alhan', 'gutentag', 'jonapot', 'kalimera', 'kwaziwai') { &next_week($LOG, $host, $lastrep, $thisrep, $nextrep); } for $host ('alhan', 'howsit') { # secure server logs &next_week($SEC, $host, $lastrep, $thisrep, $nextrep); } ###################################################################### sub makdate { my($tim) = @_; my(@mont) = ('Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'); my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($tim); $year += 1900; $datstr = sprintf("%s %2d, %4d", $mont[$mon], $mday, $year); $datnum = sprintf("%4d%02d%02d", $year, $mon+1, $mday); return($datstr, $datnum); } sub next_week { my($logd, $host, $lastsat, $thissat, $nextsat) = @_; if (!chdir("$logd/$host")) { print "Can't cd to $logd/$host because $!\n"; return; } system("$RM -rf $lastsat"); # delete last week's files # prev symlink shall now point to thisrep print "Can't remove symlink $host/prev because $!\n" unless unlink("prev"); # remove prev symlink if (!-d $thissat) { print "No such directory $host/$thissat\n"; } elsif (!symlink($thissat, prev)) { print "Can't create symlink $host/prev -> $thissat because $!\n"; return; } else { print "created symlink $host/prev -> $thissat\n"; } # curr symlink will now point to nextrep print "Can't remove symlink $host/curr because $!\n" unless unlink("curr"); # remove curr symlink if (-e $nextsat) { # create a new directory print "$host/$nextsat exists\n" ; } elsif (!mkdir($nextsat, 0750)) { print "Can't create $host/$nextsat because $!\n"; return; } system("$CHGRP dgate $nextsat"); # put in dgate group if (!symlink($nextsat, curr)) { print "Can't create symlink $host/curr -> $nextsat because $!\n"; return; } print "created symlink $host/curr -> $nextsat\n"; return; } # next_week