cel_pgsql.c 19 KB

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