# # $Id: opensips_loadbalancer.m4 9723 2013-02-01 15:10:50Z bogdan_iancu $ # # OpenSIPS loadbalancer script # by OpenSIPS Solutions # # This script was generated via "make menuconfig", from # the "Load Balancer" scenario. # You can enable / disable more features / functionalities by # re-generating the scenario with different options. # # Please refer to the Core CookBook at: # http://www.opensips.org/Resources/DocsCookbooks # for a explanation of possible statements, functions and parameters. # ####### Global Parameters ######### debug=3 log_stderror=no log_facility=LOG_LOCAL1 fork=yes children=4 /* uncomment the following lines to enable debugging */ #debug=6 #fork=no #log_stderror=yes /* uncomment the next line to enable the auto temporary blacklisting of not available destinations (default disabled) */ #disable_dns_blacklist=no /* uncomment the next line to enable IPv6 lookup after IPv4 dns lookup failures (default disabled) */ #dns_try_ipv6=yes /* comment the next line to enable the auto discovery of local aliases based on revers DNS on IPs */ auto_aliases=no listen=udp:10.150.16.16:5060 # CUSTOMIZE ME disable_tcp=no listen=tcp:10.150.16.16:5060 # CUSTOMIZE ME #disable_tls=yes ####### Modules Section ######## #set module path mpath="/usr/local/opensips/lib64/opensips/modules/" #### SIGNALING module loadmodule "signaling.so" #### StateLess module loadmodule "sl.so" #### Transaction Module loadmodule "tm.so" modparam("tm", "fr_timer", 30) modparam("tm", "fr_inv_timer", 120) modparam("tm", "restart_fr_on_each_reply", 0) modparam("tm", "onreply_avp_mode", 1) #### Record Route Module loadmodule "rr.so" /* do not append from tag to the RR (no need for this script) */ modparam("rr", "append_fromtag", 0) #### MAX ForWarD module loadmodule "maxfwd.so" #### SIP MSG OPerationS module loadmodule "sipmsgops.so" #### FIFO Management Interface loadmodule "mi_fifo.so" modparam("mi_fifo", "fifo_name", "/tmp/opensips_fifo") modparam("mi_fifo", "fifo_mode", 0666) #### URI module loadmodule "uri.so" modparam("uri", "use_uri_table", 0) #### MYSQL module loadmodule "db_mysql.so" #### AVPOPS module loadmodule "avpops.so" #### ACCounting module loadmodule "acc.so" /* what special events should be accounted ? */ modparam("acc", "early_media", 0) modparam("acc", "report_cancels", 0) /* by default we 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) modparam("acc", "failed_transaction_flag", "ACC_FAILED") /* account triggers (flags) */ modparam("acc", "log_flag", "ACC_DO") modparam("acc", "log_missed_flag", "ACC_MISSED") #### DIALOG module loadmodule "dialog.so" modparam("dialog", "dlg_match_mode", 1) modparam("dialog", "default_timeout", 840) # 14 minutes timeout modparam("dialog", "db_mode", 2) modparam("dialog", "db_url", "mysql://opensips:opensipsrw@localhost/opensips") # CUSTOMIZE ME #### LOAD BALANCER module loadmodule "load_balancer.so" modparam("load_balancer", "db_url", "mysql://opensips:opensipsrw@localhost/opensips") # CUSTOMIZE ME modparam("load_balancer", "probing_method", "OPTIONS") modparam("load_balancer", "probing_interval", 15) #### Load Statistics Module loadmodule "statistics.so" ####### Routing Logic ######## # main request routing logic route{ if (!mf_process_maxfwd_header("3")) { send_reply("483","looping"); exit; } if ( has_totag() ) { # sequential request -> obey Route indication xlog("Loose Route for $avp(iurl) over $avp(dest) $avp(calltype) \n"); loose_route(); t_relay(); exit; } # handle cancel and re-transmissions if ( is_method("CANCEL") ) { if ( t_check_trans() ) t_relay(); exit; } # from now on we have only the initial requests if (!is_method("INVITE")) { send_reply("405","Method Not Allowed"); exit; } # initial request record_route(); $avp(iurl) = $(hdr(P-Nuance-Initial-URL)); $avp(calltype) = $(hdr(P-Nuance-Call-Type)); xlog("Sending call $fu $avp(iurl) $avp(calltype) \n"); # LB function returns negative if no suitable destination (for requested resources) is found, # or if all destinations are full if ( !load_balance("1","channel") ) { xlog("All Destinations are full. Sending 500! $fu $(hdr(P-Nuance-Initial-URL))"); send_reply("500","Service full"); exit; } $avp(dest) = $du; xlog("Call sent over $avp(dest) $fu $(hdr(P-Nuance-Initial-URL))\n"); t_on_reply("reply"); # arm a failure route for be able to catch a failure event and to do # failover to the next available destination t_on_failure("LB_failed"); # send it out if (!t_relay()) { sl_reply_error(); } } onreply_route[reply] { xlog("Reply $rs received from $si for the call $avp(iurl)"); $var(node_success_stats) = "nvp_success_"+$si; $var(node_failure_stats) = "nvp_failure_"+$si; xlog("Reply Route Monitoring counts for $si Failed-$stat($var(node_failure_stats)) Success-$stat($var(node_success_stats))"); if (t_check_status("200")) { update_stat("$var(node_success_stats)","+1"); if($stat($var(node_success_stats)) > 4){ reset_stat("$var(node_success_stats)"); reset_stat("$var(node_failure_stats)"); xlog("Reply Route Monitoring Counts Reset for $si"); } } } failure_route[LB_failed] { # skip if call was canceled if (t_was_cancelled()) { exit; } xlog("Call $avp(iurl) sent over $avp(dest) failed with code $T_reply_code\n"); # Checking only for Destination Failure # If the destination is failed, disable it and reroute the call #if ( t_check_status("[56][0-9][0-9]")) { if ( t_check_status("503")|| t_check_status("486")) { $var(node_failed_stats) = "nvp_failure_"+$(avp(dest){s.select,1,:}); update_stat("$var(node_failed_stats)","+1"); xlog("Failure Route Monitoring counts for $var(node_failed_stats) Failed-$stat($var(node_failed_stats)) "); if($stat($var(node_failed_stats)) > 16){ reset_stat("$var(node_failed_stats)"); # this is a case for failover xlog("Marking $avp(dest) $var(node_failed_stats) disabled as it failed for call $avp(iurl) with code $T_reply_code $stat($var(node_failed_stats)) "); lb_disable(); } } send_reply("$T_reply_code","Call Failed"); }