6 my $DEBUG = $ENV{DEBUG} || 0;
8 my $hostname = `hostname -s`;
12 sub XXX { $DEBUG ? warn "XXX ",Dumper( @_ ) : {} };
21 @nodes = map { chomp ; $_ } `gnt-node list -o name --no-header` unless @nodes;
26 $hostname = shift @nodes;
27 $ssh = "ssh $hostname ";
28 $hostname =~ s/\..+$//; # -s
29 warn "## hostname $hostname\n";
32 # if prefixed with _ it will be hiddden from output, _args must be last!
33 my @ps_cols = qw( user pid
44 sub ps_cols_all { map { my $t = $_; $t =~ s/^_//; $t } @ps_cols };
45 sub ps_cols_visible { 'node', grep { ! /^_/ } @ps_cols };
50 my @f = reverse ( 24, 60, 60, 1 );
51 my @p = reverse split(/[-:]/, $t);
56 #warn "### $i $p[$i] $f[$i]\n";
61 warn "# DD-hh:mm:ss $t -> $t_sec\n" if $DEBUG;
66 my $cmd = join(' ', @_);
69 open(my $fh, '-|', $cmd);
73 my $ps = sh 'ps --no-headers axwwo ' . join(',', ps_cols_all);
79 @h{@ps_cols} = split(/\s+/, $_, $#ps_cols + 1);
80 $h{cputime} = DD_hh_mm_ss( $h{cputime} );
83 if ( $h{user} =~ m/gnt/ && $h{_args} =~ m/qemu.*-name\s+(\S+)/ ) {
86 $stat->{$name}->{$_} = $h{$_} foreach ps_cols_all;
88 $stat->{$name}->{node} = $hostname;
90 while ( $h{_args} =~ m/mac=([0-9a-fA-F:]+)/g ) {
91 $mac_to_name->{$hostname}->{$1} = $name;
95 # warn "## SKIP [$_]\n";
96 $stat->{ '__' . $hostname }->{$_} += $h{$_} foreach qw( pcpu pmem vsz rss cputime etimes maj_flt min_flt );
103 my $tap = sh('grep . /var/run/ganeti/kvm-hypervisor/nic/*/*');
107 my @p = split(/\//,$_);
108 push @{ $instance_tap->{$hostname}->{ $p[-2] } }, $p[-1];
111 my $ip = sh('ip -s -o link');
114 if ( m/master\s+(\S+).+ether\s+([0-9a-fA-F:]+).+RX:\s+.+\\\s+(\d+).+TX:\s+.+\\\s+(\d+)/ ) {
115 my ( $br, $mac, $rx, $tx ) = ( $1, $2, $3, $4 );
116 if ( my $name = $mac_to_name->{$mac} ) {
117 $stat->{$name}->{link}->{ $br } = [ $rx, $tx ];
119 warn "## SKIP MAC $mac [$_]\n" if $DEBUG;
122 warn "## SKIP $_\n" if $DEBUG;
126 # dump some useful data structures
129 my ( $name, $hash ) = @_;
131 foreach my $key ( sort keys %$hash ) {
132 warn $key, "\t$hostname\t", $hash->{$key}, "\n";
136 tab_dump 'mac_to_name', $mac_to_name;
137 warn Dumper( $mac_to_name );
138 tab_dump 'instance_tap', $instance_tap;
139 warn Dumper( $instance_tap );
141 goto next_node if @nodes;
144 warn "# stat ", Dumper( $stat ) if $DEBUG;
147 # dump tablable ascii output
153 foreach my $i ( 0 .. $#l ) {
154 my $len = length($l[$i]) || 0;
155 $lines->{len}->[$i] ||= $len;
156 $lines->{len}->[$i] = $len if $len > $lines->{len}->[$i];
158 push @{ $lines->{line} }, [ map { ! defined $_ ? '-' : $_ } @l ];
161 push_line '#name', ps_cols_visible;
163 foreach my $name ( sort keys %$stat ) {
164 # printf "%6.2f %6.2f %8d %6d %6s %s\n", ( map { $stat->{$name}->{$_} || '' } qw( pcpu pmem vsz pid user ) ), $name;
165 # print join("\t", $name, map { $stat->{$name}->{$_} } ps_cols_visible ), "\n";
166 push_line( $name, map { $stat->{$name}->{$_} } ps_cols_visible );
171 my $fmt = join(' ', map { '%' . $_ . 's' } @{ $lines->{len} } ) . "\n";
172 warn "# fmt = [$fmt]" if $DEBUG;
173 foreach my $line ( @{ $lines->{line} } ) {