Hi,
When I need to disable a back-end I usually need to disable that server from
ALL services, so I wrote this perl script to enable/disable a server from
all global services at once.
Note that it only acts on global services because that's all I have. If you
would like it to work for services in particular listeners, send me the
output of your poundctl -X.
- Jason
#!/usr/bin/perl
#
# yank - turn a pound backend server on or off.
# NOTE - right now this only works on global services (listener = -1).
#
# change $pound_path values to be your poundctl and pound.sock
#
# USAGE:
# -s = servername
# -d disable server
# -u enable server
# Example:
# Take down w2:
# yank -s w2 -d
#
# bring up w2:
# yank -s w2 -u
# change this to point to your poundctl and pound.sock:
my $pound_path = "/usr/local/sbin/poundctl -c /etc/pound/pound.sock";
use XML::Parser;
use Getopt::Std;
my %args;
# s = servername
# u = up (boolean)
# d = down (boolean)
getopts('uds:',\%args);
if (not $args{s}
or (not $args{u} and not $args{d})
or ($args{u} and $args{d})) {
# bad arguments. need -s and either -d OR -u
die "Usage: (d= down, u = up)\nDisable: yank -s servername -d\nEnable:
yank -s servername -u\n";
}
my $xml_data = `$pound_path -X`;
#modify the xml to make it valid for our parser:
# these properties are not in attribute=value form
$xml_data =~ s/HTTPS//g;
$xml_data =~ s/HTTP//g;
$xml_data =~ s/DISABLED//g;
my $pl = new XML::Parser(Style => 'Tree', ErrorContext=>2);
my $tree = $pl->parse($xml_data);
# Find this server in all listeners, and all global services,
# capture the listener, service and backend numbers
my $pound = $tree->[1];
# store found backends for the requested server here,
# as arrays of [listener, service, backend]
my @backends;
# LISTENTERS
#--------------
my @listeners;
for (my $l = 0; $l < @{$pound}; $l+=4) {
if ($pound->[$l+3] eq "listener") {
# found a listener
my %listener_attributes = %{$pound->[$l+4][0]};
push @listeners, \%listener_attributes;
# print "Listenter $listener_attributes{index} is
$listener_attributes{address}\n";
# TODO: go through the services of this listener, and find matching backends
}
}
# Global Services
#----------------------
# after all listeners come global services.
# go through the global services, find matching backends and add them to
@backends
#
my @global_services;
for (my $s = 0; $s < @{$pound}; $s+=4) {
if ($pound->[$s+3] eq "service") {
# found a service
my %service_attributes = %{$pound->[$s+4][0]};
push @global_services, \%service_attributes;
#print "Service $service_attributes{index} is
$service_attributes{name}\n";
my $current_service = $service_attributes{index};
# get the backends
for (my $be = 3; $be < @{$pound->[$s+4]}; $be+= 4) {
my %be_info = %{$pound->[$s+4][$be+1][0]};
my ($servername,$address) = split(":",$be_info{address});
next if $servername ne $args{s};
#print "\tBE is $be_info{index}\t$be_info{address}\n";
# this is the selected server,
# mark it for enabling/disabling. Listener index for global services
is -1:
push @backends, [-1, $current_service, $be_info{index}];
}
}
}
# now enable/disable
foreach my $backend (@backends) {
my ($listener, $service, $be) = @{$backend};
if ($args{d}) {
# disable
print "$pound_path -b $listener $service $be\n";
`$pound_path -b $listener $service $be`;
} elsif ($args{u}) {
# enable
print "$pound_path -B $listener $service $be\n";
`$pound_path -B $listener $service $be`;
}
}
# done
--
To unsubscribe send an email with subject unsubscribe to [email protected].
Please contact [email protected] for questions.