60 #define DATE_FORMAT "'%Y-%m-%d %T'" 62 #define PGSQL_MIN_VERSION_SCHEMA 70300 64 static const char name[] =
"pgsql";
65 static const char config[] =
"cdr_pgsql.conf";
104 #define LENGTHEN_BUF(size, var_sql) \ 107 if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) { \ 108 if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 3) / 512 + 1) * 512) != 0) { \ 109 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", pghostname, table); \ 112 AST_RWLIST_UNLOCK(&psql_columns); \ 113 ast_mutex_unlock(&pgsql_lock); \ 119 #define LENGTHEN_BUF1(size) \ 120 LENGTHEN_BUF(size, sql); 121 #define LENGTHEN_BUF2(size) \ 122 LENGTHEN_BUF(size, sql2); 129 e->
command =
"cdr show pgsql status";
131 "Usage: cdr show pgsql status\n" 132 " Shows current connection status for cdr_pgsql\n";
143 char status2[100] =
"";
154 snprintf(status2, 99,
" with username %s",
pgdbuser);
157 snprintf(status2, 99,
" using table %s",
table);
160 snprintf(buf,
sizeof(buf),
"%s%s for ", status, status2);
169 ast_cli(a->
fd,
"Not currently connected to a PgSQL server.\n");
226 if (PQstatus(
conn) != CONNECTION_BAD) {
231 #ifdef HAVE_PGSQL_pg_encoding_to_char 238 pgerror = PQerrorMessage(
conn);
251 char *separator =
"";
252 size_t bufsize = 513;
255 if (!escapebuf || !sql || !sql2) {
256 goto ast_log_cleanup;
266 if (strcmp(cur->
name,
"calldate") == 0 && !value) {
284 if (strcmp(cur->
name,
"start") == 0 || strcmp(cur->
name,
"calldate") == 0) {
285 if (strncmp(cur->
type,
"int", 3) == 0) {
288 }
else if (strncmp(cur->
type,
"float", 5) == 0) {
298 }
else if (strcmp(cur->
name,
"answer") == 0) {
299 if (strncmp(cur->
type,
"int", 3) == 0) {
302 }
else if (strncmp(cur->
type,
"float", 5) == 0) {
312 }
else if (strcmp(cur->
name,
"end") == 0) {
313 if (strncmp(cur->
type,
"int", 3) == 0) {
316 }
else if (strncmp(cur->
type,
"float", 5) == 0) {
318 ast_str_append(&sql2, 0,
"%s%f", separator, (
double)cdr->
end.tv_sec + (
double)cdr->
end.tv_usec / 1000000.0);
326 }
else if (strcmp(cur->
name,
"duration") == 0 || strcmp(cur->
name,
"billsec") == 0) {
327 if (cur->
type[0] ==
'i') {
332 }
else if (strncmp(cur->
type,
"float", 5) == 0) {
342 }
else if (strcmp(cur->
name,
"disposition") == 0 || strcmp(cur->
name,
"amaflags") == 0) {
343 if (strncmp(cur->
type,
"int", 3) == 0) {
357 if (strncmp(cur->
type,
"int", 3) == 0) {
359 if (value && sscanf(value,
"%30lld", &whatever) == 1) {
366 }
else if (strncmp(cur->
type,
"float", 5) == 0) {
367 long double whatever;
368 if (value && sscanf(value,
"%30Lf", &whatever) == 1) {
378 size_t required_size = strlen(value) * 2 + 1;
384 if (required_size > bufsize) {
385 char *tmpbuf =
ast_realloc(escapebuf, required_size);
389 goto ast_log_cleanup;
393 bufsize = required_size;
395 PQescapeStringConn(
conn, escapebuf, value, strlen(value),
NULL);
415 if (PQstatus(
conn) == CONNECTION_OK) {
420 if (PQstatus(
conn) == CONNECTION_OK) {
426 pgerror = PQerrorMessage(
conn);
432 goto ast_log_cleanup;
436 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
437 pgerror = PQresultErrorMessage(result);
440 ast_log(
LOG_ERROR,
"Connection may have been lost... attempting to reconnect.\n");
442 if (PQstatus(
conn) == CONNECTION_OK) {
449 if (PQresultStatus(result) != PGRES_COMMAND_OK) {
450 pgerror = PQresultErrorMessage(result);
451 ast_log(
LOG_ERROR,
"HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
545 ast_log(
LOG_NOTICE,
"cdr_pgsql configuration contains no global section, skipping module %s.\n",
546 reload ?
"reload" :
"load");
551 ast_log(
LOG_WARNING,
"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
564 tmp =
"asteriskcdrdb";
611 ast_log(
LOG_WARNING,
"PostgreSQL database port not specified. Using default 5432.\n");
677 if (PQstatus(
conn) != CONNECTION_BAD) {
679 char *fname, *ftype, *flen, *fnotnull, *fdef;
681 ast_debug(1,
"Successfully connected to PostgreSQL database.\n");
686 #ifdef HAVE_PGSQL_pg_encoding_to_char 692 version = PQserverVersion(
conn);
695 char *schemaname, *tablename, *tmp_schemaname, *tmp_tablename;
696 if (strchr(
table,
'.')) {
698 tmp_tablename = strchr(tmp_schemaname,
'.');
699 *tmp_tablename++ =
'\0';
702 tmp_tablename =
table;
704 tablename =
ast_alloca(strlen(tmp_tablename) * 2 + 1);
705 PQescapeStringConn(
conn, tablename, tmp_tablename, strlen(tmp_tablename),
NULL);
707 schemaname =
ast_alloca(strlen(tmp_schemaname) * 2 + 1);
708 PQescapeStringConn(
conn, schemaname, tmp_schemaname, strlen(tmp_schemaname),
NULL);
710 snprintf(sqlcmd,
sizeof(sqlcmd),
"SELECT a.attname, t.typname, a.attlen, a.attnotnull, pg_catalog.pg_get_expr(d.adbin, d.adrelid) adsrc, a.atttypmod FROM (((pg_catalog.pg_class c INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace AND c.relname = '%s' AND n.nspname = %s%s%s) INNER JOIN pg_catalog.pg_attribute a ON (NOT a.attisdropped) AND a.attnum > 0 AND a.attrelid = c.oid) INNER JOIN pg_catalog.pg_type t ON t.oid = a.atttypid) LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum ORDER BY n.nspname, c.relname, attnum",
714 snprintf(sqlcmd,
sizeof(sqlcmd),
"SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod FROM pg_class c, pg_type t, pg_attribute a LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid AND d.adnum = a.attnum WHERE c.oid = a.attrelid AND a.atttypid = t.oid AND (a.attnum > 0) AND c.relname = '%s' ORDER BY c.relname, attnum",
table);
717 result = PQexec(
conn, sqlcmd);
718 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
719 pgerror = PQresultErrorMessage(result);
727 rows = PQntuples(result);
729 ast_log(
LOG_ERROR,
"cdr_pgsql: Failed to query database columns. No columns found, does the table exist?\n");
739 for (i = 0; i < rows; i++) {
740 fname = PQgetvalue(result, i, 0);
741 ftype = PQgetvalue(result, i, 1);
742 flen = PQgetvalue(result, i, 2);
743 fnotnull = PQgetvalue(result, i, 3);
744 fdef = PQgetvalue(result, i, 4);
745 if (atoi(flen) == -1) {
747 flen = PQgetvalue(result, i, 5);
750 cur =
ast_calloc(1,
sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
752 sscanf(flen,
"%30d", &cur->
len);
753 cur->
name = (
char *)cur +
sizeof(*cur);
754 cur->
type = (
char *)cur +
sizeof(*cur) + strlen(fname) + 1;
755 strcpy(cur->
name, fname);
756 strcpy(cur->
type, ftype);
757 if (*fnotnull ==
't') {
774 pgerror = PQerrorMessage(
conn);
#define AST_CLI_DEFINE(fn, txt,...)
Asterisk main include file. File version handling, generic pbx functions.
#define ast_realloc(p, len)
A wrapper for realloc()
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
static const char config[]
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
static ast_mutex_t pgsql_lock
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
descriptor for a cli entry.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define CONFIG_STATUS_FILEINVALID
#define PGSQL_MIN_VERSION_SCHEMA
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
static struct ast_cli_entry cdr_pgsql_status_cli[]
#define ast_mutex_lock(a)
#define ast_strdup(str)
A wrapper for strdup()
void ast_cli(int fd, const char *fmt,...)
int args
This gets set in ast_cli_register()
#define ast_strlen_zero(foo)
void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int raw)
Format a CDR variable from an already posted CDR.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Configuration File Parser.
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_config_load(filename, flags)
Load a config file.
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
General Asterisk PBX channel definitions.
#define AST_RWLIST_TRAVERSE
void ast_config_destroy(struct ast_config *config)
Destroys a config.
#define ast_strdupa(s)
duplicate a string in memory from the stack
#define ast_malloc(len)
A wrapper for malloc()
#define CONFIG_STATUS_FILEUNCHANGED
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Responsible for call detail data.
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
#define LENGTHEN_BUF1(size)
static time_t connect_time
static char version[AST_MAX_EXTENSION]
#define ast_calloc(num, len)
A wrapper for calloc()
#define AST_RWLIST_REMOVE_HEAD
Module has failed to load, may be in an inconsistent state.
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Structure used to handle boolean flags.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
void ast_cli_print_timestr_fromseconds(int fd, int seconds, const char *prefix)
Print on cli a duration in seconds in format s year(s), s week(s), s day(s), s hour(s), s second(s)
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
#define AST_RWLIST_INSERT_TAIL
static int load_module(void)
Standard Command Line Interface.
static int pgsql_log(struct ast_cdr *cdr)
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
static int unload_module(void)
static int config_module(int reload)
#define ASTERISK_GPL_KEY
The text the key() function should return.
#define DEBUG_ATLEAST(level)
Asterisk module definitions.
static void pgsql_reconnect(void)
static char * handle_cdr_pgsql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Handle the CLI command cdr show pgsql status.
static void empty_columns(void)
#define AST_MUTEX_DEFINE_STATIC(mutex)
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
#define ast_mutex_unlock(a)
#define LENGTHEN_BUF2(size)