<%+header%> <% local sys = require "luci.sys" local http = require "luci.http" local uci = require "luci.model.uci".cursor() local function to_bytes(value, unit) local multipliers = { ["KiB"] = 1024, ["MiB"] = 1024^2, ["GiB"] = 1024^3, ["TiB"] = 1024^4 } return tonumber(value) * (multipliers[unit] or 1) end local function format_date(date_str) local month_names = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} local m, d, y = date_str:match("(%d%d)/(%d%d)/(%d%d)") if m and d and y then return string.format("%02d %s %02d", tonumber(d), month_names[tonumber(m)], tonumber(y)) end return date_str end local function get_interfaces() local ifaces = {} local p = io.popen("ls /etc/vnstat/ 2>/dev/null | grep -v '.conf$' | grep -v '.old$'") if p then for iface in p:lines() do iface = iface:match("^%s*(.-)%s*$") if iface ~= "" then table.insert(ifaces, iface) end end p:close() end table.sort(ifaces) return ifaces end local function get_daily_stats(iface) local stats = {} local output = sys.exec("vnstat -i " .. iface .. " --days 2>/dev/null") or "" for date, rx_val, rx_unit, tx_val, tx_unit in output:gmatch("(%d%d/%d%d/%d%d)%s+([%d%.]+)%s+([KMGT]iB)%s+|%s+([%d%.]+)%s+([KMGT]iB)") do table.insert(stats, { date = format_date(date), rx = to_bytes(rx_val, rx_unit), tx = to_bytes(tx_val, tx_unit) }) end return stats end local function get_monthly_stats(iface) local stats = {} local output = sys.exec("vnstat -i " .. iface .. " --months 2>/dev/null") or "" for month, rx_val, rx_unit, tx_val, tx_unit in output:gmatch("(%a+%s+'%d%d)%s+([%d%.]+)%s+([KMGT]iB)%s+|%s+([%d%.]+)%s+([KMGT]iB)") do table.insert(stats, { month = month, rx = to_bytes(rx_val, rx_unit), tx = to_bytes(tx_val, tx_unit) }) end return stats end local ifaces = get_interfaces() local current_iface = http.formvalue("iface") or "wwan0_1" local view_mode = http.formvalue("mode") or "daily" local stats, title = {}, "" if current_iface ~= "" then if view_mode == "daily" then stats = get_daily_stats(current_iface) title = "<%:Daily%>" else stats = get_monthly_stats(current_iface) title = "<%:Monthly%>" end end %>

<%:Network Usage%>

<% if current_iface ~= "" and #stats > 0 then %>

<%=title%> (<%=current_iface%>)

<% else %>
<%:No data available for %> <%=current_iface%>
<% end %>
<%+footer%>