~ubuntu-branches/ubuntu/lucid/munin/lucid

« back to all changes in this revision

Viewing changes to plugins/node.d/snmp__cpuload.in

  • Committer: Bazaar Package Importer
  • Author(s): Holger Levsen, Stig Sandbeck Mathisen, Tom Feiner
  • Date: 2010-01-14 12:10:51 UTC
  • mfrom: (8.1.13 sid)
  • Revision ID: james.westby@ubuntu.com-20100114121051-6xovy6hqfh1wrl0u
Tags: 1.4.3-2
[ Stig Sandbeck Mathisen ]
* Add versioned dependency for librrds-perl.
  If used with librrds-perl 1.2 or older, the font path is wrong.

[ Tom Feiner ]
* Update watch file.
* Add patch from munin ticket #828, to suppress "occasional" unknown 
  states to avoid alerts. Thanks to Steve Wilson for the patch!
* Removed asterisks from NEWS.Debian and rewrite as non bulleted list, as
  advised by the developers reference.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!@@PERL@@ -w
 
2
# -*- perl -*-
 
3
 
 
4
=head1 NAME
 
5
 
 
6
snmp__cpuload - Munin plugin to monitor CPU-load by use of SNMP.
 
7
 
 
8
=head1 CONFIGURATION
 
9
 
 
10
The following environment variables are used by this plugin:
 
11
 
 
12
 host      - SNMP host (default taken from plugin link name)
 
13
 port      - SNMP port (default 161)
 
14
 community - SNMP community (default "private")
 
15
 
 
16
=head1 NOTES
 
17
 
 
18
Based on snmp__df plugin.
 
19
 
 
20
=head1 AUTHOR
 
21
 
 
22
Copyright (C) 2006 Lars Strand
 
23
 
 
24
=head1 LICENSE
 
25
 
 
26
This program is free software; you can redistribute it and/or modify
 
27
it under the terms of the GNU General Public License as published by
 
28
the Free Software Foundation; version 2 dated June, 1991.
 
29
 
 
30
This program is distributed in the hope that it will be useful, but
 
31
WITHOUT ANY WARRANTY; without even the implied warranty of
 
32
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
33
General Public License for more details.
 
34
 
 
35
You should have received a copy of the GNU General Public License
 
36
along with this program; if not, write to the Free Software
 
37
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
38
02110-1301 USA.
 
39
 
 
40
=head1 MAGIC MARKERS
 
41
 
 
42
 #%# family=snmpauto
 
43
 #%# capabilities=snmpconf
 
44
 
 
45
=cut
 
46
 
 
47
use strict;
 
48
use Net::SNMP;
 
49
 
 
50
my $DEBUG = 0;
 
51
 
 
52
my $host      = $ENV{host}      || undef;
 
53
my $port      = $ENV{port}      || 161;
 
54
my $community = $ENV{community} || "public";
 
55
 
 
56
my $response;
 
57
 
 
58
if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
 
59
{
 
60
    print "index 1.3.6.1.2.1.25.3.3.1.2.\n";
 
61
    print "require 1.3.6.1.2.1.25.3.3.1.2.1\n"; # CPU #1
 
62
    exit 0;
 
63
}
 
64
 
 
65
if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_cpuload$/)
 
66
{
 
67
    $host  = $1;
 
68
    if ($host =~ /^([^:]+):(\d+)$/)
 
69
    {
 
70
        $host = $1;
 
71
        $port = $2;
 
72
    }
 
73
}
 
74
 
 
75
elsif (!defined($host))
 
76
{
 
77
    print "# Debug: $0 -- $1\n" if $DEBUG;
 
78
    die "# Error: couldn't understand what I'm supposed to monitor.";
 
79
}
 
80
 
 
81
# CPUs
 
82
my $hrProcessorLoad = "1.3.6.1.2.1.25.3.3.1.2.";
 
83
 
 
84
my ($session, $error) = Net::SNMP->session(
 
85
    -hostname  => $host,
 
86
    -community => $community,
 
87
    -port      => $port
 
88
    );
 
89
 
 
90
if (!defined ($session))
 
91
{
 
92
    die "Croaking: $error";
 
93
}
 
94
 
 
95
# Bug? Only up to 9 cpus?
 
96
my $cpus = get_by_regex($session, $hrProcessorLoad, "[1-9]");
 
97
 
 
98
 
 
99
if (defined $ARGV[0] and $ARGV[0] eq "config")
 
100
{
 
101
    print "host_name $host\n";
 
102
    print "graph_title CPU usage (in %)
 
103
graph_category system
 
104
graph_args --upper-limit 100 -l 0 
 
105
graph_vlabel % 
 
106
graph_info This graph shows the CPU load on the system.
 
107
";
 
108
    foreach my $cpu (keys %$cpus)
 
109
    {
 
110
        # CPU #1 will have $fieldname "cpu" to retain compatability with
 
111
        # old snmp_cpuload plugin.
 
112
        my $cpun=$cpu;
 
113
        $cpun='' if $cpun ==1;
 
114
        print "cpu$cpun.label CPU $cpu\n";
 
115
        print "cpu$cpun.info CPU load on CPU $cpu\n";
 
116
    }
 
117
    exit 0;
 
118
}
 
119
 
 
120
# the values
 
121
while (my ($cpu, $load) = each(%$cpus)) {
 
122
    $cpu='' if $cpu ==1;
 
123
    print "cpu$cpu.value $load\n";
 
124
}
 
125
 
 
126
 
 
127
sub get_by_regex
 
128
{
 
129
    my $handle = shift;
 
130
    my $oid    = shift;
 
131
    my $regex  = shift;
 
132
    my $result = {};
 
133
    my $num    = 0;
 
134
    my $ret    = $oid . "0";
 
135
    my $response;
 
136
    
 
137
    print "# Starting browse of $oid...\n" if $DEBUG;
 
138
    
 
139
    while (1)
 
140
    {
 
141
        if ($num == 0)
 
142
        {
 
143
            print "# Checking for $ret...\n" if $DEBUG;
 
144
            $response = $handle->get_request ($ret);
 
145
        }
 
146
        if ($num or !defined $response)
 
147
        {
 
148
            print "# Checking for sibling of $ret...\n" if $DEBUG;
 
149
            $response = $handle->get_next_request ($ret);
 
150
        }
 
151
        if (!$response)
 
152
        {
 
153
            return undef;
 
154
        }
 
155
        my @keys = keys %$response;
 
156
        $ret = $keys[0];
 
157
        print "# Analyzing $ret (compared to $oid)...\n" if $DEBUG;
 
158
        last unless ($ret =~ /^$oid/);
 
159
        $num++;
 
160
        next unless ($response->{$ret} =~ /$regex/);
 
161
        @keys = split (/\./, $ret);
 
162
        $result->{$keys[-1]} = $response->{$ret};;
 
163
        print "# Index $num: ", $keys[-1], " (", $response->{$ret}, ")\n" if $DEBUG;
 
164
    };
 
165
    return $result;
 
166
}