概要

  • Apache の error_log を HTML テーブルとして整形する。
  • mod_security 対応。

動作サンプル

ソース

index.cgi

すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
!
-
|
!
 
 
 
 
 
 
 
 
-
|
|
|
!
 
 
 
-
|
|
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
-
|
!
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
-
-
|
!
|
|
|
|
|
|
|
-
|
!
|
-
-
|
|
|
-
-
|
-
|
!
|
!
!
|
!
!
|
!
|
|
!
 
 
-
|
|
|
-
|
|
|
|
!
|
!
 
 
-
|
|
|
|
|
!
 
 
-
|
|
|
!
 
 
-
|
|
|
!
 
 
-
|
|
|
|
-
-
|
-
|
|
!
!
!
|
|
!
 
 
#!/usr/bin/perl
# Apache のエラーログを整形
 
use strict;
use warnings;
use utf8;
use Encode;
use CGI::Pretty qw( -no_xhtml *table *ol );
use Net::DNS::Resolver;
 
$CGI::POST_MAX = 80 * 1024;
 
my $charsetConsole    = 'UTF-8';
#my $charsetConsole	= 'CP932';
my $charsetFile        = 'UTF-8';
 
binmode( STDIN,  ":encoding($charsetConsole)" );
binmode( STDOUT, ":encoding($charsetConsole)" );
binmode( STDERR, ":encoding($charsetConsole)" );
 
my $cgiName = 'Apache Error Log Formatter';
 
my @pair_type = (
    {    # mod_security
        header        => qr/(ModSecurity:.*\.)\s(\[.*)\s*/,
        to_pair        => qr/\[\S+\s[^\]]+\]\s*/,
        to_keyval    => qr/\[(\S+)\s"([^\]]+)"\]\s*/,
    },
    {    # default, detail部無し
        header        => qr/(.*)\s*/,
    },
);
 
my $q = new CGI;
$q->charset( $charsetFile );
my $br = $q->br();
 
my %processer = (
    Client => \&toHostName,
    data => sub{
        my $word = shift;
        $word =~ s/(\s(ARGS|ARGS_NAMES):)/$1$br/;
        return $word;
    },
);
 
my $log = $q->param('LOG') || '';
if ( ( ! $ENV{'SERVER_ADDR'} ) && ( ! $log ) && ( my $file = $q->param('FILE') ) ){
    open( my $IN, "<:encoding($charsetFile)", $file ) or die( "$file: $!\n" );
    my @body = <$IN>;
    close( $IN );
    $log = join( "", @body );
}
 
print $q->header(
    -type => 'text/html',
    -charset => $charsetConsole,
);
print $q->start_html(
    -title => $cgiName,
    -head    => [
        $q->meta( { -http_equiv=>'Content-Type', -content=>"text/html; charset=" . $charsetConsole } ),
        $q->meta( { -http_equiv=>'Content-style-type', -content=>"text/css" } ),
    ],
    -style    => [ { 'src'=>'/take.css' }, ],
);
print $q->h1( $cgiName );
 
if ( $log ){
    print_result( $log );
} else {
    print_form();
}
 
print $q->end_html . "\n";
 
exit;
 
sub print_result
{
    my( $log ) = @_;
    $log =~ s/\r\n|\n\r/\n/gmx;
    $log =~ s/\r/\n/gmx;
    $log =~ s/"([^\"]*)"/'"'.encodeURIComponent($1).'"'/gmxe;
 
    print $q->p( $q->a( { -href => $q->url( -absolute => 1 ) }, 'form' ) );
 
    my $index = 0;
    foreach my $line ( split( "\n", $log ) ){
        if ( $line =~ /^\x{feff}?\s*$/ ){
            next;
        }
#		print $q->h2( ++$index );
        print $q->start_table( { -border => 1 } );
#		print $q->Tr( $q->th( 'Line' ), $q->td( $line ) );
        my( $datetime, $level, $client, $line2 ) =
            ( $line =~ /\[([^\]]+)\]\s\[([^\]]+)\](?:\s\[client\s([^\]]+)\])?\s*(.*)\s*$/ );
        print $q->Tr( $q->th( 'Date Time' ), $q->td( $datetime ) );
        print $q->Tr( $q->th( 'Level' ), $q->td( $level ) );
        if ( $client ){
            print $q->Tr( $q->th( 'Client' ), $q->td( toHostName( $client ) ) );
        }
#		print $q->Tr( $q->th( 'Line2' ), $q->td( $line2 ) );
        foreach my $type ( @pair_type ){
            if ( $line2 =~ /^$type->{'header'}/ ){
                my( $summary, $detail ) = ( $1, $2 );
                $summary =~ s/([\.\,])\s/$1$br/;
                print $q->Tr( $q->th( 'Summary' ), $q->td( decodeURIComponent( $summary ) ) );
                if ( $detail ){
                    foreach my $pair ( getPair( $type->{'to_pair'}, $type->{'to_keyval'}, $detail ) ){
                        my( $key, $val ) = ( $pair->[0], $pair->[1] );
                        if ( my $proc = $processer{$key} ){
                            $val = $proc->( $val );
                        }
                        print $q->Tr( $q->th( $key ), $q->td( $val ) );
                    }
                }
                last;
            }
        }
        print $q->end_table(), $q->br();
    }
 
    print $q->p( $q->a( { -href => $q->url( -absolute => 1 ) }, 'form' ) );
}
 
sub getPair
{
    my( $to_pair, $to_keyval, $line ) = @_;
    my @ret = ();
#	print "'$to_pair'\n'$line'\n";
    foreach my $pair ( $line =~ /$to_pair/g ){
#		print "'$pair'\n";
        my( $key, $val ) = ( $pair =~ /^$to_keyval$/ );
        $val = decodeURIComponent( $val );
        push( @ret, [ $key, $val ] );
    }
    return @ret;
}
 
sub print_form
{
    print 
        $q->start_form( -action => $q->url, -enctype => 'multipart/form-data' ), 
        $q->submit, " ", $q->reset, $q->br(), 
        $q->textarea( -name=>'LOG', -rows=>20, -columns=>80 ), 
        $q->end_form;
}
 
sub encodeURIComponent
{
    my $str = encode( 'utf-8', shift );
    $str =~ s/([^0-9A-Za-z!\x27()*\-._~])/sprintf("%%%02X", ord($1))/eg;    # \x27 = '
    return $str;
}
 
sub decodeURIComponent
{
    my $str = shift;
    $str =~ s/%([0-9A-F][0-9A-F])/pack( "H*", $1 )/egi;
    return decode( 'utf-8', $str );
}
 
sub toHostName
{
    my $ip_address = shift || '';
    my $ret = '';
 
    my $resolver = Net::DNS::Resolver->new;
    if ( my $ans = $resolver->query( $ip_address ) ){
        for my $rr ( $ans->answer ){
            #print $rr->string, "\n";
            if ( $rr->type eq 'PTR' ){
                $ret = $rr->ptrdname;
                last;
            }
        }
    }
 
    return $ip_address . '; ' . $ret;
}
 
# EOF

添付ファイル: fileAELF.zip 232件 [詳細]

リロード   新規 下位ページ作成 編集 凍結 差分 添付 コピー 名前変更   ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: Wed, 27 Feb 2013 10:47:40 JST (1790d)