Skip to content
Snippets Groups Projects
Commit f3f8faa5 authored by Wenpeng Song's avatar Wenpeng Song Committed by Sukru Senli
Browse files

#5892 New cdr csv rotate FIFO

parent 6234ce9c
No related branches found
No related tags found
No related merge requests found
...@@ -48,8 +48,7 @@ ...@@ -48,8 +48,7 @@
#define CSV_LOG_DIR "/cdr-csv" #define CSV_LOG_DIR "/cdr-csv"
#define CSV_MASTER "/Master.csv" #define CSV_MASTER "/Master.csv"
#define CSV_MASTER_OLD "/Master_old.csv" #define CSV_TEMP "/Temp.csv"
#define CSV_MASTER_COUNT "/Master_count"
#define DATE_FORMAT "%Y-%m-%d %T" #define DATE_FORMAT "%Y-%m-%d %T"
...@@ -60,10 +59,10 @@ static int loguserfield = 0; ...@@ -60,10 +59,10 @@ static int loguserfield = 0;
static int loaded = 0; static int loaded = 0;
static int newcdrcolumns = 0; static int newcdrcolumns = 0;
static int maxrow = 100; /* max row stored in csv file before rotate */ static int maxrow = 100; /* max row stored in csv file before rotate */
static int rowcount = -1;
static const char config[] = "cdr.conf"; static const char config[] = "cdr.conf";
static char file_csv_master[PATH_MAX]; static char file_csv_master[PATH_MAX];
static char file_csv_old[PATH_MAX]; static char file_csv_temp[PATH_MAX];
static char file_csv_master_count[PATH_MAX];
/* #define CSV_LOGUNIQUEID 1 */ /* #define CSV_LOGUNIQUEID 1 */
/* #define CSV_LOGUSERFIELD 1 */ /* #define CSV_LOGUSERFIELD 1 */
...@@ -129,10 +128,8 @@ static int load_config(int reload) ...@@ -129,10 +128,8 @@ static int load_config(int reload)
ast_mutex_lock(&f_lock); ast_mutex_lock(&f_lock);
snprintf(file_csv_master, sizeof(file_csv_master), snprintf(file_csv_master, sizeof(file_csv_master),
"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER); "%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER);
snprintf(file_csv_old, sizeof(file_csv_old), snprintf(file_csv_temp, sizeof(file_csv_temp),
"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER_OLD); "%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_TEMP);
snprintf(file_csv_master_count, sizeof(file_csv_master_count),
"%s/%s/%s", ast_config_AST_LOG_DIR, CSV_LOG_DIR, CSV_MASTER_COUNT);
ast_mutex_unlock(&f_lock); ast_mutex_unlock(&f_lock);
for (; v; v = v->next) { for (; v; v = v->next) {
...@@ -315,53 +312,58 @@ static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr) ...@@ -315,53 +312,58 @@ static int build_csv_record(char *buf, size_t bufsize, struct ast_cdr *cdr)
return -1; return -1;
} }
static int getrowcount(char *file_path_count) static int writefile(char *s, char *file_path)
{ {
/* get current recorded count from file*/ FILE *f, *ft;
FILE *f; int c = 0;
int rowcount = 0; char buf[1024];
if (!(f = fopen(file_path_count, "r"))) { /* because of the absolutely unconditional need for the
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path_count, strerror(errno)); highest reliability possible in writing billing records,
return -1; we open write and close the log file each time */
} if ((rowcount == maxrow)||(rowcount == -1)) {
fscanf(f, "%d", &rowcount); if (!(f = fopen(file_path, "r+"))) {
fclose(f); ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path, strerror(errno));
return rowcount; return -1;
} }
if (!(ft = fopen(file_csv_temp, "w"))) {
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_csv_temp, strerror(errno));
fclose(f);
return -1;
}
static int writerowcount(char *file_path_count, int count) while (!feof(f)) {
{ if (fgets(buf, sizeof(buf), f) != NULL) {
/* write count number to file */ c++;
FILE *f; if (c != 1) {
if (!(f = fopen(file_path_count, "w"))) { fprintf(ft, "%s", buf); /* skip the first record and cp the rest to Temp.csv */
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path_count, strerror(errno)); }
return -1; }
} }
fprintf(f, "%d\n", count); if (c != maxrow) {
fflush(f); rowcount = c + 1; /* correct row count if not reaching max, adding 1 for the new log below */
fclose(f); fputs(s, f); /* and put log to the original file*/
return 0; fclose(f);
} fclose(ft);
remove(file_csv_temp); /* rm Temp file*/
} else {
fclose(f);
fputs(s, ft); /* add the new record at the end */
fflush(ft);
fclose(ft);
remove(file_path); /* rm the original file and rename the temp file to it */
rename(file_csv_temp, file_path);
}
static int writefile(char *s, char *file_path, char *file_path_old, char *file_path_count) } else {
{ if (!(f = fopen(file_path, "a"))) {
FILE *f; ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path, strerror(errno));
int rowcount = getrowcount(file_path_count); return -1;
/* because of the absolutely unconditional need for the }
highest reliability possible in writing billing records, fputs(s, f);
we open write and close the log file each time */ fflush(f); /* be particularly anal here */
if (rowcount >= (maxrow/2)) { fclose(f);
rename(file_path, file_path_old); /* rotate(rename as back up) when reach max row */ rowcount++; /* update row count record if not reach max */
rowcount = 0; /* clear row count */ }
}
if (!(f = fopen(file_path, "a"))) {
ast_log(LOG_ERROR, "Unable to open file %s : %s\n", file_path, strerror(errno));
return -1;
}
fputs(s, f);
fflush(f); /* be particularly anal here */
fclose(f);
writerowcount(file_path_count, rowcount+1); /* update row count record */
return 0; return 0;
} }
...@@ -370,16 +372,12 @@ static int writefile(char *s, char *file_path, char *file_path_old, char *file_p ...@@ -370,16 +372,12 @@ static int writefile(char *s, char *file_path, char *file_path_old, char *file_p
static int writefile_account(char *s, char *acc) static int writefile_account(char *s, char *acc)
{ {
char file_account[PATH_MAX]; char file_account[PATH_MAX];
char file_account_old[PATH_MAX];
char file_account_count[PATH_MAX];
if (strchr(acc, '/') || (acc[0] == '.')) { if (strchr(acc, '/') || (acc[0] == '.')) {
ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc); ast_log(LOG_WARNING, "Account code '%s' insecure for writing file\n", acc);
return -1; return -1;
} }
snprintf(file_account, sizeof(file_account), "%s/%s/%s.csv", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc); snprintf(file_account, sizeof(file_account), "%s/%s/%s.csv", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
snprintf(file_account_old, sizeof(file_account_old), "%s/%s/%s_old.csv", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc); return writefile(s, file_account);
snprintf(file_account_count, sizeof(file_account_count), "%s/%s/%s_count", ast_config_AST_LOG_DIR,CSV_LOG_DIR, acc);
return writefile(s, file_account, file_account_old, file_account_count);
} }
static int csv_log(struct ast_cdr *cdr) static int csv_log(struct ast_cdr *cdr)
...@@ -398,7 +396,7 @@ static int csv_log(struct ast_cdr *cdr) ...@@ -398,7 +396,7 @@ static int csv_log(struct ast_cdr *cdr)
} }
ast_mutex_lock(&f_lock); ast_mutex_lock(&f_lock);
if (writefile(buf, file_csv_master, file_csv_old, file_csv_master_count)) if (writefile(buf, file_csv_master))
ast_log(LOG_WARNING, "Unable to write CSV record to master '%s' : %s\n", file_csv_master, strerror(errno)); ast_log(LOG_WARNING, "Unable to write CSV record to master '%s' : %s\n", file_csv_master, strerror(errno));
if (accountlogs && !ast_strlen_zero(cdr->accountcode)) { if (accountlogs && !ast_strlen_zero(cdr->accountcode)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment