#!/usr/bin/perl###### Update "yesterday" data file### Created by Paul Dupuy, Jr.#### What time is it?($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);$mon = $mon + 1;	## change 0-based value$mday = $mday -1;	## change day to yesterday# delete "dbyesterday"unlink "/export/home/time/dbyesterday" || die "Could not delete file: $!\n";# rename "yesterday" (d[ay ]b[efore]yesterday)rename ("/export/home/time/yesterday","/export/home/time/dbyesterday") || die "Could not rename file: $!\n";# Open "dbyesterday" for inputopen (INPUT, "/export/home/time/dbyesterday") || die "Could not open file: $!\n";# Open "yesterday" for outputopen (OUTPUT, ">/export/home/time/yesterday") || die "Could not open file: $!\n";# Process the log dataif ($mday != 0) 		## Month did *not* end yesterday{	# Load associative arrays with "dbyesterday" data	while (<INPUT>)	{		chop;		split;		$dbyuserpass{$_[0]} = $_[1];		$dbyusertime{$_[0]} = $_[2];	}		# Iterate through the portmaster log file directories		while (glob("/var/adm/radacct/*.com"))	{		# get the name of yesterday's log in this portmaster directory		opendir(DIR,"$_");		@yesterdaylog = grep(/${mday}$/, readdir(DIR));				closedir(DIR);				# next directory if there is not a log to process		$yesterdaylog[0] ne "" || next;		# open the log		open (LOGFILE, "$_/$yesterdaylog[0]") || 				die "Could not open log file $_/$yesterdaylog[0]: $!\n";		while (<LOGFILE>)	## check each line		{			chop;			tr/\t//;			split;			@usertest = grep(/User-Name/, @_);			if ($usertest[0] ne "")	## a username line			{				$username = $_[2];	## set $username = "username"			}						else					## *not* a username line			{				@timetest = grep(/Acct-Session-Time/, @_);				if ($timetest[0] ne "")					## a time line				{					$dbyusertime{$username} += $_[2];	## increase time										# if this is a new user, add password					unless ($dbyuserpass{$username} =~ /^:.*/) 	## has password					{						$dbyuserpass{$username} = "::";			## new user pass					}				}			}		} #while (<LOGFILE>)				# close log		close (LOGFILE);	} #while (glob(portmaster directories))		# write the new "yesterday" data	for $key (keys %dbyusertime)	{		print OUTPUT "$key $dbyuserpass{$key} $dbyusertime{$key}\n";	}}else 					## Month ended yesterday{	while (<INPUT>)	{		chop;		s/\d+$/0/;					## change time to a single '0' digit		print OUTPUT "$_\n";		## print the updated data	}}# Close data filesclose (INPUT);close (OUTPUT);#### Sicut adhuc.1;