forked from ffzg/gnt-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnt-i
executable file
·239 lines (183 loc) · 5.37 KB
/
gnt-i
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/perl
use warnings;
use strict;
use autodie;
use Storable;
my $DEBUG = $ENV{DEBUG} || 0;
my $INFLUX = 'http://10.60.0.89:8186/write';
my $hostname = `hostname -s`;
chomp $hostname;
use Data::Dumper;
sub XXX { $DEBUG ? warn "XXX ",Dumper( @_ ) : {} };
my $stat;
my $mac_to_name;
my $instance_tap;
my $tap_instance;
my $lines;
my @nodes = @ARGV;
@nodes = map { chomp ; s/\s+N$// ; $_ } grep { /N$/ } `gnt-node list -o name,offline --no-header` unless @nodes;
next_node:
my $ssh = '';
if ( @nodes ) {
$hostname = shift @nodes;
$ssh = "ssh $hostname ";
$hostname =~ s/\..+$//; # -s
warn "## hostname $hostname\n";
}
# if prefixed with _ it will be hiddden from output, _args must be last!
my @ps_cols = qw( user pid
pcpu pmem
vsz
rss
nlwp
cputime etimes
maj_flt min_flt
psr
_args);
sub ps_cols_all { map { my $t = $_; $t =~ s/^_//; $t } @ps_cols };
sub ps_cols_visible { 'node', grep { ! /^_/ } @ps_cols, 'rx', 'tx' };
sub DD_hh_mm_ss {
my $t = shift;
# [[DD-]hh:]mm:ss.
my @f = reverse ( 24, 60, 60, 1 );
my @p = reverse split(/[-:]/, $t);
my $t_sec = 0;
for ( 0 .. $#p ) {
my $i = $#p - $_;
#warn "### $i $p[$i] $f[$i]\n";
$t_sec += $p[$i];
$t_sec *= $f[$i];
}
# warn "# DD-hh:mm:ss $t -> $t_sec\n" if $DEBUG;
return $t_sec;
}
sub sh {
my $cmd = join(' ', @_);
$cmd = "$ssh '$cmd'";
warn "## $cmd\n";
open(my $fh, '-|', $cmd);
return $fh;
}
my $ps = sh 'ps --no-headers axwwo ' . join(',', ps_cols_all);
while(<$ps>) {
chomp;
s/^\s*//;
my %h;
@h{@ps_cols} = split(/\s+/, $_, $#ps_cols + 1);
$h{cputime} = DD_hh_mm_ss( $h{cputime} );
#XXX 'h = ', \%h;
if ( $h{user} =~ m/gnt/ && $h{_args} =~ m/qemu.*-name\s+(\S+)/ ) {
my $name = $1;
$stat->{$name}->{$_} = $h{$_} foreach ps_cols_all;
$stat->{$name}->{node} = $hostname;
while ( $h{_args} =~ m/mac=([0-9a-fA-F:]+)/g ) {
$mac_to_name->{$hostname}->{$1} = $name;
}
} else {
# warn "## SKIP [$_]\n";
$stat->{ '__' . $hostname }->{$_} += $h{$_} foreach qw( pcpu pmem vsz rss
nlwp cputime etimes
maj_flt min_flt
);
}
}
my $tap = sh('grep -H . /var/run/ganeti/kvm-hypervisor/nic/*/*');
while(<$tap>) {
chomp;
my @p = split(/\//,$_);
# warn "## tap ", Dumper( \@p ), "$_\n";
push @{ $instance_tap->{$hostname}->{ $p[-2] } }, $p[-1];
my $if = $p[-1];
$if =~ s/\d://;
$tap_instance->{$hostname}->{$if} = $p[-2];
}
=for later
my $ip = sh('ip -s -o link');
while(<$ip>) {
chomp;
if ( m/master\s+(\S+).+ether\s+([0-9a-fA-F:]+).+RX:\s+.+\\\s+(\d+).+TX:\s+.+\\\s+(\d+)/ ) {
my ( $if, $mac, $rx, $tx ) = ( $1, $2, $3, $4 );
if ( my $name = $tap_instance->{$hostname}->{$if} ) {
$stat->{$name}->{link}->{ $if } = [ $rx, $tx ];
warn "## ip $name $ip $mac $rx $tx\n";
} else {
warn "## ip SKIP $if $mac $rx $tx\n"; # XXX if $DEBUG;
}
} else {
warn "## SKIP $_\n" if $DEBUG;
}
}
=cut
my $dev = sh 'grep : /proc/net/dev';
while(<$dev>) {
chomp;
s/^\s+//;
my @l = split(/[:\s]+/, $_);
#warn "XXX $_ -> ",Dumper( \@l );
if ( my $instance = $tap_instance->{$hostname}->{ $l[0] } ) {
$stat->{$instance}->{rx} += $l[1];
$stat->{$instance}->{tx} += $l[9];
} elsif ( $l[0] =~ m/eth/ ) { # connect hardware eth devices under node
$stat->{ '__' . $hostname }->{rx} += $l[1];
$stat->{ '__' . $hostname }->{tx} += $l[9];
}
}
goto next_node if @nodes;
# dump some useful data structures
sub tab_dump {
my ( $name, $hash ) = @_;
warn "# $name\n";
foreach my $key ( sort keys %$hash ) {
warn $key, "\t", $hash->{$key}, "\n";
}
}
foreach my $node ( sort keys %$mac_to_name ) {
tab_dump "$node mac instance", $mac_to_name->{$node};
}
#warn Dumper( $mac_to_name );
#tab_dump 'instance_tap', $instance_tap;
#warn Dumper( $instance_tap );
#warn Dumper( $tap_instance );
warn "# stat ", Dumper( $stat ) if $DEBUG;
# dump tablable ascii output
#XXX( @ps_cols );
sub push_line {
my @l = @_;
foreach my $i ( 0 .. $#l ) {
my $len = length($l[$i]) || 0;
$lines->{len}->[$i] ||= $len;
$lines->{len}->[$i] = $len if $len > $lines->{len}->[$i];
}
push @{ $lines->{line} }, [ map { ! defined $_ ? '-' : $_ } @l ];
}
push_line '#name', ps_cols_visible;
foreach my $name ( sort keys %$stat ) {
# printf "%6.2f %6.2f %8d %6d %6s %s\n", ( map { $stat->{$name}->{$_} || '' } qw( pcpu pmem vsz pid user ) ), $name;
# print join("\t", $name, map { $stat->{$name}->{$_} } ps_cols_visible ), "\n";
push_line( $name, map { $stat->{$name}->{$_} } ps_cols_visible );
}
#XXX $lines;
open(my $influx_fh, '>', '/dev/shm/ganeti.influx');
my $last_lines;
$last_lines = retrieve('/dev/shm/ganeti.last.storable') if -e '/dev/shm/ganeti.last.storable';
my $fmt = join(' ', map { '%' . $_ . 's' } @{ $lines->{len} } ) . "\n";
warn "# fmt = [$fmt]" if $DEBUG;
foreach my $i ( 0 .. $#{ $lines->{line} } ) {
my $line = $lines->{line}->[$i];
printf $fmt, @$line;
next unless $last_lines;
my $last_line = $last_lines->[$i];
my @c = ( 'instance', ps_cols_visible );
my @influx;
foreach my $j ( 0 .. $#$line ) {
#push @influx, $c[$j] . '=' . $line->[$j];
push @influx, $c[$j] . '='
. ( $j >= 9 && $j != 13 ? $line->[$j] - $last_line->[$j] : $line->[$j] )
. ( $j == 3 || $j >= 6 && $j != 9 ? 'i' : '' );
}
my $influx = join(',', @influx);
$influx =~ s/,user=([^,]+)/ user="$1"/;
print $influx_fh "ganeti,$influx\n" unless $influx =~ m/(#|__)/;
}
store $lines->{line}, '/dev/shm/ganeti.last.storable';
#system "curl -i -XPOST 'http://127.0.0.1:8186/write?db=ganeti' --data-binary '@/dev/shm/ganeti.influx'";