#!/usr/bin/perl -w
# -*- perl -*-
#
# Copyright (C) 2006 Lars Strand
#
# Munin plugin to monitor network connection by use of SNMP.
# Based on snmp__df plugin.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# $Id: snmp__netstat.in 2431 2009-09-16 10:04:17Z janl $
#
#%# family=snmpauto
#%# capabilities=snmpconf

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

my $DEBUG = $ENV{'MUNIN_DEBUG'};

my $response;

my %tcpStates = ( 1 =>  [0, "GAUGE", "closed", "Connections waiting for a termination request acknowledgment from the remote TCP."],
		  2 =>  [0, "GAUGE", "listen", "Connections waiting for a request from any remote TCP and port."],
		  3 =>  [0, "GAUGE", "synSent", "Connections waiting for a matching request after having sent a connection request."],
		  4 =>  [0, "GAUGE", "synReceived", "Connections waiting for a confirming request acknowledgment after having both received and sent a connection request."],
		  5 =>  [0, "GAUGE", "established", "Connections opened and data received can be delivered to the user. The normal state for the data transfer phase of the connection."],
		  6 =>  [0, "GAUGE", "finWait1", "Connections waiting for a termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent."],
		  7 =>  [0, "GAUGE", "finWait2", "Connections waiting for a termination request from the remote TCP."],
		  8 =>  [0, "GAUGE", "closeWait", "Connections waiting for a termination request from the local user."],
		  9 =>  [0, "GAUGE", "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)."],
		  10 => [0, "GAUGE", "closing", "Connections waiting for a termination request acknowledgment from the remote TCP."],
		  11 => [0, "GAUGE", "timeWait", "Connections waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its termination request."],
		  12 => [0, "GAUGE", "deleteTCP", "Connections terminated by a SNMP Managment Station (put)"]
		  );

if (defined $ARGV[0] and $ARGV[0] eq "snmpconf") {
    print "require 1.3.6.1.2.1.6.13.1.1. [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_order closed listen synSent synReceived established finWait1 finWait2 closeWait lastAck closing timeWait deleteTCP\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 (keys %tcpStates) {
	print "$tcpStates{$state}[2].label $tcpStates{$state}[2]\n";
	print "$tcpStates{$state}[2].type $tcpStates{$state}[1]\n";
	print "$tcpStates{$state}[2].info $tcpStates{$state}[3]\n";
    }

    exit 0;
}

my $tcpConnState = "1.3.6.1.2.1.6.13.1.1.";

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

my $connections = $session->get_by_regex($tcpConnState, "[1-9]");

# the values
while (my ($id, $state) = each(%$connections)) {
    $tcpStates{$state}[0] += 1;
}

foreach my $state (keys %tcpStates) {
    print "$tcpStates{$state}[2].value $tcpStates{$state}[0]\n";
}
