-- Parse CDR,find and count 480.
-- Run daily or weakly, montly.
--
local lfs = require("lfs")
local cdr_path = "/var/log/freeswitch/cdr-csv/"
local answer_cause = "NORMAL_CLEARING"
local sip_480_cause = "Temporarily Unavailable"
local total_calls = 0
local answered_calls = 0
local total_billsec = 0
local sip_480_count = 0
-- Split CSV line into table
local function split_csv_line(line)
local t = {}
for field in line:gmatch('([^,]+)') do
table.insert(t, field)
end
return t
end
for file in lfs.dir(cdr_path) do
if file:match("%.csv$") then
local f = io.open(cdr_path .. file, "r")
if f then
local header_line = f:read("*l")
local headers = split_csv_line(header_line)
local hangup_idx, billsec_idx
for i, h in ipairs(headers) do
if h == "hangup_cause" then hangup_idx = i end
if h == "billsec" then billsec_idx = i end
end
if hangup_idx and billsec_idx then
for line in f:lines() do
total_calls = total_calls + 1
local fields = split_csv_line(line)
local cause = fields[hangup_idx]
if cause == answer_cause then
answered_calls = answered_calls + 1
local billsec = tonumber(fields[billsec_idx]) or 0
total_billsec = total_billsec + billsec
end
if cause == sip_480_cause then
sip_480_count = sip_480_count + 1
end
end
end
f:close()
end
end
end
local asr = (answered_calls / total_calls) * 100
local acd = answered_calls > 0 and (total_billsec / answered_calls) or 0
print(string.format("Total Calls: %d", total_calls))
print(string.format("Answered Calls: %d", answered_calls))
print(string.format("ASR: %.2f%%", asr))
print(string.format("ACD: %.2f seconds", acd))
print(string.format("SIP 480 (Temporarily Unavailable) Count: %d", sip_480_count))