使用数据库保存Asterisk sip账号信息(odbc方式)
在默認情況下,Asterisk的配置文件都保存在/etc/asterisk目錄中,以ini文件的格式保存。我們也可以使用數據庫來保存大多數Asterisk配置信息。
?
Asterisk使用數據庫保存配置信息有兩種方法:靜態和動態,對于不經常修改的配置數據,可以使用靜態的方式保存,這些數據都是在Asterisk對應的模塊加載時獲取配置信息。動態方式適合那些經常變化的數據,例如SIP帳號信息。使用數據庫來保存SIP帳號信息還有一個好處:Asterisk會自動把SIP帳號登錄Asterisk的相關資料保存到表中,這樣大大的方便了管理員檢查當前SIP帳號的使用情況和狀態。
?
下面開始介紹Asterisk的數據庫獲取配置的方法。我使用的是Asterisk版本是 11.0.0。
?
在這里,我只對Asterisk的基本配置和SIP相關的配置感興趣,AIX帳號的配置和SIP的配置應該類似,不想多作分析。
1.????? 基本介紹
1.1.?? 獲取數據的方法
配置信息可以保存在多種數據庫中,下面是Asterisk支持的數據庫的列表和對應的模塊:
l???????? odbc????? res_config_odbc
l???????? sqlite???? res_config_sqlite
l???????? pgsql???? res_config_pgsql
l???????? curl?????? res_config_curl
l???????? ldap????? res_config_ldap
為了通用性,我選擇了ODBC作為獲得和修改Asterisk配置信息的方式,數據庫使用mysql。在這里我不介紹如何安裝unixODBC和mysql,只對相關的表和配置文件做介紹。
?
1.2.?? 使用到的表結構
1.2.1. 靜態配置表
| CREATE TABLE `ast_config` ( ???? `id` int(11) NOT NULL auto_increment, ???? `cat_metric` int(11) NOT NULL default '0', ???? `var_metric` int(11) NOT NULL default '0', ???? `commented` int(11) NOT NULL default '0', ???? `filename` varchar(128) NOT NULL default '', ???? `category` varchar(128) NOT NULL default 'default', ???? `var_name` varchar(128) NOT NULL default '', ???? `var_val` varchar(128) NOT NULL default '', ???? PRIMARY KEY (`id`), ???? KEY `filename_comment` (`filename`,`commented`) ) |
| 這個表結構是一個通用的保存各種.conf文件信息的表結構。Asterisk可以讓模塊的配置信息保存在一個表中,或者分別保存在不同的表中。 |
下面是表的說明
| Column name | Column type | Description |
| id | Serial, auto-incrementing | An auto-incrementing unique value for each row in the table. |
| cat_metric | Integer | The weight of the category within the file. A lower metric means it appears higher in the file (see the sidebar). context類型的權重 |
| var_metric | Integer | The weight of an item within a category. A lower metric means it appears higher in the list (see the sidebar). This is useful for things like codec order in sip.conf, or iax.conf where you want disallow=all to appear first (metric of 0), followed by allow=ulaw (metric of 1), then allow=gsm (metric of 2). 變量的權重 |
| filename | Varchar 128 | The filename the module would normally read from the hard drive of your system (e.g., musiconhold.conf, sip.conf, iax.conf, etc.). 文件名 |
| category | Varchar 128 | The section name within the file, such as [general]. Do not include the square brackets around the name when saving to the database. 類型 |
| var_name | Varchar 128 | The option on the left side of the equals sign (e.g., disallow is the var_name in disallow=all). 名字 |
| var_val | Varchar 128 | The value of an option on the right side of the equals sign (e.g., all is the var_val in disallow=all). 值 |
| commented | Integer | Any value other than 0 will evaluate as if it were prefixed with a semicolon in the flat file (commented out). 注解 |
?
1.2.2. SIP用戶表
SIP用戶的存儲方式在不同的Asterisk版本中差異很大,當看網上的文檔的時候很容易犯迷糊,正確的表結構是在Asterisk源碼包的下面目錄中:
| [root@kernel asterisk-11.0.0]# ls contrib/realtime/mysql/sippeers.sql contrib/realtime/mysql/sippeers.sql |
具體的表結構是:
| # # Table structure for table `sippeers` # ? CREATE TABLE IF NOT EXISTS `sippeers` ( ????? `id` int(11) NOT NULL AUTO_INCREMENT, ??? ??`name` varchar(10) NOT NULL, ????? `ipaddr` varchar(15) DEFAULT NULL, ????? `port` int(5) DEFAULT NULL, ????? `regseconds` int(11) DEFAULT NULL, ????? `defaultuser` varchar(10) DEFAULT NULL, ????? `fullcontact` varchar(35) DEFAULT NULL, ????? `regserver` varchar(20) DEFAULT NULL, ????? `useragent` varchar(20) DEFAULT NULL, ????? `lastms` int(11) DEFAULT NULL, ????? `host` varchar(40) DEFAULT NULL, ????? `type` enum('friend','user','peer') DEFAULT NULL, ????? `context` varchar(40) DEFAULT NULL, ????? `permit` varchar(40) DEFAULT NULL, ????? `deny` varchar(40) DEFAULT NULL, ????? `secret` varchar(40) DEFAULT NULL, ????? `md5secret` varchar(40) DEFAULT NULL, ????? `remotesecret` varchar(40) DEFAULT NULL, ????? `transport` enum('udp','tcp','udp,tcp','tcp,udp') DEFAULT NULL, ????? `dtmfmode` enum('rfc2833','info','shortinfo','inband','auto') DEFAULT NULL, ????? `directmedia` enum('yes','no','nonat','update') DEFAULT NULL, ????? `nat` enum('yes','no','never','route') DEFAULT NULL, ????? `callgroup` varchar(40) DEFAULT NULL, ????? `pickupgroup` varchar(40) DEFAULT NULL, ????? `language` varchar(40) DEFAULT NULL, ????? `allow` varchar(40) DEFAULT NULL, ????? `disallow` varchar(40) DEFAULT NULL, ????? `insecure` varchar(40) DEFAULT NULL, ????? `trustrpid` enum('yes','no') DEFAULT NULL, ????? `progressinband` enum('yes','no','never') DEFAULT NULL, ????? `promiscredir` enum('yes','no') DEFAULT NULL, ????? `useclientcode` enum('yes','no') DEFAULT NULL, ????? `accountcode` varchar(40) DEFAULT NULL, ????? `setvar` varchar(40) DEFAULT NULL, ????? `callerid` varchar(40) DEFAULT NULL, ????? `amaflags` varchar(40) DEFAULT NULL, ????? `callcounter` enum('yes','no') DEFAULT NULL, ????? `busylevel` int(11) DEFAULT NULL, ????? `allowoverlap` enum('yes','no') DEFAULT NULL, ????? `allowsubscribe` enum('yes','no') DEFAULT NULL, ????? `videosupport` enum('yes','no') DEFAULT NULL, ????? `maxcallbitrate` int(11) DEFAULT NULL, ????? `rfc2833compensate` enum('yes','no') DEFAULT NULL, ????? `mailbox` varchar(40) DEFAULT NULL, ????? `session-timers` enum('accept','refuse','originate') DEFAULT NULL, ????? `session-expires` int(11) DEFAULT NULL, ????? `session-minse` int(11) DEFAULT NULL, ????? `session-refresher` enum('uac','uas') DEFAULT NULL, ????? `t38pt_usertpsource` varchar(40) DEFAULT NULL, ????? `regexten` varchar(40) DEFAULT NULL, ????? `fromdomain` varchar(40) DEFAULT NULL, ????? `fromuser` varchar(40) DEFAULT NULL, ????? `qualify` varchar(40) DEFAULT NULL, ????? `defaultip` varchar(40) DEFAULT NULL, ????? `rtptimeout` int(11) DEFAULT NULL, ????? `rtpholdtimeout` int(11) DEFAULT NULL, ????? `sendrpid` enum('yes','no') DEFAULT NULL, ????? `outboundproxy` varchar(40) DEFAULT NULL, ????? `callbackextension` varchar(40) DEFAULT NULL, ????? `timert1` int(11) DEFAULT NULL, ????? `timerb` int(11) DEFAULT NULL, ????? `qualifyfreq` int(11) DEFAULT NULL, ????? `constantssrc` enum('yes','no') DEFAULT NULL, ????? `contactpermit` varchar(40) DEFAULT NULL, ????? `contactdeny` varchar(40) DEFAULT NULL, ????? `usereqphone` enum('yes','no') DEFAULT NULL, ????? `textsupport` enum('yes','no') DEFAULT NULL, ????? `faxdetect` enum('yes','no') DEFAULT NULL, ????? `buggymwi` enum('yes','no') DEFAULT NULL, ????? `auth` varchar(40) DEFAULT NULL, ????? `fullname` varchar(40) DEFAULT NULL, ????? `trunkname` varchar(40) DEFAULT NULL, ????? `cid_number` varchar(40) DEFAULT NULL, ????? `callingpres` enum('allowed_not_screened','allowed_passed_screen','allowed_failed_screen','allowed','prohib_not_screened','prohib_passed_screen','prohib_failed_screen','prohib') DEFAULT NULL, ????? `mohinterpret` varchar(40) DEFAULT NULL, ????? `mohsuggest` varchar(40) DEFAULT NULL, ????? `parkinglot` varchar(40) DEFAULT NULL, ????? `hasvoicemail` enum('yes','no') DEFAULT NULL, ????? `subscribemwi` enum('yes','no') DEFAULT NULL, ????? `vmexten` varchar(40) DEFAULT NULL, ????? `autoframing` enum('yes','no') DEFAULT NULL, ????? `rtpkeepalive` int(11) DEFAULT NULL, ????? `call-limit` int(11) DEFAULT NULL, ????? `g726nonstandard` enum('yes','no') DEFAULT NULL, ????? `ignoresdpversion` enum('yes','no') DEFAULT NULL, ????? `allowtransfer` enum('yes','no') DEFAULT NULL, ????? `dynamic` enum('yes','no') DEFAULT NULL, ????? PRIMARY KEY (`id`), ????? UNIQUE KEY `name` (`name`), ????? KEY `ipaddr` (`ipaddr`,`port`), ????? KEY `host` (`host`,`port`) ) |
看起來這個表字段很多很嚇人,其實最近本的配置就下面幾項:
l???????? Name?????????? 帳號名字
l???????? Host???????????? 設置成dynamic
l???????? Context??????? 設置對應的context
l???????? Type???????????? 選擇friend
l???????? Secret?????????? 帳號密碼明文
其他很多字段都是SIP帳號登錄時的數據字段。
1.3.?? 使用到的配置文件
1.3.1. extconfig.conf
最基本的配置文件是extconfig.conf,這個配置文件定義了哪些模塊使用數據庫的配置,確定了SIP帳號對應的表格。這個配置文件的注釋已經基本介紹了它的使用方法,不過沒有具體的表的結構。本文上面已經介紹了兩個相關的表結構。其他表結構可以在Asterisk源碼包的contrib/realtime/目錄下找找。
1.3.2. res_odbc.conf
由于我選擇了ODBC的數據庫連接方式,所以必然的需要ODBC的資源配置。
這個配置文件中,我使用了原來的配置參數:
| [asterisk] enabled => yes dsn => asterisk |
?
如果是使用yum安裝的unixODBC,Asterisk這個數據源在/etc/odbc.ini中定義。
感覺就是:
| [asterisk] Driver??????? ??= MySQL Database??????? = asterisk Servername????? = localhost UserName??????? = root Password??????? = xxxxx |
相關的設備在/etc/odbcinst.ini中定義:
| [MySQL] Description???? = ODBC for MySQL Driver????????? = /usr/local/lib/libmyodbc5.so Setup?????????? = /usr/local/lib/libmyodbc5w.so FileUsage?????? = 1 |
1.3.3. Sip.conf
在sip.conf中有一些和動態實時數據庫配置相關的項,這些想對整個配置的實現影響不大,但是涉及是否建立內存緩存,影響性能。
1.3.4. modules.conf
這個文件是Asterisk加載模塊的配置文件。要實現配置數據保存到數據庫中,必須修改一下加載方法。
下面是modules.conf中相應的注意事項:
| ; Any modules that need to be loaded before the Asterisk core has been ; initialized (just after the logger has been initialized) can be loaded ; using 'preload'. This will frequently be needed if you wish to map all ; module configuration files into Realtime storage, since the Realtime ; driver will need to be loaded before the modules using those configuration ; files are initialized. ; ; An example of loading ODBC support would be: preload => res_odbc.so preload => res_config_odbc.so |
| 任何模塊想在Asterisk核心模塊初始化之前加載,可以使用preload方式加載。實時驅動相關的模塊需要在其他模塊加載前加載,這樣才能讓其他模塊使用數據庫的配置信息。 |
?
2.????? 靜態配置
在這里稍微介紹一個簡單的靜態配置例子,選擇的模塊是:musiconhold.conf。
在配置靜態配置文件之前,建議先看看extconfig.conf文件的下面內容:
| ; ; The following files CANNOT be loaded from Realtime storage: ;?????? asterisk.conf ;?????? extconfig.conf (this file) ;?????? logger.conf ; ; Additionally, the following files cannot be loaded from ; Realtime storage unless the storage driver is loaded ; early using 'preload' statements in modules.conf: ;?????? manager.conf ;? ?????cdr.conf ;?????? rtp.conf ; ; Named ACLs specified in realtime also can not be used ; from manager.conf unless the storage driver is preloaded. ; Attempting to use a realtime stored named ACL before the ; driver is loaded will result in an invalid ACL which ; rejects all addresses. |
2.1.?? 刪除配置文件
| $ cd /etc/asterisk $ mv musiconhold.conf musiconhold.conf.old |
2.2.?? 檢查模塊加載情況
| *CLI> core restart now *CLI> moh show classes *CLI> |
2.3.?? 插入數據記錄
| INSERT INTO ast_config (filename,category,var_name,var_val) VALUES ('musiconhold.conf','default','mode','files'); INSERT INTO ast_config (filename,category,var_name,var_val) VALUES ('musiconhold.conf','default','directory','/var/lib/asterisk/moh'); |
| asterisk=# SELECT filename,category,var_name,var_val FROM ast_config; ? ?filename???????? | category?????? | var_name???? | var_val ------------------+----------------+--------------+------------------------ ?musiconhold.conf | default??????? | mode???????? | files ?musiconhold.conf | default??????? | directory??? | /var/lib/asterisk/moh (2 rows) |
2.4.?? 設置extconfig.conf
在extconfig.conf中加入:
| musiconhold.conf => odbc,asterisk,ast_config |
2.5.?? 重載模塊
| kernel*CLI> module reload extconfig ? == Parsing '/etc/asterisk/extconfig.conf': Found ? == Binding musiconhold.conf to odbc/asterisk/ast_config |
| kernel*CLI> module reload res_musiconhold.so ??? -- Reloading module 'res_musiconhold.so' (Music On Hold Resource) |
2.6.?? 查看數據庫中的配置
| kernel*CLI> moh show classes Class: default ??????? Mode: files ??????? Directory: /var/lib/asterisk/moh |
3.????? 動態配置SIP用戶信息
在開始做SIP的動態加載之前,先看看extconfig.conf配置文件中的這部分:
| ; Realtime configuration engine ; ; maps a particular family of realtime ; configuration to a given database driver, ; database and table (or uses the name of ; the family if the table is not specified ; ;example => odbc,asterisk,alttable,1 ;example => mysql,asterisk,alttable,2 ;example2 => ldap,"dc=oxymium,dc=net",example2 ; ; Additionally, priorities are now supported for use as failover methods ; for retrieving realtime data.? If one connection fails to retrieve any ; information, the next sequential priority will be tried next.? This ; especially works well with ODBC connections, since res_odbc now caches ; when connection failures occur and prevents immediately retrying those ; connections until after a specified timeout.? Note:? priorities must ; start at 1 and be sequential (i.e. if you have only priorities 1, 2, ; and 4, then 4 will be ignored, because there is no 3). ; ; "odbc" is shown in the examples below, but is not the only valid realtime ; engine.? There is: ;??? odbc ... res_config_odbc ;??? sqlite ... res_config_sqlite ;??? pgsql ... res_config_pgsql ;??? curl ... res_config_curl ;??? ldap ... res_config_ldap ; ; Note: The res_config_pgsql and res_config_sqlite backends configure the ; database used in their respective configuration files and ignore the ; database name configured in this file. ; ;iaxusers => odbc,asterisk ;iaxpeers => odbc,asterisk ;sippeers => odbc,asterisk ;sipregs => odbc,asterisk ; (avoid sipregs if possible, e.g. by using a view) ;voicemail => odbc,asterisk ;extensions => odbc,asterisk ;meetme => mysql,general ;queues => odbc,asterisk ;queue_members => odbc,asterisk ;acls => odbc,asterisk ;musiconhold => mysql,general ;queue_log => mysql,general ; sippeers => odbc,asterisk ; ; While most dynamic realtime engines are automatically used when defined in |
?
3.1.?? 修改extconfig.conf
就修改下面一行,把下面行的注釋符去掉。
| sippeers => odbc,asterisk |
3.2.?? 修改sip.conf
把下面行的注釋符去掉。沒考證過不做這步有什么影響。^^
| rtupdate=yes?????????????????? ; Send registry updates to database using realtime? (yes|no) |
3.3.?? 添加記錄
| INSERT INTO sippeers (NAME, HOST,TYPE,context,secret) VALUES ('2012', 'dynamic','friend', 'jluotest','2012') |
3.4.?? 重載模塊
| kernel*CLI> module reload extconfig ? == Parsing '/etc/asterisk/extconfig.conf': Found ? == Binding musiconhold.conf to odbc/asterisk/ast_config ? == Binding sippeers to odbc/asterisk/sippeers kernel*CLI> sip reload ?Reloading SIP ? == Parsing '/etc/asterisk/sip.conf': Found ? == Parsing '/etc/asterisk/users.conf': Found ? == Using SIP CoS mark 4 ? == Parsing '/etc/asterisk/sip_notify.conf': Found |
3.5.?? 查看
| kernel*CLI> sip show users Username?????????????????? Secret?????????? Accountcode????? Def.Context????? ACL? ForcerPort 2134?????????????????????? not_very_secret?????????????????? jluotest???????? No?? No??????? 2136?????????????????????? peekaboo?????? ???????????????????jluotest???????? No?? No??????? 2133?????????????????????? peekaboo????????????????????????? jluotest???????? No?? No? |
看到的只有在sip.conf中配置的用戶。
3.6.?? 用2011登錄
似乎默認情況下不會顯示對應的數據庫中配置的用戶,只有該用戶登錄后才顯示。下面是2011登錄后,再查看SIP用戶列表的情況:
| ?? ????-- Unregistered SIP '2011' ??? -- Registered SIP '2011' at 192.168.0.11:4604 ?????? > Saved useragent "Linphone/3.5.2 (eXosip2/3.6.0)" for peer 2011 kernel*CLI> sip show users Username?????????????????? Secret?????????? Accountcode????? Def.Context????? ACL? ForcerPort 2134?????????????????????? not_very_secret?????????????????? jluotest???????? No?? No??????? 2136?????????????????????? peekaboo????????????? ????????????jluotest???????? No?? No??????? 2133?????????????????????? peekaboo????????????????????????? jluotest???????? No?? No??????? 2011?????????????????????? 2011????????????????????????????? jluotest???????? No?? No |
4.????? 后記
本文檔只是我的一個實踐記錄,有很多不完善和缺失,但是能與大家的實踐做一個對比。
一個更完善的配置需要更加充分地分析SIP配置和對Asterisk的各個模塊的配置信息有更充分的了解。
總結
以上是生活随笔為你收集整理的使用数据库保存Asterisk sip账号信息(odbc方式)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 招行信用卡账单分期可以提前还款吗
- 下一篇: asterisk账号和拨号方案mysql