#!@@PERL@@
# -*- perl -*-

=head1 NAME

netstat - Plugin to monitor network connections

=head1 CONFIGURATION

No configuration

=head1 AUTHOR

Unknown author

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf

=cut

use strict;
use Munin::Plugin;

if (defined $ARGV[0] and $ARGV[0] eq "autoconf") {
  print "yes\n";
  exit;
} elsif (defined $ARGV[0] and $ARGV[0] eq "config") {
  print "graph_title Netstat\n";
  print "graph_args --base 1000 --logarithmic\n";
  print "graph_vlabel active connections per \${graph_period}\n";
  print "graph_category network\n";
  print "active.label active\n";
  print "active.type DERIVE\n";
  print "active.min 0\n";
  print "active.max 50000\n";
  print_thresholds("active");
  print "passive.label passive\n";
  print "passive.type DERIVE\n";
  print "passive.min 0\n";
  print "passive.max 50000\n";
  print_thresholds("passive");
  print "failed.label failed\n";
  print "failed.type DERIVE\n";
  print "failed.min 0\n";
  print "failed.max 50000\n";
  print_thresholds("failed");
  print "resets.label resets\n";
  print "resets.type DERIVE\n";
  print "resets.min 0\n";
  print "resets.max 50000\n";
  print_thresholds("resets");
  print "established.label established\n";
  print "established.type GAUGE\n";
  print "established.max 50000\n";
  print_thresholds("established");
  exit;
}

my %trans = (
              tcpActiveOpens  => "active",
              tcpPassiveOpens => "passive",
              tcpAttemptFails => "failed",
              tcpEstabResets  => "resets",
              tcpCurrEstab    => "established"
            );

# Slurp mode
undef $/;

open(NETSTAT, '/usr/bin/netstat -s -P tcp|');
$_ = <NETSTAT>;
close(NETSTAT);

s/^\n*//;

s/^TCP/   /m;

while (/\s+(\w+)\s*=\s*(\d+)/g) {
  print "$trans{$1}.value $2\n" if exists $trans{$1};
}
