ignore unparsable lines from ifstat, two totals
[influxdb-pipes] / ifstat.pl
index 6af1577..b146801 100755 (executable)
--- a/ifstat.pl
+++ b/ifstat.pl
@@ -3,7 +3,7 @@ use warnings;
 use strict;
 use autodie;
 
-my $host = shift @ARGV || die "usage: INFLUX=http://127.0.0.1:8086/write?db=snmp COMMUNITY=snmp-comminity $0 host";
+my $host = shift @ARGV || die "usage: INFLUX=http://127.0.0.1:8086/write?db=snmp COMMUNITY=snmp-community $0 host";
 my $community = $ENV{COMMUNITY} || 'public';
 my $influx    = $ENV{INFLUX} || 'http://127.0.0.1:8086/write?db=snmp';
 
@@ -22,7 +22,7 @@ sub update_time {
 # FIXME add -A to pull interfaces if they go up and down
 # -l loopback
 # -a all
-my $cmd = qq{ifstat -s '$community@#$host' -b -n -t 1};
+my $cmd = qq{$ENV{SSH}ifstat -s '$community@#$host' -a -l -b -n -t 1};
 warn "# $cmd\n";
 open(my $ifstat, '-|', $cmd);
 
@@ -46,12 +46,15 @@ my $lines;
 my $host_tags = $host;
 $host_tags =~ s/\./,domain=/;
 
+my $last_hour = $hour;
+
 while(<$ifstat>) {
        chomp;
        #warn "# [$_]\n";
        s/^\s+//;
        s/(\w) (in|out)/$1_$2/g;
        my @v = split(/\s+/);
+       #warn "## [",join(' ',@v), "]\n";
        if ( $v[0] eq 'Time' ) {
                shift @v;
                @if = @v;
@@ -60,33 +63,55 @@ while(<$ifstat>) {
                @direction = map { s/\W+/_/g; s/^K//; $_ } @v;
        } elsif ( $v[0] =~ m/^(\d\d):(\d\d):(\d\d)/ ) {
                next unless $first_skipped++;
+
+               if ( $last_hour != $hour ) {
+                       ($sec,$min,$hour,$dd,$mm,$yyyy) = localtime(time); $mm++;
+                       $last_hour = $hour;
+               }
+
                $hour = $1; $min = $2; $sec = $3; update_time;
 
                reopen_curl;
-               my $total;
+               my @total;
 
                foreach my $i ( 0 .. $#if ) {
 
-                       my @tags = ( "host=$host_tags", $ENV{TAGS} || 'no_tags=true' );
-
-                       my $port = $if[$i];
-                       if ( $port =~ m/if(\d\d)(\d\d\d\d)/ ) {
-                               push @tags, "prefix=$1,vlan=$2";
-                       } elsif ( m/if(\d+)/ ) {
-                               push @tags, "port=$1";
+                       my $if = $if[$i];
+                       
+                       my @tags = ( "if=$if", "host=$host_tags", $ENV{TAGS} || 'no_tags=true' );
+=for later
+                       if ( $if =~ m/if(\d\d)(\d\d\d\d)/ ) {
+                               push @tags, "is_vlan=T,prefix=$1,vlan=$2";
+                       } elsif ( $if =~ m/if(\d+)/ ) {
+                               push @tags, "is_vlan=F,port=$1";
+                       } else {
+                               push @tags, "unknown_if=$if";
+                       }
+=cut
+
+                       my $v1 = $v[$i*2+1];
+                       my $v2 = $v[$i*2+2];
+
+                       if ( $v1 =~ m/^\d+\.\d++$/ && $v2 =~ m/^\d+\.\d++$/ ) {
+                               
+                               $total[0] += $v1;
+                               $total[1] += $v2;
+
+                               $v1 = int( $v1 * 1024 );
+                               $v2 = int( $v2 * 1024 );
+       
+                               print $curl "ifstat,", join(',', @tags),
+                               " ", $direction[$i*2],   "=${v1}i",
+                               ",", $direction[$i*2+1], "=${v2}i",
+                               " $time\n" if $v1 < 100_000_000_000_000 && $v2 < 100_000_000_000_000;
+
+                               $lines++;
+                       } else {
+                               warn "IGNORED $if $v1 $v2\n";
                        }
-
-                       print $curl "ifstat,", join(',', @tags),
-                               " ", $direction[$i*2],   "=", ( $v[$i*2+1] * 1024 ),
-                               ",", $direction[$i*2+1], "=", ( $v[$i*2+2] * 1024 ),
-                               " $time\n";
-
-                       $total += $v[$i*2+1];
-
-                       $lines++;
                }
 
-               warn "# $host ", $curl->tell, " total=$total\n";
+               warn "# $host ", $curl->tell, " totals: $total[0] $total[1]\n";
                close($curl);
        } else {
                die "UNPARSED [$_]";