Ubuntu 14.04 + Asterisk 11 + CDR-Stats

I offer a step-by-step guide on installing and configuring the Ubuntu server, Asterisk telephone exchange, and CDR-STATS call processing and analytics system.

Install Ubuntu


We install Ubuntu server 14.04 (LTS), downloading the distribution kit from the official site. During the installation, I pointed out SSH + LAMP + SAMBA in order to avoid the long installation process of the necessary components.

Log in to the server. The first thing I did was set up the midnight commander (I love the DOS interface, and it's convenient):

sudo apt-get install mc -y

You can skip the next step if you do not need a GUI for the server.

For convenience and in view of some features of the future server, I rolled up the GUI:

sudo apt-get install --no-install-recommends lubuntu-desktop

After installation:

sudo reboot 

At the end of the download, we see the classic graphical user interface.

We configure all the necessary network interfaces, server names and its Alias. (I will not give examples of settings, so as not to mislead people).

The next step is to install webmin on the server. I used the official documentation from webmin.com :
Using the Webmin APT repository
If you like to install and update Webmin via APT, edit the /etc/apt/sources.list file on your system and add the lines:

deb http://download.webmin.com/download/repository sarge contrib
deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib

You should also fetch and install my GPG key with which the repository is signed, with the commands:

cd /root
wget http://www.webmin.com/jcameron-key.asc
apt-key add jcameron-key.asc

You may need to run this command as root.

sudo apt-key add jcameron-key.asc

You will now be able to install with the commands:

sudo apt-getupdate
sudo apt-get install webmin

All dependencies should be resolved automatically.

After completing all the steps, webmin will be available at: your-server-ip-or-name : 10000

It is time to configure the rules for iptables. On the web, I found a very good article on securing an Asterisk server using iptables. Source: blog.ls20.com/securing-your-asterisk-voip-server-with-iptables . The article is in English, but everything is simple enough for understanding.

Now a very important point! Do not import rules into iptables in a standard way! This will not work:

iptables-restore < filename

Will not work.

You must perform the following steps:

Find the file /etc/iptables.up.rules and copy it somewhere (so to speak, make a backup), and replace the file etc / iptables.up.rules with the one you received as a result of the previous steps.
If you did everything correctly, then in webmin Networking / Linux Firewall you will see all the necessary rules. I put available updates for installed components:

sudo apt-getupdate && sudo apt-get upgrade –y && sudo reboot

So, the server is installed, configured and updated

Install Asterisk

It is time to install asterisk. In the current configuration, I will use Asterisk 11:

cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-11-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/dahdi-linux-complete-current.tar.gz
wget http://downloads.asterisk.org/pub/telephony/libpri/libpri-1.4-current.tar.gz

Unpack it all:

tar-zxvfasterisk-11-current.tar.gztar-zxvfdahdi-linux-complete-current.tar.gztar-zxvflibpri-1.4-current.tar.gz

We will put some packages, if they were not installed earlier:

sudo apt-get installbuild-essential libncurses5-dev openssl libxml2-dev

There are two ways further:

1st way:

run the script for installing dependent packages for the Asterisk server to work:

sudo /usr/src/asterisk-11.12.0/contrib/scripts/install_prereq install

The script puts all the necessary dependencies and packages, but a lot of unnecessary.

2nd way:

Installing from the sources
Before this action, I did a checkpoint on my hyper-v server, in order to avoid so to speak:

sudo apt-get install libncurses5-dev openssl libssl-dev zlib1g zlib1g-dev mpg123 linux-headers-`uname -r`build-essential mysql-server libmysqlclient15-dev php5 php5-cli php5-mysql php5-gd php-pear apache2 curl sox bison flex cpp g++ gcc make libauthen-pam-perl libio-pty-perl libnet-ssleay-perl libxml2 libxml2-dev libaudiofile-dev subversion libsqlite3-dev

OK. packages installed. Now is the time to deal with installing Asterisk itself.

We put DAHDI:

cd /usr/src/dahdi-linux-complete-2.10.0+2.10.0
sudo make all
sudo make install
sudo make config

We put LibPRI:

cd /usr/src/libpri-1.4.15
sudo make
sudo make install

Well, Asterisk itself:

cd /usr/src/asterisk-11.12.0
./configure
sudo make menuselect

We select the necessary components. Since I am going to store the CDR in the MySQL database and fasten the CDR-STATS web-interface, I note that I have included the modules format_mp3 , app_mysql , cdr_mysql and res_config_mysql .

Otherwise, with the exception of sounds (and I need en, en-gb and ru) everything remains standard:

sudo make install

During installation, when choosing format_mp3 you need to do:

contrib/scripts/get_mp3_source.sh

Continue the installation:

sudo make install

Here can be your advertising. We drink coffee, tea, cookies and expect the end of the installation process.

The next step is samples and config. Test configs:

sudo make samples

To not create an autorun script:

sudo make config

Ffuuhh! Put. We start:

sudo asterisk start

We connect:

sudo rasterisk

Check the version of DAHDI:

*CLI>dahdi showversion

Check the version of LibPRI:

*CLI>pri showversion
*CLI>exit

So, set, looked. At the moment, asterisk is launched as root, which, as it were, is not comme il faut. Therefore, we do the following:

sudo service asterisk stop

Create the asterisk user and add him to the asterisk group:

sudo adduser --system --group --home /var/lib/asterisk --no-create-home --gecos "Asterisk PBX" asterisk

Created, change the rights to the necessary directories:

sudo chown -R asterisk:asterisk /var/lib/asterisk
sudo chown -R asterisk:asterisk /var/log/asterisk
sudo chown -R asterisk:asterisk /var/run/asterisk
sudo chown -R asterisk:asterisk /var/spool/asterisk
sudo chown -R asterisk:asterisk /usr/lib/asterisk
sudo chown -R asterisk:asterisk /dev/dahdi
sudo chmod -R u=rwX,g=rX,o= /var/lib/asterisk
sudo chmod -R u=rwX,g=rX,o= /var/log/asterisk
sudo chmod -R u=rwX,g=rX,o= /var/run/asterisk
sudo chmod -R u=rwX,g=rX,o= /var/spool/asterisk
sudo chmod -R u=rwX,g=rX,o= /usr/lib/asterisk
sudo chmod -R u=rwX,g=rX,o= /dev/dahdi
sudo chown -R root:asterisk /etc/asterisk
sudo chmod -R u=rwX,g=rX,o= /etc/asterisk

In the / etc / default / asterisk file, we uncomment two lines:

AST_USER=«asterisk»
AST_GROUP=«asterisk»

We start asterisk:

sudo service asterisk start

We look from which user Asterisk started:

ps aux|grep aster

And you should see something like this:
asterisk 2793 3.0 0.8 2539536 26540? Ssl 20:21 0:00 / usr / sbin / asterisk -U asterisk -G asterisk

Did you see? So everything is ok. Now we will teach asterisk to write CDR in MySQL.

We put for starters:

sudo apt-get install mysql-client

Log in to mysql:

sudo mysql -p

Enter password:
Welcome to the MySQL monitor. Commands end with; or \ g.
mysql>

Create a base:

mysql> createdatabase asteriskcdrdb;

Create the “cdr” table in the “asteriskcdrdb” database, with the following structure:

mysql> use asteriskcdrdb;
mysql>CREATETABLE`cdr` (
`acctid`int(11) NOTNULL AUTO_INCREMENT, 
`src`varchar(80) DEFAULTNULL, 
`dst`varchar(80) DEFAULTNULL,  
`calldate` datetime NOTNULL, 
`clid`varchar(80) DEFAULTNULL, 
`dcontext`varchar(80) DEFAULTNULL, 
`channel`varchar(80) DEFAULTNULL, 
`dstchannel`varchar(80) DEFAULTNULL, 
`lastapp`varchar(80) DEFAULTNULL, 
`lastdata`varchar(80) DEFAULTNULL, 
`duration`int(10) unsignedDEFAULTNULL, 
`billsec`int(10) unsignedDEFAULTNULL, 
`disposition`varchar(80) NOTNULLDEFAULT'', 
`amaflags`varchar(80) DEFAULTNULL, 
`accountcode`int(10) unsignedDEFAULTNULL, 
`uniqueid`varchar(32) DEFAULTNULL, 
`userfield`varchar(80) DEFAULTNULL, 
`import_cdr` tinyint(4) NOTNULLDEFAULT'0', 
PRIMARY KEY (`acctid`) 
) ENGINE=InnoDBDEFAULTCHARSET=utf8;

Create an asterisk user with the password 'asterisk':

mysql> CREATEUSER asterisk_user IDENTIFIEDBY'asterisk_password';

Now we give access for the user “asterisk” with the password “asterisk ″ to the database“ asteriskcdrdb ”only with localhost:

mysql> grant all on asteriskcdrdb.* to'asterisk'@'localhost'identifiedby'asterisk';
mysql> flushprivileges;

Now we will tell our Asterisk to write CDR to the database. To do this, uncomment and change the lines in the file “/etc/asterisk/cdr_mysql.conf”:

[global]
hostname=localhost
dbname=asteriskcdrdb
table=cdr
password=asterisk
user=asterisk
sock=/var/lib/mysqld/mysqld.sock

To apply the changes, reboot the cdr_mysql.so module:

sudo rasterisk
*CLI>moduleunloadcdr_mysql.so

Unloaded cdr_mysql.so
  == Unregistered 'mysql' CDR backend

*CLI> moduleloadcdr_mysql.so

Loaded cdr_mysql.so
  == Parsing '/etc/asterisk/cdr_mysql.conf': Found
    -- Found alias startfor column calldate
 Loaded cdr_mysql.so => (MySQL CDR Backend)

To make sure that we did everything correctly, in the Asterisk console, enter:

*CLI> cdr mysql status

Connected to asterisk on socket file /var/lib/mysql/mysql.sock using table cdr for0 hours, 2 minutes, 7 seconds.
Wrote 0 records since last restart.

If you have the same message, then everything is configured correctly.

Installing CDR-STATS

We will use the instruction . Everything would seem simple:

wget –no-check-certificate https://raw.github.com/Star2Billing/cdr-stats/master/install/install-cdr-stats-asterisk.sh -O install-cdr-stats-asterisk.sh
bash ./install-cdr-stats-asterisk.sh

But, the devil, as usual, is in the details. You will not be able to install CDR-stats in this way, because the script will fail with the following error:

bash ./install-cdr-stats-asterisk.sh
./install-cdr-stats-asterisk.sh: line 1: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 2: !DOCTYPE: No such file or directory
./install-cdr-stats-asterisk.sh: line 3: html: No such file or directory
./install-cdr-stats-asterisk.sh: line 4: head: No such file or directory
./install-cdr-stats-asterisk.sh: line 5: title: No such file or directory
./install-cdr-stats-asterisk.sh: line 6: meta: No such file or directory
./install-cdr-stats-asterisk.sh: line 7: meta: No such file or directory
./install-cdr-stats-asterisk.sh: line 8: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 9: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 10: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 11: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 12: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 13: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 14: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 15: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 16: $'\r': command not found
./install-cdr-stats-asterisk.sh: line 17: meta: No such file or directory
./install-cdr-stats-asterisk.sh: line 18: meta: No such file or directory
./install-cdr-stats-asterisk.sh: line 19: meta: No such file or directory
./install-cdr-stats-asterisk.sh: line 20: base: No such file or directory
./install-cdr-stats-asterisk.sh: line 21: link: No such file or directory
./install-cdr-stats-asterisk.sh: line 22: link: No such file or directory
./install-cdr-stats-asterisk.sh: line 23: link: No such file or directory
./install-cdr-stats-asterisk.sh: line 24: meta: No such file or directory
./install-cdr-stats-asterisk.sh: line 25: meta: No such file or directory
./install-cdr-stats-asterisk.sh: line 26: link: No such file or directory
./install-cdr-stats-asterisk.sh: line 27: syntax error near unexpected token `<'
./install-cdr-stats-asterisk.sh: line 27: `

As we can see, the text above contains nonsense like! DOCTYPE. I opened this script and saw various incomprehensible things. But in the end the script itself is purposeful. We cut out all unnecessary, leaving only the body of the script. Here is its text:

Do not modify the following text to avoid problems!

#!/bin/bash## CDR-Stats License# http://www.cdr-stats.org## This Source Code Form is subject to the terms of the Mozilla Public# License, v. 2.0. If a copy of the MPL was not distributed with this file,# You can obtain one at http://mozilla.org/MPL/2.0/.## Copyright (C) 2011-2012 Star2Billing S.L.## The Initial Developer of the Original Code is# Arezqui Belaid <info@star2billing.com>## To download and run the script on your server :# cd /usr/src/ ; rm install-cdr-stats-asterisk.sh ; wget --no-check-certificate https://raw.github.com/Star2Billing/cdr-stats/master/install/install-cdr-stats-asterisk.sh -O install-cdr-stats-asterisk.sh; bash install-cdr-stats-asterisk.sh#
BRANCH='master'
INSTALL_DIR='/usr/share/cdr_stats'#INSTALL TYPE (ASTERISK or FREESWITCH)
INSTALL_TYPE='ASTERISK'
INSTALLMODE='GENTLE'# Set to FULL to update Selinux, Firewall, etc if not Set to GENTLEecho""echo""echo"> > > This is only to be installed on a fresh new installation of CentOS 6.2 or Ubuntu LTS 10.04 / 12.04! < < <"echo""echo"It will install CDR-Stats on your server"echo"Press Enter to continue or CTRL-C to exit"echo""read TEMP
#Get Scripts dependenciescd /usr/src/
wget --no-check-certificate https://raw.github.com/Star2Billing/cdr-stats/$BRANCH/install/bash-common-functions.sh -O bash-common-functions.sh
wget --no-check-certificate https://raw.github.com/Star2Billing/cdr-stats/$BRANCH/install/cdr-stats-functions.sh -O cdr-stats-functions.sh
#Include general functionssource bash-common-functions.sh
source cdr-stats-functions.sh
#Identify the OS
func_identify_os
#Request the user to accept the license
func_accept_license_mplv2
#run install menu
run_menu_cdr_stats_install

We launch:

bash install-cdr-stats-asterisk.sh

The script again fails with an error:
This script is only intended to run on Ubuntu LTS 10.04 / 12.04 or CentOS 6.X

Poking around the Internet for a second did not bring me closer to solving this issue, so I went the other way. Thoughtful smoking of the script and substances (coffee and cigarettes) led me to the following solution:

cd /etc/

Open the lsb-release file and see:
DISTRIB_CODENAME = trusty

Remember. Now:

cd /usr/src/

Open the bash-common-functions.sh file. And in it we look at the function:

func_identify_os() {
    if [ -f /etc/debian_version ] ; then
        DIST='DEBIAN'if [ "$(lsb_release -cs)" != "lucid" ] && [ "$(lsb_release -cs)" != "precise" ]; thenecho"This script is only intended to run on Ubuntu LTS 10.04 / 12.04 or CentOS 6.X"exit 255

We see two options: lucid and precise. I chose precise (Ubuntu 12.04 LTS). And do the following:

In the file:
/ etc / lsb-release change trusty to precise

Further:

cd /usr/src
bash install-cdr-stats.sh

It will install CDR-Stats on your server
Press Enter to continue or CTRL-C to exit

Press Enter
To the question:
I agree to be bound by the terms of the license - [YES / NO]

We say:

YES

The installer starts.
> CDR-Stats Installation Menu
======================================
1) Install All
2) Install CDR-Stats Web Frontend
3) Install CDR-Stats Backend / CDR-Stats-Celery
4) Install MongoDB
0) Quit
(0-4): 1

We select menu item number 1. And wait until it is installed. During installation, mongodb and postgresql will be installed. Next, we will be asked to configure the connection to MySQL:

Enter database settings for Asterisk ...
ERROR 1045 (28000): Access denied for user 'root' @ 'localhost' (using password: YES)
Configure Mysql Settings to connect to the Asterisk CDR database ...
Enter Mysql hostname (default: localhost)
localhost
Enter Mysql port (default: 3306)
3306
Enter Mysql Username (default: root)
asterisk
Enter Mysql Password (default: password)
asterisk
Enter Database name (default: asteriskcdrdb)
asteriskcdrdb
We will now add a Primary Key to your CDR database
We advice you to first backup your database prior continuing

Next, you will be asked to create a super admin for cdr-stats:
Create a super admin user ...
Username (leave blank to use 'root'):
E-mail address: your-email@server.com
Password:
Password (again):
Superuser created successfully.

We complete the installation with the button 0. And hurray!
your-server-ip-or-name : 8008
Available. We go and admire.

The following error may occur:
No data can be found in your collections, please make sure the import of data is working correctly.

What are we doing? Stop the CDR-stats:

sudo service cdr-stats-celeryd stop

We go in mongo:

mongo

We perform the following actions:

usecdr-stats;
db.monthly_analytic.remove({});
db.daily_analytic.remove({});
db.aggregate_world_report.remove({});
db.aggregate_result_cdr_view.remove({});
db.aggregate_hourly_country_report.remove({});
db.cdr_common.remove({});

Exit: Ctrl + D
Go to mysql:

sudo mysql -p 

mysql> use asteriskcdrdb;
mysql> update cdr SET import_cdr = 0;

Query OK, 0 rows affected (0.24 sec)
Rows matched: 3 Changed: 0 Warnings: 0

I currently had 3 CDR entries. Exit mysql: Ctrl + C

cd /usr/share/cdr-stats

Open the file settings_local.py and look for the line:

LOCAL_SWITCH_IP = "некое значение"

Change it to 'localhost':

LOCAL_SWITCH_IP = 'localhost'

Save the changes and run cdr-stats:

sudo service cdr-stats-celeryd start

your-server-ip-or-name : 8008

And everything starts to work.

Another important point! If you have several telephony providers or you use non-standard prefixes for accessing a local or international line, you must specify them in cdr-stats. This is done here:

/usr/share/cdr-stats/setting_local.py

Line:
PREFIX_TO_IGNORE = "+, 00,000,0000,00,000,011,55555,99999"

Do not change the current values, leave it as it is, and add yours to the end of the line.
For example, like this:

PREFIX_TO_IGNORE = "+,00,000,0000,00000,011,55555,99999,8,810,9,910"

Save the changes to the file and restart cdr-stats:

sudo service cdr-stats-celeryd restart

Now CDR-STATS will correctly process all calls, as you can see by visiting the corresponding menu item.

Also popular now: