База данных Asterisk

Материал из Xgu.ru

Перейти к: навигация, поиск

Содержание

[править] Через консоль

$ asterisk -rv
> database put mobile 3103 3102

[править] В диалплане

Прочитать из базы:

${DB(mobile/${CALLERID(num))}

Записать в базу:

exten => 8501,1,Read(redirector,/var/tmp/asterisk/record15)
exten => 8501,n,Set(DB(mobile/${CALLERID(num)})=${redirector})

[править] Механизм Realtime

Как брать пользователей из базы данных.

Пример использования:

[править] Хранение CDR-записей в MySQL

 cd /usr/src
 wget http://downloads.digium.com/pub/asterisk/releases/asterisk-addons-1.4.7
 tar xvfz asterisk-addons-1.4.7.tar.gz
 cd asterisk-addons-1.4.7/cdr/
 vi cdr_addon_mysql.c
 add #define MYSQL_LOGUNIQUEID after #define DATE_FORMAT “%Y-%m-%d  %T”

 cd ..
 ./configure
 make menuselect

Проверяем, загружен ли модуль:

 —> 1. Applications
    [*] 1. app_addon_sql_mysql
    2. Call Detail Recording
    [*] 1. cdr_addon_mysql
    3. Channel Drivers
    4. Format Interpreters
    5. Resource Mod
 

потом нажимаем q (quit) и S (save)

  make && make install

 $ vim /etc/asterisk/cdr.conf
 
 [general]
 enable=yes

 $ vim /etc/asterisk/cdr_mysql.conf

 [global]
 hostname=localhost
 dbname=asterisk
 table=cdr
 password=pass01234
 user=asterisk
 port=3306
 ;sock=/tmp/mysql.sock
 ;userfield=1

 vi /etc/asterisk/modules.conf
 load => cdr_addon_mysql.so

Создаем БД, таблицы и разрешения.

 # mysql -u root -p
 Enter password:
 Welcome to the MySQL monitor. Commands end with ; or g.
 Your MySQL connection id is 2
 Server version: 5.0.32-Debian_7etch5 Debian etch distribution

 Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.
  
 mysql> create database asterisk;
 mysql> use 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 ”,
 userfield varchar(255) NOT NULL default ”,
 uniqueid varchar(32) NOT NULL default ”
 );
  
 ALTER TABLE cdr ADD INDEX ( calldate );
 ALTER TABLE cdr ADD INDEX ( dst );
 ALTER TABLE cdr ADD INDEX ( uniqueid );

  При необходимости можно добавить больше индексов.

Предоставляем права доступа к таблице пользователю asterisk:  

 
 mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
 -> on asterisk.*
 -> to 'asterisk'@'localhost';
 Query OK, 0 rows affected (0.00 sec)

Проверяем соединение:

 # mysql -h localhost -u asterisk -p asterisk
 mysql> show tables;
 +——————–——–——–——–———–+
 | Tables_in_asterisk |
 +————————–——–——–——–—–+
 | cdr                |
 +————————–——–——–——–—–+
 1 row in set (0.00 sec)
 
 mysql> desc cdr;
 +————-+————–+——+—–+———————+——-+
 | Field | Type | Null | Key | Default | Extra |
 +————-+————–+——+—–+———————+——-+
 | calldate | datetime | NO | MUL | 0000-00-00 00:00:00 | |
 | clid | varchar(80) | NO | | | |
 | src | varchar(80) | NO | | | |
 | dst | varchar(80) | NO | MUL | | |
 | dcontext | varchar(80) | NO | | | |
 | channel | varchar(80) | NO | | | |
 | dstchannel | varchar(80) | NO | | | |
 | lastapp | varchar(80) | NO | | | |
 | lastdata | varchar(80) | NO | | | |
 | duration | int(11) | NO | | 0 | |
 | billsec | int(11) | NO | | 0 | |
 | disposition | varchar(45) | NO | | | |
 | amaflags | int(11) | NO | | 0 | |
 | accountcode | varchar(20) | NO | | | |
 | userfield | varchar(255) | NO | | | |
 | uniqueid | varchar(32) | NO | MUL | | |
 +————-+————–+——+—–+———————+——-+
 16 rows in set (0.00 sec)

  Перезапускаем Астериск, подключаемся и проверяем, видит ли он MySQL-сервер:

 # asterisk -r
 Asterisk 1.4.20, Copyright (C) 1999 - 2008 Digium, Inc. and others.
 Created by Mark Spencer
 Asterisk comes with ABSOLUTELY NO WARRANTY; type ‘core show  warranty’ for details.
 This is free software, with components licensed under the GNU  General Public
 License version 2 and other licenses; you are welcome to  redistribute it under
 certain conditions. Type ‘core show license’ for details.
 =========================================================================
 Connected to Asterisk 1.4.20 currently running on asterisk88 (pid =  26648)
 Verbosity is at least 3
 ippbx01*CLI> stop now
 ippbx01*CLI>
 Disconnected from Asterisk server
 
 # safe_asterisk
 
 # asterisk -r
 Asterisk 1.4.20, Copyright (C) 1999 - 2008 Digium, Inc. and others.
 Created by Mark Spencer
 Asterisk comes with ABSOLUTELY NO WARRANTY; type ‘core show  warranty’ for details.
 This is free software, with components licensed under the GNU  General Public
 License version 2 and other licenses; you are welcome to  redistribute it under
 certain conditions. Type ‘core show license’ for details.
 =========================================================================
 Connected to Asterisk 1.4.20 currently running on asterisk88 (pid =  26880)
 Verbosity is at least 3
 ippbx01*CLI> cdr mysql status
 Connected to asterisk@localhost, port 3306 using table cdr for 15  seconds.
 Wrote 0 records since last restart.
 ippbx01*CLI> quit