fix vlan which can't be integer since it's tag
[gnt-info] / gnt-i
diff --git a/gnt-i b/gnt-i
index 29cf191..2f4c86b 100755 (executable)
--- a/gnt-i
+++ b/gnt-i
@@ -2,8 +2,10 @@
 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;
@@ -14,11 +16,12 @@ 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 ; $_ } `gnt-node list -o name --no-header` unless @nodes;
+@nodes = map { chomp ; s/\s+N$// ; $_ } grep { /N$/ } `gnt-node list -o name,offline --no-header` unless @nodes;
 
 next_node:
 my $ssh = '';
@@ -42,7 +45,7 @@ my @ps_cols = qw( user pid
        _args);
 
 sub ps_cols_all { map { my $t = $_; $t =~ s/^_//; $t } @ps_cols };
-sub ps_cols_visible { 'node', grep { ! /^_/ } @ps_cols };
+sub ps_cols_visible { 'node', grep { ! /^_/ } @ps_cols, 'rx', 'tx' };
 
 sub DD_hh_mm_ss {
        my $t = shift;
@@ -58,7 +61,7 @@ sub DD_hh_mm_ss {
                $t_sec *= $f[$i];
        }
 
-       warn "# DD-hh:mm:ss $t -> $t_sec\n" if $DEBUG;
+#      warn "# DD-hh:mm:ss $t -> $t_sec\n" if $DEBUG;
        return $t_sec;
 }
 
@@ -93,52 +96,87 @@ while(<$ps>) {
 
        } else {
 #              warn "## SKIP [$_]\n";
-               $stat->{ '__' . $hostname }->{$_} += $h{$_} foreach qw( pcpu pmem vsz rss cputime etimes maj_flt min_flt );
+               $stat->{ '__' . $hostname }->{$_} += $h{$_} foreach qw( pcpu pmem vsz rss
+                       nlwp cputime etimes
+                       maj_flt min_flt
+               );
                
        }
 
 }
 
 
-my $tap = sh('grep . /var/run/ganeti/kvm-hypervisor/nic/*/*');
+my $tap = sh('grep -H . /var/run/ganeti/kvm-hypervisor/nic/*/*');
 while(<$tap>) {
        chomp;
-       warn "## $tap\n";
        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 ( $br, $mac, $rx, $tx ) = ( $1, $2, $3, $4 );
-               if ( my $name = $mac_to_name->{$mac} ) {
-                       $stat->{$name}->{link}->{ $br } = [ $rx, $tx ];
+               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 "## SKIP MAC $mac [$_]\n" if $DEBUG;
+                       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$hostname\t", $hash->{$key}, "\n";
+               warn $key, "\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;
+
+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;
@@ -146,7 +184,7 @@ warn "# stat ", Dumper( $stat ) if $DEBUG;
 
 # dump tablable ascii output
 
-XXX( @ps_cols );
+#XXX( @ps_cols );
 
 sub push_line {
        my @l = @_;
@@ -166,11 +204,36 @@ foreach my $name ( sort keys %$stat ) {
        push_line( $name, map { $stat->{$name}->{$_} } ps_cols_visible );
 }
 
-XXX $lines;
+#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 $line ( @{ $lines->{line} } ) {
+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 = ( 'host', 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'";