Created
March 15, 2012 04:48
-
-
Save xenomuta/2041955 to your computer and use it in GitHub Desktop.
Asterisk CDR with NodeJS and MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
cdr_fifo_mongodb: A quick and dirty hack that consists of configuring asterisk’s | |
cdr_custom.conf to write to a named pipe, which will be then read by node and | |
then written into mongodb. | |
good alternative to cdr_mongodb, 'cause I couldn't make it work stable for 1.8 | |
*/ | |
var config = { | |
"dburl": "asterisk", /* user:pass@host/db */ | |
"collections": ["cdr"], | |
"fifo": "/var/log/asterisk/cdr-custom/cdr_fifo.csv", | |
"cdr_csv": [ | |
"clid","src","dst","dcontext","channel", | |
"dstchannel","lastapp","lastdata","start", | |
"answer","end","duration","billsec", | |
"disposition","amaflags","accountcode", | |
"uniqueid","userfield","sequence" | |
] | |
} | |
var db = require("mongojs").connect(config.dburl, config.collections); | |
var fs = require('fs') | |
var fifo; | |
function openFifo() { | |
fifo = fs.createReadStream(config.fifo) | |
fifo.on('end', openFifo) | |
fifo.on('data', function(data) { | |
var fields = data.toString().trim().split('","') | |
var doc = {} | |
for (var i in fields) { | |
doc[config.cdr_csv[i]] = fields[i].replace(/(^"|"$)/, '') | |
} | |
db.cdr.save(doc, function (err, saved) { | |
if (saved) console.log("===== Record saved:\n%s", JSON.stringify(saved)) | |
}) | |
console.log() | |
}) | |
} | |
if (typeof(fifo) == 'undefined') { | |
var exec = require('child_process').exec; | |
exec("/bin/rm " + config.fifo + ";/usr/bin/mkfifo -m 777 " + config.fifo, function (err, stdout, stderr) { | |
if (err) { | |
console.error("ERROR: Can't open fifo file\n%s", err) | |
process.exit() | |
} else { | |
openFifo() | |
} | |
}) | |
} | |
function politeExit() { | |
console.log("\nBye bye!\n") | |
process.exit() | |
} | |
process.on('SIGINT', politeExit) | |
console.log("Waiting writes on " + config.fifo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[mappings] | |
cdr_fifo.csv => ${CSV_QUOTE(${CDR(clid)})},${CSV_QUOTE(${CDR(src)})},${CSV_QUOTE(${CDR(dst)})},${CSV_QUOTE(${CDR(dcontext)})},${CSV_QUOTE(${CDR(channel)})},${CSV_QUOTE(${CDR(dstchannel)})},${CSV_QUOTE(${CDR(lastapp)})},${CSV_QUOTE(${CDR(lastdata)})},${CSV_QUOTE(${CDR(start)})},${CSV_QUOTE(${CDR(answer)})},${CSV_QUOTE(${CDR(end)})},${CSV_QUOTE(${CDR(duration)})},${CSV_QUOTE(${CDR(billsec)})},${CSV_QUOTE(${CDR(disposition)})},${CSV_QUOTE(${CDR(amaflags)})},${CSV_QUOTE(${CDR(accountcode)})},${CSV_QUOTE(${CDR(uniqueid)})},${CSV_QUOTE(${CDR(userfield)})},${CDR(sequence)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment