find tap device for instance on given node
[gnt-info] / gnt-i
diff --git a/gnt-i b/gnt-i
index 19beadc..29cf191 100755 (executable)
--- a/gnt-i
+++ b/gnt-i
@@ -13,13 +13,20 @@ sub XXX { $DEBUG ? warn "XXX ",Dumper( @_ ) : {} };
 
 my $stat;
 my $mac_to_name;
+my $instance_tap;
 my $lines;
 
-next_argv:
+my @nodes = @ARGV;
+
+@nodes = map { chomp ; $_ } `gnt-node list -o name --no-header` unless @nodes;
+
+next_node:
 my $ssh = '';
-if ( @ARGV ) {
-       $hostname = shift @ARGV;
+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!
@@ -35,7 +42,7 @@ my @ps_cols = qw( user pid
        _args);
 
 sub ps_cols_all { map { my $t = $_; $t =~ s/^_//; $t } @ps_cols };
-sub ps_cols_visible { grep { ! /^_/ } @ps_cols };
+sub ps_cols_visible { 'node', grep { ! /^_/ } @ps_cols };
 
 sub DD_hh_mm_ss {
        my $t = shift;
@@ -55,9 +62,15 @@ sub DD_hh_mm_ss {
        return $t_sec;
 }
 
-my $cmd = $ssh . 'ps --no-headers axwwo ' . join(',', ps_cols_all);
-warn "## $cmd\n";
-open(my $ps, '-|', $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*//;
@@ -72,8 +85,10 @@ while(<$ps>) {
                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->{$1} = $name;
+                       $mac_to_name->{$hostname}->{$1} = $name;
                }
 
        } else {
@@ -84,7 +99,16 @@ while(<$ps>) {
 
 }
 
-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+)/ ) {
@@ -99,9 +123,29 @@ while(<$ip>) {
        }
 }
 
-goto next_argv if @ARGV;
+# 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 );
 
 sub push_line {
@@ -122,8 +166,6 @@ foreach my $name ( sort keys %$stat ) {
        push_line( $name, map { $stat->{$name}->{$_} } ps_cols_visible );
 }
 
-warn '# mac_to_name ', Dumper( $mac_to_name );
-
 XXX $lines;
 
 my $fmt = join(' ', map { '%' . $_ . 's' } @{ $lines->{len} } ) . "\n";