Asterisk Simple Realtime example

From open-voip.org

Jump to: navigation, search

So we have installed Asterisk and we have a working PBX, we can make calls between extensions and from/to our sip provider, all working with the sip.conf and extensions.conf files:

Now we will transfer these conf files to the Asterisk RealTime engine using mysql.

Contents

Creating the database

check that the mysql is running, and start it if needed:

 /etc/init.d/mysqld status
 /etc/init.d/mysqld start

in order to start mysql after reboot check the status

 chkconfig | grep mysql

If 3,4,5 are "off" turn to "on"

 chkconfig --level 345 mysqld on

set your mysql root password (in needed):

 mysqladmin -u root password password

go into the mysql CLI:

  mysql -u root -p

insert the "password" Create the database if not exist, we we call it "asteriskrealtime"

 create database if not exists `asteriskrealtime`;

change to the asteriskrealtime database:

 use asteriskrealtime;

now create the table that will configure the sip users instead of the sip.cong:

 CREATE TABLE `sip_buddies` (
   `id` int(11) NOT NULL auto_increment,
   `name` varchar(80) NOT NULL,
   `callerid` varchar(80) default NULL,
   `defaultuser` varchar(80) NOT NULL,
   `regexten` varchar(80) NOT NULL,
   `secret` varchar(80) default NULL,
   `accountcode` varchar(20) default NULL,
   `context` varchar(80) default NULL,
   `amaflags` varchar(7) default NULL,
   `callgroup` varchar(10) default NULL,
   `canreinvite` char(3) default 'yes',
   `defaultip` varchar(15) default NULL,
   `dtmfmode` varchar(7) default NULL,
   `fromuser` varchar(80) default NULL,
   `fromdomain` varchar(80) default NULL,
   `fullcontact` varchar(80) default NULL,
   `host` varchar(31) NOT NULL,
   `insecure` varchar(4) default NULL,
   `language` char(2) default NULL,
   `mailbox` varchar(50) default NULL,
   `md5secret` varchar(80) default NULL,
   `nat` varchar(5) NOT NULL default 'no',
   `deny` varchar(95) default NULL,
   `permit` varchar(95) default NULL,
   `mask` varchar(95) default NULL,
   `pickupgroup` varchar(10) default NULL,
   `port` varchar(5) NOT NULL,
   `qualify` char(3) default NULL,
   `restrictcid` char(1) default NULL,
   `rtptimeout` char(3) default NULL,
   `rtpholdtimeout` char(3) default NULL,
   `type` varchar(6) NOT NULL default 'friend',
   `disallow` varchar(100) default 'all',
   `allow` varchar(100) default 'g729;ilbc;gsm;ulaw;alaw',
   `musiconhold` varchar(100) default NULL,
   `regseconds` int(11) NOT NULL default '0',
   `ipaddr` varchar(15) NOT NULL,
   `cancallforward` char(3) default 'yes',
   `lastms` int(11) NOT NULL,
   `useragent` char(255) default NULL,
   `regserver` varchar(100) default NULL,
   PRIMARY KEY  (`id`),
   UNIQUE KEY `name` (`name`),
   KEY `name_2` (`name`)
 ) ENGINE=MyISAM AUTO_INCREMENT=890 DEFAULT CHARSET=latin1;


now create the table that will configure the dial-plan instead of the extensions.cong:

 CREATE TABLE `extensions` (
   `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 ,
   PRIMARY KEY  (`context`,`exten`,`priority`),
   KEY `id` (`id`)
 ) ENGINE=MyISAM AUTO_INCREMENT=248 DEFAULT CHARSET=latin1;

other tips

realtime script is at:

/root/asterisk-12.4.0/contrib/realtime/mysql

sometimes the default mysql server setting is to listen on localhost only. change the /etc/mysql/my.cnf to listen on all IPs:

#bind-address           = 127.0.0.1

Configure asterisk for RealTime:

create the res_mysql.conf file in /etc/asterisk vi /etc/asterisk/res_mysql.conf enter the following:

 [general]
 dbhost = 127.0.0.1
 dbname = asteriskrealtime
 dbuser = root
 dbpass = password
 dbport = 3306

save the old extconfig.conf

 cp /etc/asterisk/extconfig.conf /etc/asterisk/extconfig.conf_orig

save the following in the extconfig.conf, in order to tell asterisk to use mysql for the sip users and the dialplan (extensions):

 [settings]
 sippeers => mysql,general,sip_buddies
 extensions => mysql,general,extensions

Cahnge the extensions.conf so th "from-sip" context will be taken from the mysql:

 [general]
 [globals]
 ;
 [from-sip]
 switch =>Realtime

check the real time status by:

 asterisk -r
 realtime mysql status

Add data to the tables

now we will add the sip users to the mysql (remove the users part from the sip.conf file)

 mysql -u root -p
 password                   -> enter password when requested, in this example: "password".
 use asteriskrealtime;

insert the users to the sip_buddies table:

 INSERT INTO asteriskrealtime.sip_buddies (
 NAME, defaultuser, secret, context, HOST, nat, qualify, TYPE)
 VALUES 	(
 '1000', '1000', '1234', 'from-sip', 'dynamic', 'yes', 'no', 'friend');
 INSERT INTO asteriskrealtime.sip_buddies (
 NAME, defaultuser, secret, context, HOST, nat, qualify, TYPE)
 VALUES (
 '2000', '2000', '1234', 'from-sip', 'dynamic', 'yes', 'no', 'friend');
 INSERT INTO asteriskrealtime.sip_buddies (
 NAME, defaultuser, secret, context, HOST, nat, qualify, TYPE)
 VALUES 	(
 'deltathree', '11111111', '1234', 'from-sip', 'sipauth.deltathree.com', 'yes', 'no', 'friend');

now we will add the dialplan of the "from-sip" context to the mysql (instead of the extensions.conf)

 insert  into `extensions`(`id`,`context`,`exten`,`priority`,`app`,`appdata`) values 
 (248,'from-sip','12121111111',1,'Dial','SIP/1000|60'),
 (249,'from-sip','12122222222',1,'Dial','SIP/1000|60'),
 (250,'from-sip','_X.',1,'Dial','SIP/${EXTEN}|30'),
 (251,'from-sip','_9X.',1,'Dial','SIP/${EXTEN:1}@deltathree'),
 (252,'from-sip','_X.',2,'hangup',);

test your new real time asterisk setup: Configure the Endpoints: (X-lite in this example):

 File:Simple_example1.gif  File:Simple_example2.gif

make call between the extensions and to the sip provider (you need real user of the provider for that)

Personal tools