57 #define DATE_FORMAT "%Y-%m-%d %T.%6q" 59 #define PGSQL_BACKEND_NAME "CEL PGSQL backend" 61 #define PGSQL_MIN_VERSION_SCHEMA 70300 63 static char *
config =
"cel_pgsql.conf";
80 #define CEL_SHOW_USERDEF_DEFAULT 0 101 #define LENGTHEN_BUF(size, var_sql) \ 104 if (ast_str_strlen(var_sql) + size + 1 > ast_str_size(var_sql)) { \ 105 if (ast_str_make_space(&var_sql, ((ast_str_size(var_sql) + size + 3) / 512 + 1) * 512) != 0) { \ 106 ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CEL '%s:%s' failed.\n", pghostname, table); \ 109 AST_RWLIST_UNLOCK(&psql_columns); \ 115 #define LENGTHEN_BUF1(size) \ 116 LENGTHEN_BUF(size, sql); 117 #define LENGTHEN_BUF2(size) \ 118 LENGTHEN_BUF(size, sql2); 133 ast_str_set(&conn_info, 0,
"host=%s port=%s dbname=%s user=%s",
169 if (PQstatus(
conn) != CONNECTION_BAD) {
172 pgerror = PQerrorMessage(
conn);
183 char *escapebuf =
NULL;
186 size_t bufsize = 513;
189 if (!escapebuf || !sql || !sql2) {
190 goto ast_log_cleanup;
196 #define SEP (first ? "" : ",") 203 if (strcmp(cur->
name,
"eventtime") == 0) {
204 if (strncmp(cur->
type,
"int", 3) == 0) {
207 }
else if (strncmp(cur->
type,
"float", 5) == 0) {
212 (
double) record.
event_time.tv_usec / 1000000.0);
220 }
else if (strcmp(cur->
name,
"eventtype") == 0) {
221 if (cur->
type[0] ==
'i') {
225 }
else if (strncmp(cur->
type,
"float", 5) == 0) {
230 const char *event_name;
238 }
else if (strcmp(cur->
name,
"amaflags") == 0) {
239 if (strncmp(cur->
type,
"int", 3) == 0) {
250 if (strcmp(cur->
name,
"userdeftype") == 0) {
252 }
else if (strcmp(cur->
name,
"cid_name") == 0) {
254 }
else if (strcmp(cur->
name,
"cid_num") == 0) {
256 }
else if (strcmp(cur->
name,
"cid_ani") == 0) {
258 }
else if (strcmp(cur->
name,
"cid_rdnis") == 0) {
260 }
else if (strcmp(cur->
name,
"cid_dnid") == 0) {
262 }
else if (strcmp(cur->
name,
"exten") == 0) {
264 }
else if (strcmp(cur->
name,
"context") == 0) {
266 }
else if (strcmp(cur->
name,
"channame") == 0) {
268 }
else if (strcmp(cur->
name,
"appname") == 0) {
270 }
else if (strcmp(cur->
name,
"appdata") == 0) {
272 }
else if (strcmp(cur->
name,
"accountcode") == 0) {
274 }
else if (strcmp(cur->
name,
"peeraccount") == 0) {
276 }
else if (strcmp(cur->
name,
"uniqueid") == 0) {
278 }
else if (strcmp(cur->
name,
"linkedid") == 0) {
280 }
else if (strcmp(cur->
name,
"userfield") == 0) {
282 }
else if (strcmp(cur->
name,
"peer") == 0) {
284 }
else if (strcmp(cur->
name,
"extra") == 0) {
285 value = record.
extra;
292 }
else if (strncmp(cur->
type,
"int", 3) == 0) {
294 if (value && sscanf(value,
"%30lld", &whatever) == 1) {
301 }
else if (strncmp(cur->
type,
"float", 5) == 0) {
302 long double whatever;
303 if (value && sscanf(value,
"%30Lf", &whatever) == 1) {
313 size_t required_size = strlen(value) * 2 + 1;
319 if (required_size > bufsize) {
320 char *tmpbuf =
ast_realloc(escapebuf, required_size);
324 goto ast_log_cleanup;
328 bufsize = required_size;
330 PQescapeStringConn(
conn, escapebuf, value, strlen(value), NULL);
348 if (PQstatus(
conn) == CONNECTION_OK) {
353 if (PQstatus(
conn) == CONNECTION_OK) {
357 pgerror = PQerrorMessage(
conn);
363 goto ast_log_cleanup;
367 if (PQresultStatus(
result) != PGRES_COMMAND_OK) {
368 pgerror = PQresultErrorMessage(
result);
373 if (PQstatus(
conn) == CONNECTION_OK) {
378 if (PQresultStatus(
result) != PGRES_COMMAND_OK) {
379 pgerror = PQresultErrorMessage(
result);
380 ast_log(
LOG_ERROR,
"HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
471 ast_log(
LOG_WARNING,
"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
482 tmp =
"asteriskceldb";
575 if (PQstatus(
conn) != CONNECTION_BAD) {
577 char *fname, *ftype, *flen, *fnotnull, *fdef, *tablename, *tmp_tablename;
580 ast_debug(1,
"Successfully connected to PostgreSQL database.\n");
583 version = PQserverVersion(
conn);
585 if ((tmp_tablename = strrchr(
table,
'.'))) {
588 tmp_tablename =
table;
590 tablename =
ast_alloca(strlen(tmp_tablename) * 2 + 1);
591 PQescapeStringConn(
conn, tablename, tmp_tablename, strlen(tmp_tablename),
NULL);
595 lenschema = strlen(
schema);
599 snprintf(sqlcmd,
sizeof(sqlcmd),
600 "SELECT a.attname, t.typname, a.attlen, a.attnotnull, pg_catalog.pg_get_expr(d.adbin, d.adrelid) adsrc, a.atttypmod " 601 "FROM (((pg_catalog.pg_class c INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace " 602 "AND c.relname = '%s' AND n.nspname = %s%s%s) " 603 "INNER JOIN pg_catalog.pg_attribute a ON (" 604 "NOT a.attisdropped) AND a.attnum > 0 AND a.attrelid = c.oid) " 605 "INNER JOIN pg_catalog.pg_type t ON t.oid = a.atttypid) " 606 "LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid " 607 "AND d.adnum = a.attnum " 608 "ORDER BY n.nspname, c.relname, attnum",
610 lenschema == 0 ?
"" :
"'", lenschema == 0 ?
"current_schema()" : schemaname, lenschema == 0 ?
"" :
"'");
612 snprintf(sqlcmd,
sizeof(sqlcmd),
613 "SELECT a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc, a.atttypmod " 614 "FROM pg_class c, pg_type t, pg_attribute a " 615 "LEFT OUTER JOIN pg_attrdef d ON a.atthasdef AND d.adrelid = a.attrelid " 616 "AND d.adnum = a.attnum WHERE c.oid = a.attrelid AND a.atttypid = t.oid " 617 "AND (a.attnum > 0) AND c.relname = '%s' ORDER BY c.relname, attnum", tablename);
620 result = PQexec(
conn, sqlcmd);
621 if (PQresultStatus(result) != PGRES_TUPLES_OK) {
622 pgerror = PQresultErrorMessage(result);
629 rows = PQntuples(result);
630 for (i = 0; i < rows; i++) {
631 fname = PQgetvalue(result, i, 0);
632 ftype = PQgetvalue(result, i, 1);
633 flen = PQgetvalue(result, i, 2);
634 fnotnull = PQgetvalue(result, i, 3);
635 fdef = PQgetvalue(result, i, 4);
636 ast_verb(4,
"Found column '%s' of type '%s'\n", fname, ftype);
637 cur =
ast_calloc(1,
sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
639 sscanf(flen,
"%30d", &cur->
len);
640 cur->
name = (
char *)cur +
sizeof(*cur);
641 cur->
type = (
char *)cur +
sizeof(*cur) + strlen(fname) + 1;
642 strcpy(cur->
name, fname);
643 strcpy(cur->
type, ftype);
644 if (*fnotnull ==
't') {
659 pgerror = PQerrorMessage(
conn);
const char * account_code
const char * caller_id_name
Helper struct for getting the fields out of a CEL event.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_realloc(p, len)
A wrapper for realloc()
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
const char * user_defined_name
int ast_cel_backend_register(const char *name, ast_cel_backend_cb backend_callback)
Register a CEL backend.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
const char * application_data
const char * application_name
#define CONFIG_STATUS_FILEINVALID
#define LENGTHEN_BUF2(size)
#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).
Structure for variables, used for configurations and for channel variables.
static void pgsql_reconnect(void)
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
const char * caller_id_num
#define ast_mutex_lock(a)
#define ast_strdup(str)
A wrapper for strdup()
#define ast_verb(level,...)
static int my_load_module(int reload)
static int my_unload_module(void)
#define ast_strlen_zero(foo)
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.
General Asterisk PBX channel definitions.
#define AST_RWLIST_TRAVERSE
void ast_config_destroy(struct ast_config *config)
Destroys a config.
#define LENGTHEN_BUF1(size)
uint32_t version
struct ABI version
#define ast_malloc(len)
A wrapper for malloc()
int ast_cel_backend_unregister(const char *name)
Unregister a CEL backend.
#define CONFIG_STATUS_FILEUNCHANGED
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
enum ast_cel_event_type event_type
#define PGSQL_BACKEND_NAME
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
const char * caller_id_rdnis
static char version[AST_MAX_EXTENSION]
#define ast_calloc(num, len)
A wrapper for calloc()
static int process_my_load_module(struct ast_config *cfg)
#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...
const char * caller_id_ani
static int unload_module(void)
#define AST_CEL_EVENT_RECORD_VERSION
struct ABI version
Structure used to handle boolean flags.
Support for logging to various files, console and syslog Configuration in file logger.conf.
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",)
const char * peer_account
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 unsigned char cel_show_user_def
static ast_mutex_t pgsql_lock
Options provided by main asterisk program.
a user-defined event, the event name field should be set
const char * caller_id_dnid
static void pgsql_log(struct ast_event *event)
const char * channel_name
static int load_module(void)
#define PGSQL_MIN_VERSION_SCHEMA
#define CEL_SHOW_USERDEF_DEFAULT
show_user_def is off by default
#define ASTERISK_GPL_KEY
The text the key() function should return.
#define DEBUG_ATLEAST(level)
Asterisk module definitions.
struct timeval event_time
#define AST_MUTEX_DEFINE_STATIC(mutex)
int ast_cel_fill_record(const struct ast_event *event, struct ast_cel_event_record *r)
Fill in an ast_cel_event_record from a CEL event.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
#define ast_mutex_unlock(a)