#!/local/bin/perl5 # Copyright (c) 1998 University of Southern California. # All rights reserved. # # Redistribution and use in source and binary forms are permitted # provided that the above copyright notice and this paragraph are # duplicated in all such forms and that any documentation, advertising # materials, and other materials related to such distribution and use # acknowledge that the software was developed by the University of # Southern California, Information Sciences Institute. The name of the # University may not be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED # WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. # # PROGRAM: dailylogs.pl # # Called as: dailylogs.pl # use Time::Local; use Cwd; # # Read in configuration hash data. # $run_dir = cwd(); %config = (); if ( !(open (INFILE, ") { split; $key = shift (@_); if (!$#_) { $config{$key} = $_[0]; } else { $config{$key} = "@_"; } } close (INFILE); if ( (!defined($config{"dst_dir"})) || (!defined($config{"sort_dir"})) || (!defined($config{"log_names"})) || (!defined($config{"src_host"})) || (!defined($config{"src_dir"})) ) { print "\n\aERROR: File dailylog.config is incomplete.\n"; print " dst_dir, sort_dir, log_names, src_host and srt_dir keys\n"; print " and their values are required.\n\n"; exit(1); } $dst_dir = $config{"dst_dir"}; $sort_dir = $config{"sort_dir"}; $log_names = $config{"log_names"}; $src_host = $config{"src_host"}; $src_dir = $config{"src_dir"}; print "\nIn dailylogs.pl...\n"; print " Source log names are: $log_names\n"; print " Destination log files will be stored in: $dst_dir\n"; print " Temporary sort() directory is: $sort_dir\n"; print " Source host is: $src_host\n"; print " Source host directory is: $src_dir\n"; # # Connect to destination directory where local log data is kept. # if (!(chdir $dst_dir)) { print "\n\aIn dailylogs.pl unable to connect to directory: $dst_dir.\n"; print "Error: $!\n"; exit(1); }; # # Read the one line from the dailylogs.run_dates file that stores the date # which is to be processed by the dailylog.pl program now. Extract # the month field from that line. This line has the format YYMMDD. # $firstday = ""; $lastday = ""; if (open (INFILE,"); while () { chomp ($lastday = $_); } close (INFILE); if (!$lastday) { $lastday = $firstday; } # # Extract UNIX numeric month, day and year values from $lastday. # $year = $lastday; $year =~ s/....$//; $mon = $lastday; $mon =~ s/..//; $mon =~ s/..$//; $mon--; $month = $mon; $mday = $lastday; $mday =~ s/....//; } else { print "\n\aIn dailylogs.pl unable to open: dailylogs.run_dates.\n"; print "Error: $!\n"; exit(1); } $datefield = sprintf ("%02u%02u%02u", $year, (1 + $mon), $mday); # # Run daily processing for each series of Squid logs named. # print "\nBeginning dailylog processing for $datefield ...\n"; foreach $log_name (split /\s+/, $log_names) { # # Create the NLANR Squid file name for this day's log file. # $src_file = "$log_name.sanitized-access.$datefield.gz"; # # Get the requested daily log file via FTP. # $dst_file = $src_file; print "ftpgetfile $src_host $src_dir $src_file\n"; if (system "ftpgetfile $src_host $src_dir $src_file $dst_file") { print "\n\aIn dailylogs.pl unable to FTP: $src_file.\n"; print "Error: $!.\n"; exit(1); }; # # Decompress the gzip'd log file, which deletes the compressed file. # Adjust to resulting destination file name. # print "\ngunzip $dst_file\n"; if (system "gunzip $dst_file") { print "\n\aIn dailylogs.pl unable to decompress file: $dst_file.\n"; print "Error: $!.\n"; exit(1); } $dst_file =~ s/\.gz//i; print "dailylog.pl $dst_file $sort_dir\n"; if (system "dailylog.pl $dst_file $sort_dir") { print "\n\aIn dailylogs.pl daily processing failed for: $log_name.\n"; exit(1); } } # # Advance the time of the $lastday by 24 hours. Convert that back # into year, month and date. # $timeval = timelocal (0, 0, 0, $mday, $mon, $year) + (24*60*60); # # Get the local time and look at the month field. If it is different # than that in the "dailylogs.run_dates" file, perform the end-of-month # processing before running dailylog.pl for the first day of a new month. # ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime ($timeval); if ($mon != $month) { print "\nChange of month seen.\nStarting monthly log processing ...\n"; for $log_name (split /\s+/, $log_names) { print "monthly.pl $log_name $firstday $lastday $sort_dir\n"; if (system "monthly.pl $log_name $firstday $lastday $sort_dir") { print "\n\aIn dailylogs.pl monthly processing failed.\n"; exit(1); } } # # Delete old dailylogs.run_dates file to start a new one. # unlink ("dailylogs.run_dates"); print "\n... Finished monthly log processing.\n\n"; }; # # Add line to dailylogs.run_dates file to reflect tomorrow's processing. # if (open (OUTFILE,">>dailylogs.run_dates")) { $datefield = sprintf ("%02u%02u%02u", $year, (1 + $mon), $mday); print OUTFILE "$datefield\n"; close OUTFILE; } else { print "\n\aIn dailylogs.pl unable to write: dailylogs.run_dates.\n"; print "Error: $!.\n\n"; exit(1); } # # Resubmit dailylogs to run again tomorrow at midnight. # chdir($run_dir); if (system "at -c -m midnight dailylogs") { print "\n\aIn dailylogs.pl, unable to resubmit dailylogs.pl.\n\n"; exit(1); } print "\nIn dailylogs.pl ... finished.\n\n"; exit(0);