Skip to main content

Asterisk Quickstart with Configuration Files and/or Realtime Database

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

Popular posts from this blog

spring 2.0 bean scope

singleton Scopes the bean definition to a single instance per Spring container (default). prototype Allows a bean to be instantiated any number of times (once per use). request Scopes a bean definition to an HTTP request. Only valid when used with a web capable Spring context (such as with Spring MVC). session Scopes a bean definition to an HTTP session. Only valid when used with a webcapableSpring context (such as with Spring MVC). global-session Scopes a bean definition to a global HTTP session. Only valid when used in a portlet context.

Crocs sandals

Suddenly one special looking sandals get popular. The brand is Crocs. It even opens a brand store at Marina Square. The design idea is from Dutch wooden shoes, I guess. A pair of Crocs sandals is sold at around SGD 50. The price is justified for what it is made of - Croslite. Based on Crocs website, "Croslite™, a proprietary Closed Cell Resin (PCCR) which is NOT plastic NOR rubber. Croslite™ is closed-cell in nature and anti-microbial, which virtually eliminates odor. it is an extraordinary impact absorbing resin material developed for maximum cushioning. its closed cell properties resist odor, inhibits bacterial and fungal growth and are non toxic. this versatile material can be worn next to skin and be cleaned with just soap and water." However, it really looks like made of plastic or rubber, and the design is unique. Replica comes. they are sold at SGD 20, SGD 10, SGD 5 depending on quality.

Singapore Girl Sex Clip Posted Online

A couple days ago, a sex video clip about a female Chinese graduate student Wang Ting Ting (王婷婷) was posted on the internet. Just as it is about to cool down, another sex video clip pops. It is a sex clip of a Singapore Nanyang Polytechnic student . The video clip was stored on her cell phone. Someone stole it and posted the video clip on the Internet. This is a breaking news. It is even reported on major Singapore and Malaysia newspapers. Now we have so many cool gadgets. It is a breeze to shoot photos/videos, and share them on the internet. Everyone, even dog, is on the internet waiting for breaking news. Be careful when you do something secret or stupid. It may appear all over the world, live!

No smoking sign

Watch out this sign before you light the cigarette up. SGD 1000 fine! However, I wonder if someone had really paid so much for violation.

Prostitutes in Singapore

Singapore is very realistic about this issue. Prostitution is legal. The famous red light zone is Geylang area. I heard Hong Kong officials are considering to legalize this business in HK also. Singaporeans are not allow to work as prostitute. Maybe also SPR. The prostitutes are mainly foreign workers from poor countries in SEA area, such as Indonesia, Thailand, India, Malaysia, not from China. They come under special 2-year working pass, and must pass the health exam, yes, to prevent STD such as HIV, AIDS and so on. The brothels are mixed with normal resident houses. The brothel's house number is red lighted. The price is ranged from SGD 50 to SGD 200. Illegal sex workers are also around, and in some massage clinics. A very recent fatal case and newly effective government policy make a special social group - Chinese accompanying mom for studying kids a hot media buzz again.