cel_pgsql.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008
  5. *
  6. * Steve Murphy - adapted to CEL, from:
  7. * Matthew D. Hardeman <mhardemn@papersoft.com>
  8. * Adapted from the MySQL CDR logger originally by James Sharp
  9. *
  10. * Modified April, 2007; Dec, 2008
  11. * Steve Murphy <murf@digium.com>
  12. * Modified September 2003
  13. * Matthew D. Hardeman <mhardemn@papersoft.com>
  14. *
  15. * See http://www.asterisk.org for more information about
  16. * the Asterisk project. Please do not directly contact
  17. * any of the maintainers of this project for assistance;
  18. * the project provides a web site, mailing lists and IRC
  19. * channels for your use.
  20. *
  21. * This program is free software, distributed under the terms of
  22. * the GNU General Public License Version 2. See the LICENSE file
  23. * at the top of the source tree.
  24. */
  25. /*! \file
  26. *
  27. * \brief PostgreSQL CEL logger
  28. *
  29. * \author Steve Murphy <murf@digium.com>
  30. * \extref PostgreSQL http://www.postgresql.org/
  31. *
  32. * See also
  33. * \arg \ref Config_cel
  34. * \extref PostgreSQL http://www.postgresql.org/
  35. * \ingroup cel_drivers
  36. */
  37. /*** MODULEINFO
  38. <depend>pgsql</depend>
  39. <support_level>extended</support_level>
  40. ***/
  41. #include "asterisk.h"
  42. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  43. #include <libpq-fe.h>
  44. #include "asterisk/config.h"
  45. #include "asterisk/options.h"
  46. #include "asterisk/channel.h"
  47. #include "asterisk/cel.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/logger.h"
  50. #include "asterisk.h"
  51. #define DATE_FORMAT "%Y-%m-%d %T.%6q"
  52. static char *config = "cel_pgsql.conf";
  53. static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
  54. static int connected = 0;
  55. static int maxsize = 512, maxsize2 = 512;
  56. /*! \brief show_user_def is off by default */
  57. #define CEL_SHOW_USERDEF_DEFAULT 0
  58. /*! TRUE if we should set the eventtype field to USER_DEFINED on user events. */
  59. static unsigned char cel_show_user_def;
  60. AST_MUTEX_DEFINE_STATIC(pgsql_lock);
  61. static PGconn *conn = NULL;
  62. static PGresult *result = NULL;
  63. static struct ast_event_sub *event_sub = NULL;
  64. struct columns {
  65. char *name;
  66. char *type;
  67. int len;
  68. unsigned int notnull:1;
  69. unsigned int hasdefault:1;
  70. AST_RWLIST_ENTRY(columns) list;
  71. };
  72. static AST_RWLIST_HEAD_STATIC(psql_columns, columns);
  73. #define LENGTHEN_BUF1(size) \
  74. do { \
  75. /* Lengthen buffer, if necessary */ \
  76. if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
  77. if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 3) / 512 + 1) * 512) != 0) { \
  78. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
  79. ast_free(sql); \
  80. ast_free(sql2); \
  81. AST_RWLIST_UNLOCK(&psql_columns); \
  82. return; \
  83. } \
  84. } \
  85. } while (0)
  86. #define LENGTHEN_BUF2(size) \
  87. do { \
  88. if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
  89. if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
  90. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR failed.\n"); \
  91. ast_free(sql); \
  92. ast_free(sql2); \
  93. AST_RWLIST_UNLOCK(&psql_columns); \
  94. return; \
  95. } \
  96. } \
  97. } while (0)
  98. static void pgsql_log(const struct ast_event *event, void *userdata)
  99. {
  100. struct ast_tm tm;
  101. char timestr[128];
  102. char *pgerror;
  103. struct ast_cel_event_record record = {
  104. .version = AST_CEL_EVENT_RECORD_VERSION,
  105. };
  106. if (ast_cel_fill_record(event, &record)) {
  107. return;
  108. }
  109. ast_mutex_lock(&pgsql_lock);
  110. ast_localtime(&record.event_time, &tm, NULL);
  111. ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
  112. if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
  113. conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
  114. if (PQstatus(conn) != CONNECTION_BAD) {
  115. connected = 1;
  116. } else {
  117. pgerror = PQerrorMessage(conn);
  118. ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. Calls will not be logged!\n", pghostname);
  119. ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
  120. PQfinish(conn);
  121. conn = NULL;
  122. }
  123. }
  124. if (connected) {
  125. struct columns *cur;
  126. struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
  127. char buf[257], escapebuf[513];
  128. const char *value;
  129. int first = 1;
  130. if (!sql || !sql2) {
  131. goto ast_log_cleanup;
  132. }
  133. ast_str_set(&sql, 0, "INSERT INTO %s (", table);
  134. ast_str_set(&sql2, 0, " VALUES (");
  135. #define SEP (first ? "" : ",")
  136. AST_RWLIST_RDLOCK(&psql_columns);
  137. AST_RWLIST_TRAVERSE(&psql_columns, cur, list) {
  138. LENGTHEN_BUF1(strlen(cur->name) + 2);
  139. ast_str_append(&sql, 0, "%s\"%s\"", first ? "" : ",", cur->name);
  140. if (strcmp(cur->name, "eventtime") == 0) {
  141. if (strncmp(cur->type, "int", 3) == 0) {
  142. LENGTHEN_BUF2(13);
  143. ast_str_append(&sql2, 0, "%s%ld", SEP, (long) record.event_time.tv_sec);
  144. } else if (strncmp(cur->type, "float", 5) == 0) {
  145. LENGTHEN_BUF2(31);
  146. ast_str_append(&sql2, 0, "%s%f",
  147. SEP,
  148. (double) record.event_time.tv_sec +
  149. (double) record.event_time.tv_usec / 1000000.0);
  150. } else {
  151. /* char, hopefully */
  152. LENGTHEN_BUF2(31);
  153. ast_localtime(&record.event_time, &tm, NULL);
  154. ast_strftime(buf, sizeof(buf), DATE_FORMAT, &tm);
  155. ast_str_append(&sql2, 0, "%s'%s'", SEP, buf);
  156. }
  157. } else if (strcmp(cur->name, "eventtype") == 0) {
  158. if (cur->type[0] == 'i') {
  159. /* Get integer, no need to escape anything */
  160. LENGTHEN_BUF2(5);
  161. ast_str_append(&sql2, 0, "%s%d", SEP, (int) record.event_type);
  162. } else if (strncmp(cur->type, "float", 5) == 0) {
  163. LENGTHEN_BUF2(31);
  164. ast_str_append(&sql2, 0, "%s%f", SEP, (double) record.event_type);
  165. } else {
  166. /* Char field, probably */
  167. const char *event_name;
  168. event_name = (!cel_show_user_def
  169. && record.event_type == AST_CEL_USER_DEFINED)
  170. ? record.user_defined_name : record.event_name;
  171. LENGTHEN_BUF2(strlen(event_name) + 1);
  172. ast_str_append(&sql2, 0, "%s'%s'", SEP, event_name);
  173. }
  174. } else if (strcmp(cur->name, "amaflags") == 0) {
  175. if (strncmp(cur->type, "int", 3) == 0) {
  176. /* Integer, no need to escape anything */
  177. LENGTHEN_BUF2(13);
  178. ast_str_append(&sql2, 0, "%s%u", SEP, record.amaflag);
  179. } else {
  180. /* Although this is a char field, there are no special characters in the values for these fields */
  181. LENGTHEN_BUF2(31);
  182. ast_str_append(&sql2, 0, "%s'%u'", SEP, record.amaflag);
  183. }
  184. } else {
  185. /* Arbitrary field, could be anything */
  186. if (strcmp(cur->name, "userdeftype") == 0) {
  187. value = record.user_defined_name;
  188. } else if (strcmp(cur->name, "cid_name") == 0) {
  189. value = record.caller_id_name;
  190. } else if (strcmp(cur->name, "cid_num") == 0) {
  191. value = record.caller_id_num;
  192. } else if (strcmp(cur->name, "cid_ani") == 0) {
  193. value = record.caller_id_ani;
  194. } else if (strcmp(cur->name, "cid_rdnis") == 0) {
  195. value = record.caller_id_rdnis;
  196. } else if (strcmp(cur->name, "cid_dnid") == 0) {
  197. value = record.caller_id_dnid;
  198. } else if (strcmp(cur->name, "exten") == 0) {
  199. value = record.extension;
  200. } else if (strcmp(cur->name, "context") == 0) {
  201. value = record.context;
  202. } else if (strcmp(cur->name, "channame") == 0) {
  203. value = record.channel_name;
  204. } else if (strcmp(cur->name, "appname") == 0) {
  205. value = record.application_name;
  206. } else if (strcmp(cur->name, "appdata") == 0) {
  207. value = record.application_data;
  208. } else if (strcmp(cur->name, "accountcode") == 0) {
  209. value = record.account_code;
  210. } else if (strcmp(cur->name, "peeraccount") == 0) {
  211. value = record.peer_account;
  212. } else if (strcmp(cur->name, "uniqueid") == 0) {
  213. value = record.unique_id;
  214. } else if (strcmp(cur->name, "linkedid") == 0) {
  215. value = record.linked_id;
  216. } else if (strcmp(cur->name, "userfield") == 0) {
  217. value = record.user_field;
  218. } else if (strcmp(cur->name, "peer") == 0) {
  219. value = record.peer;
  220. } else if (strcmp(cur->name, "extra") == 0) {
  221. value = record.extra;
  222. } else {
  223. value = NULL;
  224. }
  225. if (value == NULL) {
  226. ast_str_append(&sql2, 0, "%sDEFAULT", SEP);
  227. } else if (strncmp(cur->type, "int", 3) == 0) {
  228. long long whatever;
  229. if (value && sscanf(value, "%30lld", &whatever) == 1) {
  230. LENGTHEN_BUF2(26);
  231. ast_str_append(&sql2, 0, "%s%lld", SEP, whatever);
  232. } else {
  233. LENGTHEN_BUF2(2);
  234. ast_str_append(&sql2, 0, "%s0", SEP);
  235. }
  236. } else if (strncmp(cur->type, "float", 5) == 0) {
  237. long double whatever;
  238. if (value && sscanf(value, "%30Lf", &whatever) == 1) {
  239. LENGTHEN_BUF2(51);
  240. ast_str_append(&sql2, 0, "%s%30Lf", SEP, whatever);
  241. } else {
  242. LENGTHEN_BUF2(2);
  243. ast_str_append(&sql2, 0, "%s0", SEP);
  244. }
  245. /* XXX Might want to handle dates, times, and other misc fields here XXX */
  246. } else {
  247. if (value) {
  248. PQescapeStringConn(conn, escapebuf, value, strlen(value), NULL);
  249. } else {
  250. escapebuf[0] = '\0';
  251. }
  252. LENGTHEN_BUF2(strlen(escapebuf) + 3);
  253. ast_str_append(&sql2, 0, "%s'%s'", SEP, escapebuf);
  254. }
  255. }
  256. first = 0;
  257. }
  258. AST_RWLIST_UNLOCK(&psql_columns);
  259. LENGTHEN_BUF1(ast_str_strlen(sql2) + 2);
  260. ast_str_append(&sql, 0, ")%s)", ast_str_buffer(sql2));
  261. ast_verb(11, "[%s]\n", ast_str_buffer(sql));
  262. ast_debug(2, "inserting a CEL record.\n");
  263. /* Test to be sure we're still connected... */
  264. /* If we're connected, and connection is working, good. */
  265. /* Otherwise, attempt reconnect. If it fails... sorry... */
  266. if (PQstatus(conn) == CONNECTION_OK) {
  267. connected = 1;
  268. } else {
  269. ast_log(LOG_WARNING, "Connection was lost... attempting to reconnect.\n");
  270. PQreset(conn);
  271. if (PQstatus(conn) == CONNECTION_OK) {
  272. ast_log(LOG_NOTICE, "Connection reestablished.\n");
  273. connected = 1;
  274. } else {
  275. pgerror = PQerrorMessage(conn);
  276. ast_log(LOG_ERROR, "Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
  277. ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
  278. PQfinish(conn);
  279. conn = NULL;
  280. connected = 0;
  281. goto ast_log_cleanup;
  282. }
  283. }
  284. result = PQexec(conn, ast_str_buffer(sql));
  285. if (PQresultStatus(result) != PGRES_COMMAND_OK) {
  286. pgerror = PQresultErrorMessage(result);
  287. ast_log(LOG_WARNING, "Failed to insert call detail record into database!\n");
  288. ast_log(LOG_WARNING, "Reason: %s\n", pgerror);
  289. ast_log(LOG_WARNING, "Connection may have been lost... attempting to reconnect.\n");
  290. PQreset(conn);
  291. if (PQstatus(conn) == CONNECTION_OK) {
  292. ast_log(LOG_NOTICE, "Connection reestablished.\n");
  293. connected = 1;
  294. PQclear(result);
  295. result = PQexec(conn, ast_str_buffer(sql));
  296. if (PQresultStatus(result) != PGRES_COMMAND_OK) {
  297. pgerror = PQresultErrorMessage(result);
  298. ast_log(LOG_ERROR, "HARD ERROR! Attempted reconnection failed. DROPPING CALL RECORD!\n");
  299. ast_log(LOG_ERROR, "Reason: %s\n", pgerror);
  300. }
  301. }
  302. PQclear(result);
  303. goto ast_log_cleanup;
  304. }
  305. PQclear(result);
  306. ast_log_cleanup:
  307. ast_free(sql);
  308. ast_free(sql2);
  309. }
  310. ast_mutex_unlock(&pgsql_lock);
  311. }
  312. static int my_unload_module(void)
  313. {
  314. struct columns *current;
  315. if (event_sub) {
  316. event_sub = ast_event_unsubscribe(event_sub);
  317. }
  318. AST_RWLIST_WRLOCK(&psql_columns);
  319. if (conn) {
  320. PQfinish(conn);
  321. conn = NULL;
  322. }
  323. if (pghostname) {
  324. ast_free(pghostname);
  325. pghostname = NULL;
  326. }
  327. if (pgdbname) {
  328. ast_free(pgdbname);
  329. pgdbname = NULL;
  330. }
  331. if (pgdbuser) {
  332. ast_free(pgdbuser);
  333. pgdbuser = NULL;
  334. }
  335. if (pgpassword) {
  336. ast_free(pgpassword);
  337. pgpassword = NULL;
  338. }
  339. if (pgdbport) {
  340. ast_free(pgdbport);
  341. pgdbport = NULL;
  342. }
  343. if (table) {
  344. ast_free(table);
  345. table = NULL;
  346. }
  347. while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
  348. ast_free(current);
  349. }
  350. AST_RWLIST_UNLOCK(&psql_columns);
  351. return 0;
  352. }
  353. static int unload_module(void)
  354. {
  355. return my_unload_module();
  356. }
  357. static int process_my_load_module(struct ast_config *cfg)
  358. {
  359. struct ast_variable *var;
  360. char *pgerror;
  361. const char *tmp;
  362. PGresult *result;
  363. struct columns *cur;
  364. if (!(var = ast_variable_browse(cfg, "global"))) {
  365. ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
  366. return AST_MODULE_LOAD_DECLINE;
  367. }
  368. if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
  369. ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
  370. tmp = ""; /* connect via UNIX-socket by default */
  371. }
  372. if (pghostname)
  373. ast_free(pghostname);
  374. if (!(pghostname = ast_strdup(tmp))) {
  375. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
  376. return AST_MODULE_LOAD_DECLINE;
  377. }
  378. if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
  379. ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
  380. tmp = "asteriskceldb";
  381. }
  382. if (pgdbname)
  383. ast_free(pgdbname);
  384. if (!(pgdbname = ast_strdup(tmp))) {
  385. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
  386. return AST_MODULE_LOAD_DECLINE;
  387. }
  388. if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
  389. ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
  390. tmp = "asterisk";
  391. }
  392. if (pgdbuser)
  393. ast_free(pgdbuser);
  394. if (!(pgdbuser = ast_strdup(tmp))) {
  395. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
  396. return AST_MODULE_LOAD_DECLINE;
  397. }
  398. if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
  399. ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
  400. tmp = "";
  401. }
  402. if (pgpassword)
  403. ast_free(pgpassword);
  404. if (!(pgpassword = ast_strdup(tmp))) {
  405. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
  406. return AST_MODULE_LOAD_DECLINE;
  407. }
  408. if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
  409. ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
  410. tmp = "5432";
  411. }
  412. if (pgdbport)
  413. ast_free(pgdbport);
  414. if (!(pgdbport = ast_strdup(tmp))) {
  415. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
  416. return AST_MODULE_LOAD_DECLINE;
  417. }
  418. if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
  419. ast_log(LOG_WARNING,"CEL table not specified. Assuming cel\n");
  420. tmp = "cel";
  421. }
  422. if (table)
  423. ast_free(table);
  424. if (!(table = ast_strdup(tmp))) {
  425. return AST_MODULE_LOAD_DECLINE;
  426. }
  427. cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
  428. if ((tmp = ast_variable_retrieve(cfg, "global", "show_user_defined"))) {
  429. cel_show_user_def = ast_true(tmp) ? 1 : 0;
  430. }
  431. if (option_debug) {
  432. if (ast_strlen_zero(pghostname)) {
  433. ast_debug(3, "cel_pgsql: using default unix socket\n");
  434. } else {
  435. ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
  436. }
  437. ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
  438. ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
  439. ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
  440. ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
  441. ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
  442. ast_debug(3, "cel_pgsql: got show_user_defined of %s\n",
  443. cel_show_user_def ? "Yes" : "No");
  444. }
  445. conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
  446. if (PQstatus(conn) != CONNECTION_BAD) {
  447. char sqlcmd[512];
  448. char *fname, *ftype, *flen, *fnotnull, *fdef;
  449. char *tableptr;
  450. int i, rows;
  451. ast_debug(1, "Successfully connected to PostgreSQL database.\n");
  452. connected = 1;
  453. /* Remove any schema name from the table */
  454. if ((tableptr = strrchr(table, '.'))) {
  455. tableptr++;
  456. } else {
  457. tableptr = table;
  458. }
  459. /* Query the columns */
  460. snprintf(sqlcmd, sizeof(sqlcmd), "select a.attname, t.typname, a.attlen, a.attnotnull, d.adsrc 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", tableptr);
  461. result = PQexec(conn, sqlcmd);
  462. if (PQresultStatus(result) != PGRES_TUPLES_OK) {
  463. pgerror = PQresultErrorMessage(result);
  464. ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
  465. PQclear(result);
  466. unload_module();
  467. return AST_MODULE_LOAD_DECLINE;
  468. }
  469. rows = PQntuples(result);
  470. for (i = 0; i < rows; i++) {
  471. fname = PQgetvalue(result, i, 0);
  472. ftype = PQgetvalue(result, i, 1);
  473. flen = PQgetvalue(result, i, 2);
  474. fnotnull = PQgetvalue(result, i, 3);
  475. fdef = PQgetvalue(result, i, 4);
  476. ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
  477. cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
  478. if (cur) {
  479. sscanf(flen, "%30d", &cur->len);
  480. cur->name = (char *)cur + sizeof(*cur);
  481. cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
  482. strcpy(cur->name, fname);
  483. strcpy(cur->type, ftype);
  484. if (*fnotnull == 't') {
  485. cur->notnull = 1;
  486. } else {
  487. cur->notnull = 0;
  488. }
  489. if (!ast_strlen_zero(fdef)) {
  490. cur->hasdefault = 1;
  491. } else {
  492. cur->hasdefault = 0;
  493. }
  494. AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
  495. }
  496. }
  497. PQclear(result);
  498. } else {
  499. pgerror = PQerrorMessage(conn);
  500. ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
  501. ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
  502. connected = 0;
  503. }
  504. return AST_MODULE_LOAD_SUCCESS;
  505. }
  506. static int my_load_module(int reload)
  507. {
  508. struct ast_config *cfg;
  509. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  510. if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
  511. ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
  512. return AST_MODULE_LOAD_DECLINE;
  513. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  514. return AST_MODULE_LOAD_SUCCESS;
  515. }
  516. if (reload) {
  517. my_unload_module();
  518. }
  519. process_my_load_module(cfg);
  520. ast_config_destroy(cfg);
  521. event_sub = ast_event_subscribe(AST_EVENT_CEL, pgsql_log, "CEL PGSQL backend", NULL, AST_EVENT_IE_END);
  522. if (!event_sub) {
  523. ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
  524. return AST_MODULE_LOAD_DECLINE;
  525. }
  526. return AST_MODULE_LOAD_SUCCESS;
  527. }
  528. static int load_module(void)
  529. {
  530. return my_load_module(0);
  531. }
  532. static int reload(void)
  533. {
  534. return my_load_module(1);
  535. }
  536. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
  537. .load = load_module,
  538. .unload = unload_module,
  539. .reload = reload,
  540. .load_pri = AST_MODPRI_CDR_DRIVER,
  541. );