add SSH="ssh hostname " ./ifstat.pl to run remotly
[influxdb-pipes] / ifstat.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5
6 my $host = shift @ARGV || die "usage: INFLUX=http://127.0.0.1:8086/write?db=snmp COMMUNITY=snmp-comminity $0 host";
7 my $community = $ENV{COMMUNITY} || 'public';
8 my $influx    = $ENV{INFLUX} || 'http://127.0.0.1:8086/write?db=snmp';
9
10 use Data::Dump qw(dump);
11 sub XXX { warn "XXX ", scalar(@_), Data::Dump::dump(@_), join(' ',caller), "\n"; };
12
13 my ($sec,$min,$hour,$dd,$mm,$yyyy) = localtime(time); $mm++;
14 my $time;
15 use Time::Local;
16 sub update_time {
17         #warn "# time $yyyy-$mm-$dd $hour:$min:$sec $time\n";
18         $time = timelocal($sec,$min,$hour,$dd,$mm-1,$yyyy) * 1000_000_000;
19         return $time;
20 }
21
22 # FIXME add -A to pull interfaces if they go up and down
23 # -l loopback
24 # -a all
25 my $cmd = qq{$ENV{SSH}ifstat -s '$community@#$host' -a -l -b -n -t 1};
26 warn "# $cmd\n";
27 open(my $ifstat, '-|', $cmd);
28
29 my $first_skipped = 0;
30
31 my $curl;
32 sub reopen_curl {
33         if ( $ENV{INFLUX} ) {
34                 open($curl, '|-', qq( curl -XPOST $influx --data-binary \@- ));
35         } else {
36                 open($curl, '|-', 'tee /dev/shm/curl.debug');
37         }
38 }
39
40 my $stat;
41
42 my @if;
43 my @direction;
44
45 my $lines;
46 my $host_tags = $host;
47 $host_tags =~ s/\./,domain=/;
48
49 while(<$ifstat>) {
50         chomp;
51         #warn "# [$_]\n";
52         s/^\s+//;
53         s/(\w) (in|out)/$1_$2/g;
54         my @v = split(/\s+/);
55         #warn "## [",join(' ',@v), "]\n";
56         if ( $v[0] eq 'Time' ) {
57                 shift @v;
58                 @if = @v;
59         } elsif ( $v[0] eq 'HH:MM:SS' ) {
60                 shift @v;
61                 @direction = map { s/\W+/_/g; s/^K//; $_ } @v;
62         } elsif ( $v[0] =~ m/^(\d\d):(\d\d):(\d\d)/ ) {
63                 next unless $first_skipped++;
64                 $hour = $1; $min = $2; $sec = $3; update_time;
65
66                 reopen_curl;
67                 my $total;
68
69                 foreach my $i ( 0 .. $#if ) {
70
71                         my $if = $if[$i];
72                         
73                         my @tags = ( "if=$if", "host=$host_tags", $ENV{TAGS} || 'no_tags=true' );
74 =for later
75                         if ( $if =~ m/if(\d\d)(\d\d\d\d)/ ) {
76                                 push @tags, "is_vlan=T,prefix=$1,vlan=$2";
77                         } elsif ( $if =~ m/if(\d+)/ ) {
78                                 push @tags, "is_vlan=F,port=$1";
79                         } else {
80                                 push @tags, "unknown_if=$if";
81                         }
82 =cut
83
84                         my $v1 = int( $v[$i*2+1] * 1024 );
85                         my $v2 = int( $v[$i*2+2] * 1024 );
86
87                         print $curl "ifstat,", join(',', @tags),
88                                 " ", $direction[$i*2],   "=${v1}i",
89                                 ",", $direction[$i*2+1], "=${v2}i",
90                                 " $time\n" if $v1 < 100_000_000_000_000 && $v2 < 100_000_000_000_000;
91
92                         $total += $v[$i*2+1];
93
94                         $lines++;
95                 }
96
97                 warn "# $host ", $curl->tell, " total=$total\n";
98                 close($curl);
99         } else {
100                 die "UNPARSED [$_]";
101         }
102 }
103
104