This repository has been archived by the owner on Nov 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirrors-access-count
executable file
·123 lines (91 loc) · 2.41 KB
/
mirrors-access-count
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
#!/usr/bin/env perl
use v5.20;
#
# find logs
#
my $logdir = "/var/log/nginx/";
my $logname = "xidian-access.log*";
my $mirrordir = "/srv/http/mirrors.root/mirrors/";
my ($year, $month, $month_name) = split(' ', `date '+%Y %m %b'`);
if ($ARGV[0] eq '--help') {
say "usage: $0 [--last-month]";
exit;
}
if ($ARGV[0] eq '--last-month') {
($year, $month, $month_name) = split(' ', `date -d "$year-$month-01 -1 month" '+%Y %m %b'`);
}
chomp(my $sdate = `date -d "$year-$month-01 -1 day" +%Y-%m-%d`);
chomp(my $edate = `date -d "$year-$month-02 +1 month" +%Y-%m-%d`);
chomp(my $sfile = `mktemp`);
chomp(my $efile = `mktemp`);
`touch -d "$sdate" $sfile`;
`touch -d "$edate" $efile`;
chomp(my @logfiles = `find "$logdir" -name "$logname" -type f -newer $sfile ! -newer $efile`);
`rm $sfile`;
`rm $efile`;
#
# parse logs
#
my %data;
for (@logfiles) {
if (/\.gz$/) {
open(INPUT, "-|", "zcat $_");
} else {
open(INPUT, "-|", "cat $_");
}
LINE: for (<INPUT>) {
my (
$ip, # 222.25.188.1
undef, # -
undef, # -
$time, # [15/May/2017:06:25:30
undef, # +0800]
$request, # "GET
$url, # /mirrors/ubuntu/dists/precise/Release.gpg
undef, # HTTP/1.1"
$respond, # 304
) = split;
next LINE if $request ne '"GET'; # count GET request only
next LINE if $url =~ '/$'; # ignore directory
my (undef, $mm, $yy) = split('[/:]', $time);
next LINE if $yy ne $year;
next LINE if $mm ne $month_name;
my (undef, $subpath, $distro) = split('/', $url);
next LINE if $subpath ne 'mirrors'; # skip other subpath
$data{$distro}[$respond]++;
}
close(INPUT);
}
#
# write post
#
chomp(my @distros = `ls $mirrordir`);
my ($success, $error) = (0, 0);
for my $distro (@distros) {
my $dist_ref = $data{$distro};
$success += $dist_ref->[200] + $dist_ref->[304];
$error += $dist_ref->[404];
}
my $title = "软件源使用统计";
chomp(my $date = `date "+%Y-%m-%d"`);
my $entry_syntax = "%-20s%10d%10d\n";
my $title_syntax = ($entry_syntax =~ s/d/s/gr);
say "---";
say "layout: post";
say "title: \"[$month_name $year] $title\"";
say "date: $date";
say "categories: reports";
say "author: bot";
say "excerpt: \"SUCCESS: $success; ERROR: $error\"";
say "---";
say "```";
printf($title_syntax, "ENTRY", "SUCCESS", "ERR=404");
for my $distro (@distros) {
my $dist_ref = $data{$distro};
printf($entry_syntax,
$distro,
$dist_ref->[200] + $dist_ref->[304], # SUCCESS
$dist_ref->[404], # ERR=404
);
}
say "```";