cel_pgsql.c 19 KB

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