Please read about Asterisk at http://www.asterisk.org/. It is a full featured open source PBX running on Linux. I have installed it on a Fedora Core 4 Linux machine without any problem.
1. Asterisk Quick Start
1.1 Download and install Asterisk
Download and install Asterisk is straightforward. Please follow the instructions at http://www.asterisk.org/index.php?menu=download.
Once it is done, type asterisk –vc to test it works.
# asterisk –vc
Asterisk Ready.
CLI> help
CLI> stop now
Beginning asterisk shutdown....
Executing last minute cleanups
Asterisk cleanly ending (0)
1.2 Configure Asterisk with files
Run make samples to get the sample configuration files under /etc/asterisk. Follow the Asterisk: A Bare-Bones VoIP Example at http://www.onlamp.com/lpt/a/3956. Now you should have all configurations setup under /etc/asterisk as following:
# cat sip.conf
[general]
port=5060
bindaddr=0.0.0.0
allow=all
context=bogon-calls
[2000]
type=friend
host=dynamic
defaultip=10.1.2.192
context=from-sip
mailbox=2000
[2001]
type=friend
host=dynamic
defaultip=10.1.1.220
context=from-sip
mailbox=2001
# cat extensions.conf
[general]
static=yes
writeprotect=yes
[bogon-calls]
exten => _.,1,Congestion
[from-sip]
exten => 2000,1,Dial(SIP/2000,20)
exten => 2000,2,Voicemail(u2000)
exten => 2000,102,Voicemail(b2000)
exten => 2000,103,Hangup
exten => 2001,1,Dial(SIP/2001,20)
exten => 2001,2,Voicemail(u2001)
exten => 2001,102,Voicemail(b2001)
exten => 2001,103,Hangup
exten => 2999,1,VoicemailMain(${CALLERIDNUM})
# cat voicemail.conf
[general]
format=wav
[default]
2000 => 4321, Wei Kun, wei-kun@mediaring.com
2001 => 8383, Wei Kun, kunwei2005@gmail.com
A small script tool addmailbox can be used to create voice mail folder. It is not needed anymore. The voicemail is stored under
# ls /var/spool/asterisk/voicemail
Now start Asterisk again.
1.3 Configure the SIP Phones and Make the Call
I have used 2 SIP phones to test it. In the SIP settings, set the Asterisk box’s IP as SIP proxy. Set extension as 2000 and 2001 respectively. You may also need to change the audio codec format. GSM works well for me. The 2 SIP phones will register with Asterisk. In Asterisk console, you can verify:
CLI> sip show peers
Name/username Host Dyn Nat ACL Mask Port Status
2001/2001 10.1.1.220 D 255.255.255.255 5080 Unmonitored
2000/2000 10.1.2.192 D 255.255.255.255 5060 Unmonitored
2 sip peers [2 online , 0 offline]
Now the 2 SIP phones with extension 2000 and 2001 can call each other, or leave voicemail. You can retrieve voicemail by dialing 2999. And you can also receive email with voicemail attached as another means.
Dear Wei Kun:
Just wanted to let you know you were just left a 0:06 long message (number 3) in mailbox 2001 from 2001, on Thursday, August 04, 2005 at 10:41:58 AM so you might want to check it when you get a chance. Thanks!
--Asterisk
msg0002.wav 86K Download
2. Configure CDR with MySQL
By default, CDR is stored in /var/log/asterisk/cdr-csv. To store CDR into database, following steps below:
2.1 Install MySQL and create related entries
MySQL is a popular database available from various OS distributions. Installation is very straightforward. In Linux, mysqld can be loaded by
/etc/init.d/mysqld start
You can also configure service auto-load with
chkconfig --level 345 mysqld on
Once the mysqld starts, you can access mysql with
mysql –u root –p
By default, no password is set for root. For obvious reason, you need set the root password with
mysqladmin -u root -h 'localhost' password "rootpass"
Now we need create account and password for Asterisk purpose. Connect MySQL with root account and password just assigned.
mysql> grant all privileges on *.* to 'asterman'@'localhost' identified by 'k1l1m4n4' with grant option;
mysql> quit;
We now need create database and tables. This time connect MySQL with the new account just created.
mysql> create database asterisk;
mysql> CREATE TABLE cdr ( calldate datetime NOT NULL default '0000-00-00 00:00:00', clid varchar(80) NOT NULL default '', src varchar(80) NOT NULL default '', dst varchar(80) NOT NULL default '', dcontext varchar(80) NOT NULL default '', channel varchar(80) NOT NULL default '', dstchannel varchar(80) NOT NULL default '', lastapp varchar(80) NOT NULL default '', lastdata varchar(80) NOT NULL default '', duration int(11) NOT NULL default '0', billsec int(11) NOT NULL default '0', disposition varchar(45) NOT NULL default '', amaflags int(11) NOT NULL default '0', accountcode varchar(20) NOT NULL default '', uniqueid varchar(32) NOT NULL default '', userfield varchar(255) NOT NULL default '');
mysql> mysql> desc cdr;
+-------------+--------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------------------+-------+
| calldate | datetime | | | 0000-00-00 00:00:00 | |
| clid | varchar(80) | | | | |
| src | varchar(80) | | | | |
| dst | varchar(80) | | | | |
| dcontext | varchar(80) | | | | |
| channel | varchar(80) | | | | |
| dstchannel | varchar(80) | | | | |
| lastapp | varchar(80) | | | | |
| lastdata | varchar(80) | | | | |
| duration | int(11) | | | 0 | |
| billsec | int(11) | | | 0 | |
| disposition | varchar(45) | | | | |
| amaflags | int(11) | | | 0 | |
| accountcode | varchar(20) | | | | |
| uniqueid | varchar(32) | | | | |
| userfield | varchar(255) | | | | |
+-------------+--------------+------+-----+---------------------+-------+
2.2 Install Asterisk add-on
The CDR MySQl modules are located at asterisk-addons package. You also need install asterisk-addons. Copy the /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample into /etc/asterisk, and modify as needed. If it is a localhost Unix socket connection, please check the mysql.sock location.
[root@localhost asterisk]# cat cdr_mysql.conf
[global]
hostname=localhost
dbname=asterisk
table=cdr
password=k1l1m4n4
user=asterman
port=3306
sock=/var/lib/mysql/mysql.sock
userfield=1
2.3 Test it out
Make the call and CDR will be inserted into table cdr.
3. Asterisk Realtime – Move Configuration File to Database
3.1 Install and setup Asterisk Realtime
To enable Asterisk Realtime, you need download CVS-HEAD. You also need install asterisk-addons.
cd /usr/src/
export CVSROOT=:pserver:anoncvs@cvs.digium.com:/usr/cvsroot
cvs login
cvs co –r HEAD asterisk
cd /usr/src/asterisk
make clean; make; make install
cvs co asterisk-addons
cd /usr/src/asterisk-addons
make clean; make; make install
Now copy the /usr/src/asterisk-addons/configs/res_mysql.conf.sample into /etc/asterisk, and modify as needed. If it is a localhost Unix socket connection, please check the mysql.sock location.
# cat res_mysql.conf
[general]
dbhost = localhost
dbname = asterisk
dbuser = asterman
dbpass = k1l1m4n4
dbport = 3306
dbsock = /var/lib/mysql/mysql.sock
extconfig.conf also need to be modified to allow Asterisk load configuration from database.
# cat extconfig.conf
…
sipusers => mysql,asterisk,sip_buddies
sippeers => mysql,asterisk,sip_buddies
extensions => mysql,asterisk,extensions_table
voicemail => mysql,asterisk, voicemail_users
…
3.2 Asterisk Realtime SIP
Following the configuration in extconfig.conf, we create sip_buddies table and insert data to achieve the same setup in sip.conf in section 1. Now sip.conf is not important anymore. All the sip configurations are loaded from database in realtime. In case you modify entries in this table, Asterisk will load the new configuration in realtime.
mysql> CREATE TABLE `sip_buddies` ( `id` int(11) NOT NULL auto_increment, `name` varchar(80) NOT NULL default '', `context` varchar(80) default NULL, `defaultip` varchar(15) default NULL, `host` varchar(31) NOT NULL default '', `mailbox` varchar(50) default NULL, `type` varchar(6) NOT NULL default 'friend', `username` varchar(80) NOT NULL default '', `regseconds` int(11) NOT NULL default '0', `ipaddr` varchar(15), `port` varchar(5), PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`)) TYPE=MyISAM ROW_FORMAT=DYNAMIC;
mysql> desc sip_buddies;
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| name | varchar(80) | | UNI | | |
| context | varchar(80) | YES | | NULL | |
| defaultip | varchar(15) | YES | | NULL | |
| host | varchar(31) | | | | |
| mailbox | varchar(50) | YES | | NULL | |
| type | varchar(6) | | | friend | |
| regseconds | int(11) | | | 0 | |
| ipaddr | varchar(15) | YES | | NULL | |
| username | varchar(80) | | | | |
| port | varchar(5) | | | | |
+------------+-------------+------+-----+---------+----------------+
11 rows in set (0.01 sec)
mysql> select * from sip_buddies;
+----+------+----------+------------+---------+------------+--------+------------+------------+----------+------+
| id | name | context | defaultip | host | mailbox | type | regseconds | ipaddr | username | port |
+----+------+----------+------------+---------+------------+--------+------------+------------+----------+------+
| 1 | 2000 | from-sip | 10.1.2.192 | dynamic | 2000 | friend | 1123750161 | 10.1.2.192 | 2000 | 5060 |
| 2 | 2001 | from-sip | 10.1.2.220 | dynamic | 2001 | friend | 1123750159 | 10.1.1.220 | 2001 | 5080 |
+----+------+----------+------------+---------+------------+--------+------------+------------+----------+------+
2 rows in set (0.00 sec)
3.3 Asterisk Realtime Extensions
Following the configuration in extconfig.conf, we create extensions_table table and insert data to achieve the same setup in extensions.conf in section 1. A small trick regarding Dial data is to replace comma with pipe. You also need modify extensions.conf to assist Asterisk Realtime how to response.
mysql> CREATE TABLE `extensions_table` ( `id` int(11) NOT NULL auto_increment, `context` varchar(20) NOT NULL default '', `exten` varchar(20) NOT NULL default '', `priority` tinyint(4) NOT NULL default '0', `app` varchar(20) NOT NULL default '', `appdata` varchar(128) NOT NULL default '', KEY `id` (`id`) ) TYPE=MyISAM;
mysql> desc extensions_table;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | | MUL | NULL | auto_increment |
| context | varchar(20) | | | | |
| exten | varchar(20) | | | | |
| priority | tinyint(4) | | | 0 | |
| app | varchar(20) | | | | |
| appdata | varchar(128) | | | | |
+----------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> select * from extensions_table;
+----+----------+-------+----------+---------------+----------------+
| id | context | exten | priority | app | appdata |
+----+----------+-------+----------+---------------+----------------+
| 1 | from-sip | 2000 | 1 | Dial | SIP/2000|20 |
| 2 | from-sip | 2000 | 2 | Voicemail | u2000 |
| 3 | from-sip | 2000 | 102 | Voicemail | b2000 |
| 4 | from-sip | 2000 | 103 | Hangup | |
| 5 | from-sip | 2001 | 1 | Dial | SIP/2001|20 |
| 6 | from-sip | 2001 | 2 | Voicemail | u2001 |
| 7 | from-sip | 2001 | 102 | Voicemail | b2001 |
| 8 | from-sip | 2001 | 103 | Hangup | |
| 9 | from-sip | 2999 | 1 | VoicemailMain | ${CALLERIDNUM} |
+----+----------+-------+----------+---------------+----------------+
9 rows in set (0.00 sec)
The extensions.conf now s like:
# cat extensions.conf
[general]
static=yes
writeprotect=yes
[bogon-calls]
exten => _.,1,Congestion
[from-sip]
switch => Realtime/@
The switch syntax is ‘switch =>Realtime/[context]@[family][/options]’. The family name can be anything, just need to match the line in extconfig.conf. If context is not given, current context is default. If family is not given, family of ‘extension’ is default. In our case, ‘switch => Realtime/@’ is the same as ‘switch =>Realtime/from-sip@extensions’.
3.4 Asterisk Realtime Voicemail
Following the configuration in extconfig.conf, we create voicemail_users table and insert data to achieve the same setup in voicemail.conf in section 1. Now voicemail.conf is not important anymore. All the voicemail configurations are loaded from database in realtime. In case you modify entries in this table, Asterisk will load the new configuration in realtime.
mysql> CREATE TABLE `voicemail_users` ( `uniqueid` int(11) NOT NULL auto_increment, `customer_id` int(11) NOT NULL default '0', `context` varchar(50) NOT NULL default '', `mailbox` int(5) NOT NULL default '0', `password` varchar(4) NOT NULL default '0', `fullname` varchar(50) NOT NULL default '', `email` varchar(50) NOT NULL default '', `pager` varchar(50) NOT NULL default '', `stamp` timestamp(14) NOT NULL, PRIMARY KEY (`uniqueid`), KEY `mailbox_context` (`mailbox`,`context`) ) TYPE=MyISAM;
mysql> desc voicemail_users;
+-------------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------------------+----------------+
| uniqueid | int(11) | | PRI | NULL | auto_increment |
| customer_id | int(11) | | | 0 | |
| context | varchar(50) | | | | |
| mailbox | int(5) | | MUL | 0 | |
| password | varchar(4) | | | 0 | |
| fullname | varchar(50) | | | | |
| email | varchar(50) | | | | |
| page | varchar(50) | | | | |
| stamp | datetime | | | 0000-00-00 00:00:00 | |
+-------------+-------------+------+-----+---------------------+----------------+
9 rows in set (0.00 sec)
mysql> select * from voicemail_users;
+----------+-------------+---------+---------+----------+----------+-----------------------+------+---------------------+
| uniqueid | customer_id | context | mailbox | password | fullname | email | page | stamp |
+----------+-------------+---------+---------+----------+----------+-----------------------+------+---------------------+
| 1 | 2000 | default | 2000 | 4321 | Wei Kun | wei-kun@mediaring.com | | 0000-00-00 00:00:00 |
| 2 | 2001 | default | 2001 | 8765 | Wei Kun | kunwei2005@gmail.com | | 0000-00-00 00:00:00 |
+----------+-------------+---------+---------+----------+----------+-----------------------+------+---------------------+
2 rows in set (0.03 sec)
Comments
Post a Comment