Repository navigation
Expand file tree
/
Copy pathlb_status
More file actions
executable file
·210 lines (158 loc) · 5.35 KB
/
Copy pathlb_status
File metadata and controls
executable file
·210 lines (158 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/perl
use strict;
# Display a summary of IPVS admin status
# you can also filter the status by 'service'
# so the output is suitable to be used by watch
#TCP 213.165.2.190:80 rr
# -> 213.165.2.201:80 Route 1 0 0
# -> 213.165.2.204:80 Route 1 0 0
# -> 213.165.2.209:80 Route 0 0 0
# -> 213.165.2.203:80 Route 1 0 0
# -> 213.165.2.210:80 Route 1 0 0
# -> 213.165.2.211:80 Route 1 0 0
# -> 213.165.2.213:80 Route 1 0 0
use Socket;
use Perl6::Form;
my $lb_status_ref = lb_status();
display_lb_status( $lb_status_ref, { services => [ @ARGV ] });
sub display_lb_status {
my $status_ref = shift;
my $options_ref = shift || {};
if ( !$status_ref ) {
die "No load balancing. Not running?";
}
my %status = %$status_ref;
if ( $options_ref && $options_ref->{services} ) {
%status = only_services( \%status, $options_ref->{services} );
}
my (@failures, @successes);
foreach my $service ( sort { lc($a) cmp lc($b) } keys %status ) {
my $services_ref = $status{ $service };
my $service_summary = "$service\n";
my $service_failed = 0;
foreach my $service_ref ( sort { $a->{port} <=> $b->{port} } @$services_ref ) {
my @status;
if ( $service_ref->{working} ) {
foreach my $active_ref ( sort { lc($a->{host}) cmp lc($b->{host}) } @{ $service_ref->{active} } ) {
push @status, $active_ref->{host};
}
}
else {
$service_failed = 1;
push @status, 'NOT WORKING';
}
my $protocol = 'port ' . $service_ref->{port};
$service_summary .= form " {[[[[[[[[[[[[[[[[[[[} {[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[}",
$protocol, \@status;
}
if ( $service_failed ) {
push @failures, $service_summary;
}
else {
push @successes, $service_summary;
}
}
my ( @summaries ) = grep { $_ } @failures, @successes;
print join( "--------------\n", @summaries );
}
sub lb_status {
open( my $fh, 'ipvsadm -L -n |' )
or die "Unable to run ipvsadm: $!";
my %services;
my %current_service;
my $save_service_ref = sub {
if ( $current_service{host} ) {
my $host = $current_service{host};
my $host_ref = $services{ $host } ||= [];
push @$host_ref, { %current_service };
}
%current_service = (
active => [],
inactive => [],
working => 0,
);
};
LINE:
while (<$fh>) {
chomp;
#TCP 213.165.2.190:443 wrr persistent 300
if ( m/TCP\s+(\S+)\s+(\S+)\s*(.+)?$/ ) {
$save_service_ref->();
my $ip_and_port = $1;
my $scheduler = $2;
my $options = $3;
my ($ip,$port) = split /:/, $ip_and_port;
my $host = ip_to_host( $ip );
$current_service{host} = $host || $ip;
$current_service{ip} = $ip;
$current_service{port} = $port;
$current_service{scheduler} = $scheduler;
next LINE;
}
if ( !$current_service{host} ) {
# print "Skipping $_\n";
next LINE;
}
# -> 213.165.2.213:80 Route 1 0 0
if ( m/->\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s*/ ) {
my $ip_and_port = $1;
my $type = $2;
my $weight = $3;
my $active = $4;
my $inactive = $5;
my ($ip, $port) = split /:/, $ip_and_port;
my $host = ip_to_host( $ip );
my %real_server = (
host => $host,
ip => $ip,
port => $port,
type => $type,
weight => $weight,
active => $active,
inactive => $inactive,
);
if ( $type eq 'Route' ) {
if ( $weight ) {
$current_service{working} = 1;
push @{ $current_service{active} }, \%real_server;
}
else {
push @{ $current_service{inactive} }, \%real_server;
}
}
else {
$current_service{working} = 0;
}
next LINE;
}
}
$save_service_ref->();
close $fh;
return \%services;
}
sub ip_to_host {
my $ip = shift;
my $iaddr = inet_aton( $ip );
return gethostbyaddr( $iaddr, AF_INET ) || $ip;
}
# removes all services but the ones specified
sub only_services {
my $status_ref = shift;
my $services_ref = shift;
my %status = %$status_ref;
my %only_services;
foreach my $service ( ref($services_ref) ? @$services_ref : $services_ref ) {
if ( $service ) {
$only_services{ $service } = 1;
}
}
if ( !%only_services ) {
return %status;
}
foreach my $service ( keys %status ) {
if ( !exists($only_services{ $service }) ) {
delete $status{ $service };
}
}
return %status;
}