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

=head1 NAME

netstat - Plugin to monitor network connections

=head1 CONFIGURATION

No configuration

=head1 NOTES

=head2 DESCRIPTION

This will measure the amount of network traffic coming into and
out of the server.  It will report back the number of connections
accepted, requested, established, and closed.  It uses
/usr/bin/netstat to gather its information.

=head2 RESTRICTIONS

None known.  /usr/bin/netstat should be executable by everyone by default.

=head1 AUTHOR

Unknown author

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

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

=cut

use strict;

if (defined $ARGV[0] and $ARGV[0] eq "autoconf") {
    if(-e "/usr/bin/netstat" && -X "/usr/bin/netstat") {
        print "yes\n";
    } else {
        print "no\n";
    }
    exit 0;
} elsif (defined $ARGV[0] and $ARGV[0] eq "config") {
    print "graph_title Netstat\n";
    print "graph_args --base 1000 --logarithmic\n";
    print "graph_vlabel requests connections per \${graph_period}\n";
    print "graph_category network\n";
    print "requests.label requests\n";
    print "requests.type COUNTER\n";
    print "requests.max 50000\n";
    print "accepts.label accepts\n";
    print "accepts.type COUNTER\n";
    print "accepts.max 50000\n";
    print "established.label established\n";
    print "established.type COUNTER\n";
    print "established.max 50000\n";
    print "closed.label closed\n";
    print "closed.type COUNTER\n";
    print "closed.max 50000\n";
    exit 0;
}

my(%toFind) = ("requests" => "connection requests",
               "accepts" => "connection accepts",
               "established" => "connections established",
               "closed" => "connections closed ("
              );

my($item,$line,@lineArray);

foreach $item (keys(%toFind)) {
    $line = `/usr/bin/netstat -s|grep '$toFind{$item}'`;
    @lineArray = split(/ +/,$line);
    print "$item.value $lineArray[0]\n";
}
