cdr_sqlite.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2005, Holger Schurig
  5. *
  6. *
  7. * Ideas taken from other cdr_*.c files
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Store CDR records in a SQLite database.
  22. *
  23. * \author Holger Schurig <hs4233@mail.mn-solutions.de>
  24. *
  25. * See also
  26. * \arg \ref Config_cdr
  27. * \arg http://www.sqlite.org/
  28. *
  29. * Creates the database and table on-the-fly
  30. * \ingroup cdr_drivers
  31. */
  32. #include <sys/types.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <sqlite.h>
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/channel.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/logger.h"
  42. #include "asterisk/utils.h"
  43. #define LOG_UNIQUEID 0
  44. #define LOG_USERFIELD 0
  45. /* When you change the DATE_FORMAT, be sure to change the CHAR(19) below to something else */
  46. #define DATE_FORMAT "%Y-%m-%d %T"
  47. static char *desc = "SQLite CDR Backend";
  48. static char *name = "sqlite";
  49. static sqlite* db = NULL;
  50. AST_MUTEX_DEFINE_STATIC(sqlite_lock);
  51. /*! \brief SQL table format */
  52. static char sql_create_table[] = "CREATE TABLE cdr ("
  53. " AcctId INTEGER PRIMARY KEY,"
  54. " clid VARCHAR(80),"
  55. " src VARCHAR(80),"
  56. " dst VARCHAR(80),"
  57. " dcontext VARCHAR(80),"
  58. " channel VARCHAR(80),"
  59. " dstchannel VARCHAR(80),"
  60. " lastapp VARCHAR(80),"
  61. " lastdata VARCHAR(80),"
  62. " start CHAR(19),"
  63. " answer CHAR(19),"
  64. " end CHAR(19),"
  65. " duration INTEGER,"
  66. " billsec INTEGER,"
  67. " disposition INTEGER,"
  68. " amaflags INTEGER,"
  69. " accountcode VARCHAR(20)"
  70. #if LOG_UNIQUEID
  71. " ,uniqueid VARCHAR(32)"
  72. #endif
  73. #if LOG_USERFIELD
  74. " ,userfield VARCHAR(255)"
  75. #endif
  76. ");";
  77. static int sqlite_log(struct ast_cdr *cdr)
  78. {
  79. int res = 0;
  80. char *zErr = 0;
  81. struct tm tm;
  82. time_t t;
  83. char startstr[80], answerstr[80], endstr[80];
  84. int count;
  85. ast_mutex_lock(&sqlite_lock);
  86. t = cdr->start.tv_sec;
  87. localtime_r(&t, &tm);
  88. strftime(startstr, sizeof(startstr), DATE_FORMAT, &tm);
  89. t = cdr->answer.tv_sec;
  90. localtime_r(&t, &tm);
  91. strftime(answerstr, sizeof(answerstr), DATE_FORMAT, &tm);
  92. t = cdr->end.tv_sec;
  93. localtime_r(&t, &tm);
  94. strftime(endstr, sizeof(endstr), DATE_FORMAT, &tm);
  95. for(count=0; count<5; count++) {
  96. res = sqlite_exec_printf(db,
  97. "INSERT INTO cdr ("
  98. "clid,src,dst,dcontext,"
  99. "channel,dstchannel,lastapp,lastdata, "
  100. "start,answer,end,"
  101. "duration,billsec,disposition,amaflags, "
  102. "accountcode"
  103. # if LOG_UNIQUEID
  104. ",uniqueid"
  105. # endif
  106. # if LOG_USERFIELD
  107. ",userfield"
  108. # endif
  109. ") VALUES ("
  110. "'%q', '%q', '%q', '%q', "
  111. "'%q', '%q', '%q', '%q', "
  112. "'%q', '%q', '%q', "
  113. "%d, %d, %d, %d, "
  114. "'%q'"
  115. # if LOG_UNIQUEID
  116. ",'%q'"
  117. # endif
  118. # if LOG_USERFIELD
  119. ",'%q'"
  120. # endif
  121. ")", NULL, NULL, &zErr,
  122. cdr->clid, cdr->src, cdr->dst, cdr->dcontext,
  123. cdr->channel, cdr->dstchannel, cdr->lastapp, cdr->lastdata,
  124. startstr, answerstr, endstr,
  125. cdr->duration, cdr->billsec, cdr->disposition, cdr->amaflags,
  126. cdr->accountcode
  127. # if LOG_UNIQUEID
  128. ,cdr->uniqueid
  129. # endif
  130. # if LOG_USERFIELD
  131. ,cdr->userfield
  132. # endif
  133. );
  134. if (res != SQLITE_BUSY && res != SQLITE_LOCKED)
  135. break;
  136. usleep(200);
  137. }
  138. if (zErr) {
  139. ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
  140. free(zErr);
  141. }
  142. ast_mutex_unlock(&sqlite_lock);
  143. return res;
  144. }
  145. char *description(void)
  146. {
  147. return desc;
  148. }
  149. int unload_module(void)
  150. {
  151. if (db)
  152. sqlite_close(db);
  153. ast_cdr_unregister(name);
  154. return 0;
  155. }
  156. int load_module(void)
  157. {
  158. char *zErr;
  159. char fn[PATH_MAX];
  160. int res;
  161. /* is the database there? */
  162. snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
  163. db = sqlite_open(fn, 0660, &zErr);
  164. if (!db) {
  165. ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
  166. free(zErr);
  167. return -1;
  168. }
  169. /* is the table there? */
  170. res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
  171. if (res) {
  172. res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
  173. if (res) {
  174. ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
  175. free(zErr);
  176. goto err;
  177. }
  178. /* TODO: here we should probably create an index */
  179. }
  180. res = ast_cdr_register(name, desc, sqlite_log);
  181. if (res) {
  182. ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
  183. return -1;
  184. }
  185. return 0;
  186. err:
  187. if (db)
  188. sqlite_close(db);
  189. return -1;
  190. }
  191. int reload(void)
  192. {
  193. return 0;
  194. }
  195. int usecount(void)
  196. {
  197. return 0;
  198. }
  199. char *key()
  200. {
  201. return ASTERISK_GPL_KEY;
  202. }