root / plugins / system / raminfo @ 81e9ffca
Historique | Voir | Annoter | Télécharger (6,17 ko)
| 1 | bef79a13 | Christian B | #!/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 | 67470547 | Stig Sandbeck Mathisen | eval 'use Sun::Solaris::Kstat; 1;' |
| 77 | or die 'Please install Sun::Solaros::Kstat'; |
||
| 78 | bef79a13 | Christian B | my $Kstat = Sun::Solaris::Kstat->new(); |
| 79 | |||
| 80 | # --- Fetch Hardware info --- |
||
| 81 | ### pagesize |
||
| 82 | $ENV{PATH} = "/usr/bin";
|
||
| 83 | chomp(my $PAGESIZE = `pagesize`); |
||
| 84 | my $PAGETOMB = $PAGESIZE / (1024 * 1024); |
||
| 85 | my $PAGETOKB = $PAGESIZE / (1024); |
||
| 86 | my $PAGETOBYTE = $PAGESIZE; |
||
| 87 | my $BLOCKTOP = 512 / $PAGESIZE; |
||
| 88 | |||
| 89 | ### RAM total |
||
| 90 | my $ram_total = 0; |
||
| 91 | foreach my $i (keys(%{$Kstat->{lgrp}})) { # instance
|
||
| 92 | foreach my $c (keys(%{$Kstat->{lgrp}->{$i}})) { # class
|
||
| 93 | $ram_total += $Kstat->{lgrp}->{$i}->{$c}->{"pages installed"};
|
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | ### RAM available for the OS |
||
| 98 | my $pagestotal = $Kstat->{unix}->{0}->{system_pages}->{pagestotal};
|
||
| 99 | |||
| 100 | |||
| 101 | # --- Fetch VM info --- |
||
| 102 | my %VMnow; |
||
| 103 | my %VMinfo; |
||
| 104 | my %VMold; |
||
| 105 | |||
| 106 | foreach my $count (0..12) |
||
| 107 | {
|
||
| 108 | # |
||
| 109 | # The values are counters that increment each second, here we |
||
| 110 | # check them several times and look for the value changing. |
||
| 111 | # (reading them once then again a second later was not reliable). |
||
| 112 | # |
||
| 113 | |||
| 114 | foreach my $var ( "freemem" ) |
||
| 115 | {
|
||
| 116 | $VMnow{$var} = $Kstat->{unix}->{0}->{vminfo}->{$var};
|
||
| 117 | unless ($count) {
|
||
| 118 | $VMold{$var} = $VMnow{$var};
|
||
| 119 | next; |
||
| 120 | } |
||
| 121 | if (($VMnow{$var} != $VMold{$var}) && (! $VMinfo{$var})) {
|
||
| 122 | $VMinfo{$var} = $VMnow{$var} - $VMold{$var};
|
||
| 123 | } |
||
| 124 | } |
||
| 125 | select(undef, undef, undef, 0.1); |
||
| 126 | $Kstat->update(); |
||
| 127 | } |
||
| 128 | |||
| 129 | my $freemem = $Kstat->{unix}->{0}->{system_pages}->{freemem};
|
||
| 130 | my $pageslocked = $Kstat->{unix}->{0}->{system_pages}->{pageslocked};
|
||
| 131 | my $pp_kernel = $Kstat->{unix}->{0}->{system_pages}->{pp_kernel};
|
||
| 132 | |||
| 133 | ### ---RAM Calculations --- |
||
| 134 | my $ram_unusable = $ram_total - $pagestotal; |
||
| 135 | my $ram_kernel; |
||
| 136 | my $ram_locked; |
||
| 137 | |||
| 138 | if ($pp_kernel < $pageslocked) {
|
||
| 139 | # here we assume all pp_kernel pages are in memory, |
||
| 140 | $ram_kernel = $pp_kernel; |
||
| 141 | $ram_locked = $pageslocked - $pp_kernel; |
||
| 142 | } else {
|
||
| 143 | # here we assume pageslocked is entirerly kernel, |
||
| 144 | $ram_kernel = $pageslocked; |
||
| 145 | $ram_locked = 0; |
||
| 146 | } |
||
| 147 | |||
| 148 | my $ram_used = $pagestotal - $freemem - $ram_kernel - $ram_locked; |
||
| 149 | |||
| 150 | ### format values |
||
| 151 | |||
| 152 | my $freemem_B = sprintf( "%d ", $freemem * $PAGETOBYTE ); |
||
| 153 | my $pp_kernel_B = sprintf( "%d ", $pp_kernel * $PAGETOBYTE ); |
||
| 154 | my $pageslocked_B = sprintf( "%d ", $pageslocked * $PAGETOBYTE ); |
||
| 155 | my $pagestotal_B = sprintf( "%d ", $pagestotal * $PAGETOBYTE ); |
||
| 156 | my $ram_unusable_B = sprintf( "%d ", $ram_unusable * $PAGETOBYTE ); |
||
| 157 | my $ram_kernel_B = sprintf( "%d ", $ram_kernel * $PAGETOBYTE ); |
||
| 158 | my $ram_locked_B = sprintf( "%d ", $ram_locked * $PAGETOBYTE ); |
||
| 159 | my $ram_used_B = sprintf( "%d ", $ram_used * $PAGETOBYTE ); |
||
| 160 | my $ram_total_B = sprintf( "%d ", $ram_total * $PAGETOBYTE ); |
||
| 161 | |||
| 162 | # --- assign the variables --- |
||
| 163 | $h_ramvalues{"Unusable.value"} = "$ram_unusable_B";
|
||
| 164 | $h_ramvalues{"Kernel.value"} = "$ram_kernel_B";
|
||
| 165 | $h_ramvalues{"Locked.value"} = "$ram_locked_B";
|
||
| 166 | $h_ramvalues{"Used.value"} = "$ram_used_B";
|
||
| 167 | $h_ramvalues{"Avail.value"} = "$freemem_B";
|
||
| 168 | $h_ramvalues{"Total.value"} = "$ram_total_B";
|
||
| 169 | |||
| 170 | return %h_ramvalues; |
||
| 171 | } # sub value |
||
| 172 | |||
| 173 | sub output # print out the values of the variables. |
||
| 174 | {
|
||
| 175 | my %h_ramvalue=value(); |
||
| 176 | |||
| 177 | print "Unusable.value " . $h_ramvalue{"Unusable.value"} . "\n";
|
||
| 178 | print "Kernel.value " . $h_ramvalue{"Kernel.value"} . "\n";
|
||
| 179 | print "Locked.value " . $h_ramvalue{"Locked.value"} . "\n";
|
||
| 180 | print "Used.value " . $h_ramvalue{"Used.value"} . "\n";
|
||
| 181 | print "Avail.value " . $h_ramvalue{"Avail.value"} . "\n";
|
||
| 182 | } |
||
| 183 | |||
| 184 | sub config # print config message and exit. |
||
| 185 | {
|
||
| 186 | my %h_ramvalue=value(); |
||
| 187 | |||
| 188 | print "graph_args --base 1024 -l 0 --upper-limit " . $h_ramvalue{"Total.value"}. "--rigid" . "\n";
|
||
| 189 | print "graph_vlabel Bytes\n"; |
||
| 190 | print "graph_title RAM usage\n"; |
||
| 191 | print "graph_category system\n"; |
||
| 192 | print "graph_info This graph shows what the machine uses RAM for.\n"; |
||
| 193 | print "graph_order "; |
||
| 194 | |||
| 195 | #print "vmalloc_used " if exists $mems{'VmallocUsed'};
|
||
| 196 | |||
| 197 | |||
| 198 | print "Unusable ", |
||
| 199 | "Kernel ", |
||
| 200 | "Locked ", |
||
| 201 | "Used ", |
||
| 202 | "Avail ", |
||
| 203 | "\n"; |
||
| 204 | |||
| 205 | print "Unusable.label Unusable \n"; |
||
| 206 | print "Unusable.draw AREA \n"; |
||
| 207 | print "Unusable.info RAM consumed by the OBP and TSBs.\n"; |
||
| 208 | print "Kernel.label Kernel \n"; |
||
| 209 | print "Kernel.draw STACK \n"; |
||
| 210 | print "Kernel.info Kernel resident in RAM.\n"; |
||
| 211 | print "Locked.label Locked \n"; |
||
| 212 | print "Locked.draw STACK \n"; |
||
| 213 | print "Locked.info Locked memory pages from swap.\n"; |
||
| 214 | print "Used.label Used\n"; |
||
| 215 | print "Used.draw STACK\n"; |
||
| 216 | print "Used.info RAM used.\n"; |
||
| 217 | print "Avail.label Avail\n"; |
||
| 218 | print "Avail.draw STACK\n"; |
||
| 219 | print "Avail.info Free RAM.\n"; |
||
| 220 | } # sub config |
