Projet

Général

Profil

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

root / plugins / snmp / snmp_memory @ 7da1b039

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

1
#!/usr/bin/perl -w
2
#
3
# Copyright (C) 2006 Lars Strand
4
#
5
# Munin plugin to monitor memory usage 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
my $DEBUG = 0;
31

    
32
my $host      = $ENV{host}      || undef;
33
my $port      = $ENV{port}      || 161;
34
my $community = $ENV{community} || "public";
35

    
36
my $response;
37

    
38
if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
39
{
40
    print "index   1.3.6.1.2.1.25.5.1.1.2.\n";
41
    print "require 1.3.6.1.2.1.25.5.1.1.2. [1-9]\n";
42
    print "require 1.3.6.1.2.1.25.2.2.0\n"; # memsize
43
    exit 0;
44
}
45

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

    
61
# memory usage pr. process
62
my $hrSWRunPerfMem = "1.3.6.1.2.1.25.5.1.1.2.";
63

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

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

    
75
# total memory
76
my $memsize = &get_single($session, "1.3.6.1.2.1.25.2.2.0") * 1024;
77

    
78
if (defined $ARGV[0] and $ARGV[0] eq "config")
79
{
80
    print "host_name $host\n";
81
    print "graph_title Memory usage\n";
82
    print "graph_category system\n";
83
    print "graph_vlabel Bytes\n";
84
    print "graph_info This grap shows memory usage.\n";
85

    
86
    # some devices reports negative memtotal value
87
    print "# Total memsize reported $memsize..." if $DEBUG;
88

    
89
    if ($memsize > 0) 
90
    {
91
    print "graph_args --base 1024 -l 0 --upper-limit $memsize\n";
92
    }
93
    else
94
    {
95
    print "graph_args --base 1024 -l 0\n";
96
    }
97

    
98
    print "memory.draw AREA\n";
99
    print "memory.label memory\n";
100

    
101
    exit 0;
102
}
103

    
104
my $processes = get_by_regex($session, $hrSWRunPerfMem, "[1-9]");
105

    
106
# the values
107
my $memtotal = 0;
108
while (my ($pid, $mem) = each(%$processes)) {
109
    $memtotal += $mem;
110
}
111
$memtotal*=1024;
112
printf "memory.value $memtotal\n";
113

    
114
sub get_single
115
{
116
    my $handle = shift;
117
    my $oid    = shift;
118

    
119
    print "# Getting single $oid..." if $DEBUG;
120

    
121
    $response = $handle->get_request ($oid);
122

    
123
    if (!defined $response->{$oid})
124
    {
125
    print "undef\n" if $DEBUG;
126
    return undef;
127
    }
128
    else
129
    {
130
    print "\"$response->{$oid}\"\n" if $DEBUG;
131
    return $response->{$oid};
132
    }
133
}
134

    
135
sub get_by_regex
136
{
137
    my $handle = shift;
138
    my $oid    = shift;
139
    my $regex  = shift;
140
    my $result = {};
141
    my $num    = 0;
142
    my $ret    = $oid . "0";
143
    my $response;
144

    
145
    print "# Starting browse of $oid...\n" if $DEBUG;
146

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