control server failover from nagios via DNS

Val Vechnyak val at nycity.com
Tue Oct 28 18:39:37 CET 2003


Dear All,

I wrote this hack to run nsupdate when my primary server is not available to redirect traffic to my backup server.  this perl script uses two config files to give nsupdate all the stuff it needs to do the dynamic update.  One config is for the general configuration and one for each server.  I am finding this script useful and a) want to share with other people and b) hopefully improve it as I am FAR from being a programmer.  Nagios configuration is described in event handler doc.

failover.conf
# DNS server IP
server=192.168.1.1
# requesting server
tsigname=msoeq
# requesting server key
tsigkey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==


www.server.com.conf
# DNS records (hostnames) that will be effected when failover occures
# hostnames must be separated by a (,)
records=www.server.com,www1.server.com,www2.server.com,server.com
# TTL to set for each record
ttl=300
# IP of the primary server
primary=x.x.x.x
# IP of the backup server
backup=y.y.y.y



The actual script:

failover
#!/usr/bin/perl

use IO::File;
use POSIX qw(tmpnam);

$conf_path="/usr/local/failover";

$DOMAIN = "$ARGV[ 0 ]\.conf";
$SERVICESTATE = $ARGV[ 1 ];
$STATETYPE = $ARGV[ 2 ];
$SERVICEATTEMPT = $ARGV[ 3 ];

open(GEN_CONFIG, "< $conf_path/failover.conf")
        or die "Can't open $conf_path/failover.conf: $!\n";
while (<GEN_CONFIG>) {
    chomp;
    s/#.*//;
    s/^\s+//;
    s/\s+$//;
    next unless length;
    my ($var, $value) = split(/\s*=\s*/, $_, 2);
    $failover_params{$var} = $value;
}

open(SERVER_CONFIG, "< $conf_path/$DOMAIN")
    or die "Couldn't open $DOMAIN for reading: $!\n";

while (<SERVER_CONFIG>) {
    chomp;
    s/#.*//;
    s/^\s+//;
    s/\s+$//;
    next unless length;
    my ($var, $value) = split(/\s*=\s*/, $_, 2);
    $server_params{$var} = $value;
} 

# Build temp files based on how many hostnames will be effected.
        @array = split(/\s*,\s*/, $server_params{records});
        foreach $url (@array) {

                # Create a temp file
                $tmp_file = tmpnam();
                $fh = IO::File->new($tmp_file, O_RDWR|O_CREAT|O_EXCL);
                open(OUTFILE, ">>$tmp_file") or die "Can't write to $tmp_file: $!\n";

                if ($SERVICESTATE eq 'CRITICAL' && $STATETYPE eq 'HARD') {
                        print OUTFILE "server $failover_params{server}\nsend\n";
                        print OUTFILE "key $failover_params{tsigname} $failover_params{tsigkey}\nsend\n";
                        print OUTFILE "update delete $url A\nsend\n";
                        print OUTFILE "update add $url $server_params{ttl} A $server_params{backup}\nsend\n";

                        # perform actual update
                        system("/usr/bin/nsupdate -d $tmp_file");

                        }

                elsif ($SERVICESTATE eq 'CRITICAL' && $STATETYPE eq 'SOFT') {
                        print "Soft Critical State... Please wait... \n";
                        }

                elsif ($SERVICESTATE eq 'OK' && $STATETYPE eq 'HARD') {
                        print OUTFILE "server $failover_params{server}\nsend\n";
                        print OUTFILE "key $failover_params{tsigname} $failover_params{tsigkey}\nsend\n";
                        print OUTFILE "update delete $url A\nsend\n";
                        print OUTFILE "update add $url $server_params{ttl} A $server_params{primary}\nsend\n";

                        # perform actual update
                        system("/usr/bin/nsupdate -d $tmp_file");

                        }

                else {
                        }
                        # Delete temp file
                        unlink($tmp_file) or die "Can't delete $tmp_file: $!\n";
        }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/users/attachments/20031028/9215a0d9/attachment.html>


More information about the Users mailing list