-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhits
executable file
·73 lines (58 loc) · 1.35 KB
/
hits
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
#!/usr/bin/perl
# vim:ft=perl:
# abstract: show httpd resources with most hits
use strict;
use Data::Dumper;
use File::LsColor qw(ls_color);
use Term::ExtendedColor qw(fg);
my $access_log = shift // "$ENV{HOME}/pi/var/log/access.log";
parse_log($access_log);
pop_ips();
pop_files();
my(%ips, %files);
sub parse_log {
my $log = shift;
my @logs;
if(ref($log) eq 'ARRAY') {
push(@logs, @{$log});
}
else {
push(@logs, $log);
}
for my $logfile(@logs) {
open(my $fh, '<', $logfile) or die($!);
while(my $line = <$fh>) {
my $re = '(.+?)';
$line =~ /^$re $re $re \[$re:$re $re\] "$re $re $re" $re $re/;
my $ip = $1;
my $ref = $2;
my $name = $3;
my $date = $4;
my $time = $5;
my $gmt = $6;
my $req = $7;
my $file = $8;
my $ptcl = $9;
my $code = $10;
my $size = $11;
$ips{$ip}++;
$files{$file}++;
}
}
}
sub pop_ips {
my $sum;
for my $ip(sort {$ips{$a} <=> $ips{$b}} keys(%ips)) {
printf("% 5d %s\n", $ips{$ip}, $ip);
$sum += $ips{$ip};
}
printf("%5s IPs\n", fg('red1', fg('bold', $sum)));
}
sub pop_files {
my $sum;
for my $file(sort {$files{$a}<=> $files{$b}} keys(%files)) {
printf("% 5d %s\n", $files{$file}, ls_color($file));
$sum += $files{$file};
}
printf("%5s requests\n", fg('orange2', fg('bold', $sum)));
}