Projet

Général

Profil

Révision 0a9c0b87

ID0a9c0b871cc8151d39cafd0e55cbbad924330577
Parent f16bb2cb
Enfant 477e745e

Ajouté par Stefan Seidel il y a presque 14 ans

Initial version

Voir les différences:

plugins/other/snmp__memory_win
1
#!/usr/bin/perl -w
2
#
3
# Copyright (C) 2004 Jimmy Olsen
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; version 2 dated June,
8
# 1991.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
#
19
#
20
# $Log$
21
# Revision 1.10  2004/11/16 20:08:26  jimmyo
22
# License cleanups.
23
#
24
# Revision 1.9  2004/09/08 15:25:33  ilmari
25
# Use /usr/bin/perl in all perl shebang lines.
26
#
27
# Revision 1.8  2004/09/07 13:19:22  ilmari
28
# SNMP plugins now honour the "host" environment variable if they can't deduce the hostname from zsh.
29
#
30
# Revision 1.7  2004/09/07 12:58:41  ilmari
31
# SNMP plugin "df" properly strips the label and serial number fromWindows drive labels.
32
#
33
# Revision 1.6  2004/09/05 12:00:18  jimmyo
34
# Set family and capabilities.
35
#
36
# Revision 1.5  2004/09/04 21:58:28  jimmyo
37
# Set category and info fields.
38
#
39
# Revision 1.4  2004/09/04 21:33:12  jimmyo
40
# Handle strange characters better.
41
#
42
# Revision 1.3  2004/09/04 21:08:16  jimmyo
43
# SNMP df plugin now talks Windowese.
44
#
45
# Revision 1.2  2004/09/03 22:56:51  jimmyo
46
# Added support for SNMP probing.
47
#
48
# Revision 1.1  2004/04/29 22:29:57  jimmyo
49
# New SNMP plugin for disk usage.
50
#
51
# Revision 1.3  2004/02/22 20:17:58  jimmyo
52
# Typo fix
53
#
54
# Revision 1.2  2004/02/18 21:54:56  jimmyo
55
# Did a bit of work on the snmp-thingie.
56
#
57
# Revision 1.1  2004/01/02 18:50:00  jimmyo
58
# Renamed occurrances of lrrd -> munin
59
#
60
# Revision 1.1.1.1  2004/01/02 15:18:07  jimmyo
61
# Import of LRRD CVS tree after renaming to Munin
62
#
63
# Revision 1.1  2003/12/19 20:53:45  jimmyo
64
# Created by jo
65
#
66
#%# family=snmpauto
67
#%# capabilities=snmpconf
68

  
69
use strict;
70
use Net::SNMP;
71

  
72
my $DEBUG = 0;
73
my $MAXLABEL = 20;
74

  
75
my $host      = $ENV{host}      || undef;
76
my $port      = $ENV{port}      || 161;
77
my $community = $ENV{community} || "public";
78
my $iface     = $ENV{interface} || undef;
79

  
80
my $response;
81

  
82
if (defined $ARGV[0] and $ARGV[0] eq "snmpconf")
83
{
84
	print "index   1.3.6.1.2.1.25.2.3.1.1.\n";
85
	print "index   1.3.6.1.2.1.25.5.1.1.2.\n";
86
	print "require 1.3.6.1.2.1.25.5.1.1.2. [1-9]\n";
87
	print "require 1.3.6.1.2.1.25.2.2.0\n"; # memsize
88
	print "require 1.3.6.1.2.1.25.2.3.1.2. 1.3.6.1.2.1.25.2.1.(2|3)\n"; # Type=fixed disk
89
	print "require 1.3.6.1.2.1.25.2.3.1.5. [1-9]\n"; # Size > 0
90
	exit 0;
91
}
92

  
93
if ($0 =~ /^(?:|.*\/)snmp_([^_]+)_memory_win$/)
94
{
95
	$host  = $1;
96
	if ($host =~ /^([^:]+):(\d+)$/)
97
	{
98
		$host = $1;
99
		$port = $2;
100
	}
101
}
102
elsif (!defined($host))
103
{
104
	print "# Debug: $0 -- $1\n" if $DEBUG;
105
	die "# Error: couldn't understand what I'm supposed to monitor.";
106
}
107

  
108
# Disk level
109
my $hrDeviceType           = "1.3.6.1.2.1.25.3.2.1.2."; # Should be iso.3.6.1.2.1.25.3.1.6 (DiskStorage)
110
my $hrDiskStorageRemoveble = "1.3.6.1.2.1.25.3.6.1.3."; # Should be false (2).
111
                                                        # Windows reports 0.
112
my $hrDiskStorageCapacity  = "1.3.6.1.2.1.25.3.6.1.4."; # Should be more than 0
113

  
114
# Partition level
115
my $hrPartitionFSIndex     = "1.3.6.1.2.1.25.3.7.1.5."; # Should be more than 0
116
my $hrFSMountPoint         = "1.3.6.1.2.1.25.3.8.1.2."; # Used to look up filesystem
117

  
118
# Filesystem level
119
my $hrStorageType          = "1.3.6.1.2.1.25.2.3.1.2."; # Backup for hrFS*
120
my $hrStorageDesc          = "1.3.6.1.2.1.25.2.3.1.3."; # Used as key from partitions
121
my $hrStorageSize          = "1.3.6.1.2.1.25.2.3.1.5."; # Data point 1
122
my $hrStorageUnit          = "1.3.6.1.2.1.25.2.3.1.4."; # Data point 3
123
my $hrStorageUsed          = "1.3.6.1.2.1.25.2.3.1.6."; # Data point 2
124

  
125
# Memory
126
my $hrSWRunPerfMem = "1.3.6.1.2.1.25.5.1.1.2.";
127

  
128

  
129
my ($session, $error) = Net::SNMP->session(
130
		-hostname  => $host,
131
		-community => $community,
132
		-port      => $port
133
	);
134

  
135
if (!defined ($session))
136
{
137
	die "Croaking: $error";
138
}
139

  
140
my %partitions;
141
my $stor_id;
142
$stor_id = get_by_regex ($session, $hrStorageType, "1.3.6.1.2.1.25.2.1.(3|2)");
143
%partitions = ();
144

  
145
foreach my $id (keys %$stor_id)
146
{
147
	my $part = get_single ($session, $hrStorageDesc . $id);
148
	$partitions{$part}{storage} = $id;
149
	$partitions{$part}{extinfo} = $part;
150
	$stor_id->{$id} = $part;
151
}
152

  
153
if (defined $ARGV[0] and $ARGV[0] eq "config")
154
{
155
	print "host_name $host\n";
156
	print "graph_title Windows memory usage\n";
157
	print "graph_args -l 0 --base 1024\n";
158
	print "graph_vlabel Bytes\n";
159
	print "graph_category system\n";
160
	print "graph_info This graph shows Windows memory usage in bytes.\n";
161

  
162
	foreach my $part (keys %partitions)
163
	{
164
		print (&get_name_by_mp ($part), ".label $part Usage\n");
165
#		print (&get_name_by_mp ($part), ".warning 92\n");
166
#		print (&get_name_by_mp ($part), ".critical 98\n");
167
		print (&get_name_by_mp ($part) . "_s", ".label $part Size\n");
168
		print (&get_name_by_mp ($part) . "_s", ".graph no\n");
169
	}
170
	print "pused.label In use by programs\n";
171
	print "pused.draw AREA\n";
172
	exit 0;
173
}
174

  
175
foreach my $storage (keys %$stor_id)
176
{
177
    $partitions{$stor_id->{$storage}}{storage} = $storage;
178
    $partitions{$stor_id->{$storage}}{size}    = get_single ($session, $hrStorageSize . $storage);
179
    $partitions{$stor_id->{$storage}}{unit}    = get_single ($session, $hrStorageUnit . $storage);
180
    $partitions{$stor_id->{$storage}}{used}    = get_single ($session, $hrStorageUsed . $storage);
181
}
182

  
183
foreach my $part (keys %partitions)
184
{
185
    print (&get_name_by_mp ($part), ".value ", $partitions{$part}{used}*$partitions{$part}{unit}, "\n");
186
    print (&get_name_by_mp ($part) . "_s", ".value ", $partitions{$part}{size}*$partitions{$part}{unit}, "\n");
187
}
188
my $memsize = &get_single($session, "1.3.6.1.2.1.25.2.2.0") * 1024;
189
my $processes = get_by_regex($session, $hrSWRunPerfMem, "[1-9]");
190
# the values
191
my $memtotal = 0;
192
while (my ($pid, $mem) = each(%$processes)) {
193
    $memtotal += $mem;
194
}
195
$memtotal*=1024;
196
printf "pused.value $memtotal\n";
197

  
198
sub get_single
199
{
200
	my $handle = shift;
201
	my $oid    = shift;
202

  
203
	print "# Getting single $oid..." if $DEBUG;
204

  
205
	$response = $handle->get_request ($oid);
206

  
207
	if (!defined $response->{$oid})
208
	{
209
	    print "undef\n" if $DEBUG;
210
	    return undef;
211
	}
212
	else
213
	{
214
	    print "\"$response->{$oid}\"\n" if $DEBUG;
215
	    return $response->{$oid};
216
	}
217
}
218

  
219
sub get_by_regex
220
{
221
	my $handle = shift;
222
	my $oid    = shift;
223
	my $regex  = shift;
224
	my $result = {};
225
	my $num    = 0;
226
	my $ret    = $oid . "0";
227
	my $response;
228

  
229
	print "# Starting browse of $oid...\n" if $DEBUG;
230

  
231
	while (1)
232
	{
233
		if ($num == 0)
234
		{
235
			print "# Checking for $ret...\n" if $DEBUG;
236
			$response = $handle->get_request ($ret);
237
		}
238
		if ($num or !defined $response)
239
		{
240
			print "# Checking for sibling of $ret...\n" if $DEBUG;
241
			$response = $handle->get_next_request ($ret);
242
		}
243
		if (!$response)
244
		{
245
			return undef;
246
		}
247
		my @keys = keys %$response;
248
		$ret = $keys[0];
249
		print "# Analyzing $ret (compared to $oid)...\n" if $DEBUG;
250
		last unless ($ret =~ /^$oid/);
251
		$num++;
252
		next unless ($response->{$ret} =~ /$regex/);
253
		@keys = split (/\./, $ret);
254
		$result->{$keys[-1]} = ($1) ? $1 : $response->{$ret};
255
		print "# Index $num: ", $keys[-1], " (", $response->{$ret}, ")\n" if $DEBUG;
256
	};
257
	return $result;
258
}
259

  
260
sub get_name_by_mp
261
{
262
    my $mp = shift;
263
    $mp =~ s/[^a-z0-9_]/_/gi;
264
    $mp =~ tr/A-Z/a-z/;
265
    return "p" . $mp;
266
}

Formats disponibles : Unified diff