Skip to content

Commit

Permalink
MT#61757 support writing error CDRs to file
Browse files Browse the repository at this point in the history
Change-Id: I10ab1a6ade41f3318d055d932ced1171c3b15773
  • Loading branch information
rfuchs committed Dec 17, 2024
1 parent 77f1cd6 commit ed0e5bf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1728,3 +1728,23 @@ bool cdr_verify_fields(const cdr_entry_t *cdr) {

return true;
}

bool cdr_write_error_record(const char *fn, const char *s, size_t len) {
FILE *fp = fopen(fn, "a");
if (!fp) {
L_CRITICAL("Could not open CDR error file for writing: %s", strerror(errno));
critical("Could not open CDR error file for writing, check log file for more details");
return false;
}
if (fprintf(fp, "%.*s\n", (int) len, s) <= 0) {
L_CRITICAL("Failed to write to CDR error file: %s", strerror(errno));
critical("Failed to write to CDR error file, check log file for more details");
return false;
}
if (fclose(fp)) {
L_CRITICAL("Could not write to CDR error file: %s", strerror(errno));
critical("Could not write to CDR error file, check log file for more details");
return false;
}
return true;
}
1 change: 1 addition & 0 deletions cdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ int cdr_process_records(GQueue *records, uint64_t *cdr_count, struct medmysql_ba
void cdr_parse_entry(med_entry_t *);
void cdr_truncate_call_id_suffix(char *);
bool cdr_verify_fields(const cdr_entry_t *);
bool cdr_write_error_record(const char *fn, const char *s, size_t);


#endif /* _CDR_H */
8 changes: 8 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ char *config_cdr_db;
unsigned int config_cdr_port = MEDIATOR_DEFAULT_CDRPORT;
char *config_intermediate_cdr_host;
unsigned int config_intermediate_cdr_port = MEDIATOR_DEFAULT_CDRPORT;
char *config_cdr_error_file;

char *config_prov_host;
char *config_prov_user;
Expand Down Expand Up @@ -68,6 +69,7 @@ enum config_option {
OPT_CDR_DB = 'B',
OPT_INTERMEDIATE_CDR_HOST = 'y',
OPT_INTERMEDIATE_CDR_PORT = 'Y',
OPT_CDR_ERROR_FILE = 'E',
OPT_PROV_HOST = 'S',
OPT_PROV_PORT = 'T',
OPT_PROV_USER = 'R',
Expand Down Expand Up @@ -110,6 +112,7 @@ struct option long_options[] = {
{ "cdr-db", required_argument, NULL, OPT_CDR_DB },
{ "intermediate-cdr-host", required_argument, NULL, OPT_INTERMEDIATE_CDR_HOST },
{ "intermediate-cdr-port", required_argument, NULL, OPT_INTERMEDIATE_CDR_PORT },
{ "cdr-error-file", required_argument, NULL, OPT_CDR_ERROR_FILE },
{ "prov-host", required_argument, NULL, OPT_PROV_HOST },
{ "prov-port", required_argument, NULL, OPT_PROV_PORT },
{ "prov-user", required_argument, NULL, OPT_PROV_USER },
Expand Down Expand Up @@ -157,6 +160,7 @@ static void config_help(const char *self, int rc)
" -B, --cdr-db DB\tThe CDR db name (default = '%s').\n" \
" -y, --intermediate-cdr-host HOST\tThe CDR db host (default = '%s').\n" \
" -Y, --intermediate-cdr-port PORT\tThe CDR db port (default = '%d').\n" \
" -E, --cdr-error-file FILE\tThe CDR db port (default = none).\n" \
" -S, --prov-host HOST\tThe prov db host (default = '%s').\n" \
" -T, --prov-port PORT\tThe prov db port (default = '%d').\n" \
" -R, --prov-user USER\tThe prov db user (default = '%s').\n" \
Expand Down Expand Up @@ -268,6 +272,9 @@ static void config_set_option(enum config_option option, const char *value)
case OPT_INTERMEDIATE_CDR_PORT:
config_intermediate_cdr_port = atoi(value);
break;
case OPT_CDR_ERROR_FILE:
config_set_string_option(&config_cdr_error_file, value);
break;
case OPT_PROV_HOST:
config_set_string_option(&config_prov_host, value);
break;
Expand Down Expand Up @@ -463,6 +470,7 @@ void config_cleanup()
free(config_cdr_pass);
free(config_cdr_db);
free(config_intermediate_cdr_host);
free(config_cdr_error_file);
free(config_med_host);
free(config_med_user);
free(config_med_pass);
Expand Down
1 change: 1 addition & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern char *config_cdr_pass;
extern char *config_cdr_db;
extern char *config_intermediate_cdr_host;
extern unsigned int config_intermediate_cdr_port;
extern char *config_cdr_error_file;

extern char *config_prov_host;
extern unsigned int config_prov_port;
Expand Down
3 changes: 3 additions & 0 deletions medmysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,9 @@ int medmysql_insert_cdrs(cdr_entry_t *entries, uint64_t count, struct medmysql_b
if (!cdr_verify_fields(e)) {
L_WARNING("CDR field verification failed for record");
// backtrack
if (config_cdr_error_file)
if (!cdr_write_error_record(config_cdr_error_file, begin_ptr, cdr_len))
return -1;
batch->cdrs.len -= cdr_len;
continue;
}
Expand Down

0 comments on commit ed0e5bf

Please sign in to comment.