LOAD-BALANCER module
Admin Guide
Section titled “Admin Guide”Overview
Section titled “Overview”The Load-Balancer module comes to provide traffic routing based on load. Shortly, when OpenSIPS routes calls to a set of destinations, it is able to keep the load status (as number of ongoing calls) of each destination and to choose to route to the less loaded destination (at that moment). OpenSIPS is aware of the capacity of each destination - it is preconfigured with the maximum load accepted by the destinations. To be more precise, when routing, OpenSIPS will consider the less loaded destination not the destination with the smallest number of ongoing calls, but the destination with the largest available slot.
Also the module has the capability to do failover (to try a new destination if the selected one done not responde), to keep state of the destinations (to remember the failed destination and avoid using them agai) and to check the health of the destination (by doing probing of the destination and auto re-enabling).
How it works
Section titled “How it works”Please refer to the Load-Balancer tutorial from the OpenSIPS website: /tutorials/loadbalancing/.
Probing and Disabling destinations
Section titled “Probing and Disabling destinations”The module has the capability to monitor the status of the destinations by doing SIP probing (sending SIP requests like OPTIONS).
For each destination, you can configure what kind of probing should be done (probe_mode column):
- (0) - no probing at all;
- (1) - probing only when the destination is in disabled mode (disabling via MI command will competely stop the probing also). The destination will be automatically re-enabled when the probing will succeed next time;
- (2) - probing all the time. If disabled, the destination will be automatically re-enabled when the probing will succeed next time;
A destination can become disabled in two ways:
- script detection
- MI command
Dependencies
Section titled “Dependencies”OpenSIPS Modules
Section titled “OpenSIPS Modules”The following modules must be loaded before this module:
- Dialog - Dialog module
- Dialog - TM module (only if probing enabled)
- database - one of the DB modules
External Libraries or Applications
Section titled “External Libraries or Applications”The following libraries or applications must be installed before running OpenSIPS with this module loaded:
- None.
Exported Parameters
Section titled “Exported Parameters”db_url (string)
Section titled “db_url (string)”The URL pointing to the database where the load-balancing rules are stored.
Default value is “mysql://opensips:opensipsrw@localhost/opensips”.
...modparam("load_balancer", "db_url", "dbdriver://username:password@dbhost/dbname")...db_table (string)
Section titled “db_table (string)”The name of the DB table containing the load-balancing rules.
Default value is “load_balancer”.
...modparam("load_balancer", "db_table", "lb")...probing_interval (integer)
Section titled “probing_interval (integer)”How often (in seconds) the probing of a destination should be done. If set to 0, the probing will be disabled as functionality (for all destinations)
Default value is “30”.
...modparam("load_balancer", "probing_interval", 60)...probing_method (string)
Section titled “probing_method (string)”The SIP method to be used for the probing requests.
Default value is ""OPTIONS"".
...modparam("load_balancer", "probing_method", "INFO")...probing_from (string)
Section titled “probing_from (string)”The FROM SIP URI to be advertised in the SIP probing requests.
Default value is ""sip:prober@localhost"".
...modparam("load_balancer", "probing_from", "sip:pinger@192.168.2.10")...probing_reply_codes (string)
Section titled “probing_reply_codes (string)”A comma separted list of SIP reply codes. The codes defined here will be considered as valid reply codes for probing messages, apart for 200.
Default value is “NULL”.
...modparam("load_balancer", "probing_reply_codes", "501, 403")...lb_define_blacklist (string)
Section titled “lb_define_blacklist (string)”Defines a blacklist based on a lb group. This list will contain the IPs (no port, all protocols) of the destinations matching the given group.
Multiple instances of this param are allowed.
Default value is “NULL”.
...modparam("load_balancer", "lb_define_blacklist", "list= 1,4,3")modparam("load_balancer", "lb_define_blacklist", "blist2= 2,10,6")...Exported Functions
Section titled “Exported Functions”load_balance(grp,resources[,alg])
Section titled “load_balance(grp,resources[,alg])”The function performs load-balancing over the available destinations in order to find the less loaded destination that can provide the requested resources and belong to a requested group.
Meaning of the parameters is as follows:
-
grp - group id for the destinations; the destination may be grouped in several groups you can you for differnet scenarios; this can be a number or a variable containing a numerical value.
-
resources - string containing a semi-colon list of resources required by the current call.
-
algorithm - algorithm used for computing the available load on the system:
- 0 - Absolute value - the effective available load ( maximum_load - current_load) is used in computing the load of each pear/resource.
- 1 - Relative value - the relative available load (how many percentages are free) is used in computing the load of each pear/resource. If missing, the default algorithm in 0.
Function returns true is a new destination URI is set pointing to the selected destiantion. NOTE that the RURI will not be changed by this function.
This function can be used from REQUEST_ROUTE, BRANCH_ROUTE and FAILURE_ROUTE.
...if (load_balance("1","trascoding;conference")) { # dst URI points to the new destination xlog("sending call to $du\n"); t_relay(); exit;}...lb_disable()
Section titled “lb_disable()”Marks as disabled the last destination that was used for the current call. The disabling done via this function will prevent the destination to be used for usage from now on. The probing mechanism can re-enable this peer (see the probing section in the begining)
This function can be used from REQUEST_ROUTE and FAILURE_ROUTE.
...if (t_check_status("(408)|(5[0-9][0-9])")) { lb_disable(); if ( load_balance("1","pstn") ) { t_on_failure("1"); xlog("-----------new dst is $du\n"); t_relay(); } else { t_reply("500","Error"); }}
...lb_is_destination(ip,port[,group[,active]])
Section titled “lb_is_destination(ip,port[,group[,active]])”Checks if the given IP and PORT belongs to a destination configured in the load-balancer’s list. Returns true if found and active (see the “active” parameter).
This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE, BRANCH_ROUTE and LOCAL_ROUTE.
Meaning of the parameters is as follows:
- ip and port - IP and PORT to be checked (any kind of variables are allowed, but take care as the PORT variables should have an integer value); A value 0 for the port means “any” - will match any port.
- group - in what LB group the destination should be looked for; If not specified, the search will be in all groups.
- active - if “1”, the search will be performed only over “active” (not disabled) destinations. If missing, the search will consider any kind of destinations.
...if (lb_is_destination("$si","$sp") ) { # request from a LB destination}...lb_count_call(ip,port,grp,resources)
Section titled “lb_count_call(ip,port,grp,resources)”The function counts the current call as load for a given destination with some given resources. Note that this call is not going through the load-balancing logic (there are not routing decision taken for the call); it is simply counted by LB as ongoing call for a destination;
Meaning of the parameters is as follows:
- ip and port - IP and PORT to identify the destination the call has to be counted for.
- grp - group id for the destinations; if no knows, “-1” will mean all groups.
- resources - string containing a semi-colon list of resources required by the current call.
Function returns true if the call was properly taken into consideration for estimating the load on the destination.
This function can be used from REQUEST_ROUTE, BRANCH_ROUTE and FAILURE_ROUTE.
...# count as load also the calls orgininated by lb destinationsif (lb_is_destination("$si","$sp") ) { # inbound call from destination lb_count_call("$si","$sp","-1","conference");} else { # outbound call to destinations if ( !load_balance("1","conference") ) { send_reply("503","unavailable"); exit(); } # dst URI points to the new destination xlog("sending call to $du\n"); t_relay(); exit;}...Exported Statistics
Section titled “Exported Statistics”NONE
Exported MI Functions
Section titled “Exported MI Functions”lb_reload
Section titled “lb_reload”Trigers the reload of the load balancing data from the DB.
MI FIFO Command Format:
:lb_reload:_reply_fifo_file__empty_line_lb_resize
Section titled “lb_resize”Changes the capacity for a resource of a destination. The function receives as parameters the ID (as per DB) of the destination along with the name of the resource you want to resize.
MI FIFO Command Format:
:lb_resize:_reply_fifo_file_11 /*dstination id*/voicemail /*resource name*/56 /* new resource capacity*/_empty_line_lb_list
Section titled “lb_list”Lists all the destinations and the maximum and current load for each resource of the destination.
MI FIFO Command Format:
:lb_list:_reply_fifo_file_ _empty_line_$ ./opensipsctl fifo lb_listDestination:: sip:127.0.0.1:5100 id=1 enabled=yes auto-re=on Resource:: pstn max=3 load=0 Resource:: transc max=5 load=1 Resource:: vm max=5 load=2Destination:: sip:127.0.0.1:5200 id=2 enabled=no auto-re=on Resource:: pstn max=6 load=0 Resource:: trans max=57 load=0 Resource:: vm max=5 load=0lb_status
Section titled “lb_status”Gets or sets the status (enabled or disabled) of a destination. The function takes 2 parameters, first mandatory, the id of the destiantion and second, optional, the new status. If no new status is given, the function will return the current status. If a new status is given (0 - disable, 1 - enable), this status will be forced for the destination.
MI FIFO Command Format:
:lb_status:_reply_fifo_file_ id status (optional) _empty_line_$ ./opensipsctl fifo lb_status 2enable:: no$ ./opensipsctl fifo lb_status 2 1$ ./opensipsctl fifo lb_status 2enable:: yesExported Pseudo-Variables
Section titled “Exported Pseudo-Variables”NONE
Developer Guide
Section titled “Developer Guide”Available Functions
Section titled “Available Functions”NONE
Frequently Asked Questions
Section titled “Frequently Asked Questions”Q: Where can I find more about OpenSIPS?
Take a look at http://www.opensips.org/.
Q: Where can I post a question about this module?
First at all check if your question was already answered on one of our mailing lists:
E-mails regarding any stable OpenSIPS release should be sent to users@lists.opensips.org and e-mails regarding development versions should be sent to devel@lists.opensips.org.
If you want to keep the mail private, send it to users@lists.opensips.org.
Q: How can I report a bug?
Please follow the guidelines provided at: https://github.com/OpenSIPS/opensips/issues.
Contributors
Section titled “Contributors”By Commit Statistics
Section titled “By Commit Statistics”Top contributors by DevScore(1), authored commits(2) and lines added/removed(3)
| # | Name | DevScore | Commits | Lines++ | Lines— |
|---|---|---|---|---|---|
| 1. | Bogdan-Andrei Iancu (@bogdan-iancu) | 86 | 50 | 3465 | 307 |
| 2. | Razvan Crainea (@razvancrainea) | 15 | 11 | 137 | 113 |
| 3. | Stefan Darius (@dariusstefan) | 13 | 5 | 638 | 103 |
| 4. | Liviu Chircu (@liviuchircu) | 11 | 6 | 373 | 50 |
| 5. | Vlad Paiu (@vladpaiu) | 5 | 3 | 7 | 5 |
| 6. | Anca Vamanu | 3 | 1 | 20 | 9 |
| 7. | Andrei Dragus | 3 | 1 | 7 | 4 |
| 8. | James Van Vleet | 3 | 1 | 4 | 1 |
| 9. | Dusan Klinec (@ph4r05) | 3 | 1 | 2 | 2 |
(1) DevScore = author_commits + author_lines_added / (project_lines_added / project_commits) + author_lines_deleted / (project_lines_deleted / project_commits)
(2) including any documentation-related commits, excluding merge commits
(3) ignoring whitespace edits, renamed files and auto-generated files
By Commit Activity
Section titled “By Commit Activity”| # | Name | Commit Activity |
|---|---|---|
| 1. | Razvan Crainea (@razvancrainea) | Oct 2010 - Jul 2026 |
| 2. | Bogdan-Andrei Iancu (@bogdan-iancu) | Feb 2009 - Jul 2026 |
| 3. | Stefan Darius (@dariusstefan) | Jun 2026 - Jul 2026 |
| 4. | Dusan Klinec (@ph4r05) | Dec 2015 - Dec 2015 |
| 5. | Liviu Chircu (@liviuchircu) | Sep 2012 - Mar 2014 |
| 6. | Vlad Paiu (@vladpaiu) | Aug 2011 - Nov 2013 |
| 7. | Andrei Dragus | Mar 2010 - Mar 2010 |
| 8. | James Van Vleet | Mar 2010 - Mar 2010 |
| 9. | Anca Vamanu | Feb 2009 - Feb 2009 |
(1) including any documentation-related commits, excluding merge commits
Documentation
Section titled “Documentation”Contributors
Section titled “Contributors”Last edited by: Razvan Crainea (@razvancrainea), Bogdan-Andrei Iancu (@bogdan-iancu), Vlad Paiu (@vladpaiu), Liviu Chircu (@liviuchircu).
License
Section titled “License”All documentation files (i.e. .md extension) are licensed under the Creative Common License 4.0