summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Sulfrian <alexander@sulfrian.net>2013-01-10 01:36:49 +0100
committerAlexander Sulfrian <alexander@sulfrian.net>2013-01-10 01:36:49 +0100
commit079f520913487f557070859b6be2cb484e8441fd (patch)
treedf0156f0251bd4f8a21ac3eb55e6a0686caf7ec5
downloadnet_if-079f520913487f557070859b6be2cb484e8441fd.tar.gz
net_if-079f520913487f557070859b6be2cb484e8441fd.tar.bz2
net_if-079f520913487f557070859b6be2cb484e8441fd.zip
inital commit
-rwxr-xr-xnet_if102
1 files changed, 102 insertions, 0 deletions
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 (<IFCONFIG>) {
+ if ($_ =~ m/$if: flags=([0-9]+)</) {
+ if ($1 & 1) {
+ push(@up, $if);
+ }
+ else {
+ push (@down, $if);
+ }
+ }
+ }
+
+ close(IFCONFIG) or push(@error, $if);
+ }
+
+ return (\@up, \@down, \@error);
+}
+
+sub get_if_stats {
+ my $if = shift @_;
+ my $stats = {};
+
+ open(PFCTL, "-|", "pfctl", "-vvsI", "-i", $if);
+ while (<PFCTL>) {
+ 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";