--- loncom/metadata_database/parse_activity_log.pl 2005/09/19 18:31:57 1.16 +++ loncom/metadata_database/parse_activity_log.pl 2006/03/30 05:29:01 1.22 @@ -2,7 +2,7 @@ # # The LearningOnline Network # -# $Id: parse_activity_log.pl,v 1.16 2005/09/19 18:31:57 matthew Exp $ +# $Id: parse_activity_log.pl,v 1.22 2006/03/30 05:29:01 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -48,7 +48,9 @@ # parameter is not set $logthis is set to ¬hing, which does what you # would expect. # - +BEGIN { + eval "use Apache2::compat();"; +}; use strict; use DBI; use lib '/home/httpd/lib/perl/Apache'; @@ -131,7 +133,7 @@ if ($log) { print STDERR "$0: logging to $logfile".$/; if (! open(LOGFILE,">$logfile")) { warn("Unable to open $logfile for writing. Run aborted."); - &cleanup_and_exit(5); + &clean_up_and_exit(5); } else { $logthis = \&log_to_file; } @@ -144,6 +146,7 @@ if ($log) { my $sourcefilename; # activity log data my $newfilename; # $sourcefilename will be renamed to this my $error_filename; # Errors in parsing the activity log will be written here +my $chunk_filename; # where we save data we are not going to write to db if ($srcfile) { $sourcefilename = $srcfile; } else { @@ -153,6 +156,8 @@ my $sql_filename = $sourcefilename; $sql_filename =~ s|[^/]*$|activity.log.sql|; my $gz_sql_filename = $sql_filename.'.gz'; # +$chunk_filename = $sourcefilename.".unprocessed_chunks"; +# my $xml_filename = $sourcefilename; my $gz_xml_filename = $xml_filename.'.gz'; if (defined($xmlfile)) { @@ -180,7 +185,9 @@ if (! defined($xmlfile)) { open(LOCKFILE,'>'.$lockfilename); if (!flock(LOCKFILE,LOCK_EX|LOCK_NB)) { warn("Unable to lock $lockfilename. Aborting".$/); - &clean_up_and_exit(6); + # don't call clean_up_and_exit another instance is running and + # we don't want to 'cleanup' their files + exit 6; } if (! -e $newfilename && -e $sourcefilename) { @@ -484,8 +491,18 @@ sub process_courselog { if (! defined($host)) { $host = 'unknown'; } my $prevchunk = 'none'; foreach my $chunk (split(/\&\&\&/,$log)) { + if (length($chunk) > 20000) { + # avoid putting too much data into the database + # (usually an uploaded file or something similar) + if (! &savechunk(\$chunk,$timestamp,$host)) { + close(IN); + return undef; + } + next; + } my $warningflag = ''; my ($time,$res,$uname,$udom,$action,@values)= split(/:/,$chunk); + # if (! defined($res) || $res =~ /^\s*$/) { $res = '/adm/roles'; $action = 'LOGIN'; @@ -499,8 +516,12 @@ sub process_courselog { if ($action !~ /^(LOGIN|VIEW|POST|CSTORE|STORE)$/) { $warningflag .= 'action'; print $error_fh 'full log entry:'.$log.$/; - print $error_fh 'error on chunk:'.$chunk.$/; - $logthis->('(action) Unable to parse '.$/.$chunk.$/. + print $error_fh 'error on chunk (saving)'.$/; + if (! &savechunk(\$chunk,$timestamp,$host)) { + close(IN); + return undef; + } + $logthis->('(action) Unable to parse chunk'.$/. 'got '. 'time = '.$time.$/. 'res = '.$res.$/. @@ -537,6 +558,20 @@ sub process_courselog { } close IN; return $linecount; + ## + ## + sub savechunk { + my ($chunkref,$timestamp,$host) = @_; + my $chunk = &escape(${$chunkref}); + if (! open(CHUNKFILE,">>$chunk_filename") || + ! print CHUNKFILE $timestamp.':'.$host.':'.$chunk.$/) { + # abort + close(CHUNKFILE); + return 0; + } + close(CHUNKFILE); + return 1; + } }