#!/usr/local/bin/perl # # Written by Warren A. Kibbe, 10/5/96 # # will merge multiple files into a single destination file use Config; if ($Config{'osname'} =~ /^macos/i) { # do mac specific stuff here $srcdir = "public:ncsalogs:"; @srcfiles = (); $destfile = "pub:wwwlogs:ntwww.log"; &MacPerl'Quit(1); #quit if a stand alone app # $inmacintosh = 0; @fileAttributes = ("R*ch", "TEXT"); $nativedirchar=":"; # throw the logs in the working directory $logfile = "merge.log"; $errlogfile ="mergeError.log"; } elsif ($Config{'osname'} =~ /^win/i) { $srcdir = "f:\\pub\\iislogs:"; $destfile = "f:\\pub\\wwwlogs\\ntwww.log"; $nativedirchar="\\"; $logfile = $ENV{'HOME'}.$nativedirchar."merge.log"; $errlogfile = $ENV{'HOME'}.$nativedirchar."mergeError.log"; } else{ $srcdir = "/iislogs:"; $destfile = "/wwwlogs/ntwww.log"; $nativedirchar="/"; $logfile = $ENV{'HOME'}.$nativedirchar."merge.log"; $errlogfile = $ENV{'HOME'}.$nativedirchar."mergeError.log"; } $debug=0; #turn on debugging output $dontoverwritedest = 0; #allow script to overwrite the destination file, if it exists? $srcsuffix = ".log"; %monthassoc = ( 'Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6', 'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11', 'Dec', '12' ); open (LOG,">>".$logfile) || &Die("Unable to open $logfile"); close(STDERR); open(STDERR,">>".$errlogfile) || &Die("Unable to open $errlogfile"); select(STDERR); $| = 1; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $mon++; if($min >= 10) { print LOG "\n\n$mon/$mday/$year $hour:$min\n"; } else { print LOG "\n\n$mon/$mday/$year $hour:0$min\n"; } &domerge(); if ($Config{'osname'} =~ /^macos/i) { # do mac specific stuff here $srcdir = "public:oldaollogs:"; @srcfiles = ("^access.log"); $destfile = "pub:wwwlogs:access.log"; } &domerge(); if ($Config{'osname'} =~ /^macos/i) { # do mac specific stuff here $srcdir = "public:oldaollogs:"; @srcfiles = ("^nucbaccess.log","^nucb.log"); $destfile = "pub:wwwlogs:nucbaccess.log"; } &domerge(); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $mon++; if($min >= 10) { print LOG "Finished at $mon/$mday/$year $hour:$min\n"; } else { print LOG "Finished at $mon/$mday/$year $hour:0$min\n"; } close(LOG); if ($Config{'osname'} =~ /^macos/i) { # do mac specific stuff here chdir(); #should go back to the default home directory &MacPerl::SetFileInfo(@fileAttributes, "$logfile"); &MacPerl::SetFileInfo(@fileAttributes, "$errlogfile"); } sub domerge { if (open(SRC,"<".$destfile) && $dontoverwritedest) { print LOG "Destination file $destfile already exists! Quitting!\n"; close(LOG); close(SRC); die("destination file $destfile already exists! Quitting!"); } close (SRC); $err=opendir (WORKDIR, $srcdir); if ($err < 1 ){ die "working directory '$srcdir' invalid!"; } $err=closedir (WORKDIR); $err=chdir $srcdir; if ($err < 1 ){ die "Cannot set directory to working directory 'srcdir'!"; } open (DEST,">".$destfile) || &Die("Unable to open $destfile for writing!"); opendir(TMPD, $srcdir) || &die("Unable to find source directory $srcdir!"); @dirlist = readdir(TMPD); closedir(TMPD); @dirlist=sort @dirlist; print LOG "Checking $srcdir for valid files.\n" if $debug; $srcmatch=length($srcfiles[0]); $matchfound=1; @filemergelist=(); foreach $in (@dirlist) { if ($srcmatch>0) { $matchfound=0; foreach $filegrep (@srcfiles) { if ($in =~ m/$filegrep/i) { $matchfound=1; print LOG "file $in matches grep $filegrep\n" if $debug; last; } } } if ($matchfound) { if ($in !~ m/$srcsuffix/) { print LOG "not a log file: skipping $in\n" if $debug; next; } push (@filemergelist, $in); } else { print LOG "no match: skipping $in\n" if $debug; next; } } foreach $in (sort (filebyentrydate @filemergelist)) { print LOG "Processing $in. srcmatch=$srcmatch\n" if $debug; open(SRC,"<".$in) || &Die("Unable to open input file"); while($line = ) { print DEST $line; } close(SRC); } close(DEST); if ($Config{'osname'} =~ /^macos/i) { # do mac specific stuff here &MacPerl::SetFileInfo(@fileAttributes, "$destfile"); } } sub filebyentrydate { $filea=$a; $fileb=$b; open(FA,"<".$filea) || &Die("Unable to open input file $filea"); open(FB,"<".$fileb) || &Die("Unable to open input file $fileb"); $linea=; $lineb=; close FA; close FB; $datetimea = (split(/,?\s+/,$linea))[3]; $datetimeb = (split(/,?\s+/,$lineb))[3]; ($mday, $month, $year) = ($datetimea =~ /(\d*)\/(\w*)\/(\d*)/); $newmonth=$monthassoc{$montha}; $sortdatea="$year$newmonth$mday"; ($mday, $month, $year) = ($datetimeb =~ /(\d*)\/(\w*)\/(\d*)/); $newmonth=$monthassoc{$montha}; $sortdateb="$year$newmonth$mday"; return $sortdatea <=> $sortdateb; } #exit(0); __END__