Merge pull request #1 from ivuk/fix-typo
authorDobrica Pavlinušić <dpavlin@rot13.org>
Mon, 12 Dec 2016 06:12:07 +0000 (07:12 +0100)
committerGitHub <noreply@github.com>
Mon, 12 Dec 2016 06:12:07 +0000 (07:12 +0100)
Fix a typo in ifstat.pl

1  2 
ifstat.pl

diff --combined ifstat.pl
+++ b/ifstat.pl
@@@ -3,18 -3,18 +3,18 @@@ 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';
  
  use Data::Dump qw(dump);
 -sub XXX { warn "XXX ",scalar @_, Data::Dump::dump(@_) };
 +sub XXX { warn "XXX ", scalar(@_), Data::Dump::dump(@_), join(' ',caller), "\n"; };
  
  my ($sec,$min,$hour,$dd,$mm,$yyyy) = localtime(time); $mm++;
  my $time;
  use Time::Local;
  sub update_time {
 -      warn "# time $yyyy-$mm-$dd $hour:$min:$sec $time\n";
 +      #warn "# time $yyyy-$mm-$dd $hour:$min:$sec $time\n";
        $time = timelocal($sec,$min,$hour,$dd,$mm-1,$yyyy) * 1000_000_000;
        return $time;
  }
@@@ -22,7 -22,7 +22,7 @@@
  # 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);
  
@@@ -31,9 -31,9 +31,9 @@@ my $first_skipped = 0
  my $curl;
  sub reopen_curl {
        if ( $ENV{INFLUX} ) {
 -              open($curl, '|-', qq( curl -i -XPOST $influx --data-binary \@- ));
 +              open($curl, '|-', qq( curl -XPOST $influx --data-binary \@- ));
        } else {
 -              open($curl, '|-', 'cat');
 +              open($curl, '|-', 'tee /dev/shm/curl.debug');
        }
  }
  
@@@ -43,8 -43,6 +43,8 @@@ my @if
  my @direction;
  
  my $lines;
 +my $host_tags = $host;
 +$host_tags =~ s/\./,domain=/;
  
  while(<$ifstat>) {
        chomp;
@@@ -52,7 -50,6 +52,7 @@@
        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;
@@@ -61,6 -58,7 +61,6 @@@
                @direction = map { s/\W+/_/g; s/^K//; $_ } @v;
        } elsif ( $v[0] =~ m/^(\d\d):(\d\d):(\d\d)/ ) {
                next unless $first_skipped++;
 -XXX $stat;
                $hour = $1; $min = $2; $sec = $3; update_time;
  
                reopen_curl;
  
                foreach my $i ( 0 .. $#if ) {
  
 -                      my $port = $if[$i];
 -                      my $vlan = '';
 -                      my $is_port = 'T';
 -                      $vlan = ",vlan=$1" if $port =~ m/if(\d\d\d\d\d\d)/;
 -
 -                      print $curl "ifstat,host=$host,port=$port$vlan ",
 -                              $direction[$i*2],   "=", $v[$i*2+1]   * 1024, ",",
 -                              $direction[$i*2+1], "=", $v[$i*2+2] * 1024,
 -                              " $time\n";
 +                      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 = int( $v[$i*2+1] * 1024 );
 +                      my $v2 = int( $v[$i*2+2] * 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;
  
                        $total += $v[$i*2+1];