#!/usr/local/bin/perl # # Uploader # V1.1 pl 5 - 19 Feb 1997 # # Written by # Jouko Valta (jopi@x2ftp.oulu.fi) # # # Get uploader's e-mail address from xferlog files. # $Usage="\ Options:\ -h Print Usage.\ -d Debug\ -f Force\ -L,-w Specify output Logfile\ -r,-nw Read only. Do not update log file.\ -u,-in Find uploads only.\ -q Quiet mode.\ -v Verbose mode.\ -# 0 to 9 xferlog files to read.\ xxx search pattern\n\n"; # # Return Values # 0 Completed OK. # 1 Error occurred while processing. # # BUGS: Modifying the upload log may result in previous week's data # left unread. # # Performance: Creating uploadlog ~ 6:20 # Updating uploadlog ~ 0:__ # # # V1.0 - 28 Jun 1996 # Print filename, transfer mode (a/b), and uploader's address. # # V1.1 pl 2 - 15 Oct 1996 # Create separate incoming log file to reduce workload. # Print real hostname if different from e-mail host. # Print upload date as well. # Added '-u' option to skip download attempts. # # V1.1 pl 3 - 11 Jan 1997 # Logfile moved to /pc/log, except upon failure onto /var/tmp. # # V1.1 pl 4 - 6 Feb 1997 # -d Debug option # -f option to silence errors. # -r option to disable log file update. # Parse mail domains # # V1.1 pl 5 - 6 Feb 1997 # Strip out '-' before login name # Print file size # $EGREP = "/bin/egrep"; $ZCAT = "/usr/local/bin/gunzip --stdout"; ## Filenames $LOGHOME = "/pc/log"; if (! -d $LOGHOME || ! -w $LOGHOME) { printf STDERR "Cannot access $LOGHOME. Please, check the setting or run\n create_dosadm_logs script.\n"; $LOGHOME = "/var/tmp"; } $XFERLOG = "/ftp-service/adm/xferlog"; $UPLOADLOG = $LOGHOME . "/uploadlog"; $TMPF = "/var/tmp/upldtmp"; $InMode = 0; $Verbose = 1; $Debug = 0; $Force = 0; $Readonly= 0; $Weeks = 9; ### Command line options while ($_ = $ARGV[0], /^-/) { shift; last if /^--$/; if (/^-L|^-w/) { $UPLOADLOG = $ARGV[0]; shift; next; } if (/^-d/) { ++$Debug; next; } if (/^-f/) { ++$Force; next; } if (/^-r|^-nw/) { ++$Readonly; next; } if (/^-q/) { $Verbose = 0; next; } if (/^-v/) { ++$Verbose; next; } if (/^-u|^-in/) { $InMode = 1; next; } if (/^-(\d)/) { $Weeks = $1; next; } if (/^-h/) { die $Usage; } if (/^-/) { print STDERR "Unsupported argument near $_\n"; die $Usage; } } if ($Weeks < 0 || $Weeks > 9) { printf "Number of weeks to update is invalid.\n"; } # ----------------------------------------------------------------------------- # Update uploads log file if (!$ARGV[0] || grep(/incoming/, $ARGV[0]) ) { # Do we use the log at all ? @ustat = stat ($UPLOADLOG); @xstat = stat ($XFERLOG); @xstat1 = stat ($XFERLOG.1); # Create if ( ! -e $UPLOADLOG || -z $UPLOADLOG ) { # First time, create the log system ("rm -f $TMPF"); printf STDERR "\nCreating logfile '$UPLOADLOG'.\n"; for ($n = $Weeks; $n >= 2; --$n) { $Verbose && printf STDERR "Reading $XFERLOG.$n.gz\n"; system ("$ZCAT $XFERLOG.$n.gz | $EGREP \"/etc/|incoming\" >> $TMPF"); } # for $Verbose && printf STDERR "Reading $XFERLOG.1\n"; system ("$EGREP \"/etc/|incoming\" $XFERLOG.1 >> $TMPF"); $Verbose && printf STDERR "Reading $XFERLOG\n"; system ("$EGREP \"/etc/|incoming\" $XFERLOG >> $TMPF"); $Verbose && printf STDERR "Removing index fecthes...\n\n"; system ("$EGREP -v \"incoming/README.uploader|/README.UPLOAD|/.message|/.cache|please_supply_the_desc\" $TMPF > $UPLOADLOG"); system ("rm $TMPF"); system ("chgrp -f ftpadm $UPLOADLOG"); chmod 0660, $UPLOADLOG; } # create else { # Update uploadlog # Check if xferlog.1 and the older files need to be updated. # All log after the last updater are read to a temp file. # # BUGS: Modifying the upload log may result in previous week's data # left unread. system ("rm -f $TMPF"); if ($xstat1[9] > $ustat[9]) { # This file is never written to printf STDERR "Updating $UPLOADLOG.\n"; for ($n = $Weeks; $n >= 2; --$n) { @xstat1 = stat ("$XFERLOG.$n.gz"); if ($xstat1[9] > $ustat[9]) { $Verbose && printf STDERR "Reading $XFERLOG.$n.gz\n"; system ("$ZCAT $XFERLOG.$n.gz | $EGREP \"/etc/|incoming\" >> $TMPF"); } # if } # for # we tested this one already. $Verbose && printf STDERR "Reading $XFERLOG.1\n"; system ("$EGREP \"/etc/|incoming\" $XFERLOG.1 >> $TMPF"); } # update xferlog.1 info # Update current # Update new info from the current xferlog. # Always true when the previous section is true as well. if (($xstat[9] - $ustat[9]) > 14000) { # ~ 4 hours $Verbose && printf STDERR "Reading $XFERLOG\n"; system ("$EGREP \"/etc/|incoming\" $XFERLOG >> $TMPF"); print "DEBUG: reading completed.\n"; # Find the first new entry, and copy the rest to uploadlog. # $lastold ="`/bin/tail -1 $UPLOADLOG`"; system ("/bin/tail -1 $UPLOADLOG > /tmp/last_upload"); open (inf, "/tmp/last_upload") || printf STDERR "Cannot stat $UPLOADLOG"; $lastold = ; close inf; system ("/bin/rm -f /tmp/last_upload"); print "DEBUG: Last line '$lastold'"; open (inf, $TMPF); open (outf, ">>$UPLOADLOG"); $count = 0; while () { (/$lastold/) && break; # skip to the last old line } print "DEBUG: $_"; if ($_ ne $lastold) { printf STDERR "\n***ERROR: uploadlog is not continuous.\n"; seek(inf, 0, 0); } printf outf "### Updated %s", `/bin/date`; while () { #print "DEBUG: $_"; if (/incoming\/README.uploader|README.UPLOAD|.message|.cache|please_supply_the_desc/) { next; } print outf $_; ++$count; } system ("rm -f $TMPF"); $Verbose && printf STDERR "Read $count lines.\n"; } # update current } # update # Syntax check. (Note: this block is enabled if !ARGV[0] ) if (!$ARGV[0]) { printf STDERR "uploader: No file to check. Aborting.\n"; exit 1; # Make sure we don't accept empty field } } # log created or updated # --------------------------------------------------------------------------- # Scan selected transfer log if (!open (M, "$EGREP -i $ARGV[0] $UPLOADLOG |")) { print STDERR "$0: Cannot read uploader data log.\n"; exit 1; } # Parse line $found = 0; while () { # date year host size file ascii inout user ftp 0 * if (/^(\S+ \S+ \S+ \S+\s+\S+)\s+\S+\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S) _ (\S) \S\s+\-*(\S+)\s+\S+\s+\S+ \*$/) { $date = $1; $host = $2; $size = $3; $file = $4; $mode = $5; # ascii / binary $inout= $6; $mail = $7; if ($InMode && $inout ne 'i') { next; } do parse_fields($mail); } } exit 0; sub parse_fields { local $address = $mail; ($user, $edomain) = split /\@/, $address; $Debug && print "DEBUG: $user\t$edomain\t$host\n\n"; if (!$edomain) { $edomain = $host; $Force || printf STDERR "NO HOST SPECIFIED. Assuming '$user\@$host'\n"; } # $edomain expected as domain or parallel machine for $host # elsif (! ($host =~ /$edomain/) ) { # elsif (! ($host =~ /$edomain/) && $edomain ne $host) { ($Verbose && !$Force) && printf STDERR "DIFFERENT HOST SPECIFIED. Real '$host'; claimed '$edomain'\n"; printf "$file\t$mode\t$size\t$user\@$edomain\t$host\t\[$date\]\n"; return; } printf "$file\t$mode\t$size\t$user\@$edomain\t*\t[$date]\n"; }