Projet

Général

Profil

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

root / plugins / other / snmp__swap @ e908d2d2

Historique | Voir | Annoter | Télécharger (4,03 ko)

1
#!/usr/bin/perl -w
2
#
3
# Copyright (C) 2006 Lars Strand
4
#
5
# Munin plugin to monitor swap usage by use of SNMP.
6
# Based on the 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
my $DEBUG = 0;
31
my $MAXLABEL = 20;
32

    
33
my $host      = $ENV{host}      || undef;
34
my $port      = $ENV{port}      || 161;
35
my $community = $ENV{community} || "public";
36
my $iface     = $ENV{interface} || undef;
37

    
38
my $response;
39

    
40
if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
41
{
42
    # HOST-RESOURCES-MIB::hrStorage
43
    # HOST-RESOURCES-TYPES::hrStorageVirtualMemory
44
    print "require 1.3.6.1.2.1.25.2. 1.3.6.1.2.1.25.2.1.3\n"; 
45
    exit 0;
46
}
47

    
48
if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_swap$/)
49
{
50
    $host  = $1;
51
    if ($host =~ /^([^:]+):(\d+)$/)
52
    {
53
	$host = $1;
54
	$port = $2;
55
    }
56
}
57
elsif (!defined($host))
58
{
59
    print "# Debug: $0 -- $1\n" if $DEBUG;
60
    die "# Error: couldn't understand what I'm supposed to monitor.";
61
}
62

    
63
my ($session, $error) = Net::SNMP->session(
64
     -hostname  => $host,
65
     -community => $community,
66
     -port      => $port
67
    );
68

    
69
if (!defined ($session))
70
{
71
    die "Croaking: $error";
72
}
73

    
74
my $hrStorage = "1.3.6.1.2.1.25.2.";
75
my $hrStorageVirtualMemory = "1.3.6.1.2.1.25.2.1.3";
76
my $hrStorageSize = "1.3.6.1.2.1.25.2.3.1.5.";
77
my $hrStorageUsed = "1.3.6.1.2.1.25.2.3.1.6.";
78

    
79
my $swap_d = get_by_regex($session, $hrStorage, $hrStorageVirtualMemory);
80

    
81
my $swapsize = 0; my $swapused = 0;
82

    
83
foreach my $swap (keys %$swap_d) 
84
{
85
    $swapsize += get_single($session, $hrStorageSize . $swap);
86
    $swapused += get_single($session, $hrStorageUsed . $swap);
87
}
88

    
89
if (defined $ARGV[0] and $ARGV[0] eq "config")
90
{
91
    print "host_name $host\n";
92
    print "graph_title Virtual memory usage\n";
93
    if ($swapsize > 0) 
94
    {
95
	print "graph_args -l 0 --base 1000 --upper-limit $swapsize\n";
96
    } 
97
    else 
98
    {
99
	print "graph_args -l 0 --base 1000\n";
100
    }
101
    print "graph_vlabel Bytes\n";
102
    print "graph_category disk\n";
103
    print "graph_info This graph shows swap usage in bytes.\n";
104
    print "swap.label swap\n";
105
    print "swap.type DERIVE\n";
106
    print "swap.min 0\n";
107
    exit 0;
108
}
109

    
110
print "swap.value $swapused\n";
111

    
112
sub get_single
113
{
114
    my $handle = shift;
115
    my $oid    = shift;
116
    
117
    print "# Getting single $oid..." if $DEBUG;
118
    
119
    $response = $handle->get_request ($oid);
120
    
121
    if (!defined $response->{$oid})
122
    {
123
	print "undef\n" if $DEBUG;
124
	return undef;
125
    }
126
    else
127
    {
128
	print "\"$response->{$oid}\"\n" if $DEBUG;
129
	return $response->{$oid};
130
    }
131
}
132

    
133
sub get_by_regex
134
{
135
    my $handle = shift;
136
    my $oid    = shift;
137
    my $regex  = shift;
138
    my $result = {};
139
    my $num    = 0;
140
    my $ret    = $oid . "0";
141
    my $response;
142
    
143
    print "# Starting browse of $oid...\n" if $DEBUG;
144
    
145
    while (1)
146
    {
147
	if ($num == 0)
148
	{
149
	    print "# Checking for $ret...\n" if $DEBUG;
150
	    $response = $handle->get_request ($ret);
151
	}
152
	if ($num or !defined $response)
153
	{
154
	    print "# Checking for sibling of $ret...\n" if $DEBUG;
155
	    $response = $handle->get_next_request ($ret);
156
	}
157
	if (!$response)
158
	{
159
	    return undef;
160
	}
161
	my @keys = keys %$response;
162
	$ret = $keys[0];
163
	print "# Analyzing $ret (compared to $oid)...\n" if $DEBUG;
164
	last unless ($ret =~ /^$oid/);
165
	$num++;
166
	next unless ($response->{$ret} =~ /$regex/);
167
	@keys = split (/\./, $ret);
168
	$result->{$keys[-1]} = $response->{$ret};;
169
	print "# Index $num: ", $keys[-1], " (", $response->{$ret}, ")\n" if $DEBUG;
170
    };
171
    return $result;
172
}