001 #!/usr/local/bin/perl -w 002 use strict; 003 use Mojolicious::Lite; 004 use ApacheLog::Parser 005 qw(parse_line_to_hash); 006 use Mojo::IOLoop; 007 use POSIX; 008 use Socket; 009 use JSON qw(encode_json); 010 011 my $listen = "http://localhost:8083"; 012 @ARGV = (qw(daemon --listen), $listen); 013 014 my $base_url = "http://website.com"; 015 016 my $file = "access.log"; 017 sysopen my $fh, "$file", 018 O_NONBLOCK|O_RDONLY or die $!; 019 020 my $loop = Mojo::IOLoop->singleton(); 021 022 ########################################### 023 websocket "/myws" => sub { 024 ########################################### 025 my($self) = @_; 026 027 my $timer_cb; 028 $timer_cb = sub { 029 for my $line ( @{ tail( $fh ) } ) { 030 my %fields = 031 parse_line_to_hash( $line ); 032 033 if( $fields{ request } eq "GET" and 034 $fields{ file } =~ /\.html?$/ and 035 # skip our own requests 036 $fields{ client } ne 037 $self->tx->remote_address 038 ) { 039 my $url = $base_url . 040 $fields{ file }; 041 my $data = { 042 url => $url, 043 host => 044 revlookup( $fields{ client } ), 045 }; 046 $self->send_message( 047 encode_json( $data ) ); 048 last; 049 } 050 } 051 $loop->timer( 5, $timer_cb); 052 }; 053 $timer_cb->(); 054 }; 055 056 ########################################### 057 get '/' => sub { 058 ########################################### 059 my ($self) = @_; 060 061 (my $ws_url = $listen) =~ s/http/ws/; 062 $ws_url .= "/myws"; 063 $self->{stash}->{ws_url} = $ws_url; 064 } => 'index'; 065 066 app->start(); 067 068 ########################################### 069 sub tail { 070 ########################################### 071 my($fh) = @_; 072 073 my($buf, $chunk, $result); 074 075 while( $result = sysread $fh, 076 $chunk, 1024 ) { 077 $buf .= $chunk; 078 } 079 080 if( defined $result ) { 081 chomp $buf; 082 my @lines = map { s/\s+$//g; $_; } 083 split /\n/, $buf; 084 return \@lines; 085 } 086 087 return undef; 088 } 089 090 ########################################### 091 sub revlookup { 092 ########################################### 093 my($ip) = @_; 094 095 my $host = (gethostbyaddr( 096 inet_aton($ip), AF_INET ))[0]; 097 $host = $ip unless defined $host; 098 return $host; 099 } 100 101 __DATA__ 102 @@ index.html.ep 103 % layout 'default'; 104 105 Host: 106 URL: 107 108 110 111 @@ layouts/default.html.ep 112 113 Apache Peek 114 129 130 <%== content %> 131