find tap device for instance on given node
[gnt-info] / gnt-i
diff --git a/gnt-i b/gnt-i
index f1aa4fa..29cf191 100755 (executable)
--- a/gnt-i
+++ b/gnt-i
@@ -9,22 +9,40 @@ my $hostname = `hostname -s`;
 chomp $hostname;
 
 use Data::Dumper;
-sub XXX { warn "XXX ",Dumper( @_ ) };
+sub XXX { $DEBUG ? warn "XXX ",Dumper( @_ ) : {} };
 
+my $stat;
+my $mac_to_name;
+my $instance_tap;
+my $lines;
+
+my @nodes = @ARGV;
+
+@nodes = map { chomp ; $_ } `gnt-node list -o name --no-header` unless @nodes;
+
+next_node:
 my $ssh = '';
-if ( @ARGV ) {
-       $hostname = $ARGV[0];
+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 cputime etimes _args);
+my @ps_cols = qw( user pid
+       pcpu pmem
+       vsz
+       rss
+       nlwp
+       cputime etimes 
+       maj_flt min_flt
+       psr
 
-sub ps_cols_all { map { my $t = $_; $t =~ s/^_//; $t } @ps_cols };
-sub ps_cols_visible { grep { ! /^_/ } @ps_cols };
+       _args);
 
-my $stat;
-my $mac_to_name;
+sub ps_cols_all { map { my $t = $_; $t =~ s/^_//; $t } @ps_cols };
+sub ps_cols_visible { 'node', grep { ! /^_/ } @ps_cols };
 
 sub DD_hh_mm_ss {
        my $t = shift;
@@ -40,19 +58,26 @@ sub DD_hh_mm_ss {
                $t_sec *= $f[$i];
        }
 
-       warn "# DD-hh:mm:ss $t -> $t_sec\n";
+       warn "# DD-hh:mm:ss $t -> $t_sec\n" if $DEBUG;
        return $t_sec;
 }
 
-my $cmd = 'ps --no-headers axwwo ' . join(',', ps_cols_all);
-warn "## $cmd\n";
-open(my $ps, '-|', $ssh . $cmd);
+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+)/ ) {
@@ -60,23 +85,30 @@ while(<$ps>) {
                my $name = $1;
                $stat->{$name}->{$_} = $h{$_} foreach ps_cols_all;
 
-               $stat->{$name}->{cputime_} = $stat->{$name}->{cputime} = DD_hh_mm_ss( $stat->{$name}->{cputime} );
+               $stat->{$name}->{node} = $hostname;
 
                while ( $h{_args} =~ m/mac=([0-9a-fA-F:]+)/g ) {
-                       $mac_to_name->{$1} = $name;
+                       $mac_to_name->{$hostname}->{$1} = $name;
                }
 
        } else {
 #              warn "## SKIP [$_]\n";
-               $stat->{ '__' . $hostname }->{$_} += $h{$_} foreach qw( pcpu pmem vsz );
+               $stat->{ '__' . $hostname }->{$_} += $h{$_} foreach qw( pcpu pmem vsz rss cputime etimes maj_flt min_flt );
                
        }
 
 }
 
-warn "# mac_to_name ", XXX( $mac_to_name );
 
-open(my $ip, '-|', $ssh . 'ip -s -o link');
+my $tap = sh('grep . /var/run/ganeti/kvm-hypervisor/nic/*/*');
+while(<$tap>) {
+       chomp;
+       warn "## $tap\n";
+       my @p = split(/\//,$_);
+       push @{ $instance_tap->{$hostname}->{ $p[-2] } }, $p[-1];
+}
+
+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+)/ ) {
@@ -91,10 +123,31 @@ while(<$ip>) {
        }
 }
 
-warn "# stat = ", XXX( $stat );
+# dump some useful data structures
+
+sub tab_dump {
+       my ( $name, $hash ) = @_;
+       warn "# $name\n";
+       foreach my $key ( sort keys %$hash ) {
+               warn $key, "\t$hostname\t", $hash->{$key}, "\n";
+       }
+}
+
+tab_dump 'mac_to_name',  $mac_to_name;
+warn Dumper( $mac_to_name );
+tab_dump 'instance_tap', $instance_tap;
+warn Dumper( $instance_tap );
+
+goto next_node if @nodes;
+
+
+warn "# stat ", Dumper( $stat ) if $DEBUG;
+
+
+# dump tablable ascii output
+
 XXX( @ps_cols );
 
-my $lines;
 sub push_line {
        my @l = @_;
        foreach my $i ( 0 .. $#l ) {
@@ -102,7 +155,7 @@ sub push_line {
                $lines->{len}->[$i] ||= $len;
                $lines->{len}->[$i] = $len if $len > $lines->{len}->[$i];
        }
-       push @{ $lines->{line} }, [ @l ];
+       push @{ $lines->{line} }, [ map { ! defined $_ ? '-' : $_ } @l ];
 }
 
 push_line '#name', ps_cols_visible;
@@ -116,7 +169,8 @@ foreach my $name ( sort keys %$stat ) {
 XXX $lines;
 
 my $fmt = join(' ', map { '%' . $_ . 's' } @{ $lines->{len} } ) . "\n";
-warn "# fmt = [$fmt]";
+warn "# fmt = [$fmt]" if $DEBUG;
 foreach my $line ( @{ $lines->{line} } ) {
        printf $fmt, @$line;
 }
+