Follow the tutorial here to implement LCR with kamailio :

  1. Add lcr.so to load module

loadmodule lcr.so

  1. Add required parameters to the module
    modparam(“lcr”,”db_url”,DBURL)
    modparam(“lcr”, “gw_uri_avp”, “$avp(i:709)”)
    modparam(“lcr”, “ruri_user_avp”, “$avp(i:500)”)
    modparam(“lcr”, “flags_avp”, “$avp(i:712)”)
    modparam(“lcr”, “lcr_id_avp”, “$avp(s:lcr_id_avp)”)

Now you can use create route[LCR] and put in routing block before route[location] or where ever you want to place it.

route[LCR] {
if (!load_gws(1)) {
sl_send_reply(“503”, “Unable to load gateways”);
exit;
} else {
$var(i) = 0;
while(is_avp_set(“$(avp(i:709)[$var(i)])”)) {
xlog(“L_INFO”, “loading gw_uri_avp[$var(i)]=’$(avp(i:709)[$var(i)])’\n”);
$var(i) = $var(i) + 1;
};
if(is_avp_set(“$avp(i:709)”)) {
xlog(“L_INFO”, “Trying gateway ‘$avp(i:709)’\n”);
} else {
xlog(“L_INFO”, “No More Gateways …\n”);
};

try the first matched gateway

if (next_gw()) {
xlog(“L_INFO”, “ruri_user_avp=’$avp(i:500)’\n”);

Route to failure for failover

t_on_failure(“2”);
route(RELAY);
} else {
sl_send_reply(“503”, “No available gateways”);
exit;
};
};
exit;
}

failure_route[2] {
xlog(“L_INFO”, “entering failure_route[2] for reply code ‘$T_reply_code’\n”);

the previous gateway is no good

if (t_check_status(“408|50[34]”)) {
if(is_avp_set(“$avp(i:709)”)) {
xlog(“L_INFO”, “trying next gateway ‘$avp(i:709)’\n”);
} else {
xlog(“L_INFO”, “no more gateways to try …\n”);
};

route to the next gateway

if (next_gw()) {

prepare for lcr failover

t_on_failure(“2”);
route(RELAY);
} else {
t_reply(“503”, “No gateways”);
exit;
};
exit;
};
}