# Having trouble Setup Kamailio as a load balancer for Asterisk Server using module Dispatcher

**URL:** <https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378>\
**Category:** Asterisk Integration\
**Created:** [November 9, 2020, 2:56pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378 "2020-11-09T14:56:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShaikhTECH](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ShaikhTECH](https://community.asterisk.org/u/ShaikhTECH)\
**Post date:** [November 9, 2020, 2:56pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/1 "2020-11-09T14:56:41Z")

</div>

Hello Everyone.

I am having trouble to setting up kamailio for load balancing for asterisk servers.

I am using module dispatcher for load balancing AND two different servers for kamailio and asterisk.

I am doing it first time and have not exact idea about its all configs… but i have researched and done so far, now need help from you guys. Currently, trying to setup with the one kamailio and one asterisk then will go ahead if I get any success…  
using mariadb mysql for the dispatcher data.

Simply I want to do is :

1. Register my asterisk devices with kamailioIP (and I am able to do this using below configs)  
Devices are getting register with the asterisk from with kamailioIP

2. I want to receive calls on the registered devices with KamailioIP (and i am not able to do this)

WORK FLOW:

KAMAILIO IP = XX.XX.XX.95 on port 5060 and Asterisk IP = XX.XX.XX.164 on port 5060

SIP Device(ZOIPER) --------\>\>\>\>KAMAILIO SERVER(device register/load balancer)---------\>\>\> ASTERISK SERVER

Please have a look into my configuration files and let me know if anything is missing or wrong in these. Or anything to add or remove from the confs…

PLEASE HELP to do this, I have search a lot and tried so much of stuff in configs but no success still kindly guide me if anything I am missing to do in the process or anything I am doing wrong.  
I will be eagerly waiting for any help or pointer…

Thanks in advance

---

<div class="post-metadata">

**Author:** ![ShaikhTECH](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ShaikhTECH](https://community.asterisk.org/u/ShaikhTECH)\
**Post date:** [November 9, 2020, 2:57pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/2 "2020-11-09T14:57:47Z")

</div>

“kamctl dispatcher dump”  
looks like this:

{  
“jsonrpc”: “2.0”,  
“result”: {  
“NRSETS”: 1,  
“RECORDS”: [{  
“SET”: {  
“ID”: 1,  
“TARGETS”: [{  
“DEST”: {  
“URI”: “sip:ASTERISKIP:5060”,  
“FLAGS”: “AP”,  
“PRIORITY”: 1,  
“ATTRS”: {  
“BODY”: “rweight=50;weight=50;cc=1”,  
“DUID”: “”,  
“MAXLOAD”: 0,  
“WEIGHT”: 50,  
“RWEIGHT”: 50,  
“SOCKET”: “”,  
“SOCKNAME”: “”,  
“OBPROXY”: “”  
}  
}  
}]  
}  
}]  
},  
“id”: 13403  
}

---

<div class="post-metadata">

**Author:** ![ShaikhTECH](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ShaikhTECH](https://community.asterisk.org/u/ShaikhTECH)\
**Post date:** [November 9, 2020, 2:59pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/3 "2020-11-09T14:59:20Z")

</div>

my kamailio.cfg

“”------------------------------------------------------------------------------------------------------  
#!KAMAILIO

#!define WITH\_MYSQL  
#!define WITH\_AUTH  
#!define WITH\_ASTERISK  
#define WITH\_NAT  
#!define WITH\_DEBUG  
#!define WITH\_USRLOCDB

####### Defined Values #########

####### Global Parameters #########

#!ifdef WITH\_DEBUG  
debug=4  
log\_stderror=yes  
#!else  
debug=2  
log\_stderror=no  
#!endif

memdbg=5  
memlog=5

log\_facility=LOG\_LOCAL7

log\_name=“kamailio”

children=4

/\* uncomment the next line to disable TCP (default on) \*/  
#disable\_tcp=yes

/\* uncomment the next line to disable the auto discovery of local aliases  
based on reverse DNS on IPs (default on) \*/  
#auto\_aliases=no

/\* add local domain aliases \*/  
#alias=“[sip.mydomain.com](http://sip.mydomain.com)”

/\* uncomment and configure the following line if you want Kamailio to  
bind on a specific interface/port/proto (default bind on all available) \*/  
#listen=udp:0.0.0.0:5060  
#listen=udp: KamailioIP:5060

/\* port to listen to

- 
  - can be specified more than once if needed to listen on many ports \*/  
port=5060

#!ifdef WITH\_TLS  
enable\_tls=yes  
#!endif

#life time of TCP connection when there is no traffic  
#a bit higher than registration expires to cope with UA behind NAT  
tcp\_connection\_lifetime=3605

####### Custom Parameters #########

#These parameters can be modified runtime via RPC interface  
#-see the documentation of ‘cfg\_rpc’ module.

# 

#Format: group.id = value ‘desc’ description  
#Access: $sel(cfg\_get.group.id) or @cfg\_get.group.id

# 

#!ifdef WITH\_PSTN  
#PSTN GW Routing

# 

#-pstn.gw\_ip: valid IP or hostname as string value, example:  
#pstn.gw\_ip = “10.0.0.101” desc “My PSTN GW Address”

# 

#-by default is empty to avoid misrouting  
pstn.gw\_ip = “” desc “PSTN GW Address”  
#!endif

#!ifdef WITH\_VOICEMAIL  
#VoiceMail Routing on offline, busy or no answer

# 

#-by default Voicemail server IP is empty to avoid misrouting  
voicemail.srv\_ip = “” desc “VoiceMail IP Address”  
voicemail.srv\_port = “5060” desc “VoiceMail Port”  
#!endif

#!ifdef WITH\_ASTERISK  
asterisk.bindip = " ASTERISKIP.164" desc “Asterisk IP Address”  
asterisk.bindport = “5060” desc “Asterisk Port”  
kamailio.bindip = " KamailioIP .95" desc “Kamailio IP Address”  
kamailio.bindport = “5060” desc “Kamailio Port”  
#!endif

####### Modules Section ########

#set paths to location of modules (to sources or installation folders)  
#!ifdef WITH\_SRCPATH  
mpath="/usr/local/kamailio-devel/lib64/kamailio/modules/"  
#!else  
mpath="/usr/local/kamailio-devel/lib64/kamailio/modules/"  
#!endif

#!ifdef WITH\_MYSQL  
loadmodule “db\_mysql.so”  
#!endif

loadmodule “kex.so”  
loadmodule “tm.so”  
loadmodule “tmx.so”  
loadmodule “sl.so”  
loadmodule “rr.so”  
loadmodule “pv.so”  
loadmodule “maxfwd.so”  
loadmodule “usrloc.so”  
loadmodule “registrar.so”  
loadmodule “textops.so”  
loadmodule “siputils.so”  
loadmodule “xlog.so”  
loadmodule “sanity.so”  
loadmodule “ctl.so”  
loadmodule “cfg\_rpc.so”  
loadmodule “acc.so”  
loadmodule “sctp.so”  
loadmodule “dialog.so”  
loadmodule “siptrace.so”

#!ifdef WITH\_AUTH  
loadmodule “auth.so”  
loadmodule “auth\_db.so”  
#!ifdef WITH\_IPAUTH  
loadmodule “permissions.so”  
#!endif  
#!endif

#!ifdef WITH\_ALIASDB  
loadmodule “alias\_db.so”  
#!endif

#!ifdef WITH\_SPEEDDIAL  
loadmodule “speeddial.so”  
#!endif

#!ifdef WITH\_MULTIDOMAIN  
loadmodule “domain.so”  
#!endif

#!ifdef WITH\_PRESENCE  
loadmodule “presence.so”  
loadmodule “presence\_xml.so”  
#!endif

#!ifdef WITH\_NAT  
loadmodule “nathelper.so”  
loadmodule “rtpproxy.so”  
#!endif

#!ifdef WITH\_TLS  
loadmodule “tls.so”  
#!endif

#!ifdef WITH\_ANTIFLOOD  
loadmodule “htable.so”  
loadmodule “pike.so”  
#!endif

#!ifdef WITH\_XMLRPC  
loadmodule “xmlrpc.so”  
#!endif

#!ifdef WITH\_DEBUG  
loadmodule “debugger.so”  
#!endif

#!ifdef WITH\_ASTERISK

loadmodule “uac.so”  
loadmodule “dispatcher.so”  
loadmodule “jsonrpcs.so”  
loadmodule “htable.so”

# ---------------------- mod dispatcher params ------------------------------

modparam(“dispatcher”, “db\_url”, DBURL)  
modparam(“dispatcher”, “table\_name”, “dispatcher”)  
modparam(“dispatcher”, “ds\_ping\_interval”, 15)  
modparam(“dispatcher”, “ds\_ping\_from”, "sip:dispathcer@ KamailioIP ")  
modparam(“dispatcher”, “force\_dst”, 1)  
modparam(“dispatcher”, “ds\_ping\_method”, “INFO”)  
#modparam(“dispatcher”, “ds\_probing\_mode”, 1)  
modparam(“dispatcher”, “ds\_probing\_threshold”, 1)  
#modparam(“dispatcher”, “ds\_ping\_reply\_codes”, “class=2;code=403;code=404;code=484;code=488;code=481;class=3”)  
modparam(“dispatcher”, “ds\_ping\_reply\_codes”, “class=2;class=3;class=4”)  
#modparam(“dispatcher”, “priority\_col”, “dstpriority”)  
#modparam(“dispatcher”, “dstid\_avp”, “$avp(dsdstid)”)  
#do failover  
modparam(“dispatcher”, “flags”, 2)  
#modparam(“dispatcher”, “dst\_avp”, “$avp(AVP\_DST)”)  
#modparam(“dispatcher”, “grp\_avp”, “$avp(AVP\_GRP)”)  
#modparam(“dispatcher”, “cnt\_avp”, “$avp(AVP\_CNT)”)

#!endif

#---------------- setting module-specific parameters ---------------  
modparam(“siptrace”, “db\_url”, DBURL)  
modparam(“siptrace”, “trace\_flag”, 22)  
modparam(“siptrace”, “trace\_on”, 1)  
#----- jsonrpcs params -----  
modparam(“jsonrpcs”, “pretty\_format”, 1)  
/\* set the path to RPC fifo control file _/  
 #modparam(“jsonrpcs”, “fifo\_name”, “/run/kamailio/kamailio\_rpc.fifo”)  
/_ set the path to RPC unix socket control file \*/  
#modparam(“jsonrpcs”, “dgram\_socket”, “/run/kamailio/kamailio\_rpc.sock”)  
#!ifdef WITH\_JSONRPC  
modparam(“jsonrpcs”, “transport”, 7)  
#!endif

#---------------------- mod uac params ------------------------------

modparam(“uac”, “reg\_contact\_addr”, “ASTERISKIP:5060”)  
modparam(“uac”, “reg\_db\_url”, “mysql://kamailio:kamailiorw@localhost/kamailio”)  
modparam(“uac”, “reg\_db\_table”, “uacreg”)  
modparam(“uac”, “reg\_timer\_interval”, 60)  
modparam(“uac”,“restore\_mode”,“auto”)  
modparam(“uac”, “restore\_dlg”, 1)  
modparam(“uac”,“auth\_realm\_avp”,"$avp(s:auth\_realm\_avp)")  
modparam(“uac”,“auth\_username\_avp”,"$avp(s:auth\_username\_avp)")  
modparam(“uac”,“auth\_password\_avp”,"$avp(s:auth\_password\_avp)")

#----- mi\_fifo params -----  
#modparam(“mi\_fifo”, “fifo\_name”, “/tmp/kamailio\_fifo”)

#----- tm params -----

# auto-discard branches from previous serial forking leg

modparam(“tm”, “failure\_reply\_mode”, 3)

# default retransmission timeout: 30sec

modparam(“tm”, “fr\_timer”, 30000)

# default invite retransmission timeout after 1xx: 120sec

modparam(“tm”, “fr\_inv\_timer”, 120000)  
modparam(“tm”, “ruri\_matching”, 0)

#----- rr params -----

# add value to ;lr param to cope with most of the UAs

modparam(“rr”, “enable\_full\_lr”, 1)

# do not append from tag to the RR (no need for this script)

#!ifdef WITH\_ASTERISK  
modparam(“rr”, “append\_fromtag”, 1)  
#!else  
modparam(“rr”, “append\_fromtag”, 0)  
#!endif

#----- registrar params -----  
modparam(“registrar”, “method\_filtering”, 1)  
/\* uncomment the next line to disable parallel forking via location \*/

# modparam(“registrar”, “append\_branches”, 0)

/\* uncomment the next line not to allow more than 10 contacts per AOR \*/  
#modparam(“registrar”, “max\_contacts”, 10)

# max value for expires of registrations

modparam(“registrar”, “max\_expires”, 3600)

# set it to 1 to enable GRUU

modparam(“registrar”, “gruu\_enabled”, 0)

#----- acc params -----  
/\* what special events should be accounted ? _/  
modparam(“acc”, “early\_media”, 0)  
modparam(“acc”, “report\_ack”, 0)  
modparam(“acc”, “report\_cancels”, 0)  
/_ by default ww do not adjust the direct of the sequential requests.  
if you enable this parameter, be sure the enable “append\_fromtag”  
in “rr” module _/  
modparam(“acc”, “detect\_direction”, 0)  
/_ account triggers (flags) _/  
modparam(“acc”, “log\_flag”, FLT\_ACC)  
modparam(“acc”, “log\_missed\_flag”, FLT\_ACCMISSED)  
modparam(“acc”, “log\_extra”,  
“src\_user=$fU;src\_domain=$fd;src\_ip=$si;”  
“dst\_ouser=$tU;dst\_user=$rU;dst\_domain=$rd”)  
modparam(“acc”, “failed\_transaction\_flag”, FLT\_ACCFAILED)  
/_ enhanced DB accounting \*/  
#!ifdef WITH\_ACCDB  
modparam(“acc”, “db\_flag”, FLT\_ACC)  
modparam(“acc”, “db\_missed\_flag”, FLT\_ACCMISSED)  
modparam(“acc”, “db\_url”, DBURL)  
modparam(“acc”, “db\_extra”,  
“src\_user=$fU;src\_domain=$fd;src\_ip=$si;”  
“dst\_ouser=$tU;dst\_user=$rU;dst\_domain=$rd”)  
#!endif

# ----- usrloc params -----

/\* enable DB persistency for location entries \*/  
#!ifdef WITH\_USRLOCDB  
modparam(“usrloc”, “db\_url”, DBURL)  
modparam(“usrloc”, “db\_mode”, 2)  
modparam(“usrloc”, “use\_domain”, MULTIDOMAIN)  
modparam(“usrloc”, “matching\_mode”, 1)  
modparam(“usrloc”, “db\_insert\_update”, 1)  
#!endif

#----- auth\_db params -----  
#!ifdef WITH\_AUTH

modparam(“auth\_db”, “db\_url”, DBURL)  
modparam(“auth\_db”, “calculate\_ha1”, yes)  
modparam(“auth\_db”, “load\_credentials”, “”)

#!ifdef WITH\_ASTERISK  
modparam(“auth\_db”, “user\_column”, “username”)  
modparam(“auth\_db”, “password\_column”, “secret”)  
modparam(“auth\_db”, “db\_url”, DBASTURL)  
modparam(“auth\_db”, “version\_table”, 0)  
#!else  
modparam(“auth\_db”, “db\_url”, DBURL)  
modparam(“auth\_db”, “password\_column”, “password”)  
modparam(“auth\_db”, “use\_domain”, MULTIDOMAIN)  
#!endif

#----- permissions params -----  
#!ifdef WITH\_IPAUTH  
modparam(“permissions”, “db\_url”, DBURL)  
modparam(“permissions”, “db\_mode”, 1)  
modparam(“permissions”, “trusted\_table”, “trusted”)  
#!endif

#!endif

#----- alias\_db params -----  
#!ifdef WITH\_ALIASDB  
modparam(“alias\_db”, “db\_url”, DBURL)  
modparam(“alias\_db”, “use\_domain”, MULTIDOMAIN)  
#!endif

#----- speedial params -----  
#!ifdef WITH\_SPEEDDIAL  
modparam(“speeddial”, “db\_url”, DBURL)  
modparam(“speeddial”, “use\_domain”, MULTIDOMAIN)  
#!endif

#----- domain params -----  
#!ifdef WITH\_MULTIDOMAIN  
modparam(“domain”, “db\_url”, DBURL)  
#register callback to match myself condition with domains list  
modparam(“domain”, “register\_myself”, 1)  
#!endif

#!ifdef WITH\_PRESENCE  
#----- presence params -----  
modparam(“presence”, “db\_url”, DBURL)

#----- presence\_xml params -----  
modparam(“presence\_xml”, “db\_url”, DBURL)  
modparam(“presence\_xml”, “force\_active”, 1)  
#!endif

#!ifdef WITH\_NAT  
#----- rtpproxy params -----  
modparam(“rtpproxy”, “rtpproxy\_sock”, “udp:127.0.0.1:7722”)

#----- nathelper params -----  
modparam(“nathelper”, “natping\_interval”, 30)  
modparam(“nathelper”, “ping\_nated\_only”, 1)  
modparam(“nathelper”, “sipping\_bflag”, FLB\_NATSIPPING)  
modparam(“nathelper”, “sipping\_from”, “[sip:pinger@kamailio.org](mailto:sip:pinger@kamailio.org)”)

#params needed for NAT traversal in other modules  
modparam(“nathelper|registrar”, “received\_avp”, “$avp(RECEIVED)”)  
modparam(“usrloc”, “nat\_bflag”, FLB\_NATB)  
#!endif

#!ifdef WITH\_TLS  
#----- tls params -----  
modparam(“tls”, “config”, “/usr/local/etc/kamailio/tls.cfg”)  
#!endif

#!ifdef WITH\_ANTIFLOOD  
#----- pike params -----  
modparam(“pike”, “sampling\_time\_unit”, 2)  
modparam(“pike”, “reqs\_density\_per\_unit”, 16)  
modparam(“pike”, “remove\_latency”, 4)

#----- htable params -----

# ip ban htable with autoexpire after 5 minutes

#modparam(“htable”, “htable”, “ipban=\>size=8;autoexpire=300;”)  
modparam(“htable”, “htable”, “stats=\>size=6;”)

#!endif

#!ifdef WITH\_XMLRPC  
#----- xmlrpc params -----  
modparam(“xmlrpc”, “route”, “XMLRPC”);  
modparam(“xmlrpc”, “url\_match”, “^/RPC”)  
#!endif

#!ifdef WITH\_DEBUG  
#----- debugger params -----  
#modparam(“debugger”, “cfgtrace”, 1)  
#modparam(“debugger”, “breakpoint”, 1)  
modparam(“debugger”, “log\_level\_name”, “exec”)  
#!endif

####### Routing Logic ########

#Main SIP request routing logic  
#-processing of any incoming SIP request starts with this route  
#-note: this is the same as route { … }

request\_route {

```
            route(DISPATCH);

            # per request initial checks
            route(REQINIT);

            # NAT detection
            route(NATDETECT);

            #handle requests within SIP dialogs
            route(WITHINDLG);

            ###only initial requests (no To tag)

            if(is_method("OPTIONS"))
            {
            sl_send_reply("200","Keepalive");

           exit;
    }
            if(is_method("NOTIFY"))
            {
            sl_send_reply("200","OK");

            exit;
    }

            #CANCEL processing
            if (is_method("CANCEL"))
            {
                            if (t_check_trans())
                                            t_relay();
                            exit;
            }

            t_check_trans();

            #authentication

            route(AUTH);

            #record routing for dialog forming requests (in case they are routed)
            #-remove preloaded route headers
            remove_hf("Route");

            if (is_method("INVITE|SUBSCRIBE"))
                            record_route();

            # account only INVITEs
            if (is_method("INVITE"))
            {      

                            setflag(FLT_ACC); # do accounting
            }

            #dispatch requests to foreign domains
            route(SIPOUT);

            ###requests for my local domains

            #handle presence related requests
            route(PRESENCE);

            #handle registrations
            route(REGISTRAR);

            if ($rU==$null)
            {
                            # request with no Username in RURI
                            sl_send_reply("484","Address Incomplete");
                            exit;
            }

            #dispatch destinations to PSTN
            route(PSTN);

            #user location service
            route(LOCATION);

            route(RELAY);

```

}

route[RELAY] {

```
            #enable additional event routes for forwarded requests
            #-serial forking, RTP relaying handling, a.s.o.
            if (is_method("INVITE|SUBSCRIBE")) {
                            t_on_branch("MANAGE_BRANCH");
                            t_on_reply("MANAGE_REPLY");
            }
            if (is_method("INVITE")) {
                            t_on_failure("MANAGE_FAILURE");
            }

            if (!t_relay()) {
                            sl_reply_error();
            }
            exit;

```

}

#Per SIP request initial checks  
route[REQINIT] {  
#!ifdef WITH\_ANTIFLOOD  
# flood dection from same IP and traffic ban for a while  
# be sure you exclude checking trusted peers, such as pstn gateways  
# - local host excluded (e.g., loop to self)  
if(src\_ip!=myself)  
{  
if($sht(ipban=\>$si)!=$null)  
{  
# ip is already blocked  
xdbg(“request from blocked IP - $rm from $fu (IP:$si:$sp)\n”);  
exit;  
}  
if (!pike\_check\_req())  
{  
xlog(“L\_ALERT”,“ALERT: pike blocking $rm from $fu (IP:$si:$sp)\n”);  
$sht(ipban=\>$si) = 1;  
exit;  
}  
}  
#!endif

```
            if (!mf_process_maxfwd_header("10")) {
                            sl_send_reply("483","Too Many Hops");
                            exit;
            }

            if(!sanity_check("1511", "7"))
            {
                            xlog("Malformed SIP message from $si:$sp\n");
                            exit;
            }

```

}

#Handle requests within SIP dialogs  
route[WITHINDLG] {  
if (has\_totag()) {  
# sequential request withing a dialog should  
# take the path determined by record-routing  
if (loose\_route()) {  
if (is\_method(“BYE”)) {  
setflag(FLT\_ACC); # do accounting …  
setflag(FLT\_ACCFAILED); # … even if the transaction fails  
}  
if ( is\_method(“ACK”) ) {  
# ACK is forwarded statelessy  
route(NATMANAGE);  
}  
route(RELAY);  
} else {  
if (is\_method(“SUBSCRIBE”) && uri == myself) {  
# in-dialog subscribe requests  
route(PRESENCE);  
exit;  
}  
if ( is\_method(“ACK”) ) {  
if ( t\_check\_trans() ) {  
# no loose-route, but stateful ACK;  
# must be an ACK after a 487  
# or e.g. 404 from upstream server  
t\_relay();  
exit;  
} else {  
# ACK without matching transaction … ignore and discard  
exit;  
}  
}  
sl\_send\_reply(“404”,“Not here”);  
}  
exit;  
}  
}

#Handle SIP registrations  
route[REGISTRAR] {  
if (is\_method(“REGISTER”))  
{  
if(isflagset(FLT\_NATS))  
{  
setbflag(FLB\_NATB);  
# uncomment next line to do SIP NAT pinging  
## setbflag(FLB\_NATSIPPING);  
}  
if (!save(“location”))  
sl\_reply\_error();

#!ifdef WITH\_ASTERISK  
route(REGFWD);  
#!endif

```
                            exit;
            }

```

}

#USER location service  
route[LOCATION] {

#!ifdef WITH\_SPEEDIAL  
# search for short dialing - 2-digit extension  
if(rU=~"^[0-9][0-9]")  
if(sd\_lookup(“speed\_dial”))  
route(SIPOUT);  
#!endif

#!ifdef WITH\_ALIASDB  
# search in DB-based aliases  
if(alias\_db\_lookup(“dbaliases”))  
route(SIPOUT);  
#!endif

#!ifdef WITH\_ASTERISK  
if(is\_method(“INVITE”) && (!route(FROMASTERISK))) {  
# if new call from out there - send to Asterisk  
# - non-INVITE request are routed directly by Kamailio  
# - traffic from Asterisk is routed also directy by Kamailio  
append\_hf(“FromIp: $src\_ip\r\n”, “Call-ID”);  
route(TOASTERISK);  
exit;  
}  
#!endif

```
            $avp(oexten) = $rU;
            if (!lookup("location")) {
                            $var(rc) = $rc;
                            route(TOVOICEMAIL);
                            t_newtran();
                            switch ($var(rc)) {
                                            case -1:
                                            case -3:
                                                            send_reply("404", "Not Found");
                                                            exit;
                                            case -2:
                                                            send_reply("405", "Method Not Allowed");
                                                            exit;
                            }
            }

            # when routing via usrloc, log the missed calls also
            if (is_method("INVITE"))
            {
                            setflag(FLT_ACCMISSED);
            }

```

}

#Presence server route  
route[PRESENCE] {  
if(!is\_method(“PUBLISH|SUBSCRIBE”))  
return;

#!ifdef WITH\_PRESENCE  
if (!t\_newtran())  
{  
sl\_reply\_error();  
exit;  
};

```
            if(is_method("PUBLISH"))
            {
                            handle_publish();
                            t_release();
            }
            else
            if( is_method("SUBSCRIBE"))
            {
                            handle_subscribe();
                            t_release();
            }
            exit;

```

#!endif

```
            # if presence enabled, this part will not be executed
            if (is_method("PUBLISH") || $rU==$null)
            {
                            sl_send_reply("404", "Not here");
                            exit;
            }
            return;

```

}

#Authentication route  
route[AUTH] {  
#!ifdef WITH\_AUTH

#!ifdef WITH\_ASTERISK  
# do not auth traffic from Asterisk - trusted!  
if(route(FROMASTERISK))  
return;  
#!endif

#!ifdef WITH\_IPAUTH

```
            if((!is_method("REGISTER")) && allow_source_address())
            {      

                            # source IP allowed
                            return;
            }

```

if (is\_method(“REGISTER”) || from\_uri==myself){  
if (!allow\_trusted()) {  
xlog(‘wokred here for ipauth check’);  
sl\_send\_reply(“403”, “Forbidden”);  
}  
}

#!endif

```
            if (is_method("REGISTER") || from_uri==myself)
            {
                            # authenticate requests

```

#!ifdef WITH\_ASTERISK

```
                            if (!auth_check("$fd", "sip_users", "1")) {

```

#!else  
if (!auth\_check("$fd", “subscriber”, “1”)) {  
#!endif  
auth\_challenge("$fd", “0”);  
exit;  
}  
# user authenticated - remove auth header  
if(!is\_method(“REGISTER|PUBLISH”))  
consume\_credentials();  
}  
# if caller is not local subscriber, then check if it calls  
# a local destination, otherwise deny, not an open relay here  
if (from\_uri!=myself && uri!=myself)  
{  
sl\_send\_reply(“403”,“Not relaying”);  
exit;  
}

#!endif  
return;  
}

#Caller NAT detection route  
route[NATDETECT] {  
#!ifdef WITH\_NAT  
force\_rport();  
if (nat\_uac\_test(“19”)) {  
if (is\_method(“REGISTER”)) {  
fix\_nated\_register();  
} else {  
fix\_nated\_contact();  
}  
setflag(FLT\_NATS);  
}  
#!endif  
return;  
}

#RTPProxy control  
route[NATMANAGE] {  
#!ifdef WITH\_NAT  
if (is\_request()) {  
if(has\_totag()) {  
if(check\_route\_param(“nat=yes”)) {  
setbflag(FLB\_NATB);  
}  
}  
}  
if (!(isflagset(FLT\_NATS) || isbflagset(FLB\_NATB)))  
return;

```
            rtpproxy_manage();

            if (is_request()) {
                            if (!has_totag()) {
                                            add_rr_param(";nat=yes");
                            }
            }
            if (is_reply()) {
                            if(isbflagset(FLB_NATB)) {
                                            fix_nated_contact();
                            }
            }

```

#!endif  
return;  
}

#Routing to foreign domains  
route[SIPOUT] {  
if (!uri==myself)  
{  
append\_hf(“P-hint: outbound\r\n”);  
route(RELAY);  
}  
}

#PSTN GW routing  
route[PSTN] {  
#!ifdef WITH\_PSTN  
# check if PSTN GW IP is defined  
if (strempty($sel(cfg\_get.pstn.gw\_ip))) {  
xlog(“SCRIPT: PSTN rotuing enabled but pstn.gw\_ip not defined\n”);  
return;  
}

```
            # route to PSTN dialed numbers starting with '+' or '00'
            # (international format)
            # - update the condition to match your dialing rules for PSTN routing
            if(!($rU=~"^(\+|00)[1-9][0-9]{3,20}$"))
                            return;

            # only local users allowed to call
            if(from_uri!=myself) {
                            sl_send_reply("403", "Not Allowed");
                            exit;
            }

            $ru = "sip:" + $rU + "@" + $sel(cfg_get.pstn.gw_ip);

            route(RELAY);
            exit;

```

#!endif

```
            return;

```

}

#XMLRPC routing  
#!ifdef WITH\_XMLRPC  
route[XMLRPC] {  
# allow XMLRPC from localhost  
if ((method==“POST” || method==“GET”)  
&& (src\_ip==127.0.0.1)) {  
# close connection only for xmlrpclib user agents (there is a bug in  
# xmlrpclib: it waits for EOF before interpreting the response).  
if ($hdr(User-Agent) =~ “xmlrpclib”)  
set\_reply\_close();  
set\_reply\_no\_connect();  
dispatch\_rpc();  
exit;  
}  
send\_reply(“403”, “Forbidden”);  
exit;  
}  
#!endif

#route to voicemail server  
route[TOVOICEMAIL] {  
#!ifdef WITH\_VOICEMAIL  
if(!is\_method(“INVITE”))  
return;

```
            # check if VoiceMail server IP is defined
            if (strempty($sel(cfg_get.voicemail.srv_ip))) {
                            xlog("SCRIPT: VoiceMail rotuing enabled but IP not defined\n");
                            return;
            }
            if($avp(oexten)==$null)
                            return;

            $ru = "sip:" + $avp(oexten) + "@" + $sel(cfg_get.voicemail.srv_ip)
                                                            + ":" + $sel(cfg_get.voicemail.srv_port);
            route(RELAY);
            exit;

```

#!endif

```
            return;

```

}

#manage outgoing branches  
branch\_route[MANAGE\_BRANCH] {  
xdbg(“new branch [$T\_branch\_idx] to $ru\n”);  
route(NATMANAGE);  
}

#manage incoming replies  
onreply\_route[MANAGE\_REPLY] {  
xdbg(“incoming reply\n”);  
if(status=~"[12][0-9][0-9]")  
route(NATMANAGE);  
}

#manage failure routing cases  
failure\_route[MANAGE\_FAILURE] {  
route(NATMANAGE);

```
            if (t_is_canceled()) {
                            exit;
            }
            if(t_check_status("401")) {
      uac_auth();
   }

```

#!ifdef WITH\_BLOCK3XX  
# block call redirect based on 3xx replies.  
if (t\_check\_status(“3[0-9][0-9]”)) {  
t\_reply(“404”,“Not found”);  
exit;  
}  
#!endif

#!ifdef WITH\_VOICEMAIL  
# serial forking  
# - route to voicemail on busy or no answer (timeout)  
if (t\_check\_status(“486|408”)) {  
route(TOVOICEMAIL);  
exit;  
}  
t\_relay();  
#!endif  
}

#Test if coming from Asterisk  
route[FROMASTERISK] {

# if($si==$sel(cfg\_get.asterisk.bindip)

# && $sp==$sel(cfg\_get.asterisk.bindport))

# return 1;

# return -1;

```
            # dispatch: is this asterisk ip ?
            if (ds_is_from_list("1"))
                            return 1;
            return -1;

```

}

route[TOASTERISK] {  
#$du = “sip:” + $sel(cfg\_get.asterisk.bindip) + “:”+ $sel(cfg\_get.asterisk.bindport);  
#append\_hf(“X-Asterisk-Context: dexter-phones\r\n”);  
#route(RELAY);  
#exit;  
# select asterisk port  
append\_hf(“X-Asterisk-Context: dexter-phones\r\n”);  
route(DISPATCH);  
}

route[DISPATCH] {  
# round robin dispatching on gateways group ‘1’  
#xlog("\<\<\<\<\<\<\<\<\<\<\<\<\<\<\<\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>\>");

```
            #if (is_method("INVITE")) {
            # dst_select( "GROUP", "HASH METHOD")
            #ds_select_dst("1","4");
            #sl_send_reply("100","Trying");
           # forward();
           # exit();
            #}
    if (!ds_select_domain("1", "4")) {
      send_reply("404", "No destination");
      exit;
    }
    t_on_failure("RTF_DISPATCH");
            route(RELAY);
            exit;

```

}

route[REGFWD] {  
if(!is\_method(“REGISTER”))  
{  
return;  
}

```
            $var(rip) = $sel(cfg_get.asterisk.bindip);
            $uac_req(method)="REGISTER";
            $uac_req(ruri)="sip:" + $var(rip) + ":" + $sel(cfg_get.asterisk.bindport);
            $uac_req(furi)="sip:" + $au + "@" + $var(rip);
            $uac_req(turi)="sip:" + $au + "@" + $var(rip);
            $uac_req(hdrs)="Contact: <sip:" + $au + "@" + $sel(cfg_get.kamailio.bindip) + ":" + $sel(cfg_get.kamailio.bindport) + ">\r\n";

            if($sel(contact.expires) != $null)
                            $uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $sel(contact.expires) + "\r\n";
            else
                            $uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $hdr(Expires) + "\r\n";
            uac_req_send();

```

}

---

<div class="post-metadata">

**Author:** ![ShaikhTECH](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ShaikhTECH](https://community.asterisk.org/u/ShaikhTECH)\
**Post date:** [November 9, 2020, 3:01pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/4 "2020-11-09T15:01:28Z")

</div>

And also have set trunk in ASTERISK sip.conf

register =\> kamailioSIP:XXXX@KamailioIP:5060

[kamailio]  
type=friend  
host= KamailioIP  
port=5060  
transport=udp  
allow=all  
allow=gsm  
allow=alaw,ulaw  
insecure=invite,port  
Permit = 0.0.0.0/0.0.0.0  
sipdebug=yes  
username=kamailioSIP  
canreinvite=no  
secret=XXXX  
qualify=yes  
dtmfmode=auto

after all the above configs when i Register my asterisk devices with kamailioIP (and I am able to do this)  
Devices are getting register with the asterisk from with kamailioIP asterisk -r : (here 8101 and 8100 is registered with Kamailio IP )

[Nov 9 10:36:02] – Registered SIP ‘8101’ at XX.XX.XX.95:5060  
[Nov 9 10:36:02] – Unregistered SIP ‘8101’  
[Nov 9 10:36:03] – Registered SIP ‘8101’ at XX.XX.XX…95:5060  
[Nov 9 10:36:03] == Manager ‘listencron’ logged on from 127.0.0.1  
[Nov 9 10:36:04] == Manager ‘sendcron’ logged off from 127.0.0.1  
[Nov 9 10:36:07] – Unregistered SIP ‘8100’  
[Nov 9 10:36:09] – Registered SIP ‘8100’ at XX.XX.XX…95:5060  
[Nov 9 10:36:09] \> Saved useragent “Z 3.15.40006 rv2.8.20” for peer 8100  
[Nov 9 10:36:09] NOTICE[8453]: chan\_sip.c:23869 handle\_response\_peerpoke: Peer ‘8100’ is now  
Reachable. (51ms / 2000ms)

when i dial 8100 to 8101 i get in my asteris -r

NOTICE[8453][C-0000000b]: chan\_sip.c:26002 handle\_request\_invite: Call from ‘8101’ (95.217.223.95:5060) to extension ‘8100’ rejected because extension not found in context ‘none-dial’.

and not able to call extension to extension.

PLESE HELP!! any help will be appreciated 🙂

I will be looking forward for help,  
Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![Chano](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/chano/32/3448_2.png) [@Chano](https://community.asterisk.org/u/Chano)\
**Post date:** [November 10, 2020, 9:32am UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/5 "2020-11-10T09:32:02Z")

</div>

In our setup, we do not require authentication from Kamailio, this works fine… Also you need to set the context the calls coming from kamailio, should be processed in. Currently they are processed in the context none-dial

Just at a line with something like this, to the kamailio peer.  
context=whatever-context-allows-you-to-dial-8100

If that does not work, try changing the permit line to the IP of kamailio, and removing the username and secret.

---

<div class="post-metadata">

**Author:** ![ShaikhTECH](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ShaikhTECH](https://community.asterisk.org/u/ShaikhTECH)\
**Post date:** [November 10, 2020, 1:03pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/6 "2020-11-10T13:03:40Z")

</div>

> [@Chano](#):
>
> Just at a line with

Hi Chano, Thanks a lot for your response. 🙂

actually I am doing it for the first time and, as I said before, don’t have exact idea about it that how stuff goes on…

i have researched and got kamailio configured and succeeded till now that my devices are registering from kamailio IP using asterisk creds.

please Help me in this as you mentioned  
“In our setup, we do not require authentication from Kamailio, this works fine.”

what should I add or remove from my kamailio.cfg file that as you are doing so it do not require authentication from Kamailio… if you can please share your kamailio.cfg file or any pointers.

and the second thing, i do not have anything in my asterisk exensions.conf named  
“none-dial” no idea from where it is coming from…so please also help me in this too… which dialplan I have to put in so that i can do calling successfully.

I have tried doing changing the permit line to the IP of kamailio, and removing the username and secret.

Please Help me in doing this.

Your help would me much appreciated.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![Chano](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/chano/32/3448_2.png) [@Chano](https://community.asterisk.org/u/Chano)\
**Post date:** [November 11, 2020, 7:58am UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/7 "2020-11-11T07:58:01Z")

</div>

I have NO idea in regards to the Kamailio part, I’ve only made minor changes to our configuration.

But before migrating to pjsip, I had the following in my sip.conf:

```auto
[general]
context=default
allowoverlap=no
udpbindaddr=0.0.0.0
tcpenable=no
tcpbindaddr=0.0.0.0
srvlookup=no
vmexten=*902
disallow=all
allow=alaw
language=en
notifyhold=yes
notifycid=yes
callcounter=yes
useragent=<Some custom user agent>
minexpiry=30
fromdomain=<Asterisk hostname>
nat=force_rport,comedia
force_rport=yes
t38pt_udptl=yes

[trunks](!)
insecure=port,invite
host=<Kamailio IP>
nat=force_rport,comedia
canreinvite=no
type=friend
language=en
dtmfmode=auto
dtmf=auto
force_rport=yes
t38pt_udptl=yes

[inboundTrunk](trunks)
context=general_inboundRouting
accountcode=inboundTrunk

```

Of cause there were more, but this is the part that allowed incoming calls to Asterisk to be processed. They were processed in the general\_inboundRouting context.

On the Kamailio side, everything was just routed to the Asterisk server for processing.

In our particular setup, our customers register to the Asterisk server, with their extensions, Asterisk then handles calls between extensions, while Kamailio handles outbound calls, either to other customers, or the public phone network.

As I see it you seem to try to make Kamailio handle your internal calls between extensions as well? Or are the 8xxx numbers just placeholders and intended to be real numbers in the end?

---

<div class="post-metadata">

**Author:** ![ShaikhTECH](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ShaikhTECH](https://community.asterisk.org/u/ShaikhTECH)\
**Post date:** [November 11, 2020, 9:32am UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/8 "2020-11-11T09:32:31Z")

</div>

Hi,  
Thanks for sharing your sip.conf file.  
But I think I have issue in my dialplan, probably I have not set extn in my extension.conf. And  
Have not much idea about it.  
If you can please share your extension.conf.

Bcz now when I dial 101 which I have set for a m Media file. And when I dial from my device and which is registered from kamailio IP and I am able to hear that media.

Now I am not sure where I am doing wrong in the extension.conf which is for calling. Please if you can go through my extension.conf and point out where I am putting wrong extn and what should I put for internally calling and also for conference.

You help is much appreciated.  
Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![ShaikhTECH](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ShaikhTECH](https://community.asterisk.org/u/ShaikhTECH)\
**Post date:** [November 11, 2020, 10:09am UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/9 "2020-11-11T10:09:52Z")

</div>

this is now in my extensions.conf i have set the name with none-dial

[none-dial]; I added these below linesexten =\> \_XXXX,1,Answer()  
exten =\> \_XXXX,n,Playback(demo-congrats)  
exten =\> \_XXXX,n,Hangup()exten =\> \_XXXX,2,NoOp(internal context called on Server A to {EXTEN}, CALLERID\_num {CALLERID(num)}, CALLERID\_num\_f2 {CALLERID(num):0:2}) exten =\> \_XXXX,3,Dial(SIP/{EXTEN},15)exten =\> s,n,Dial(SIP/{[EXTEN}@95.217.223.95:5060](http://EXTEN}@95.217.223.95:5060),,) exten =\> s,n,Hangup()exten =\> \_XXXX,1,AGI(agi://127.0.0.1:4577/call\_log) exten =\> \_XXXX,2,Dial(SIP/kamailio/{[EXTEN}@95.217.223.95:5060](http://EXTEN%7D@95.217.223.95:5060),55,To) ; any call forward back to kamailio  
exten =\> \_XXXX,3,Hangup()exten =\> \_XXXXXXXXXX.,1,AGI(agi://127.0.0.1:4577/call\_log)  
exten =\> \_XXXXXXXXXX.,2,Dial(SIP/{[EXTEN:1}@95.217.223.95:5060](http://EXTEN:1}@95.217.223.95:5060),,tTo) exten =\> \_XXXXXXXXXX.,3,Hangup()exten =\> \_sXXXX.,1,AGI(agi://127.0.0.1:4577/call\_log) exten =\> \_sXXXX.,2,Dial(SIP/{[EXTEN:1}@95.217.223.95:5060](http://EXTEN:1%7D@95.217.223.95:5060),tTo)  
exten =\> \_sXXXX.,3,Hangup()exten =\> \_sXXXXXXXXXX,1,AGI(agi-DID\_route.agi)  
exten =\> \_sXXXXXXXXXX,2,Dial({Kamailio}/{EXTEN:1},tTo)  
exten =\> \_sXXXXXXXXXX,3,Hangup()exten =\> \_sXXXXXXXXXX,n,Dial(sip/{EXTEN:1}@kamailio,55,o)exten =\> s,1,NoOp({SIP\_HEADER(TO)})exten =\> s,n,Set(DN={SIP\_HEADER(TO)}) exten =\> s,n,Set(DN={CUT(DN,:,2)})  
exten =\> s,n,Set(DN={CUT(DN,@,1)}) exten =\> s,n,Set(DN={STRREPLACE(DN,+,00)})  
exten =\> s,n,Goto(trunkinbound,{DN},1)exten =\> s,1,Verbose(4,calling user on kamailio) exten =\> s,n,Dial(SIP/{EXTEN}@kamailio,)  
exten =\> s,n,Hangup(); I added these lines till here

and this is my current sip.conf kamailio truck

register =\> [kamailio@95.217.223.95:5060](http://kamailio@95.217.223.95:5060)[kamailio]  
type=friend  
host=95.217.223.95  
port=5060  
transport=udp  
disallow=all  
allow=all  
allow=gsm  
allow=g729  
allow=alaw  
allow=ulaw  
context=none-dial  
insecure=invite,port  
Permit=95.217.223.95  
sipdebug=yes  
username=kamailio  
;secret=vici1234  
canreinvite=no  
qualify=no  
dtmfmode=auto  
srvlookup=yes  
canreinvite=no

please go through this and lemme know what is crap here and what to add and remove in order to make calls…  
this below extn is working currently, ans i am able to listen the media from my device which is registered from kamailio IP  
exten =\> \_XXXX,1,Answer()  
exten =\> \_XXXX,n,Playback(demo-congrats)  
exten =\> \_XXXX,n,Hangup()

---

<div class="post-metadata">

**Author:** ![Chano](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/chano/32/3448_2.png) [@Chano](https://community.asterisk.org/u/Chano)\
**Post date:** [November 11, 2020, 2:45pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/10 "2020-11-11T14:45:15Z")

</div>

Ok, just cleaned up what you posted. If you click the “\</\>” icon in the editor, and past your dial plan within the code block, it’s much easier to read.

```auto
exten => _XXXX,1,Answer()
exten => _XXXX,n,Playback(demo-congrats)
exten => _XXXX,n,Hangup()
exten => _XXXX,2,NoOp(internal context called on Server A to {EXTEN}, CALLERID_num {CALLERID(num)}, CALLERID_num_f2 {CALLERID(num):0:2}) 
exten => _XXXX,3,Dial(SIP/{EXTEN},15)

exten => s,n,Dial(SIP/{[EXTEN}@95.217.223.95:5060](http://EXTEN}@95.217.223.95:5060),,) 
exten => s,n,Hangup()

exten => _XXXX,1,AGI(agi://127.0.0.1:4577/call_log) 
exten => _XXXX,2,Dial(SIP/kamailio/{EXTEN}@95.217.223.95:5060,55,To) ; any call forward back to kamailio
exten => _XXXX,3,Hangup()

exten => _XXXXXXXXXX.,1,AGI(agi://127.0.0.1:4577/call_log)
exten => _XXXXXXXXXX.,2,Dial(SIP/{[EXTEN:1}@95.217.223.95:5060](http://EXTEN:1}@95.217.223.95:5060),,tTo) 
exten => _XXXXXXXXXX.,3,Hangup()

exten => _sXXXX.,1,AGI(agi://127.0.0.1:4577/call_log) 
exten => _sXXXX.,2,Dial(SIP/{EXTEN:1}@95.217.223.95:5060,tTo)
exten => _sXXXX.,3,Hangup()

exten => _sXXXXXXXXXX,1,AGI(agi-DID_route.agi)
exten => _sXXXXXXXXXX,2,Dial({Kamailio}/{EXTEN:1},tTo)
exten => _sXXXXXXXXXX,3,Hangup()
exten => _sXXXXXXXXXX,n,Dial(sip/{EXTEN:1}@kamailio,55,o)

exten => s,1,NoOp({SIP_HEADER(TO)})
exten => s,n,Set(DN={SIP_HEADER(TO)}) 
exten => s,n,Set(DN={CUT(DN,:,2)})
exten => s,n,Set(DN={CUT(DN,@,1)}) 
exten => s,n,Set(DN={STRREPLACE(DN,+,00)})
exten => s,n,Goto(trunkinbound,{DN},1)
exten => s,1,Verbose(4,calling user on kamailio) 
exten => s,n,Dial(SIP/{EXTEN}@kamailio,)
exten => s,n,Hangup(); I added these lines till here

```

If the above is indeed your current dial plan it’s mound to cause problems for you.

Lets have a look at this section first

```auto
exten => _XXXX,1,Answer()
exten => _XXXX,n,Playback(demo-congrats)
exten => _XXXX,n,Hangup()
exten => _XXXX,2,NoOp(internal context called on Server A to {EXTEN}, CALLERID_num {CALLERID(num)}, CALLERID_num_f2 {CALLERID(num):0:2}) 
exten => _XXXX,3,Dial(SIP/{EXTEN},15)

```

The first line tells asterisk at priority 1, to answer the line for extension \_XXXX.  
The next line with priority n becomes priority 2, and plays an audio file  
Next line, priority 3, will hangup the call.  
Then in the 4th line, you set priority 2 to log som text to screen using the NoOp app (I prefer LOG() but whatever). This line OVERWRITES the previous priority 2!  
The next line, line 5 you overwrite priority 3 with the Dial command. The resulting dial plan in asterisk, from my understanding, will be this:

```auto
exten => _XXXX,1,Answer()
exten => _XXXX,2,NoOp(internal context called on Server A to {EXTEN}, CALLERID_num {CALLERID(num)}, CALLERID_num_f2 {CALLERID(num):0:2}) 
exten => _XXXX,3,Dial(SIP/{EXTEN},15)

```

That is, until we process the rest of the dial plan, where you once again, overwrite all 3 priorities of extension \_XXXX, and end up with this:

```auto
exten => _XXXX,1,AGI(agi://127.0.0.1:4577/call_log) 
exten => _XXXX,2,Dial(SIP/kamailio/{EXTEN}@95.217.223.95:5060,55,To) ; any call forward back to kamailio
exten => _XXXX,3,Hangup()

```

Same for your s extension, btw.

Also any AGI script you throw in to the mix, will make dial plan problems harder to diagnose.

If you are not completely confident with how Asterisk works, and how to write a proper dial plan, that should be where to start. Make a basic, version, that just dials whatever you want, then add whatever vicidicl functionality you want.

Another thing I notices, is the missing dollar sign in front of every variable, I assume this to be a. “Feature” of the forum software, if not, you need to add it.

To verify your dial plan looks like you expect, when loaded into asterisk, you can use `dialplan show` from the asterisk cli. If you want to only see a specifik context use `dialplan show context` and if you want to see how Asterisk will process a dialed extension in the specific content, use `dialplan show xxxx@context`

I suppose the dialplan above, is compiled from snippets copy and pasted from multiple sources, and not a complete example?

As for sharing my own extensions.conf, it will be impossible, as we are talking about 100s of lines. Most of then only relevant to our specific setup. Also I migrated everything away from chan\_sip about a year ago, and as such have not actual configuration left. (Config files are generated by custom software, and as such I only have the templates, that makes little sense as a generalized example.)

---

<div class="post-metadata">

**Author:** ![david551](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@david551](https://community.asterisk.org/u/david551)\
**Post date:** [November 11, 2020, 3:00pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/11 "2020-11-11T15:00:32Z")

</div>

> [@Chano](#):
>
> he missing dollar sign in front of every variable, I assume this to be a. “Feature” of the forum software

This is a very common effect of failing to use \</\>.

---

<div class="post-metadata">

**Author:** ![ShaikhTECH](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@ShaikhTECH](https://community.asterisk.org/u/ShaikhTECH)\
**Post date:** [November 11, 2020, 5:51pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/12 "2020-11-11T17:51:25Z")

</div>

Hi, Thank you So much for the detailed Response bro, I read it out and for sure gonna learn this all… I appreciate it.

and yes, you are right I don’t have much idea about the dialplans i have in my extensions.conf.

but today i researched something else and tried it, i got some success in this…  
(what i did was to put some of the conf dialplans from general context to my none-dial context) and i successfully got call when i login agent and able to listen the  
“conf-onlyperson.gsm” the only person audio… and also i was able to call 8101 to 8102 (but i dont know why not able to call vice versa)

but the issue now is (i suppose) now only at asterisk side because now again i am not able to call which was working before please help me in this and if you have idea why the behavior is intermittent so please point out the stuff…`Preformatted text`

and thanks a lot for the tip to post code in a better format… i had no idea about it as it is my first time posting here, and i am gonna remember this ahead. 🙂

```auto
my extensions.conf look like this now

[none-dial]

include => vicidial-auto

exten => _1122,1,Answer()
exten => _1122,n,Playback(demo-congrats)
exten => _1122,n,Hangup()

exten => _XXXXXXXXXX,1,AGI(agi-DID_route.agi)
exten => s,1,AGI(agi-DID_route.agi)

; Local blind monitoring
;exten => _08600XXX,1,Dial(${TRUNKblind}/6${EXTEN:1},55,To)
;exten => _08600XXX,n,Hangup()

; Local agent alert extensions
exten => _8600XXX*.,1,AGI(agi-VDADfixCXFER.agi)
exten => _8600XXX*.,n,Hangup()
exten => _78600XXX*.,1,AGI(agi-VDADfixCXFER.agi)
exten => _78600XXX*.,n,Hangup()

; astGUIclient conferences
exten => _86000[0-4]X,1,Meetme(${EXTEN},q)
exten => _86000[0-4]X,n,Hangup()

; VICIDIAL conferences
exten => _86000[5-9]X,1,Meetme(${EXTEN},F)
exten => _86000[5-9]X,n,Hangup()
exten => _8600[1-2]XX,1,Meetme(${EXTEN},F)
exten => _8600[1-2]XX,n,Hangup()

exten => _XXXX,1,AGI(agi://127.0.0.1:4577/call_log)
exten => _XXXX,2,Dial(${KAMAILIOTRUNK}/${EXTEN},,To) ; any call forward back to kamailio
exten => _XXXX,3,Hangup()

;exten => _XXXX,2,NoOp(internal context called on Server A to ${EXTEN},
;CALLERID_num ${CALLERID(num)}, CALLERID_num_f2 ${CALLERID(num):0:2})
;exten => _XXXX,3,Dial(SIP/${EXTEN},15)

exten => s,n,Dial(SIP/${EXTEN},,)
exten => s,n,Hangup()

; MeetMe Adjustment Extensions
exten => _X48600XXX,1,MeetMeAdmin(${EXTEN:2},T,${EXTEN:0:1})
exten => _X48600XXX,2,Hangup()

exten => _X38600XXX,1,MeetMeAdmin(${EXTEN:2},t,${EXTEN:0:1})
exten => _X38600XXX,2,Hangup()

exten => _X28600XXX,1,MeetMeAdmin(${EXTEN:2},m,${EXTEN:0:1})
exten => _X28600XXX,2,Hangup()

exten => _X18600XXX,1,MeetMeAdmin(${EXTEN:2},M,${EXTEN:0:1})
exten => _X18600XXX,2,Hangup()

exten => _55558600XXX,1,MeetMeAdmin(${EXTEN:4},K)
exten => _55558600XXX,2,Hangup()

exten => _8[0-2]XX,1,Meetme(8600${EXTEN:1},mqX) ; Listen
exten => _8[0-2]XX,n,Hangup()
exten => _99[0-2]XX,1,Meetme(8600${EXTEN:2},X) ; Barge
exten => _99[0-2]XX,n,Hangup()

; quiet entry and leaving conferences for VICIDIAL (inbound announce, SendDTMF and ring-agent)
exten => _78600XXX,1,Meetme(${EXTEN:1},Fq)
exten => _78600XXX,n,Hangup()

; quiet monitor-only extensions for meetme rooms (for room managers)
exten => _68600XXX,1,Meetme(${EXTEN:1},Fmq)
exten => _68600XXX,n,Hangup()

; Asterisk 1.8 workaround
exten => _58600XXX,1,Meetme(${EXTEN:1},Fmq)
exten => _58600XXX,n,Hangup()

; quiet monitor-only entry and leaving conferences for VICIDIAL (recording)
exten => _558600XXX,1,Meetme(${EXTEN:2},Fmq)
exten => _558600XXX,n,Hangup()

; voicelab exten
exten => _86009XX,1,Meetme(${EXTEN},Fmq)
; voicelab exten moderator
exten => _986009XX,1,Meetme(${EXTEN:1})

```

and now my sip.conf is

```auto
[kamailio]
type=friend
host=95.217.223.95
port=5060
transport=udp
disallow=all
allow=all
allow=gsm
allow=g729
allow=alaw
allow=ulaw
context=none-dial
insecure=invite,port
Permit=95.217.223.95
sipdebug=yes
canreinvite=no
dtmfmode=auto
srvlookup=yes
canreinvite=no

```

these are the logs in my asterisk -r while i am dialing and calling not working

```auto
[Nov 11 13:33:12] -- Registered SIP '8101' at 175.107.201.168:61846
[Nov 11 13:33:54] -- Registered SIP '8101' at 88.198.141.166:61846
[Nov 11 13:33:57] -- Unregistered SIP '8101'
[Nov 11 13:33:59] -- Registered SIP '8101' at 88.198.141.166:61846
[Nov 11 13:33:59] -- Unregistered SIP '8101'
[Nov 11 13:34:00] -- Registered SIP '8101' at 88.198.141.166:61846
[Nov 11 13:34:01] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:34:01] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:34:01] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:34:04] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:34:05] -- Unregistered SIP '8100'
[Nov 11 13:34:06] -- Registered SIP '8100' at 192.168.0.101:56643
[Nov 11 13:34:06] > Saved useragent "Z 3.15.40006 rv2.8.20" for peer 8100
[Nov 11 13:34:06] -- Registered SIP '8101' at 175.107.201.168:61846
[Nov 11 13:34:16] -- Unregistered SIP '8102'
[Nov 11 13:34:25] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:34:25] NOTICE[7689][C-00000001]: channel.c:5690 __ast_request_and_dial: Unable to request channel SIP/8101
[Nov 11 13:34:26] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:34:38] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:34:38] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:34:38] -- Executing [55558600054@default:1] MeetMeAdmin("Local/55558600054@default-00000000;2", "8600054,K") in new stack
[Nov 11 13:34:38] WARNING[7709][C-00000002]: app_meetme.c:5053 admin_exec: Conference number '8600054' not found!
[Nov 11 13:34:38] -- Executing [55558600054@default:2] Hangup("Local/55558600054@default-00000000;2", "") in new stack
[Nov 11 13:34:38] == Spawn extension (default, 55558600054, 2) exited non-zero on 'Local/55558600054@default-00000000;2'
[Nov 11 13:34:38] -- Executing [h@default:1] AGI("Local/55558600054@default-00000000;2", "agi://127.0.0.1:4577/call_log--HVcauses--PRI-----NODEBUG-----16---------------") in new stack
[Nov 11 13:34:38] -- <Local/55558600054@default-00000000;2>AGI Script agi://127.0.0.1:4577/call_log--HVcauses--PRI-----NODEBUG-----16--------------- completed, returning 0
[Nov 11 13:34:39] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:34:39] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:34:48] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:34:48] NOTICE[7726][C-00000003]: channel.c:5690 __ast_request_and_dial: Unable to request channel SIP/8102
[Nov 11 13:34:49] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:34:54] -- Registered SIP '8101' at 88.198.141.166:61846
[Nov 11 13:35:00] -- Registered SIP '8101' at 175.107.201.168:61846
[Nov 11 13:35:02] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:35:02] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:35:02] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:35:04] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:35:37] -- Registered SIP '8102' at 192.168.0.101:51817
[Nov 11 13:35:37] > Saved useragent "Bria release 6.0.1 stamp 102003" for peer 8102
[Nov 11 13:35:37] -- Unregistered SIP '8102'
[Nov 11 13:35:38] -- Registered SIP '8102' at 88.198.141.166:51817
[Nov 11 13:35:42] NOTICE[5105]: chan_sip.c:29904 sip_poke_noanswer: Peer '8102' is now UNREACHABLE! Last qualify: 0
[Nov 11 13:35:43] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:35:43] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:35:43] -- Executing [55558600056@default:1] MeetMeAdmin("Local/55558600056@default-00000001;2", "8600056,K") in new stack
[Nov 11 13:35:43] WARNING[7849][C-00000004]: app_meetme.c:5053 admin_exec: Conference number '8600056' not found!
[Nov 11 13:35:43] -- Executing [55558600056@default:2] Hangup("Local/55558600056@default-00000001;2", "") in new stack
[Nov 11 13:35:43] == Spawn extension (default, 55558600056, 2) exited non-zero on 'Local/55558600056@default-00000001;2'
[Nov 11 13:35:43] -- Executing [h@default:1] AGI("Local/55558600056@default-00000001;2", "agi://127.0.0.1:4577/call_log--HVcauses--PRI-----NODEBUG-----16---------------") in new stack
[Nov 11 13:35:43] -- <Local/55558600056@default-00000001;2>AGI Script agi://127.0.0.1:4577/call_log--HVcauses--PRI-----NODEBUG-----16--------------- completed, returning 0
[Nov 11 13:35:44] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:35:44] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:35:48] -- Registered SIP '8101' at 88.198.141.166:61846
[Nov 11 13:35:50] == Manager 'sendcron' logged on from 127.0.0.1
[Nov 11 13:35:50] NOTICE[7861][C-00000005]: channel.c:5690 __ast_request_and_dial: Unable to request channel SIP/8102
[Nov 11 13:35:51] == Manager 'sendcron' logged off from 127.0.0.1
[Nov 11 13:35:55] -- Registered SIP '8101' at 175.107.201.168:61846

```

and in my asterisk -r : "sip show peers " says

```auto
8100/8100 192.168.0.101 D Auto (No) No 61595 UNREACHABLE
8101/8101 88.198.141.166 D Auto (No) No 58293 UNREACHABLE
8102/8102 88.198.141.166 D Auto (No) No 57323 UNREACHABLE

and while kamailio peer looks fine in "sip show peers " 

kamailio 95.217.223.95 Yes Yes A 5060 OK (54 ms)

```

these are the extensions i have registered with Kamailio IP , and i am still not able to make or receive call. Please Help me in this too.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![Chano](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/chano/32/3448_2.png) [@Chano](https://community.asterisk.org/u/Chano)\
**Post date:** [November 20, 2020, 12:02pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/13 "2020-11-20T12:02:03Z")

</div>

It’s been a while since I used the sip module… I’ve switched to pjsip long ago. Therefore I’m not sure id the “UNREACHABLE” state won the peers, matters, or not.

But to move forward, you would first have to determine where the failure is.

If you do what I would expect, your extensions (8100, 8101, 8102) registers to the Asterisk server, and performs their calls there. Asterisk then uses Kamailio for outbound calls to the PSTN.

Calls local to Asterisk, should never leave Asterisk, unless you have a good reason (Eg. billing dependent on CDRs from Kamailio) and in that case, ONLY calls that needs to be routed for billing purposes, should be sent out of Asterisk. That is, calls between 8100, 8101 and 8102 should never leave Asterisk. From what you’ve written, I can’t really get an idea of how your signal flow is.

You make some calls to AGI scripts in the section posted above. These scripts are like black boxes, that can change internal state of Asterisk in almost anyway they want to, unless they are strictly needed to actually process the calls, you should take them out of the dial plan, until it’s working as expected, simply to make debugging much easier, when not having to worry what the AGI scripts may change.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/asterisk/original/2X/4/475d0c889911cfb84ac4881ad6df8b523e3f9105.png) [@system](https://community.asterisk.org/u/system)\
**Post date:** [December 20, 2020, 12:02pm UTC](https://community.asterisk.org/t/having-trouble-setup-kamailio-as-a-load-balancer-for-asterisk-server-using-module-dispatcher/86378/14 "2020-12-20T12:02:14Z")

</div>

Closed on December 20, 2020, 12:02pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
