0

I am trying to incorporate load balancing into the cfg file below. I'd like to route all my calls to available servers at table load_balancer. Please help. How do I ensure that my calls get distributed to my PBXs?

route{

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

    if (has_totag()) {

        # handle hop-by-hop ACK (no routing required)
        if ( is_method("ACK") && t_check_trans() ) {
            t_relay();
            exit;
        }

        # sequential request within a dialog should
        # take the path determined by record-routing
        if ( !loose_route() ) {
            if (is_method("SUBSCRIBE") && is_myself("$rd")) {
                # in-dialog subscribe requests
                route(handle_presence);
                exit;
            }
            # we do record-routing for all our traffic, so we should not
            # receive any sequential requests without Route hdr.
            send_reply(404,"Not here");
            exit;
        }

        # validate the sequential request against dialog
        if ( $DLG_status!=NULL && !validate_dialog() ) {
            xlog("In-Dialog $rm from $si (callid=$ci) is not valid according to dialog\n");
            ## exit;
        }

        if (is_method("BYE")) {
            # do accounting even if the transaction fails
            do_accounting("db","failed");

        }

        # route it out to whatever destination was set by loose_route()
        # in $du (destination URI).
        route(relay);
        exit;
    }

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

    # absorb retransmissions, but do not create transaction
    t_check_trans();

    if ( !(is_method("REGISTER")  || is_from_gw() ) ) {

        if (is_myself("$fd")) {

            # authenticate if from local subscriber
            # authenticate all initial non-REGISTER request that pretend to be
            # generated by local subscriber (domain from FROM URI is local)
            if (!proxy_authorize("", "subscriber")) {
                proxy_challenge("", "auth");
                exit;
            }
            if ($au!=$fU) {
                send_reply(403,"Forbidden auth ID");
                exit;
            }

            consume_credentials();
            # caller authenticated

        } else {
            # if caller is not local, then called number must be local

            if (!is_myself("$rd")) {
                send_reply(403,"Relay Forbidden");
                exit;
            }
        }

    }

    # preloaded route checking
    if (loose_route()) {
        xlog("L_ERR",
            "Attempt to route with preloaded Route's [$fu/$tu/$ru/$ci]");
        if (!is_method("ACK"))
            send_reply(403,"Preload Route denied");
        exit;
    }

    # record routing
    if (!is_method("REGISTER|MESSAGE"))
        record_route();

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

        # create dialog with timeout
        if ( !create_dialog("B") ) {
            send_reply(500,"Internal Server Error");
            exit;
        }

        do_accounting("db");

    }


    if (!is_myself("$rd")) {
        append_hf("P-hint: outbound\r\n");

        route(relay);
    }

    # requests for my domain

    if( is_method("PUBLISH|SUBSCRIBE"))
            route(handle_presence);

    if (is_method("REGISTER")) {
        # authenticate the REGISTER requests
        if (!www_authorize("", "subscriber")) {
            www_challenge("", "auth");
            exit;
        }

        if ($au!=$tU) {
            send_reply(403,"Forbidden auth ID");
            exit;
        }
        # store the registration and generate a SIP reply
        if (!save("location"))
            xlog("failed to register AoR $tu\n");

        exit;
    }

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


    # apply DB based aliases
    alias_db_lookup("dbaliases");


    # apply transformations from dialplan table
    dp_translate( 0, "$rU", $rU);


    if ($rU=~"^\+[1-9][0-9]+$") {

        strip(1);
        if (!do_routing(0)) {
            send_reply(500,"No PSTN Route found");
            exit;
        }

        route(relay);
        exit;
    }


    # do lookup with method filtering
    if (!lookup("location","m")) {
        if (!db_does_uri_exist("$ru","subscriber")) {
            send_reply(420,"Bad Extension");
            exit;
        }

        t_reply(404, "Not Found");
        exit;
    }



    # when routing via usrloc, log the missed calls also
    do_accounting("db","missed");

    route(relay);
}


route[relay] {
    # for INVITEs enable some additional helper routes
    if (is_method("INVITE")) {



        t_on_branch("per_branch_ops");
        t_on_reply("handle_nat");
        t_on_failure("missed_call");
    }



    if (!t_relay()) {
        send_reply(500,"Internal Error");
    }
    exit;
}


# Presence route
route[handle_presence]
{
    if (!t_newtran()) {
        sl_reply_error();
        exit;
    }

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

    exit;
}


branch_route[per_branch_ops] {
    xlog("new branch at $ru\n");
}


onreply_route[handle_nat] {

    xlog("incoming reply\n");
}


failure_route[missed_call] {
    if (t_was_cancelled()) {
        exit;
    }

    # uncomment the following lines if you want to block client
    # redirect based on 3xx replies.
    ##if (t_check_status("3[0-9][0-9]")) {
    ##t_reply(404,"Not found");
    ##  exit;
    ##}


}



local_route {
    if (is_method("BYE") && $DLG_dir=="UPSTREAM") {

        acc_db_request("200 Dialog Timeout", "acc");

    }
}

I am new to this. I tried the basic load-balancer script but it did not work. How do I ensure that my calls get distributed to my PBXs? I would like to route out going calls to a server with attribute=out and incoming calls with attribute=in THe PBX servers are asterisk instances.

  • Sample load balancing script DO work. You have debug what is problem, but that is offtopic on SO, go superuser. You also can try default dispatcher script. – arheops Jul 05 '23 at 16:22

0 Answers0