2

My scenario is: A SIP trunk is connected to a Kamailio server which is connected to multiple Asterisk servers locally and should load balance calls among these asterisk servers. How? There is a mysql database table inside Kamailio server which maps TO part of sip header of incoming calls to one of asterisks' IPs.

Kamailio should read sip header and search inside database and after getting IP, forward the call to the proper asterisk server.

For example, value of To inside incoming sip header is 123456 so kamailio does query database and finds number 123456 is inside 192.168.1.10 so call should be forwarded to server 192.168.1.10.

I have read multiple articles and Kamailio's help from it's website but couldn't find anything related to this scenario. Does anyone know how to write the route inside kamailio.cfg?

AmirA
  • 133
  • 2
  • 15

2 Answers2

1

There are some ways to do that. One of them is by using the dispatcher module.

This module offers SIP load balancer functionality and it can be used as a SIP traffic dispatcher. There are many load balancing and traffic dispaching algorithms that you can choose from, for example: round-robin, weight based load balancing, call load distribution, and hashing over SIP message attributes.

The module can be used as a stateless load balancer; it does not depend on any call state tracking module. It requires the TM module if you enable auto-discovery of active/inactive gateways.

It is very lightweight, therefore suitable for handling heavy SIP traffic. As the module has a small footprint and the ability to load balancing rules from a plain text file, it is suitable for embedded systems.

To make it work:

You need to add a dispatcher.list file with a list of the IPs of your asterisks like so:

1 sip:192.168.0.10 #asterisk 01
1 sip:192.168.0.11 #asterisk 02

Then before relaying the request you will do a ds_select_dst(1, 0);


Be sure that you specify the list file for your dispatcher module:

loadmodule "dispatcher.so"
modparam("dispatcher", "list_file", "/var/run/kamailio/dispatcher.list")

If you want to use a database instead of a file you can do that by specifying the db:

modparam("dispatcher", "db_url", "mysql://user:passwb@localhost/database")

Also, there are other parameters for specifying the database table etc.

You can read more in the kamailio dispatcher documentation

Community
  • 1
  • 1
sotoz
  • 3,100
  • 3
  • 34
  • 41
  • Thank you. How do I query database table? Could you give me a route sample for doing this? – AmirA Feb 07 '18 at 05:36
  • The dispatcher module will use the database automatically. The kamailio documentation has examples that you can get. If you need something specific, give us an example of your configuration file. Creating a route block for you without seeing first how your setup works it's not very useful I think. – sotoz Feb 07 '18 at 11:12
  • Ok. I have 5 asterisk voicemail servers with ip from `192.168.10.10` to `192.168.10.14`. In each server, I have specific range of mailboxes. These servers are connected to kamailio server which is connected to a sip trunk. I have incoming calls to kamailio with sip header `TO` equals mailbox number such as 33333333 which is inside one of asterisk servers. Now, I somehow want to map mailbox numbers to ip addresses in which they exist and forward call to that specific server to do voicemail related actions. So, in database I only need 2 columns one is mailbox number and the other is ip address. – AmirA Feb 07 '18 at 21:57
  • You have query your db using avpops(see my answer), after that use RELAY or DISPATCHER. don't forget overload protection, kamailio can do 50000 queries/second, while db usualy much less. – arheops Feb 09 '18 at 14:26
0

Just random or round-robin calling usualy done by dispatcher or loadbalance calling.

If you need query db, you choice is use AVPOPS module(which allow do custom query to db) and HTABLE for cache response.

avp_db_query("select password, ha1 from subscriber where username='$tu'",
    "$avp(678);$avp(679)");

http://www.opensips.org/html/docs/modules/1.7.x/avpops.html#id293960

For sure you have also do PIKE and DDoS protection, becuase db can do much less sql queries then kamailio and/or attacking tools like SIPP/SIPSack. That bring down your db and hard to restore.

arheops
  • 15,544
  • 1
  • 21
  • 27