X-Git-Url: http://git.rot13.org/?p=influxdb-pipes;a=blobdiff_plain;f=ifstat.pl;h=b146801305ba18be4a53848cb1a52687da856331;hp=8ea85347c0fc5f14c95c31150793fea5bcf0b10f;hb=d525c37dd12bd32ce6ded86e609692bb3c6c65ac;hpb=7876288ebb751c79227346514b667248c00f6017 diff --git a/ifstat.pl b/ifstat.pl index 8ea8534..b146801 100755 --- 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' -a -l -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,6 +46,8 @@ my $lines; my $host_tags = $host; $host_tags =~ s/\./,domain=/; +my $last_hour = $hour; + while(<$ifstat>) { chomp; #warn "# [$_]\n"; @@ -61,10 +63,16 @@ 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 ) { @@ -81,20 +89,29 @@ while(<$ifstat>) { } =cut - my $v1 = int( $v[$i*2+1] * 1024 ); - my $v2 = int( $v[$i*2+2] * 1024 ); + my $v1 = $v[$i*2+1]; + my $v2 = $v[$i*2+2]; - print $curl "ifstat,", join(',', @tags), + 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; - $total += $v[$i*2+1]; - - $lines++; + $lines++; + } else { + warn "IGNORED $if $v1 $v2\n"; + } } - warn "# $host ", $curl->tell, " total=$total\n"; + warn "# $host ", $curl->tell, " totals: $total[0] $total[1]\n"; close($curl); } else { die "UNPARSED [$_]";