db_berkeley_admin.xml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?xml version="1.0" encoding='ISO-8859-1'?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <chapter>
  9. <title>&adminguide;</title>
  10. <section>
  11. <title>Overview</title>
  12. <para>
  13. This is a module which integrates the Berkeley DB into Kamailio.
  14. It implements the DB API defined in Kamailio.
  15. </para>
  16. </section>
  17. <section>
  18. <title>Dependencies</title>
  19. <section>
  20. <title>&kamailio; Modules</title>
  21. <para>
  22. The following modules must be loaded before this module:
  23. <itemizedlist>
  24. <listitem>
  25. <para>
  26. <emphasis>No dependencies on other &kamailio; modules</emphasis>.
  27. </para>
  28. </listitem>
  29. </itemizedlist>
  30. </para>
  31. </section>
  32. <section>
  33. <title>External Libraries or Applications</title>
  34. <para>
  35. The following libraries or applications must be installed before running
  36. &kamailio; with this module loaded:
  37. <itemizedlist>
  38. <listitem>
  39. <para>
  40. <emphasis>Berkeley Berkeley DB 4.6</emphasis> - an embedded database.
  41. </para>
  42. </listitem>
  43. </itemizedlist>
  44. </para>
  45. </section>
  46. </section>
  47. <section>
  48. <title>Exported Parameters</title>
  49. <section>
  50. <title><varname>auto_reload</varname> (integer)</title>
  51. <para>
  52. The auto-reload will close and reopen a Berkeley DB when the
  53. files inode has changed. The operation occurs only duing a query.
  54. Other operations such as insert or delete, do not invoke auto_reload.
  55. </para>
  56. <para>
  57. <emphasis>
  58. Default value is 0 (1 - on / 0 - off).
  59. </emphasis>
  60. </para>
  61. <example>
  62. <title>Set <varname>auto_reload</varname> parameter</title>
  63. <programlisting format="linespecific">
  64. ...
  65. modparam("db_berkeley", "auto_reload", 1)
  66. ...
  67. </programlisting>
  68. </example>
  69. </section>
  70. <section>
  71. <title><varname>log_enable</varname> (integer)</title>
  72. <para>
  73. The log_enable boolean controls when to create journal files.
  74. The following operations can be journaled:
  75. INSERT, UPDATE, DELETE. Other operations such as SELECT, do not.
  76. This journaling are required if you need to recover from a corrupt
  77. DB file. That is, kambdb_recover requires these to rebuild
  78. the db file. If you find this log feature useful, you may
  79. also be interested in the METADATA_LOGFLAGS bitfield that each
  80. table has. It will allow you to control which operations to
  81. journal, and the destination (like syslog, stdout, local-file).
  82. Refer to bdblib_log() and documentation on METADATA.
  83. </para>
  84. <para>
  85. <emphasis>
  86. Default value is 0 (1 - on / 0 - off).
  87. </emphasis>
  88. </para>
  89. <example>
  90. <title>Set <varname>log_enable</varname> parameter</title>
  91. <programlisting format="linespecific">
  92. ...
  93. modparam("db_berkeley", "log_enable", 1)
  94. ...
  95. </programlisting>
  96. </example>
  97. </section>
  98. <section>
  99. <title><varname>journal_roll_interval</varname> (integer seconds)</title>
  100. <para>
  101. The journal_roll_interval will close and open a new log file.
  102. The roll operation occurs only at the end of writing a log,
  103. so it is not guaranteed to to roll 'on time'.
  104. </para>
  105. <para>
  106. <emphasis>
  107. Default value is 0 (off).
  108. </emphasis>
  109. </para>
  110. <example>
  111. <title>Set <varname>journal_roll_interval</varname> parameter</title>
  112. <programlisting format="linespecific">
  113. ...
  114. modparam("db_berkeley", "journal_roll_interval", 3600)
  115. ...
  116. </programlisting>
  117. </example>
  118. </section>
  119. </section>
  120. <section>
  121. <title>Exported Functions</title>
  122. <para>
  123. No function exported to be used from configuration file.
  124. </para>
  125. </section>
  126. <section>
  127. <title>Exported MI Functions</title>
  128. <section>
  129. <title><function moreinfo="none">bdb_reload</function></title>
  130. <para>
  131. Causes db_berkeley module to re-read the contents of specified table (or dbenv).
  132. The db_berkeley DB actually loads each table on demand, as opposed to loading all
  133. at mod_init time. The bdb_reload operation is implemented as a close followed by a reopen.
  134. Note- bdb_reload will fail if a table has not been accessed before (because the close
  135. will fail).
  136. </para>
  137. <para>
  138. Name: <emphasis>bdb_reload</emphasis>
  139. </para>
  140. <para>Parameters: <emphasis>tablename (or db_path); to reload a particular table
  141. provide the tablename as the arguement (eg subscriber); to reload all tables provide the db_path to
  142. the db files. The path can be found in &ctltool;rc DB_PATH variable.
  143. </emphasis></para>
  144. </section>
  145. </section>
  146. <section>
  147. <title>Installation and Running</title>
  148. <para>
  149. First download, compile and install the Berkeley DB. This is
  150. outside the scope of this document. Documentation for this
  151. procedure is available on the Internet.
  152. </para>
  153. <para>
  154. Next, prepare to compile Kamailio with the db_berkeley module.
  155. In the directory /modules/db_berkeley, modify the Makefile to point
  156. to your distribution of Berkeley DB. You may also define 'BDB_EXTRA_DEBUG'
  157. to compile in extra debug logs. However, it is not a recommended
  158. deployment to production servers.
  159. </para>
  160. <para>
  161. Because the module dependes on an external library, the db_berkeley module is not
  162. compiled and installed by default. You can use one of the next options.
  163. </para>
  164. <itemizedlist>
  165. <listitem>
  166. <para>
  167. edit the "Makefile" and remove "db_berkeley" from "excluded_modules"
  168. list. Then follow the standard procedure to install &kamailio;:
  169. "make all; make install".
  170. </para>
  171. </listitem>
  172. <listitem>
  173. <para>
  174. from command line use: 'make all include_modules="db_berkeley";
  175. make install include_modules="db_berkeley"'.
  176. </para>
  177. </listitem>
  178. </itemizedlist>
  179. <para>
  180. Installation of Kamailio is performed by simply running make install
  181. as root user of the main directory. This will install the binaries
  182. in /usr/local/sbin/.
  183. If this was successful, openser control engine files should now
  184. be installed as /usr/local/sbin/kamdbctl.
  185. </para>
  186. <para>
  187. Decide where (on the filesystem) you want to install the Berkeley DB files.
  188. For instance, '/usr/local/etc/kamailio/db_berkeley' directory.
  189. Make note of this directory as we need to add this path to the &ctltool;rc file.
  190. Note: Kamailio will not startup without these DB files.
  191. </para>
  192. <para>
  193. Edit &ctltool;rc - There are two parameters in this file that should be
  194. configured before openserctrdb script can work properly: DBENGINE and DB_PATH.
  195. Edit file: '/usr/local/etc/kamailio/&ctltool;rc'
  196. </para>
  197. <programlisting format="linespecific">
  198. ## database type: MYSQL, PGSQL, DB_BERKELEY, or DBTEXT, by default none is loaded
  199. # DBENGINE=DB_BERKELEY
  200. ## database path used by dbtext or db_berkeley
  201. # DB_PATH="/usr/local/etc/kamailio/db_berkeley"
  202. </programlisting>
  203. <para>
  204. (Optional) Pre creation step- Customize your meta-data.
  205. The DB files are initially seeded with necessary meta-data.
  206. This is a good time to review the meta-data section details,
  207. before making modifications to your tables dbschema.
  208. By default, the files are installed in '/usr/local/share/kamailio/db_berkeley/openser'
  209. By default these tables are created Read/Write and without any journalling as
  210. shown. These settings can be modified on a per table basis.
  211. Note: If you plan to use kambdb_recover, you must change the LOGFLAGS.
  212. </para>
  213. <programlisting format="linespecific">
  214. METADATA_READONLY
  215. 0
  216. METADATA_LOGFLAGS
  217. 0
  218. </programlisting>
  219. <para>
  220. Execute kamdbctl - There are three (3) groups of tables you may need depending
  221. on your situation.
  222. </para>
  223. <programlisting format="linespecific">
  224. kamdbctl create (required)
  225. kamdbctl presence (optional)
  226. kamdbctl extra (optional)
  227. </programlisting>
  228. <para>
  229. Modify the Kamailio configuration file to use db_berkeley module.
  230. The database URL for modules must be the path to the directory where
  231. the Berkeley DB table-files are located, prefixed by "berkeley://",
  232. e.g., "berkeley:///usr/local/etc/kamailio/db_berkeley".
  233. </para>
  234. <para>
  235. A couple other IMPORTANT things to consider are the 'db_mode' and the 'use_domain'
  236. modparams. The description of these parameters are found in usrloc documentation.
  237. </para>
  238. <para>
  239. Note on db_mode-
  240. The db_berkeley module will only journal the moment usrloc writes back
  241. to the DB. The safest mode is mode 3 , since the db_berkeley journal files will always
  242. be up-to-date. The main point is the db_mode vs. recovery by journal file interaction.
  243. Writing journal entries is 'best effort'. So if the hard drive becomes full, the
  244. attempt to write a journal entry may fail.
  245. </para>
  246. <para>
  247. Note on use_domain-
  248. The db_berkeley module will attempt natural joins when performing a query.
  249. This is basically a lexigraphical string compare using the keys provided.
  250. In most places in the db_berkeley dbschema (unless you customize), the domainname
  251. is identified as a natural key.
  252. Consider an example where use_domain = 0. In table subscriber, the db will be keying on
  253. 'username|NULL' because the default value will be used when that key column is not provided.
  254. This effectivly means that later queries must consistently use the username (w.o domain)
  255. in order to find a result to that particular subscriber query.
  256. The main point is 'use_domain' can not be changed once the db_berkeley is setup.
  257. </para>
  258. </section>
  259. <section>
  260. <title>Database Schema and Metadata</title>
  261. <para>
  262. All Berkeley DB tables are created via the kamdbctl script.
  263. This section provides details as to the content and
  264. format of the DB file upon creation.
  265. </para>
  266. <para>
  267. Since the Berkeley DB stores key value pairs, the database is seeded
  268. with a few meta-data rows . The keys to these rows must begin with 'METADATA'.
  269. Here is an example of table meta-data, taken from the table 'version'.
  270. </para>
  271. <para>
  272. Note on reserved character-
  273. The '|' pipe character is used as a record delimiter within the
  274. Berkeley DB implementation and must not be present in any DB field.
  275. </para>
  276. <example>
  277. <title>METADATA_COLUMNS</title>
  278. <programlisting format="linespecific">
  279. METADATA_COLUMNS
  280. table_name(str) table_version(int)
  281. METADATA_KEY
  282. 0
  283. </programlisting>
  284. </example>
  285. <para>
  286. In the above example, the row METADATA_COLUMNS defines the column names
  287. and type, and the row METADATA_KEY defines which column(s) form the key.
  288. Here the value of 0 indicates that column 0 is the key(ie table_name).
  289. With respect to column types, the db_berkeley modules only has the following
  290. types: string, str, int, double, and datetime. The default type is string,
  291. and is used when one of the others is not specified. The columns of the
  292. meta-data are delimited by whitespace.
  293. </para>
  294. <para>
  295. The actual column data is stored as a string value, and delimited by
  296. the '|' pipe character. Since the code tokenizes on this delimiter,
  297. it is important that this character not appear in any valid data field.
  298. The following is the output of the 'db_berkeley.sh dump version' command.
  299. It shows contents of table 'version' in plain text.
  300. </para>
  301. <example>
  302. <title>contents of version table</title>
  303. <programlisting format="linespecific">
  304. VERSION=3
  305. format=print
  306. type=hash
  307. h_nelem=21
  308. db_pagesize=4096
  309. HEADER=END
  310. METADATA_READONLY
  311. 1
  312. address|
  313. address|3
  314. aliases|
  315. aliases|1004
  316. dbaliases|
  317. dbaliases|1
  318. domain|
  319. domain|1
  320. gw_grp|
  321. gw_grp|1
  322. gw|
  323. gw|4
  324. speed_dial|
  325. speed_dial|2
  326. subscriber|
  327. subscriber|6
  328. uri|
  329. uri|1
  330. METADATA_COLUMNS
  331. table_name(str) table_version(int)
  332. METADATA_KEY
  333. 0
  334. acc|
  335. acc|4
  336. grp|
  337. grp|2
  338. lcr|
  339. lcr|2
  340. location|
  341. location|1004
  342. missed_calls|
  343. missed_calls|3
  344. re_grp|
  345. re_grp|1
  346. silo|
  347. silo|5
  348. trusted|
  349. trusted|4
  350. usr_preferences|
  351. usr_preferences|2
  352. DATA=END
  353. </programlisting>
  354. </example>
  355. </section>
  356. <section>
  357. <title>METADATA_COLUMNS (required)</title>
  358. <para>
  359. The METADATA_COLUMNS row contains the column names and types.
  360. Each is space delimited. Here is an example of the data taken from table subscriber :
  361. </para>
  362. <example>
  363. <title>METADATA_COLUMNS</title>
  364. <programlisting>
  365. METADATA_COLUMNS
  366. username(str) domain(str) password(str) ha1(str) ha1b(str) first_name(str) last_name(str) email_address(str) datetime_created(datetime) timezone(str) rpid(str)
  367. </programlisting>
  368. </example>
  369. <para>
  370. Related (hardcoded) limitations:
  371. <itemizedlist>
  372. <listitem>
  373. <para>maximum of 32 columns per table.</para>
  374. </listitem>
  375. <listitem>
  376. <para>maximum tablename size is 64.</para>
  377. </listitem>
  378. <listitem>
  379. <para>maximum data length is 2048</para>
  380. </listitem>
  381. </itemizedlist>
  382. </para>
  383. <para>
  384. Currently supporting these five types: str, datetime, int, double, string.
  385. </para>
  386. </section>
  387. <section>
  388. <title>METADATA_KEYS (required)</title>
  389. <para>
  390. The METADATA_KEYS row indicates the indexes of the key columns,
  391. with respect to the order specified in METADATA_COLUMNS.
  392. Here is an example taken from table subscriber that brings up a good point:
  393. </para>
  394. <example>
  395. <title>METADATA_KEYS</title>
  396. <programlisting>
  397. METADATA_KEY
  398. 0 1
  399. </programlisting>
  400. </example>
  401. <para>
  402. The point is that both the username and domain name are require
  403. as the key to this record. Thus, usrloc modparam
  404. use_domain = 1 must be set for this to work.
  405. </para>
  406. </section>
  407. <section>
  408. <title>METADATA_READONLY (optional)</title>
  409. <para>
  410. The METADATA_READONLY row contains a boolean 0 or 1.
  411. By default, its value is 0. On startup the DB will
  412. open initially as read-write (loads metadata) and then if this
  413. is set=1, it will close and reopen as read only (ro).
  414. I found this useful because readonly has impacts on the
  415. internal db locking etc.
  416. </para>
  417. </section>
  418. <section>
  419. <title>METADATA_LOGFLAGS (optional)</title>
  420. <para>
  421. The METADATA_LOGFLAGS row contains a bitfield that customizes the
  422. journaling on a per table basis. If not present the default value
  423. is taken as 0. Here are the masks so far (taken from bdb_lib.h):
  424. </para>
  425. <example>
  426. <title>METADATA_LOGFLAGS</title>
  427. <programlisting>
  428. #define JLOG_NONE 0
  429. #define JLOG_INSERT 1
  430. #define JLOG_DELETE 2
  431. #define JLOG_UPDATE 4
  432. #define JLOG_STDOUT 8
  433. #define JLOG_SYSLOG 16
  434. </programlisting>
  435. </example>
  436. <para>
  437. This means that if you want to journal INSERTS to local file and syslog the value
  438. should be set to 1+16=17. Or if you do not want to journal at all, set this to 0.
  439. </para>
  440. </section>
  441. <section>
  442. <title>DB Maintaince Script : kamdbctl </title>
  443. <para>
  444. Use the kamdbctl script for maintaining Kamailio Berkeley DB tables.
  445. This script assumes you have DBENGINE and DB_PATH setup correctly in &ctltool;rc.
  446. Note Unsupported commands are- backup, restore, migrate, copy, serweb.
  447. <example>
  448. <title>kamdbctl</title>
  449. <programlisting>
  450. usage: kamdbctl create
  451. kamdbctl presence
  452. kamdbctl extra
  453. kamdbctl drop
  454. kamdbctl reinit
  455. kamdbctl bdb list (lists the underlying db files in DB_PATH)
  456. kamdbctl bdb cat db (prints the contents of db file to STDOUT in plain-text)
  457. kamdbctl bdb swap db (installs db.new by db -> db.old; db.new -> db)
  458. kamdbctl bdb append db datafile (appends data to a new instance of db; output DB_PATH/db.new)
  459. kamdbctl bdb newappend db datafile (appends data to a new instance of db; output DB_PATH/db.new)
  460. </programlisting>
  461. </example>
  462. </para>
  463. </section>
  464. <section>
  465. <title>DB Recovery : kambdb_recover</title>
  466. <para>
  467. The db_berkeley module uses the Concurrent Data Store (CDS) architecture.
  468. As such, no transaction or journaling is provided by the DB natively.
  469. The application kambdb_recover is specifically written to recover data from
  470. journal files that Kamailio creates.
  471. The kambdb_recover application requires an additional text file that contains
  472. the table schema.
  473. </para>
  474. <para>
  475. The schema is loaded with the '-s' option and is required for all operations.
  476. Provide the path to the db_berkeley plain-text schema files. By default, these
  477. install to '/usr/local/share/kamailio/db_berkeley/kamailio/'.
  478. </para>
  479. <para>
  480. The '-h' home option is the DB_PATH path. Unlike the Berkeley utilities,
  481. this application does not look for the DB_PATH environment variable,
  482. so you have to specify it. If not specified, it will assume the current
  483. working directory. The last argument is the operation.
  484. There are fundamentally only two operations- create and recover.
  485. </para>
  486. <para>
  487. The following illustrates the four operations available to the administrator.
  488. <example>
  489. <title>kambdb_recover usage</title>
  490. <programlisting>
  491. usage: ./kambdb_recover -s schemadir [-h home] [-c tablename]
  492. This will create a brand new DB file with metadata.
  493. usage: ./kambdb_recover -s schemadir [-h home] [-C all]
  494. This will create all the core tables, each with metadata.
  495. usage: ./kambdb_recover -s schemadir [-h home] [-r journal-file]
  496. This will rebuild a DB and populate it with operation from journal-file.
  497. The table name is embedded in the journal-file name by convention.
  498. usage: ./kambdb_recover -s schemadir [-h home] [-R lastN]
  499. This will iterate over all core tables enumerated. If journal files exist in 'home',
  500. a new DB file will be created and populated with the data found in the last N files.
  501. The files are 'replayed' in chronological order (oldest to newest). This
  502. allows the administrator to rebuild the db with a subset of all possible
  503. operations if needed. For example, you may only be interested in
  504. the last hours data in table location.
  505. </programlisting>
  506. </example>
  507. </para>
  508. <para>
  509. Important note- A corrupted DB file must be moved out of the way before kambdb_recover is executed.
  510. </para>
  511. </section>
  512. <section>
  513. <title>Known Limitations</title>
  514. <para>
  515. The Berkeley DB does not nativly support an autoincrement (or sequence) mechanism.
  516. Consequently, this version does not support surragate keys in dbschema. These
  517. are the id columns in the tables.
  518. </para>
  519. </section>
  520. </chapter>