#!@@PERL@@ -w
# -*- perl -*-
# vim: ft=perl

=head1 NAME

snmp__netstat - Munin plugin to monitor the number of TCP network
connections on an SNMP device.

=head1 APPLICABLE SYSTEMS

Any SNMP-capable device that reports the TCP-MIB::tcpConnState
column of tcpConnTable.

=head1 CONFIGURATION

As a rule SNMP plugins need site specific configuration.  The default
configuration (shown here) will only work on insecure sites/devices.

   [snmp_*]
	env.version 2
        env.community public

In general SNMP is not very secure at all unless you use SNMP version
3 which supports authentication and privacy (encryption).  But in any
case the community string for your devices should not be "public".

Please see 'perldoc Munin::Plugin::SNMP' for further configuration
information.

=head1 INTERPRETATION

The total number of TCP connections in each state.  See RFC 793 or the
detailed information on the graph page for the meaning of each state.

=head1 MIB INFORMATION

This plugin requires support for the TCP-MIB (RFC 4022).  It reports
the contents of the tcpConnState column of tcpConnTable.

=head1 MAGIC MARKERS

  #%# family=snmpauto
  #%# capabilities=snmpconf

=head1 BUGS

None known.

=head1 AUTHOR

Copyright (C) 2006 Lars Strand

Tidied and documented by Matthew Boyle in 2010.

=head1 LICENSE

GPLv2.

=cut

use strict;
use Munin::Plugin::SNMP;

my $oid = '1.3.6.1.2.1.6.13.1.1';

my @tcpStates = (
	[ 0, 'closed',      'Connections waiting for a termination request acknowledgment from the remote TCP.' ],
	[ 0, 'listen',      'Connections waiting for a request from any remote TCP and port.' ],
	[ 0, 'synSent',     'Connections waiting for a matching request after having sent a connection request.' ],
	[ 0, 'synReceived', 'Connections waiting for a confirming request acknowledgment after having both received and sent a connection request.' ],
	[ 0, 'established', 'Connections opened and data received can be delivered to the user. The normal state for the data transfer phase of the connection.' ],
	[ 0, 'finWait1',    'Connections waiting for a termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent.' ],
	[ 0, 'finWait2',    'Connections waiting for a termination request from the remote TCP.' ],
	[ 0, 'closeWait',   'Connections waiting for a termination request from the local user.' ],
	[ 0, 'lastAck',     'Connections waiting for an acknowledgment of the termination request previously sent to the remote TCP (which includes an acknowledgment of its connection termination request).' ],
	[ 0, 'closing',     'Connections waiting for a termination request acknowledgment from the remote TCP.' ],
	[ 0, 'timeWait',    'Connections waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its termination request.' ],
	[ 0, 'deleteTCP',   'Connections terminated by a SNMP Managment Station (put)' ]
);

if (defined $ARGV[0] and $ARGV[0] eq 'snmpconf') {
    print "require ${oid}. [0-9]\n";
    exit 0;
}

if (defined $ARGV[0] and $ARGV[0] eq 'config') {
    my ($host) = Munin::Plugin::SNMP->config_session();

    print "host_name $host\n" unless $host eq 'localhost';
    print "graph_title Netstat\n";
    print "graph_args --base 1000 --logarithmic\n";
    print "graph_period seconds\n";
    print "graph_category network\n";
    print "graph_vlabel active connection\n";
    print "graph_info This graph shows the TCP activity of all the network interfaces combined.\n";

    foreach my $state (@tcpStates) {
        print "$state->[1].label $state->[1]\n";
        print "$state->[1].info  $state->[2]\n";
    }

    exit 0;
}

my ($session, $error) = Munin::Plugin::SNMP->session();

my $connections = $session->get_entries(-columns => [ $oid ]);

# sum up the rows
foreach my $state (values %$connections) {
    $tcpStates[$state-1]->[0]++;  # states are counted from 1, not 0
}

foreach my $state (@tcpStates) {
    print "$state->[1].value $state->[0]\n";
}

