Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / network / snmp__netstat @ dd4afac8

Historique | Voir | Annoter | Télécharger (5,31 ko)

1 18549a2b Lars Strand
#!/usr/bin/perl -w
2
#
3
# Copyright (C) 2006 Lars Strand
4
#
5
# Munin plugin to monitor network connection by use of SNMP.
6
# Based on snmp__df plugin.
7
#
8
# This program is free software; you can redistribute it and/or
9
# modify it under the terms of the GNU General Public License
10
# as published by the Free Software Foundation; version 2 dated June,
11
# 1991.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program; if not, write to the Free Software
20
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
#
22
# $Log$
23
#
24
#%# family=snmpauto
25
#%# capabilities=snmpconf
26
27
use strict;
28
use Net::SNMP;
29
30 f16bb2cb Stefan Seidel
my $DEBUG = 1;
31 18549a2b Lars Strand
32
my $host      = $ENV{host}      || undef;
33
my $port      = $ENV{port}      || 161;
34
my $community = $ENV{community} || "public";
35
36
my $response;
37
38
my %tcpStates = ( 1 =>  [0, "GAUGE", "closed", "Connections waiting for a termination request acknowledgment from the remote TCP."],
39
		  2 =>  [0, "GAUGE", "listen", "Connections waiting for a request from any remote TCP and port."],
40
		  3 =>  [0, "GAUGE", "synSent", "Connections waiting for a matching request after having sent a connection request."],
41
		  4 =>  [0, "GAUGE", "synReceived", "Connections waiting for a confirming request acknowledgment after having both received and sent a connection request."],
42
		  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."],
43
		  6 =>  [0, "GAUGE", "finWait1", "Connections waiting for a termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent."],
44
		  7 =>  [0, "GAUGE", "finWait2", "Connections waiting for a termination request from the remote TCP."],
45
		  8 =>  [0, "GAUGE", "closeWait", "Connections waiting for a termination request from the local user."],
46
		  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)."],
47
		  10 => [0, "GAUGE", "closing", "Connections waiting for a termination request acknowledgment from the remote TCP."],
48
		  11 => [0, "GAUGE", "timeWait", "Connections waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its termination request."],
49
		  12 => [0, "GAUGE", "deleteTCP", "Connections terminated by a SNMP Managment Station (put)"]
50
		  );
51
52
if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
53
{
54
    print "require 1.3.6.1.2.1.6.13.1.1. [0-9]\n";
55
    exit 0;
56
}
57
58
if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_netstat$/)
59
{
60
    $host  = $1;
61
    if ($host =~ /^([^:]+):(\d+)$/)
62
    {
63
	$host = $1;
64
	$port = $2;
65
    }
66
}
67
elsif (!defined($host))
68
{
69
    print "# Debug: $0 -- $1\n" if $DEBUG;
70
    die "# Error: couldn't understand what I'm supposed to monitor.";
71
}
72
73
if (defined $ARGV[0] and $ARGV[0] eq "config")
74
{
75
    print "host_name $host\n";
76
    print "graph_title Netstat\n";
77
    print "graph_args -l 0 --base 1000\n";
78
    print "graph_period seconds\n";
79
    print "graph_category network\n";
80
    print "graph_order closed listen synSent synReceived established finWait1 finWait2 closeWait lastAck closing timeWait deleteTCP\n";
81
    print "graph_vlabel active connection\n";
82
    print "graph_info This graph shows the TCP activity of all the network interfaces combined.\n";
83
    
84
    foreach my $state (keys %tcpStates) {
85
	print "$tcpStates{$state}[2].label $tcpStates{$state}[2]\n";
86
	print "$tcpStates{$state}[2].type $tcpStates{$state}[1]\n";
87
	print "$tcpStates{$state}[2].max 50000\n";
88
	print "$tcpStates{$state}[2].min 0\n";
89
	print "$tcpStates{$state}[2].info $tcpStates{$state}[3]\n";
90
    }
91
   
92
    exit 0;
93
}
94
95
my $tcpConnState = "1.3.6.1.2.1.6.13.1.1.";
96
97
my ($session, $error) = Net::SNMP->session(
98
	   -hostname  => $host,
99
	   -community => $community,
100
	   -port      => $port
101
        );
102
103
if (!defined ($session))
104
{
105
    die "Croaking: $error";
106
}
107
108
my $connections = get_by_regex($session, $tcpConnState, "[1-9]");
109
110
# the values
111
while (my ($id, $state) = each(%$connections)) {
112
    $tcpStates{$state}[0] += 1;
113
}
114
115
foreach my $state (keys %tcpStates) {
116
    print "$tcpStates{$state}[2].value $tcpStates{$state}[0]\n";
117
}
118
119
sub get_by_regex
120
{
121
    my $handle = shift;
122
    my $oid    = shift;
123
    my $regex  = shift;
124
    my $result = {};
125
    my $num    = 0;
126
    my $ret    = $oid . "0";
127
    my $response;
128
    
129
    print "# Starting browse of $oid...\n" if $DEBUG;
130
    
131
    while (1)
132
    {
133
	if ($num == 0)
134
	{
135
	    print "# Checking for $ret...\n" if $DEBUG;
136
	    $response = $handle->get_request ($ret);
137
	}
138
	if ($num or !defined $response)
139
	{
140
	    print "# Checking for sibling of $ret...\n" if $DEBUG;
141
	    $response = $handle->get_next_request ($ret);
142
	}
143
	if (!$response)
144
	{
145
	    return undef;
146
	}
147
	my @keys = keys %$response;
148
	$ret = $keys[0];
149
	print "# Analyzing $ret (compared to $oid)...\n" if $DEBUG;
150
	last unless ($ret =~ /^$oid/);
151
	$num++;
152
	next unless ($response->{$ret} =~ /$regex/);
153
	@keys = split (/\./, $ret);
154 f16bb2cb Stefan Seidel
	$result->{$num} = $response->{$ret};;
155 18549a2b Lars Strand
	print "# Index $num: ", $keys[-1], " (", $response->{$ret}, ")\n" if $DEBUG;
156
    };
157
    return $result;
158
}