3
use POSIX ':sys_wait_h';
8
while($ARGV[0] eq '-q') {
13
usage() if ($#ARGV != 1);
14
my $sleeptime = sprintf('%d', $ARGV[1]);
15
die("$0: bad interval $ARGV[1]\n") if ($sleeptime < 2);
17
# Clean up any zombies
18
while (waitpid(-1, WNOHANG) > 0) { }
23
# Sleep for the duration of the interval
25
while ($slept < $sleeptime) {
26
$slept += sleep($sleeptime - $slept);
30
# Show usage and croak
34
Usage: xr-client-ping [-q] WEBINTERFACE-URL INTERVAL
35
The web interface is queried for clients. Connections to non-pingable clients
36
are killed. The process is repeated each interval.
39
-q: quiet mode, suppresses verbose messaging
40
WEBINTERFACE-URL: the URL of XR's web interface, include http://
41
INTERVAL: number of seconds
49
msg ("-----------------------------------------------------------------\n");
50
msg ("Starting check run\n");
53
$xml = http_get($ARGV[0]);
56
msg ("Could not access web interface: $@\n");
57
die ("Too many tries now, giving up...\n") if ($_tries++ > 5);
64
for my $line (split(/\n/, $xml)) {
65
$active = 1 if ($line =~ /<thread>/);
66
$active = 0 if ($line =~ /<\/thread>/);
69
if ($line =~ /<id>/) {
73
} elsif ($line =~ /<clientip>/) {
75
$clientip =~ s/\s*<clientip>//;
76
$clientip =~ s/<\/clientip>//;
77
check_client($id, $clientip) if ($clientip ne '0.0.0.0');
83
# Check one thread ID and client IP
84
sub check_client($$) {
85
my ($id, $clientip) = @_;
88
msg ("Checking connection for client $clientip (XR thread $id)\n");
91
my $cmd = "ping -c3 -t3 $clientip >/dev/null";
92
msg ("$clientip: pinging (external '$cmd')\n");
93
my $status = system($cmd);
95
msg ("$clientip: ping status '$status' $!\n");
96
msg ("$clientip: not reachable, stopping XR thread $id\n");
98
http_get("$ARGV[0]/thread/kill/$id");
100
msg ("Failed to stop thread $id\n") if ($@);
102
msg ("$clientip: reachable, connection assumed valid\n");
107
# Do a HTTP GET. Try LWP::UserAgent if available, else try wget.
114
require LWP::UserAgent;
117
$ua = LWP::UserAgent->new();
119
my $res = $ua->get($url);
120
die ("Could not access url '$url'\n")
121
unless ($res->is_success());
122
return $res->content();
125
# Try wget or curl, or any other command (can be put in here)
126
for my $cmd ("wget -q -O- -T3 '$url'",
127
"curl --connect-timeout 3 -s '$url'") {
128
msg ("Running: $cmd\n");
129
open (my $if, "$cmd |");
132
while (my $line = <$if>) {
138
msg("$cmd failed: $!\n");
143
# All failed, now what?
144
die ("No method to access url '$url'\n");
149
print ($$, ' ', scalar(localtime()), ' ', @_) unless ($quiet);