From 079f520913487f557070859b6be2cb484e8441fd Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 10 Jan 2013 01:36:49 +0100 Subject: inital commit --- net_if | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100755 net_if diff --git a/net_if b/net_if new file mode 100755 index 0000000..09a5b91 --- /dev/null +++ b/net_if @@ -0,0 +1,102 @@ +#!/usr/bin/perl -T + +use warnings; +use strict; + +$ENV{'PATH'} = '/bin:/usr/bin:/sbin:/usr/sbin'; +delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; + +sub critical { + print "CRITICAL - @_\n"; + exit 2; +} + +sub warning { + print "WARNING - @_\n"; + exit 1; +} + +sub ok { + print "OK - @_\n"; + exit 0; +} + +sub get_if_status { + my (@up, @down, @error); + + while (@_) { + my $if = shift @_; + next unless ($if =~ m/^([-\w]+)$/); + $if = $1; + + my $pid = open(IFCONFIG, "-|"); + die "$0: fork: $!" unless defined $pid; + + if ($pid == 0) { + # child + open(STDERR, ">", "/dev/null"); + exec("ifconfig", $if) + } + + while () { + if ($_ =~ m/$if: flags=([0-9]+)) { + if ($_ =~ m{In4/Pass.*Bytes: ([0-9]+)}) { + $stats->{'in4'} = $1; + } + elsif ($_ =~ m{Out4/Pass.*Bytes: ([0-9]+)}) { + $stats->{'out4'} = $1; + } + elsif ($_ =~ m{In6/Pass.*Bytes: ([0-9]+)}) { + $stats->{'in6'} = $1; + } + elsif ($_ =~ m{Out6/Pass.*Bytes: ([0-9]+)}) { + $stats->{'out6'} = $1; + } + } + close(PFCTL); + + return $stats; +} + +my ($up, $down, $error) = get_if_status(@ARGV); + +if (@$error) { + $error = join ", ", @$error; + critical("Some interfaces does not exists: $error"); +} + +if (@$down) { + $down = join ", ", @$down; + warning("Some interfaces are down: $down"); +} + +my $stats = {}; +while (@$up) { + my $if = shift @$up; + $stats->{$if} = get_if_stats($if); +} + +use Data::Dumper; +print Dumper $stats; +print "\n"; -- cgit v1.2.3-1-g7c22