Projet

Général

Profil

Révision bef79a13

IDbef79a139a8db2b8ccda94c003815c5dc661bf97
Parent b6024a55
Enfant abaf4133

Ajouté par Christian B il y a presque 14 ans

Initial version

Voir les différences:

plugins/other/raminfo
1
#!/usr/bin/perl
2
# Munin plugin for monitoring memory usage.
3
#
4
# FIELDS:
5
#		RAM Kernel	Kernel resident in RAM (and usually lobel swap_total\n";
6
#		RAM Locked	Locked memory pages from swap (Anon)
7
#		RAM Used	Anon, Exec + Libs, Page cache
8
#		RAM Avail	Free memory that can be immediately used
9
#
10
# Core logic developed by Brendan Gregg. 
11
# REFERENCE: http://www.brendangregg.com/k9toolkit.html - the swap diagram.
12
#
13
# COPYRIGHT: Copyright (c) 2004 Brendan Gregg.
14
#
15
#  This program is free software; you can redistribute it and/or
16
#  modify it under the terms of the GNU General Public License
17
#  as published by the Free Software Foundation; either version 2
18
#  of the License, or (at your option) any later version.
19
#
20
#  This program is distributed in the hope that it will be useful,
21
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
#  GNU General Public License for more details.
24
#
25
#  You should have received a copy of the GNU General Public License
26
#  along with this program; if not, write to the Free Software Foundation,
27
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28
#
29
#  (http://www.gnu.org/copyleft/gpl.html)
30

  
31
# Perldoc
32

  
33
=pod
34

  
35
=head1 NAME
36

  
37
raminfo - Plugin for monitoring memory usage
38

  
39
=head1 AUTHOR
40

  
41
Christian Braum, chrisi_braum@web.de
42
 
43
Core logic developed by Brendan Gregg. See K9Toolkit:
44
http://www.brendangregg.com/K9Toolkit/swapinfo
45

  
46
=head1 LICENSE
47

  
48
GPL 2.
49

  
50
=cut
51

  
52
# Main
53
use strict;
54
use warnings;
55

  
56
if( defined $ARGV[0] )
57
{
58
	if( $ARGV[0] eq "config" )
59
	{
60
		&config();
61
	}
62
	else
63
	{
64
		&output();
65
	}
66
}
67
else
68
{
69
	&output();
70
}
71

  
72

  
73
sub value # get value for variables
74
{
75
	my %h_ramvalues;
76
	use Sun::Solaris::Kstat;
77
	my $Kstat = Sun::Solaris::Kstat->new();
78

  
79
	# --- Fetch Hardware info ---
80
	### pagesize
81
	$ENV{PATH} = "/usr/bin";
82
	chomp(my $PAGESIZE = `pagesize`);
83
	my $PAGETOMB = $PAGESIZE / (1024 * 1024);
84
	my $PAGETOKB = $PAGESIZE / (1024);
85
	my $PAGETOBYTE = $PAGESIZE;
86
	my $BLOCKTOP = 512 / $PAGESIZE;
87

  
88
	### RAM total
89
	my $ram_total = 0;
90
	foreach my $i (keys(%{$Kstat->{lgrp}})) {                          # instance
91
       		foreach my $c (keys(%{$Kstat->{lgrp}->{$i}})) {            # class
92
                	$ram_total += $Kstat->{lgrp}->{$i}->{$c}->{"pages installed"};
93
        	}
94
	}
95

  
96
	### RAM available for the OS
97
	my $pagestotal = $Kstat->{unix}->{0}->{system_pages}->{pagestotal};
98

  
99

  
100
	# --- Fetch VM info ---
101
	my %VMnow;
102
	my %VMinfo;
103
	my %VMold;
104

  
105
	foreach my $count (0..12) 
106
	{
107
		#
108
		#  The values are counters that increment each second, here we
109
		#  check them several times and look for the value changing.
110
		#  (reading them once then again a second later was not reliable).
111
		#
112

  
113
		foreach my $var ( "freemem" ) 
114
		{
115
			$VMnow{$var} = $Kstat->{unix}->{0}->{vminfo}->{$var};
116
			unless ($count) {
117
				$VMold{$var} = $VMnow{$var};
118
				next;
119
			}
120
			if (($VMnow{$var} != $VMold{$var}) && (! $VMinfo{$var})) {
121
				$VMinfo{$var} = $VMnow{$var} - $VMold{$var};
122
			}
123
		}
124
		select(undef, undef, undef, 0.1);
125
		$Kstat->update();
126
	}
127

  
128
	my $freemem = $Kstat->{unix}->{0}->{system_pages}->{freemem};
129
	my $pageslocked = $Kstat->{unix}->{0}->{system_pages}->{pageslocked};
130
	my $pp_kernel = $Kstat->{unix}->{0}->{system_pages}->{pp_kernel};
131

  
132
	### ---RAM Calculations ---
133
	my $ram_unusable = $ram_total - $pagestotal;
134
	my $ram_kernel;
135
	my $ram_locked;
136

  
137
	if ($pp_kernel < $pageslocked) {
138
		# here we assume all pp_kernel pages are in memory,
139
		$ram_kernel = $pp_kernel;
140
		$ram_locked = $pageslocked - $pp_kernel;
141
	} else {
142
		# here we assume pageslocked is entirerly kernel,
143
		$ram_kernel = $pageslocked;
144
		$ram_locked = 0;
145
	}
146

  
147
	my $ram_used = $pagestotal - $freemem - $ram_kernel - $ram_locked;
148

  
149
	### format values
150
	
151
	my $freemem_B      = sprintf( "%d ", $freemem * $PAGETOBYTE );
152
	my $pp_kernel_B    = sprintf( "%d ", $pp_kernel * $PAGETOBYTE );
153
	my $pageslocked_B  = sprintf( "%d ", $pageslocked * $PAGETOBYTE );
154
	my $pagestotal_B   = sprintf( "%d ", $pagestotal * $PAGETOBYTE );
155
	my $ram_unusable_B = sprintf( "%d ", $ram_unusable * $PAGETOBYTE );
156
	my $ram_kernel_B   = sprintf( "%d ", $ram_kernel * $PAGETOBYTE ); 
157
	my $ram_locked_B   = sprintf( "%d ", $ram_locked * $PAGETOBYTE );
158
	my $ram_used_B     = sprintf( "%d ", $ram_used * $PAGETOBYTE );
159
	my $ram_total_B    = sprintf( "%d ", $ram_total * $PAGETOBYTE );
160

  
161
	# --- assign the variables  ---	
162
	$h_ramvalues{"Unusable.value"} = "$ram_unusable_B";
163
	$h_ramvalues{"Kernel.value"} = "$ram_kernel_B";
164
	$h_ramvalues{"Locked.value"} = "$ram_locked_B";
165
	$h_ramvalues{"Used.value"} = "$ram_used_B";
166
	$h_ramvalues{"Avail.value"} = "$freemem_B";
167
	$h_ramvalues{"Total.value"} = "$ram_total_B";
168

  
169
	return %h_ramvalues;
170
}  # sub value
171

  
172
sub output       # print out the values of the variables.
173
{
174
	my %h_ramvalue=value();
175

  
176
	print "Unusable.value " . $h_ramvalue{"Unusable.value"} . "\n";
177
	print "Kernel.value " . $h_ramvalue{"Kernel.value"} . "\n";
178
	print "Locked.value " . $h_ramvalue{"Locked.value"} . "\n";
179
	print "Used.value " . $h_ramvalue{"Used.value"} . "\n";
180
	print "Avail.value " . $h_ramvalue{"Avail.value"} . "\n";
181
}
182

  
183
sub config       # print config message and exit.
184
{
185
	    my %h_ramvalue=value();
186

  
187
	    print "graph_args --base 1024 -l 0 --upper-limit " .  $h_ramvalue{"Total.value"}. "--rigid"  . "\n";
188
	    print "graph_vlabel Bytes\n";
189
	    print "graph_title RAM usage\n";
190
	    print "graph_category system\n";
191
	    print "graph_info This graph shows what the machine uses RAM for.\n";
192
	    print "graph_order ";
193

  
194
	    #print "vmalloc_used " if exists $mems{'VmallocUsed'};
195

  
196

  
197
	    print "Unusable ",
198
	      "Kernel ",
199
		"Locked ",
200
		  "Used ",
201
		    "Avail ",
202
		    "\n";
203
	  
204
	    print "Unusable.label Unusable \n";
205
	    print "Unusable.draw AREA \n";
206
	    print "Unusable.info RAM consumed by the OBP and TSBs.\n";
207
	    print "Kernel.label Kernel \n";
208
	    print "Kernel.draw STACK \n";
209
	    print "Kernel.info Kernel resident in RAM.\n";
210
	    print "Locked.label Locked \n";
211
	    print "Locked.draw STACK \n";
212
	    print "Locked.info Locked memory pages from swap.\n";
213
	    print "Used.label Used\n";
214
	    print "Used.draw STACK\n";
215
	    print "Used.info RAM used.\n";
216
	    print "Avail.label Avail\n";
217
	    print "Avail.draw STACK\n";
218
	    print "Avail.info Free RAM.\n";
219
} # sub config

Formats disponibles : Unified diff