#! /usr/bin/perl -w # Last edited on 2008-02-04 20:11:18 by stolfi # CGI-BIN script that calls scan-apache-logs and # formats the output as HTML my(%params); sub get_params(); sub main(); sub protect_shell($); sub scan_logs($$$$$); sub format_report($$$$$); sub protect_html($); sub main() { get_params(); my($year) = $params{'year'}; my($month) = $params{'month'}; my($day) = $params{'day'}; my($date) = sprintf("%04d/%02d/%02d", $year,$month,$day); my($order) = $params{'order'}; my($regexp) = $params{'regexp'}; my($tempfile) = "/tmp/$$.log"; my($errfile) = "/tmp/$$.err"; scan_logs($tempfile,$errfile,$order,$regexp,$date); format_report($tempfile,$errfile,$order,$regexp,$date); unlink($tempfile,$errfile); } sub get_params() { # Get form info my ($info_size) = $ENV{'CONTENT_LENGTH'}; my ($info); read STDIN,$info, $info_size; # Split form info into name=value pairs: foreach my $pair (split(/&/, $info)) { # Split pair into key and value: my ($key,$value) = split(/=/, $pair); # Decode % encoding: $key =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C",hex($1))/eg; $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C",hex($1))/eg; # Should we worry about "+" being used for space? # Save in %params hash: $params{$key} = $value; } } sub scan_logs($$$$$) { my($tempfile,$errfile,$order,$regexp,$date) = @_; $order = protect_shell($order); $regexp = protect_shell($regexp); $date = protect_shell($date); system("${STOLFIHOME}/csh/http-log-tools/cgi-lib/scan-apache-logs -sort ${order} ${regexp} ${date} 1> ${tempfile} 2> ${errfile}"); } sub format_report($$$$$) { my($tempfile,$errfile,$order,$regexp,$date) = @_; my(@t) = localtime(time()); my($gendate) = sprintf( "%04d/%02d/%02d %02d:%02d:%02d (local)", 1900+$t[5],1+$t[4],$t[3],$t[2],$t[1],$t[0] ); $order = protect_html($order); $regexp = protect_html($regexp); $date = protect_html($date); print STDOUT "Content-type: text/html\n\n"; print STDOUT < IC-UNICAMP - HTTP SERVER LOG REPORT

Generated ${gendate}

IC-Unicamp HTTP Server - Log Report

Parameters

EOF print STDOUT "\n

Date: ${date}

\n"; print STDOUT "\n

Regexp: ${regexp}

\n"; print STDOUT "\n

Sorted by: ${order}

\n\n"; if (! -z ${errfile}) { print STDOUT "

Messages

\n\n"; print STDOUT protect_html(`cat ${errfile}`) . "\n\n"; } print STDOUT "

Results

\n\n"; print STDOUT "\n\n"; print STDOUT "\n"; print STDOUT " \n"; open(REPORT, "<${tempfile}"); my($numaccs,$numok,$totbytes,$numerrs,$numother)=(0,0,0,0,0); my(@flds); while () { chomp($_); @flds = split(/[ ]+/,$_); my($host,$date,$time,$file,$code,$bytes) = @flds; my($color) = ""; $numaccs++; if ($code eq "-") { $numother++; $color = "#cc00ff"; } elsif (($code eq 200) || ($code eq 206)) { $numok++; } elsif ($code >= 400) { $numerrs++; $color = "#ff0000"; } else { $numother++; $color = "#cc00ff"; } if ($bytes ne "-") { $totbytes += $bytes; } ($host,$date,$time,$file,$code,$bytes) = map { $_ = protect_html($_); } @flds; if ($color ne "") { $file = "${file}"; $code = "${code}"; } print STDOUT " \n"; } print STDOUT "
datetimehoststatusbytesfile
$date$time$host$code$bytes$file
\n\n"; print STDOUT "
\n\n"; print STDOUT "

Summary

\n\n"; print STDOUT < ${numaccs} requests made
${numok} files served successfully
${numerrs} failed requests
${numother} other server responses
${totbytes} bytes transferred

EOF print STDOUT "\n"; print STDOUT "\n"; } sub protect_shell($) { my($val) = @_; $val =~ s/[']/'"'"'/g; $val =~ s/[\n]//g; $val =~ s/[!]/\\!/g; return "'" . $val . "'"; } sub protect_html($) { my($lin) = @_; $lin =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack("C",hex($1))/eg; $lin =~ s/[&]/\&/g; $lin =~ s/[ ]/\ /g; $lin =~ s//\>/g; return $lin; } main();