123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- #include <sys/types.h>
- #include <asterisk/channel.h>
- #include <asterisk/cdr.h>
- #include <asterisk/module.h>
- #include <asterisk/logger.h>
- #include <asterisk/utils.h>
- #include "../asterisk.h"
- #include "../astconf.h"
- #define CSV_LOG_DIR "/cdr-csv"
- #define CSV_MASTER "/Master.csv"
- #define DATE_FORMAT "%Y-%m-%d %T"
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <time.h>
- static char *desc = "Comma Separated Values CDR Backend";
- static char *name = "csv";
- static FILE *mf = NULL;
- static int append_string(char *buf, char *s, size_t bufsize)
- {
- int pos = strlen(buf);
- int spos = 0;
- int error = 0;
- if (pos >= bufsize - 4)
- return -1;
- buf[pos++] = '\"';
- error = -1;
- while(pos < bufsize - 3) {
- if (!s[spos]) {
- error = 0;
- break;
- }
- if (s[spos] == '\"')
- buf[pos++] = '\"';
- buf[pos++] = s[spos];
- spos++;
- }
- buf[pos++] = '\"';
- buf[pos++] = ',';
- buf[pos++] = '\0';
- return error;
- }
- static int append_int(char *buf, int s, size_t bufsize)
- {
- char tmp[32];
- int pos = strlen(buf);
- snprintf(tmp, sizeof(tmp), "%d", s);
- if (pos + strlen(tmp) > bufsize - 3)
- return -1;
- strncat(buf, tmp, bufsize - strlen(buf) - 1);
- pos = strlen(buf);
- buf[pos++] = ',';
- buf[pos++] = '\0';
- return 0;
- }
- static int append_date(char *buf, struct timeval tv, size_t bufsize)
- {
- char tmp[80] = "";
- struct tm tm;
- time_t t;
- t = tv.tv_sec;
- if (strlen(buf) > bufsize - 3)
- return -1;
- if (!tv.tv_sec && !tv.tv_usec) {
- strncat(buf, ",", bufsize - strlen(buf) - 1);
- return 0;
- }
- localtime_r(&t,&tm);
- strftime(tmp, sizeof(tmp), DATE_FORMAT, &tm);
- return append_string(buf, tmp, bufsize);
- }
- static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
- {
- buf[0] = '\0';
-
- append_string(buf, cdr->accountcode, bufsize);
-
- append_string(buf, cdr->src, bufsize);
-
- append_string(buf, cdr->dst, bufsize);
-
- append_string(buf, cdr->dcontext, bufsize);
-
- append_string(buf, cdr->clid, bufsize);
-
- append_string(buf, cdr->channel, bufsize);
-
- append_string(buf, cdr->dstchannel, bufsize);
-
- append_string(buf, cdr->lastapp, bufsize);
-
- append_string(buf, cdr->lastdata, bufsize);
-
- append_date(buf, cdr->start, bufsize);
-
- append_date(buf, cdr->answer, bufsize);
-
- append_date(buf, cdr->end, bufsize);
-
- append_int(buf, cdr->duration, bufsize);
-
- append_int(buf, cdr->billsec, bufsize);
-
- append_string(buf, ast_cdr_disp2str(cdr->disposition), bufsize);
-
- append_string(buf, ast_cdr_flags2str(cdr->amaflags), bufsize);
- #ifdef CSV_LOGUNIQUEID
-
- append_string(buf, cdr->uniqueid, bufsize);
- #endif
- #ifdef CSV_LOGUSERFIELD
-
- append_string(buf, cdr->userfield,bufsize);
- #endif
-
- if (strlen(buf) < bufsize - 5) {
-
- buf[strlen(buf) - 1] = '\0';
- strncat(buf, "\n", bufsize - strlen(buf) - 1);
- return 0;
- }
- return -1;
- }
- static int writefile(char *s, char *acc)
- {
- char tmp[AST_CONFIG_MAX_PATH];
- FILE *f;
- if (strchr(acc, '/') || (acc[0] == '.')) {
- ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
- return -1;
- }
- snprintf(tmp, sizeof(tmp), "%s/%s/%s.csv", (char *)ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
- f = fopen(tmp, "a");
- if (!f)
- return -1;
- fputs(s, f);
- fflush(f);
- fclose(f);
- return 0;
- }
- static int csv_log(struct ast_cdr *cdr)
- {
-
- char buf[1024];
- char csvmaster[AST_CONFIG_MAX_PATH];
- snprintf(csvmaster, sizeof(csvmaster),"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
- #if 0
- printf("[CDR] %s ('%s' -> '%s') Dur: %ds Bill: %ds Disp: %s Flags: %s Account: [%s]\n", cdr->channel, cdr->src, cdr->dst, cdr->duration, cdr->billsec, ast_cdr_disp2str(cdr->disposition), ast_cdr_flags2str(cdr->amaflags), cdr->accountcode);
- #endif
- if (build_csv_record(buf, sizeof(buf), cdr)) {
- ast_log(LOG_WARNING, "Unable to create CSV record in %d bytes. CDR not recorded!\n", (int)sizeof(buf));
- } else {
-
- mf = fopen(csvmaster, "a");
- if (!mf) {
- ast_log(LOG_ERROR, "Unable to re-open master file %s : %s\n", csvmaster, strerror(errno));
- }
- if (mf) {
- fputs(buf, mf);
- fflush(mf);
- fclose(mf);
- mf = NULL;
- }
- if (!ast_strlen_zero(cdr->accountcode)) {
- if (writefile(buf, cdr->accountcode))
- ast_log(LOG_WARNING, "Unable to write CSV record to account file '%s' : %s\n", cdr->accountcode, strerror(errno));
- }
- }
- return 0;
- }
- char *description(void)
- {
- return desc;
- }
- int unload_module(void)
- {
- if (mf)
- fclose(mf);
- ast_cdr_unregister(name);
- return 0;
- }
- int load_module(void)
- {
- int res;
- res = ast_cdr_register(name, desc, csv_log);
- if (res) {
- ast_log(LOG_ERROR, "Unable to register CSV CDR handling\n");
- if (mf)
- fclose(mf);
- }
- return res;
- }
- int reload(void)
- {
- return 0;
- }
- int usecount(void)
- {
- return 0;
- }
- char *key()
- {
- return ASTERISK_GPL_KEY;
- }
|