-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogstat.awk
executable file
·180 lines (143 loc) · 3.64 KB
/
logstat.awk
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
#!/usr/bin/gawk -f
BEGIN {
FPAT = "([^ ]+)|(\"[^\"]+\")"
PROCINFO["sorted_in"] = "@val_num_desc"
}
# successful requests without auth
$3 == "-" && $6 ~ /^[123]/ {
match($5, /^"([A-Z]+)\s+(.*)"$/, m)
val = sprintf("%3s %-6s %s", $6, m[1], m[2])
++vals[val]
}
# all requests without auth
$3 == "-" {
++ips["anonymous"][$1]
}
# requests with auth
$3 != "-" && NF == 10 {
++users[$3][$1]
++usertotal[$3]
++ips["authenticated"][$1]
}
# count users per day
NF == 10 {
match($4, /^.([0-9]{4}-[0-9]{2}-[0-9]{2}).*$/,m)
++days[m[1]][$3]
}
# count users per blueprint
$3 != "-" && NF == 10 {
match($10, /^"(.*)"$/, m)
++blueprints[$3][m[1]]
++blueprintstotal[$3]
}
# count error pages
$6 >= 400 && $6 ~ /^[0-9]+$/ {
match($5, /^"([A-Z]+)\s+(.*)"$/, m)
val = sprintf("%3s %-6s %s", $6, m[1], m[2])
if ($6 >= 500) ++errs5xx[val];
else ++errs4xx[val];
}
# distinct user agents
NF == 10 {
++user_agents[$9];
}
END {
printf "\n"
printf " Successful Requests without Authentication\n"
printf " ==========================================\n\n"
for (val in vals) {
printf "%7s %s\n", vals[val], val
}
printf "\n"
printf " Unsuccessful Requests\n"
printf " =====================\n\n"
for (err in errs5xx) {
printf "%7s %s\n", errs5xx[err], err
}
printf "\n"
for (err in errs4xx) {
printf "%7s %s\n", errs4xx[err], err
}
printf "\n"
printf " Distinct Users\n"
printf " ==============\n\n"
for (user in usertotal) {
printf "%7s %s\n", usertotal[user], user
for (ip in users[user]) {
printf "%7s %s\n", users[user][ip], ip
}
printf "\n"
}
for (ip in ips["authenticated"]) {
numauthreq += ips["authenticated"][ip]
}
for (ip in ips["anonymous"]) {
numanonreq += ips["anonymous"][ip]
}
for (k in ips) {
for (ip in ips[k]) {
if (ip in country_for_ip) {
by_country[country_for_ip[ip]] += ips[k][ip]
} else {
cmd = sprintf("whois %s | awk '/^country:/{print $2}' | head -n1", ip)
if ( ( cmd | getline result ) > 0 ) {
country = result
} else {
country = "-?-"
}
by_country[country] += ips[k][ip]
country_for_ip[ip] = country
}
}
}
printf "\n"
printf " Connected IPs\n"
printf " =============\n\n"
printf "%7s %s\n", numauthreq, "AUTHENTICATED"
for (ip in ips["authenticated"]) {
printf "%7s %-3s %15s\n", ips["authenticated"][ip], country_for_ip[ip], ip
}
printf "\n"
printf "%7s %s\n", numanonreq, "ANONYMOUS"
for (ip in ips["anonymous"]) {
printf "%7s %-3s %15s\n", ips["anonymous"][ip], country_for_ip[ip], ip
}
printf "\n"
printf "%7s %s\n", numanonreq + numauthreq, "BY COUNTRY"
for (c in by_country) {
printf "%7s %-3s\n", by_country[c], c
}
printf "\n"
printf " Activity per Day\n"
printf " ================\n\n"
n = asorti(days, dayidx, "@ind_str_asc")
for (i=1; i<=n; ++i) {
delete dayusers
total = 0
for (user in days[dayidx[i]]) {
dayusers[user] = days[dayidx[i]][user]
total += days[dayidx[i]][user]
}
printf "%7s %s\n", total, dayidx[i]
for (user in dayusers) {
printf "%7s %s\n", dayusers[user], user
}
printf "\n"
}
printf "\n"
printf " User Agents\n"
printf " ===========\n\n"
for (ua in user_agents) {
printf "%7s %s\n", user_agents[ua], ua
}
printf "\n"
printf " Blueprints\n"
printf " ===========\n\n"
for (user in blueprintstotal) {
printf "%7s %s\n", blueprintstotal[user], user
for (blueprint in blueprints[user]) {
printf "%7s %s\n", blueprints[user][blueprint], blueprint
}
printf "\n"
}
}