BEGIN { $sockaddr_t = 'S n a4 x8'; $WEIGHT_PER_USER = 100; # should be consistent with poller $need_maint = 0; $poller_sleep = 300; $poller_config = "sweet.config"; $hostmaster = "root.corelab11"; $hostIndex = 0; %touched = ("corelab3" => 0, "corelab5" => 0, "corelab9" => 0); $c1 = &convertIP("172.20.13.1"); $c2 = &convertIP("172.20.13.2"); $c3 = &convertIP("172.20.13.3"); $c4 = &convertIP("172.20.13.4"); $c5 = &convertIP("172.20.13.5"); $c6 = &convertIP("172.20.13.6"); $c7 = &convertIP("172.20.13.7"); $c8 = &convertIP("172.20.13.8"); $c9 = &convertIP("172.20.13.9"); $c10 = &convertIP("172.20.13.10"); $c11 = &convertIP("172.20.13.11"); sub convertIP { ($ip) = @_; $_ = $ip; ($a, $b, $c, $d) = /(\d+)\.(\d+)\.(\d+)\.(\d+)/; return ($a<<24)|($b<<16)|($c<<8)|$d; } # sub convertIP } #BEGIN ## NOTE: handle_best_request still contains two hard-coded references ## to stanford.edu. You should change them to match your domain. LBDB::add_static("hobbit",T_SOA,rr_SOA(hostname,$hostmaster,time,86400,86400,86400,0)); LBDB::add_static("hobbit",T_NS,rr_NS("corelab11.hobbit")); LBDB::add_static("corelab11",T_A,rr_A($c11)); LBDB::add_static("corelab11.hobbit",T_A,rr_A($c11)); LBDB::add_static("11.13.20.172.in-addr.arpa",T_PTR,rr_PTR("corelab11")); LBDB::add_static("corelab9",T_A,rr_A($c9)); LBDB::add_static("corelab9.hobbit",T_A,rr_A($c9)); LBDB::add_static("9.13.20.172.in-addr.arpa",T_PTR,rr_PTR("corelab9")); LBDB::add_static("corelab5",T_A,rr_A($c5)); LBDB::add_static("corelab5.hobbit",T_A,rr_A($c5)); LBDB::add_static("5.13.20.172.in-addr.arpa",T_PTR,rr_PTR("corelab5")); LBDB::add_static("corelab3",T_A,rr_A($c3)); LBDB::add_static("corelab3.hobbit",T_A,rr_A($c3)); LBDB::add_static("3.13.20.172.in-addr.arpa",T_PTR,rr_PTR("corelab3")); LBDB::add_dynamic("round.robin.hobbit" => \&handle_round_robin_request); LBDB::add_dynamic("best.hobbit" => \&handle_best_request); ############################################################ # dynamic domain handlers... sub handle_round_robin_request { my($domain,$residual,$qtype,$qclass,$dm) = @_; my($the_host,$the_ip,$answer,$qname,$group); @hostArray = ("corelab3", "corelab5", "corelab9"); $hostCount = scalar @hostArray; %ipArray = ( "corelab3" => "172.20.13.3", "corelab5" => "172.20.13.5", "corelab9" => "172.20.13.9" ); $qname = $residual; if ($qtype == T_A || $qtype == T_MX || $qtype == T_ANY) { $the_host = $hostArray[$hostIndex]; &write_log("Round Robin: Using: $the_host"); $hostIndex++; if ($hostIndex >= $hostCount) { $hostIndex = 0; } $ipaddr = $ipArray{$the_host}; $_ = $ipaddr; ($a,$b,$c,$d) = /(\d+)\.(\d+)\.(\d+)\.(\d+)/; $the_ip = ($a<<24)|($b<<16)|($c<<8)|$d; $the_host .= ".hobbit" if ($the_host !~ /\.hobbit/i); $answer = dns_answer(QPTR, T_CNAME, C_IN, 0, rr_CNAME($the_host)); $answer .= dns_answer( dns_simple_dname($the_host), T_A,C_IN,3600, rr_A($the_ip)); $dm->{'answer'} .= $answer; $dm->{'ancount'} += 2; } else { $dm->{'rcode'} = NXDOMAIN; } return 1; } sub by_weight { $weight{$b} <=> $weight{$a}; } sub handle_best_request { my($domain,$residual,$qtype,$qclass,$dm) = @_; my($the_host,$the_ip,$answer,$qname,$group); %ipArray = ( "corelab3" => "172.20.13.3", "corelab5" => "172.20.13.5", "corelab9" => "172.20.13.9" ); $qname = $residual; open(STATFILE, ") { @lineArray = split /:/, $line; $host = $lineArray[0]; $_ = $host; $host = $1 if (/^(\w+)\./); $hosts[$i] = $host; $weight = $lineArray[1]; #$weight -= $touched{$host}; #$weight -= $touched{$host}*10; $weight{$host} = $weight; $i++; } @hosts = sort by_weight @hosts; if ($qtype == T_A || $qtype == T_MX || $qtype == T_ANY) { $the_host = @hosts[0]; &write_log("Best: Using: $the_host with $weight{$the_host}"); $touched{$the_host} += 1; foreach $h (keys %touched) { if (($h ne $the_host) && ($touched{$h} != 0)) { $touched{$h} -= 1; } } $ipaddr = $ipArray{$the_host}; $_ = $ipaddr; ($a,$b,$c,$d) = /(\d+)\.(\d+)\.(\d+)\.(\d+)/; $the_ip = ($a<<24)|($b<<16)|($c<<8)|$d; $the_host .= ".hobbit" if ($the_host !~ /\.hobbit/i); $answer = dns_answer(QPTR, T_CNAME, C_IN, 0, rr_CNAME($the_host)); $answer .= dns_answer( dns_simple_dname($the_host), T_A,C_IN,3600, rr_A($the_ip)); $dm->{'answer'} .= $answer; $dm->{'ancount'} += 2; } else { $dm->{'rcode'} = NXDOMAIN; } close(STATFILE); return 1; } ############################################################ # standard hooks sub catch_hup { $need_maint=1; $need_reload=1; } sub clean_exit { &write_log("received signal, exiting...") ; &close_log; exit(0); } sub init_signals { $::SIG{'INT'} = 'clean_exit'; $::SIG{'QUIT'} = 'clean_exit'; $::SIG{'TERM'} = 'clean_exit'; $::SIG{'HUP'} = 'catch_hup'; } &init_signals; 1;