使用数据库保存Asterisk配置.docx
《使用数据库保存Asterisk配置.docx》由会员分享,可在线阅读,更多相关《使用数据库保存Asterisk配置.docx(15页珍藏版)》请在冰豆网上搜索。
使用数据库保存Asterisk配置
在默认情况下,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支持的数据库的列表和对应的模块:
●odbcres_config_odbc
●sqliteres_config_sqlite
●pgsqlres_config_pgsql
●curlres_config_curl
●ldapres_config_ldap
为了通用性,我选择了ODBC作为获得和修改Asterisk配置信息的方式,数据库使用mysql。
在这里我不介绍如何安装unixODBC和mysql,只对相关的表和配置文件做介绍。
1.2.使用到的表结构
1.2.1.静态配置表
CREATETABLE`ast_config`(
`id`int(11)NOTNULLauto_increment,
`cat_metric`int(11)NOTNULLdefault'0',
`var_metric`int(11)NOTNULLdefault'0',
`commented`int(11)NOTNULLdefault'0',
`filename`varchar(128)NOTNULLdefault'',
`category`varchar(128)NOTNULLdefault'default',
`var_name`varchar(128)NOTNULLdefault'',
`var_val`varchar(128)NOTNULLdefault'',
PRIMARYKEY(`id`),
KEY`filename_comment`(`filename`,`commented`)
)
这个表结构是一个通用的保存各种.conf文件信息的表结构。
Asterisk可以让模块的配置信息保存在一个表中,或者分别保存在不同的表中。
下面是表的说明
Columnname
Columntype
Description
id
Serial,auto-incrementing
Anauto-incrementinguniquevalueforeachrowinthetable.
cat_metric
Integer
Theweightofthecategorywithinthefile.Alowermetricmeansitappearshigherinthefile(seethesidebar).
context类型的权重
var_metric
Integer
Theweightofanitemwithinacategory.Alowermetricmeansitappearshigherinthelist(seethesidebar).Thisisusefulforthingslikecodecorderinsip.conf,oriax.confwhereyouwantdisallow=alltoappearfirst(metricof0),followedbyallow=ulaw(metricof1),thenallow=gsm(metricof2).
变量的权重
filename
Varchar128
Thefilenamethemodulewouldnormallyreadfromtheharddriveofyoursystem(e.g.,musiconhold.conf,sip.conf,iax.conf,etc.).
文件名
category
Varchar128
Thesectionnamewithinthefile,suchas[general].Donotincludethesquarebracketsaroundthenamewhensavingtothedatabase.
类型
var_name
Varchar128
Theoptionontheleftsideoftheequalssign(e.g.,disallowisthevar_nameindisallow=all).
名字
var_val
Varchar128
Thevalueofanoptionontherightsideoftheequalssign(e.g.,allisthevar_valindisallow=all).
值
commented
Integer
Anyvalueotherthan0willevaluateasifitwereprefixedwithasemicolonintheflatfile(commentedout).
注解
1.2.2.SIP用户表
SIP用户的存储方式在不同的Asterisk版本中差异很大,当看网上的文档的时候很容易犯迷糊,正确的表结构是在Asterisk源码包的下面目录中:
[root@kernelasterisk-11.0.0]#lscontrib/realtime/mysql/sippeers.sql
contrib/realtime/mysql/sippeers.sql
具体的表结构是:
#
#Tablestructurefortable`sippeers`
#
CREATETABLEIFNOTEXISTS`sippeers`(
`id`int(11)NOTNULLAUTO_INCREMENT,
`name`varchar(10)NOTNULL,
`ipaddr`varchar(15)DEFAULTNULL,
`port`int(5)DEFAULTNULL,
`regseconds`int(11)DEFAULTNULL,
`defaultuser`varchar(10)DEFAULTNULL,
`fullcontact`varchar(35)DEFAULTNULL,
`regserver`varchar(20)DEFAULTNULL,
`useragent`varchar(20)DEFAULTNULL,
`lastms`int(11)DEFAULTNULL,
`host`varchar(40)DEFAULTNULL,
`type`enum('friend','user','peer')DEFAULTNULL,
`context`varchar(40)DEFAULTNULL,
`permit`varchar(40)DEFAULTNULL,
`deny`varchar(40)DEFAULTNULL,
`secret`varchar(40)DEFAULTNULL,
`md5secret`varchar(40)DEFAULTNULL,
`remotesecret`varchar(40)DEFAULTNULL,
`transport`enum('udp','tcp','udp,tcp','tcp,udp')DEFAULTNULL,
`dtmfmode`enum('rfc2833','info','shortinfo','inband','auto')DEFAULTNULL,
`directmedia`enum('yes','no','nonat','update')DEFAULTNULL,
`nat`enum('yes','no','never','route')DEFAULTNULL,
`callgroup`varchar(40)DEFAULTNULL,
`pickupgroup`varchar(40)DEFAULTNULL,
`language`varchar(40)DEFAULTNULL,
`allow`varchar(40)DEFAULTNULL,
`disallow`varchar(40)DEFAULTNULL,
`insecure`varchar(40)DEFAULTNULL,
`trustrpid`enum('yes','no')DEFAULTNULL,
`progressinband`enum('yes','no','never')DEFAULTNULL,
`promiscredir`enum('yes','no')DEFAULTNULL,
`useclientcode`enum('yes','no')DEFAULTNULL,
`accountcode`varchar(40)DEFAULTNULL,
`setvar`varchar(40)DEFAULTNULL,
`callerid`varchar(40)DEFAULTNULL,
`amaflags`varchar(40)DEFAULTNULL,
`callcounter`enum('yes','no')DEFAULTNULL,
`busylevel`int(11)DEFAULTNULL,
`allowoverlap`enum('yes','no')DEFAULTNULL,
`allowsubscribe`enum('yes','no')DEFAULTNULL,
`videosupport`enum('yes','no')DEFAULTNULL,
`maxcallbitrate`int(11)DEFAULTNULL,
`rfc2833compensate`enum('yes','no')DEFAULTNULL,
`mailbox`varchar(40)DEFAULTNULL,
`session-timers`enum('accept','refuse','originate')DEFAULTNULL,
`session-expires`int(11)DEFAULTNULL,
`session-minse`int(11)DEFAULTNULL,
`session-refresher`enum('uac','uas')DEFAULTNULL,
`t38pt_usertpsource`varchar(40)DEFAULTNULL,
`regexten`varchar(40)DEFAULTNULL,
`fromdomain`varchar(40)DEFAULTNULL,
`fromuser`varchar(40)DEFAULTNULL,
`qualify`varchar(40)DEFAULTNULL,
`defaultip`varchar(40)DEFAULTNULL,
`rtptimeout`int(11)DEFAULTNULL,
`rtpholdtimeout`int(11)DEFAULTNULL,
`sendrpid`enum('yes','no')DEFAULTNULL,
`outboundproxy`varchar(40)DEFAULTNULL,
`callbackextension`varchar(40)DEFAULTNULL,
`timert1`int(11)DEFAULTNULL,
`timerb`int(11)DEFAULTNULL,
`qualifyfreq`int(11)DEFAULTNULL,
`constantssrc`enum('yes','no')DEFAULTNULL,
`contactpermit`varchar(40)DEFAULTNULL,
`contactdeny`varchar(40)DEFAULTNULL,
`usereqphone`enum('yes','no')DEFAULTNULL,
`textsupport`enum('yes','no')DEFAULTNULL,
`faxdetect`enum('yes','no')DEFAULTNULL,
`buggymwi`enum('yes','no')DEFAULTNULL,
`auth`varchar(40)DEFAULTNULL,
`fullname`varchar(40)DEFAULTNULL,
`trunkname`varchar(40)DEFAULTNULL,
`cid_number`varchar(40)DEFAULTNULL,
`callingpres`enum('allowed_not_screened','allowed_passed_screen','allowed_failed_screen','allowed','prohib_not_screened','prohib_passed_screen','prohib_failed_screen','prohib')DEFAULTNULL,
`mohinterpret`varchar(40)DEFAULTNULL,
`mohsuggest`varchar(40)DEFAULTNULL,
`parkinglot`varchar(40)DEFAULTNULL,
`hasvoicemail`enum('yes','no')DEFAULTNULL,
`subscribemwi`enum('yes','no')DEFAULTNULL,
`vmexten`varchar(40)DEFAULTNULL,
`autoframing`enum('yes','no')DEFAULTNULL,
`rtpkeepalive`int(11)DEFAULTNULL,
`call-limit`int(11)DEFAULTNULL,
`g726nonstandard`enum('yes','no')DEFAULTNULL,
`ignoresdpversion`enum('yes','no')DEFAULTNULL,
`allowtransfer`enum('yes','no')DEFAULTNULL,
`dynamic`enum('yes','no')DEFAULTNULL,
PRIMARYKEY(`id`),
UNIQUEKEY`name`(`name`),
KEY`ipaddr`(`ipaddr`,`port`),
KEY`host`(`host`,`port`)
)
看起来这个表字段很多很吓人,其实最近本的配置就下面几项:
●Name帐号名字
●Host设置成dynamic
●Context设置对应的context
●Type选择friend
●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=ODBCforMySQL
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中相应的注意事项:
;AnymodulesthatneedtobeloadedbeforetheAsteriskcorehasbeen
;initialized(justaftertheloggerhasbeeninitialized)canbeloaded
;using'preload'.Thiswillfrequentlybeneededifyouwishtomapall
;moduleconfigurationfilesintoRealtimestorage,sincetheRealtime
;driverwillneedtobeloadedbeforethemodulesusingthoseconfiguration
;filesareinitialized.
;
;AnexampleofloadingODBCsupportwouldbe:
preload=>res_odbc.so
preload=>res_config_odbc.so
任何模块想在Asterisk核心模块初始化之前加载,可以使用preload方式加载。
实时驱动相关的模块需要在其他模块加载前加载,这样才能让其他模块使用数据库的配置信息。
2.静态配置
在这里稍微介绍一个简单的静态配置例子,选择的模块是:
musiconhold.conf。
在配置静态配置文件之前,建议先看看extconfig.conf文件的下面内容:
;
;ThefollowingfilesCANNOTbeloadedfromRealtimestorage:
;asterisk.conf
;extconfig.conf(thisfile)
;logger.conf
;
;Additionally,thefollowingfilescannotbeloadedfrom
;Realtimestorageunlessthestoragedriverisloaded
;earlyusing'preload'statementsinmodules.conf:
;manager.conf
;cdr.conf
;rtp.conf
;
;NamedACLsspecifiedinrealtimealsocannotbeused
;frommanager.confunlessthestoragedriverispreloaded.
;AttemptingtousearealtimestorednamedACLbeforethe
;driverisloadedwillresultinaninvalidACLwhich
;rejectsalladdresses.
2.1.删除配置文件
$cd/etc/asterisk
$mvmusiconhold.confmusiconhold.conf.old
2.2.检查模块加载情况
*CLI>corerestartnow
*CLI>mohshowclasses
*CLI>
2.3.插入数据记录
INSERTINTOast_config(filename,category,var_name,var_val)VALUES('musiconhold.conf','default','mode','files');
INSERTINTOast_config(filename,category,var_name,var_val)VALUES('musiconhold.conf','default','directory','/var/lib/asterisk/moh');
asterisk=#SELECTfilename,category,var_name,var_valFROMast_config;
filename|category|var_name|var_val
------------------+----------------+--------------+------------------------
musiconhold.conf|default|mode|files
musiconhold.conf|default|directory|/var/lib/asterisk/moh
(2rows)
2.4.设置extconfig.conf
在extconfig.