Projet

Général

Profil

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

root / plugins / snmp / snmp__cpu_usage @ 7da1b039

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

1 827f36a5 Xavier Montagutelli
#!/usr/bin/perl -w
2
#
3
# Copyright (C) 2004 Jimmy Olsen
4
# Copyright (C) 2009 Xavier Montagutelli
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; version 2 dated June,
9
# 1991.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
#
20
#
21
# $Log: snmp__cpu_usage,v $
22
# Revision 1.1  2009/09/21 06:59:33  root
23
# Initial revision
24
#
25
# Revision 1.1  2009/09/17 13:43:35  root
26
# Initial revision
27
#
28
# Revision 1.11  2004/12/10 18:51:43  jimmyo
29
# linux/apt* has been forced to LANG=C, to get predictable output.
30
#
31
# Revision 1.10  2004/12/10 10:47:47  jimmyo
32
# Change name from ${scale} to ${graph_period}, to be more consistent.
33
#
34
# Revision 1.9  2004/12/09 22:12:55  jimmyo
35
# Added "graph_period" option, to make "graph_sums" usable.
36
#
37
# Revision 1.8  2004/11/21 00:16:56  jimmyo
38
# Changed a lot of plugins so they use DERIVE instead of COUNTER.
39
#
40
# Revision 1.7  2004/11/16 20:08:26  jimmyo
41
# License cleanups.
42
#
43
# Revision 1.6  2004/11/12 20:28:03  ilmari
44
# No debugging info by default
45
#
46
# Revision 1.5  2004/09/08 15:25:33  ilmari
47
# Use /usr/bin/perl in all perl shebang lines.
48
#
49
# Revision 1.4  2004/09/07 13:19:22  ilmari
50
# SNMP plugins now honour the "host" environment variable if they can't deduce the hostname from zsh.
51
#
52
# Revision 1.3  2004/09/05 12:00:18  jimmyo
53
# Set family and capabilities.
54
#
55
# Revision 1.2  2004/09/04 21:58:28  jimmyo
56
# Set category and info fields.
57
#
58
# Revision 1.1  2004/04/30 22:22:07  jimmyo
59
# Added to SNMP based fibre-channel plugins: fc_if and fc_if_err.
60
#
61
# Revision 1.4  2004/04/30 16:58:14  jimmyo
62
# Added max.
63
#
64
# Revision 1.3  2004/02/22 20:17:58  jimmyo
65
# Typo fix
66
#
67
# Revision 1.2  2004/02/18 21:54:56  jimmyo
68
# Did a bit of work on the snmp-thingie.
69
#
70
# Revision 1.1  2004/01/02 18:50:00  jimmyo
71
# Renamed occurrances of lrrd -> munin
72
#
73
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
74
# Import of LRRD CVS tree after renaming to Munin
75
#
76
# Revision 1.1  2003/12/19 20:53:45  jimmyo
77
# Created by jo
78
#
79
#
80
#%# family=snmpauto
81
#%# capabilities=snmpconf
82
83
use strict;
84 72736dae Tom Feiner
use Munin::Plugin::SNMP;
85 827f36a5 Xavier Montagutelli
86
my $DEBUG = 0;
87
88
my $host      = $ENV{host}      || undef;
89
my $port      = $ENV{port}      || 161;
90
my $community = $ENV{community} || "public";
91
my $iface     = $ENV{interface} || undef;
92
93
my $response;
94
95
if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
96
{
97
	print "number  1.3.6.1.4.1.2021.11.9\n";
98
	exit 0;
99
}
100
101
if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_cpu_usage$/)
102
{
103
	$host  = $1;
104
	if ($host =~ /^([^:]+):(\d+)$/)
105
	{
106
		$host = $1;
107
		$port = $2;
108
	}
109
}
110
elsif (!defined($host))
111
{
112
	print "# Debug: $0 -- $1 -- $2\n" if $DEBUG;
113
	die "# Error: couldn't understand what I'm supposed to monitor.";
114
}
115
116
my $cpuUser     = "1.3.6.1.4.1.2021.11.9";
117
my $cpuSystem   = "1.3.6.1.4.1.2021.11.10";
118
my $cpuIdle     = "1.3.6.1.4.1.2021.11.11";
119
120
my $cpu = 0;
121
122
my %cpuCounters = ( 
123
	cpuUser 	=> "1.3.6.1.4.1.2021.11.9.$cpu", 	# The percentage of CPU time spent processing user-level code, calculated over the last minute
124
	cpuSystem	=> "1.3.6.1.4.1.2021.11.10.$cpu", 	# The percentage of CPU time spent processing system-level code, calculated over the last minute
125
	cpuIdle		=> "1.3.6.1.4.1.2021.11.11.$cpu",	# The percentage of processor time spent idle, calculated over the last minute
126
);
127
128 f00e552b Tom Feiner
my $session = Munin::Plugin::SNMP->session(-translate =>
129
					   [ -timeticks => 0x0 ]);
130
131
if (!defined ($session))
132
{
133
	die "Croaking: could not establish SNMP object";
134
}
135
136 827f36a5 Xavier Montagutelli
137
if (!defined ($session))
138
{
139
	die "Croaking: $error";
140
}
141
142
if ($ARGV[0] and $ARGV[0] eq "config")
143
{
144
	print "host_name $host\n";
145
	my $warn = undef;
146
	print "graph_title CPU Usage on $host\n";
147
	print "graph_args --base 1000 --lower-limit 0\n";
148
	print "graph_vlabel Percentage on one minute\n";
149
	print "graph_category system\n";
150
	print "graph_info This graph shows the CPU usage on one minute\n";
151
152
	my $firstCounter = 1;
153
	foreach my $c (keys %cpuCounters) {
154
		print $c . ".label $c\n";
155
		print $c . ".type GAUGE\n";
156
		if ($firstCounter) { 
157
			print $c . ".draw AREA\n";
158
			$firstCounter = 0;
159
		} else {
160
			print $c . ".draw STACK\n";
161
		}
162
		print $c . ".min 0\n";
163
	}
164
165
	exit 0; 
166
}
167
168
foreach my $c (keys %cpuCounters) {
169
	if (defined ($response = $session->get_request($cpuCounters{$c})) && $response->{$cpuCounters{$c}} =~ /^[0-9]*$/) {
170
		print $c . ".value ", $response->{$cpuCounters{$c}}, "\n";
171
	} else {
172
		print $c . ".value U\n";
173
	}
174
}