cel_pgsql.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. * PostgreSQL http://www.postgresql.org/
  31. *
  32. * See also
  33. * \arg \ref Config_cel
  34. * 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. #define PGSQL_BACKEND_NAME "CEL PGSQL backend"
  53. static char *config = "cel_pgsql.conf";
  54. static char *pghostname = NULL, *pgdbname = NULL, *pgdbuser = NULL, *pgpassword = NULL, *pgdbport = NULL, *table = NULL;
  55. static int connected = 0;
  56. static int maxsize = 512, maxsize2 = 512;
  57. /*! \brief show_user_def is off by default */
  58. #define CEL_SHOW_USERDEF_DEFAULT 0
  59. /*! TRUE if we should set the eventtype field to USER_DEFINED on user events. */
  60. static unsigned char cel_show_user_def;
  61. AST_MUTEX_DEFINE_STATIC(pgsql_lock);
  62. static PGconn *conn = NULL;
  63. static PGresult *result = 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(struct ast_event *event)
  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. ast_cel_backend_unregister(PGSQL_BACKEND_NAME);
  316. AST_RWLIST_WRLOCK(&psql_columns);
  317. if (conn) {
  318. PQfinish(conn);
  319. conn = NULL;
  320. }
  321. if (pghostname) {
  322. ast_free(pghostname);
  323. pghostname = NULL;
  324. }
  325. if (pgdbname) {
  326. ast_free(pgdbname);
  327. pgdbname = NULL;
  328. }
  329. if (pgdbuser) {
  330. ast_free(pgdbuser);
  331. pgdbuser = NULL;
  332. }
  333. if (pgpassword) {
  334. ast_free(pgpassword);
  335. pgpassword = NULL;
  336. }
  337. if (pgdbport) {
  338. ast_free(pgdbport);
  339. pgdbport = NULL;
  340. }
  341. if (table) {
  342. ast_free(table);
  343. table = NULL;
  344. }
  345. while ((current = AST_RWLIST_REMOVE_HEAD(&psql_columns, list))) {
  346. ast_free(current);
  347. }
  348. AST_RWLIST_UNLOCK(&psql_columns);
  349. return 0;
  350. }
  351. static int unload_module(void)
  352. {
  353. return my_unload_module();
  354. }
  355. static int process_my_load_module(struct ast_config *cfg)
  356. {
  357. struct ast_variable *var;
  358. char *pgerror;
  359. const char *tmp;
  360. PGresult *result;
  361. struct columns *cur;
  362. if (!(var = ast_variable_browse(cfg, "global"))) {
  363. ast_log(LOG_WARNING,"CEL pgsql config file missing global section.\n");
  364. return AST_MODULE_LOAD_DECLINE;
  365. }
  366. if (!(tmp = ast_variable_retrieve(cfg,"global","hostname"))) {
  367. ast_log(LOG_WARNING,"PostgreSQL server hostname not specified. Assuming unix socket connection\n");
  368. tmp = ""; /* connect via UNIX-socket by default */
  369. }
  370. if (pghostname)
  371. ast_free(pghostname);
  372. if (!(pghostname = ast_strdup(tmp))) {
  373. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying host info\n");
  374. return AST_MODULE_LOAD_DECLINE;
  375. }
  376. if (!(tmp = ast_variable_retrieve(cfg, "global", "dbname"))) {
  377. ast_log(LOG_WARNING,"PostgreSQL database not specified. Assuming asterisk\n");
  378. tmp = "asteriskceldb";
  379. }
  380. if (pgdbname)
  381. ast_free(pgdbname);
  382. if (!(pgdbname = ast_strdup(tmp))) {
  383. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying dbname info\n");
  384. return AST_MODULE_LOAD_DECLINE;
  385. }
  386. if (!(tmp = ast_variable_retrieve(cfg, "global", "user"))) {
  387. ast_log(LOG_WARNING,"PostgreSQL database user not specified. Assuming asterisk\n");
  388. tmp = "asterisk";
  389. }
  390. if (pgdbuser)
  391. ast_free(pgdbuser);
  392. if (!(pgdbuser = ast_strdup(tmp))) {
  393. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying user info\n");
  394. return AST_MODULE_LOAD_DECLINE;
  395. }
  396. if (!(tmp = ast_variable_retrieve(cfg, "global", "password"))) {
  397. ast_log(LOG_WARNING, "PostgreSQL database password not specified. Assuming blank\n");
  398. tmp = "";
  399. }
  400. if (pgpassword)
  401. ast_free(pgpassword);
  402. if (!(pgpassword = ast_strdup(tmp))) {
  403. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying password info\n");
  404. return AST_MODULE_LOAD_DECLINE;
  405. }
  406. if (!(tmp = ast_variable_retrieve(cfg,"global","port"))) {
  407. ast_log(LOG_WARNING,"PostgreSQL database port not specified. Using default 5432.\n");
  408. tmp = "5432";
  409. }
  410. if (pgdbport)
  411. ast_free(pgdbport);
  412. if (!(pgdbport = ast_strdup(tmp))) {
  413. ast_log(LOG_WARNING,"PostgreSQL Ran out of memory copying port info\n");
  414. return AST_MODULE_LOAD_DECLINE;
  415. }
  416. if (!(tmp = ast_variable_retrieve(cfg, "global", "table"))) {
  417. ast_log(LOG_WARNING,"CEL table not specified. Assuming cel\n");
  418. tmp = "cel";
  419. }
  420. if (table)
  421. ast_free(table);
  422. if (!(table = ast_strdup(tmp))) {
  423. return AST_MODULE_LOAD_DECLINE;
  424. }
  425. cel_show_user_def = CEL_SHOW_USERDEF_DEFAULT;
  426. if ((tmp = ast_variable_retrieve(cfg, "global", "show_user_defined"))) {
  427. cel_show_user_def = ast_true(tmp) ? 1 : 0;
  428. }
  429. if (option_debug) {
  430. if (ast_strlen_zero(pghostname)) {
  431. ast_debug(3, "cel_pgsql: using default unix socket\n");
  432. } else {
  433. ast_debug(3, "cel_pgsql: got hostname of %s\n", pghostname);
  434. }
  435. ast_debug(3, "cel_pgsql: got port of %s\n", pgdbport);
  436. ast_debug(3, "cel_pgsql: got user of %s\n", pgdbuser);
  437. ast_debug(3, "cel_pgsql: got dbname of %s\n", pgdbname);
  438. ast_debug(3, "cel_pgsql: got password of %s\n", pgpassword);
  439. ast_debug(3, "cel_pgsql: got sql table name of %s\n", table);
  440. ast_debug(3, "cel_pgsql: got show_user_defined of %s\n",
  441. cel_show_user_def ? "Yes" : "No");
  442. }
  443. conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
  444. if (PQstatus(conn) != CONNECTION_BAD) {
  445. char sqlcmd[512];
  446. char *fname, *ftype, *flen, *fnotnull, *fdef;
  447. char *tableptr;
  448. int i, rows;
  449. ast_debug(1, "Successfully connected to PostgreSQL database.\n");
  450. connected = 1;
  451. /* Remove any schema name from the table */
  452. if ((tableptr = strrchr(table, '.'))) {
  453. tableptr++;
  454. } else {
  455. tableptr = table;
  456. }
  457. /* Query the columns */
  458. 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);
  459. result = PQexec(conn, sqlcmd);
  460. if (PQresultStatus(result) != PGRES_TUPLES_OK) {
  461. pgerror = PQresultErrorMessage(result);
  462. ast_log(LOG_ERROR, "Failed to query database columns: %s\n", pgerror);
  463. PQclear(result);
  464. unload_module();
  465. return AST_MODULE_LOAD_DECLINE;
  466. }
  467. rows = PQntuples(result);
  468. for (i = 0; i < rows; i++) {
  469. fname = PQgetvalue(result, i, 0);
  470. ftype = PQgetvalue(result, i, 1);
  471. flen = PQgetvalue(result, i, 2);
  472. fnotnull = PQgetvalue(result, i, 3);
  473. fdef = PQgetvalue(result, i, 4);
  474. ast_verb(4, "Found column '%s' of type '%s'\n", fname, ftype);
  475. cur = ast_calloc(1, sizeof(*cur) + strlen(fname) + strlen(ftype) + 2);
  476. if (cur) {
  477. sscanf(flen, "%30d", &cur->len);
  478. cur->name = (char *)cur + sizeof(*cur);
  479. cur->type = (char *)cur + sizeof(*cur) + strlen(fname) + 1;
  480. strcpy(cur->name, fname);
  481. strcpy(cur->type, ftype);
  482. if (*fnotnull == 't') {
  483. cur->notnull = 1;
  484. } else {
  485. cur->notnull = 0;
  486. }
  487. if (!ast_strlen_zero(fdef)) {
  488. cur->hasdefault = 1;
  489. } else {
  490. cur->hasdefault = 0;
  491. }
  492. AST_RWLIST_INSERT_TAIL(&psql_columns, cur, list);
  493. }
  494. }
  495. PQclear(result);
  496. } else {
  497. pgerror = PQerrorMessage(conn);
  498. ast_log(LOG_ERROR, "cel_pgsql: Unable to connect to database server %s. CALLS WILL NOT BE LOGGED!!\n", pghostname);
  499. ast_log(LOG_ERROR, "cel_pgsql: Reason: %s\n", pgerror);
  500. connected = 0;
  501. }
  502. return AST_MODULE_LOAD_SUCCESS;
  503. }
  504. static int my_load_module(int reload)
  505. {
  506. struct ast_config *cfg;
  507. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  508. if ((cfg = ast_config_load(config, config_flags)) == NULL || cfg == CONFIG_STATUS_FILEINVALID) {
  509. ast_log(LOG_WARNING, "Unable to load config for PostgreSQL CEL's: %s\n", config);
  510. return AST_MODULE_LOAD_DECLINE;
  511. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  512. return AST_MODULE_LOAD_SUCCESS;
  513. }
  514. if (reload) {
  515. my_unload_module();
  516. }
  517. process_my_load_module(cfg);
  518. ast_config_destroy(cfg);
  519. if (ast_cel_backend_register(PGSQL_BACKEND_NAME, pgsql_log)) {
  520. ast_log(LOG_WARNING, "Unable to subscribe to CEL events for pgsql\n");
  521. return AST_MODULE_LOAD_DECLINE;
  522. }
  523. return AST_MODULE_LOAD_SUCCESS;
  524. }
  525. static int load_module(void)
  526. {
  527. return my_load_module(0);
  528. }
  529. static int reload(void)
  530. {
  531. return my_load_module(1);
  532. }
  533. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PostgreSQL CEL Backend",
  534. .load = load_module,
  535. .unload = unload_module,
  536. .reload = reload,
  537. .load_pri = AST_MODPRI_CDR_DRIVER,
  538. );