diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2011-12-21 13:05:39 +0100 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2011-12-21 13:05:39 +0100 |
commit | 772b68220d9d0dc9310aaaab66cdb327689b07c1 (patch) | |
tree | 432fa35fe837ae2cfc2188d63aa77efe895a4153 | |
parent | 09f636eae556d660ea8ca82c92c4841d4fbeac7e (diff) | |
download | squlogan-772b68220d9d0dc9310aaaab66cdb327689b07c1.tar.bz2 squlogan-772b68220d9d0dc9310aaaab66cdb327689b07c1.tar.xz |
Allow user specify sub group keys
-rw-r--r-- | squlogan | 60 |
1 files changed, 35 insertions, 25 deletions
@@ -26,7 +26,10 @@ function parseline(line) end -function add_by_key(tbl, key, entry) +function add_by_subkey(tbl, key, entry) + if key == nil then + return tbl + end if tbl[key] == nil then tbl[key] = { lines = entry.lines, @@ -42,11 +45,12 @@ function add_by_key(tbl, key, entry) end -function add_stats(total, entry) - add_by_key(total.code, entry.code, entry) - add_by_key(total.category, entry.squarkcategory, entry) - if entry.squarkaction then - add_by_key(total.action, entry.squarkaction, entry) +function add_stats(total, subkeys, entry) + local _,k + for _,k in pairs(subkeys) do + if entry[k] ~= nil then + add_by_subkey(total[k], entry[k], entry) + end end total.elapsed = total.elapsed + entry.elapsed @@ -55,39 +59,45 @@ function add_stats(total, entry) return total end -local function newstats(keyname) - return { +local function newstats(keyname, subkeys) + local t = { bytes = 0, lines = 0, elapsed = 0, - code = {}, category = {}, action = {}, key = keyname, } + local _,k + for _,k in pairs(subkeys) do + t[k] = {} + end + return t end -local function add_stats_by_key(tbl, key, entry) +-- code = {}, category = {}, action = {}, +local function add_stats_by_key(tbl, key, subkeys, entry) if key == nil then return tbl end if tbl[key] == nil then - tbl[key] = newstats(key) + tbl[key] = newstats(key, subkeys) end - add_stats(tbl[key], entry) + add_stats(tbl[key], subkeys, entry) return tbl end -function collect_stats(stats, entry, keytbl) +function collect_stats(stats, keytbl, subkeys, entry) local _,k - add_stats(stats.total, entry) + add_stats(stats.total, subkeys, entry) for _,k in pairs(keytbl) do - add_stats_by_key(stats[k], entry[k], entry) + add_stats_by_key(stats[k], entry[k], subkeys, entry) end return stats end -function parsefile(file, keytbl) +function parsefile(file, keytbl, subkeys) local numlines = 0 local f = assert(io.open(file)) local stats = {} - stats.total = newstats() + subkeys = subkeys or {"code", "category", "action" } + stats.total = newstats("total", subkeys) local _,k for _,k in pairs(keytbl or {"site"}) do stats[k] = {} @@ -95,7 +105,7 @@ function parsefile(file, keytbl) local line for line in f:lines() do - collect_stats(stats, parseline(line), keytbl) + collect_stats(stats, keytbl, subkeys, parseline(line)) end f:close() return stats @@ -146,7 +156,7 @@ function top_n_by_key(tbl, n, key) end stats = parsefile(arg[1] or "/var/log/squid/access.log", - {"site", "squarkcategory", "clientuserid"}) + {"site", "squarkaction", "clientuserid"}) --[[ numusers = 0 @@ -165,13 +175,13 @@ for site, tbl in pairs(stats.site) do end --]] -print("Top 10 sites by hits") -top_n_by_key(stats.site, 10, "lines") +print("Top 5 sites by hits") +top_n_by_key(stats.site, 5, "lines") print() -print("Top 10 sites by bytes") -top_n_by_key(stats.site, 10, "bytes") +print("Top 5 sites by bytes") +top_n_by_key(stats.site, 5, "bytes") print() --[[ @@ -180,7 +190,7 @@ top_n_by_key(stats.clientuserid, 5, "bytes") print() ]]-- -print("Top 5 category by hits") -top_n_by_key(stats.squarkcategory, 5, "lines") +print("Top 5 squark action by hits") +top_n_by_key(stats.squarkaction, 5, "lines") print() |