summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--squlogan86
1 files changed, 43 insertions, 43 deletions
diff --git a/squlogan b/squlogan
index 4f717fa..43b42dd 100644
--- a/squlogan
+++ b/squlogan
@@ -13,7 +13,7 @@ function parseline(line)
clientip=words[3],
code=string.match(words[4] or "", "^[^/]*"),
status=string.match(words[4] or "", "[^/]*$"),
- bytes=words[5],
+ bytes=tonumber(words[5]),
method=words[6],
URL=words[7],
site = string.match(words[7], "^(%a+://[^/]*)"),
@@ -59,10 +59,10 @@ function add_stats(total, subkeys, entry)
return total
end
-local function newstats(keyname, subkeys)
+local function newstats(groupname, subkeys)
local t = {
bytes = 0, lines = 0, elapsed = 0,
- key = keyname,
+ group = groupname,
}
local _,k
for _,k in pairs(subkeys) do
@@ -96,7 +96,7 @@ function parsefile(file, keytbl, subkeys)
local numlines = 0
local f = assert(io.open(file))
local stats = {}
- subkeys = subkeys or {"code", "category", "action" }
+ subkeys = subkeys or {"code", "squarkcategory", "squarkaction" }
stats.total = newstats("total", subkeys)
local _,k
for _,k in pairs(keytbl or {"site"}) do
@@ -132,65 +132,65 @@ function print_totals(header, tbl)
io.write("\n")
end
-function sort_by_key(tbl, key)
+function sort_by_func(tbl, func)
local t = {}
for k, v in pairs(tbl) do
table.insert(t, v)
end
- table.sort(t, function(a,b)
+ table.sort(t, func)
+ return t
+end
+
+function sort_by_key(tbl, key)
+ return sort_by_func(tbl, function(a,b)
return a[key] > b[key]
end)
- return t
end
-function top_n_by_key(tbl, n, key)
- sorted = sort_by_key(tbl, key)
+function top_n_by_func(tbl, n, sortfunc, callback)
+ sorted = sort_by_func(tbl, sortfunc)
local i
for i = 1, #sorted do
- print(sorted[i].key, sorted[i][key])
+ callback(sorted[i])
if i > n then
break
end
end
- print()
end
-stats = parsefile(arg[1] or "/var/log/squid/access.log",
- {"site", "squarkaction", "clientuserid"})
-
---[[
-numusers = 0
-for user,tbl in pairs(stats.user) do
- print_totals("user "..user, tbl)
- numusers = numusers + 1
+function top_n_by_key(tbl, n, statskey)
+ top_n_by_func(tbl, n,
+ function(a, b)
+ return a[statskey] > b[statskey]
+ end,
+ function(rec)
+ print(rec.group, rec[statskey])
+ end)
end
---]]
---[[
-numsites = 0
-for site, tbl in pairs(stats.site) do
- print_totals("site "..site, tbl)
- numsites = numsites + 1
-end
---]]
-
-print("Top 5 sites by hits")
-top_n_by_key(stats.site, 5, "lines")
-print()
-
+stats = parsefile(arg[1] or "/var/log/squid/access.log",
+ {"site"},
+ {"code"})
-print("Top 5 sites by bytes")
-top_n_by_key(stats.site, 5, "bytes")
-print()
---[[
-print("Top 5 users by bytes")
-top_n_by_key(stats.clientuserid, 5, "bytes")
-print()
-]]--
+print("= Top 10 sites by hits =")
+top_n_by_key(stats.site, 10, "lines")
-print("Top 5 squark action by hits")
-top_n_by_key(stats.squarkaction, 5, "lines")
-print()
+print("= Top 10 sites by TCP_HIT bytes =")
+top_n_by_func(stats.site, 10,
+ function(a, b)
+ local an, bn = 0, 0
+ if a.code.TCP_HIT and a.code.TCP_HIT.bytes then
+ an = a.code.TCP_HIT.bytes
+ end
+ if b.code.TCP_HIT and b.code.TCP_HIT.bytes then
+ bn = b.code.TCP_HIT.bytes
+ end
+ print(an, bn)
+ return an > bn
+ end,
+ function(rec)
+ print(rec.group, rec.code.TCP_HIT.bytes)
+ end)