| 1 |
package Core::Memstat; |
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use warnings; |
|---|
| 5 |
|
|---|
| 6 |
use base 'Resmon::Module'; |
|---|
| 7 |
|
|---|
| 8 |
use Resmon::ExtComm qw(run_command cache_command); |
|---|
| 9 |
|
|---|
| 10 |
my $osname = $^O; |
|---|
| 11 |
my $usekstat = 0; |
|---|
| 12 |
my $kstat; |
|---|
| 13 |
if ($osname eq 'solaris') { |
|---|
| 14 |
eval "use Sun::Solaris::Kstat"; |
|---|
| 15 |
unless ($@) { |
|---|
| 16 |
$usekstat = 1; |
|---|
| 17 |
$kstat = Sun::Solaris::Kstat->new(); |
|---|
| 18 |
} |
|---|
| 19 |
} |
|---|
| 20 |
|
|---|
| 21 |
=pod |
|---|
| 22 |
|
|---|
| 23 |
=head1 NAME |
|---|
| 24 |
|
|---|
| 25 |
Core::Memstat - Monitor memory statistics using vmstat |
|---|
| 26 |
|
|---|
| 27 |
=head1 SYNOPSIS |
|---|
| 28 |
|
|---|
| 29 |
Core::Memstat { |
|---|
| 30 |
local : noop |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
Core::Memstat { |
|---|
| 34 |
local : vmstat_path => /usr/sbin/vmstat |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
=head1 DESCRIPTION |
|---|
| 38 |
|
|---|
| 39 |
This module returns statistics on active and free memory. The type and |
|---|
| 40 |
number of metrics returned depend on the capabilities of each platform's |
|---|
| 41 |
respective vmstat command. |
|---|
| 42 |
|
|---|
| 43 |
=head1 CONFIGURATION |
|---|
| 44 |
|
|---|
| 45 |
=over |
|---|
| 46 |
|
|---|
| 47 |
=item check_name |
|---|
| 48 |
|
|---|
| 49 |
The check name is used for descriptive purposes only. It is not used |
|---|
| 50 |
for anything functional. |
|---|
| 51 |
|
|---|
| 52 |
=item vmstat_path |
|---|
| 53 |
|
|---|
| 54 |
Provide an alternate path to the vmstat command (optional). |
|---|
| 55 |
|
|---|
| 56 |
=back |
|---|
| 57 |
|
|---|
| 58 |
=head1 METRICS |
|---|
| 59 |
|
|---|
| 60 |
The metrics returned by this module vary by OS and method used: |
|---|
| 61 |
|
|---|
| 62 |
=head2 VMSTAT METRICS |
|---|
| 63 |
|
|---|
| 64 |
The default/fallback method used is vmstat, which returns the following |
|---|
| 65 |
metrics: |
|---|
| 66 |
|
|---|
| 67 |
=over |
|---|
| 68 |
|
|---|
| 69 |
=item actv_mem |
|---|
| 70 |
|
|---|
| 71 |
Active virtual pages. |
|---|
| 72 |
|
|---|
| 73 |
=item free_mem |
|---|
| 74 |
|
|---|
| 75 |
Free real memory. |
|---|
| 76 |
|
|---|
| 77 |
=back |
|---|
| 78 |
|
|---|
| 79 |
=head2 KSTAT METRICS |
|---|
| 80 |
|
|---|
| 81 |
Solaris provides an interface to numerous kernel statistics. If the |
|---|
| 82 |
Perl Sun::Solaris::Kstat library is locally available, this module will |
|---|
| 83 |
prefer that method first, bypassing vmstat collection. Otherwise, this |
|---|
| 84 |
module falls back on the standard vmstat collection method. |
|---|
| 85 |
|
|---|
| 86 |
=head2 PROC METRICS |
|---|
| 87 |
|
|---|
| 88 |
Linux configurations use /proc/meminfo for memory statistics. |
|---|
| 89 |
|
|---|
| 90 |
=head2 SYSCTL METRICS |
|---|
| 91 |
|
|---|
| 92 |
FreeBSD configurations use sysctl to extract the most common memory |
|---|
| 93 |
statistics. With the exception of hw.physmem, all metrics are pulled |
|---|
| 94 |
from the vm.stats.vm branch. |
|---|
| 95 |
|
|---|
| 96 |
=cut |
|---|
| 97 |
|
|---|
| 98 |
sub handler { |
|---|
| 99 |
my $self = shift; |
|---|
| 100 |
my $disk = $self->{'check_name'}; |
|---|
| 101 |
my $config = $self->{'config'}; |
|---|
| 102 |
my $vmstat_path = $config->{'vmstat_path'} || 'vmstat'; |
|---|
| 103 |
|
|---|
| 104 |
if ($osname eq 'solaris') { |
|---|
| 105 |
my $pagesize = run_command('pagesize'); |
|---|
| 106 |
if ($usekstat && $pagesize) { |
|---|
| 107 |
my %metrics; |
|---|
| 108 |
$kstat->update(); |
|---|
| 109 |
my $syspages = $kstat->{'unix'}->{0}->{'system_pages'}; |
|---|
| 110 |
|
|---|
| 111 |
foreach (keys %$syspages) { |
|---|
| 112 |
$metrics{"kstat_${_}"} = [int($syspages->{$_} * $pagesize / 1024), 'L'] unless ($_ eq 'class'); |
|---|
| 113 |
} |
|---|
| 114 |
$metrics{'kstat_cache_mem'} = [int($kstat->{'zfs'}->{0}->{'arcstats'}->{'size'} / 1024), 'L']; |
|---|
| 115 |
return \%metrics; |
|---|
| 116 |
} else { |
|---|
| 117 |
my $output = run_command("$vmstat_path"); |
|---|
| 118 |
if ($output =~ /.*cs\s+us\s+sy\s+id\n\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\d+).*/) { |
|---|
| 119 |
return { |
|---|
| 120 |
'actv_mem' => [$1, 'L'], |
|---|
| 121 |
'free_mem' => [$2, 'L'] |
|---|
| 122 |
}; |
|---|
| 123 |
} else { |
|---|
| 124 |
die "Unable to extract statistics\n"; |
|---|
| 125 |
} |
|---|
| 126 |
} |
|---|
| 127 |
} elsif ($osname eq 'linux') { |
|---|
| 128 |
my %metrics; |
|---|
| 129 |
open(MEMINFO, '/proc/meminfo') || die "Unable to read proc: $!\n"; |
|---|
| 130 |
while (<MEMINFO>) { |
|---|
| 131 |
/(\w+)\:\s+(\d+).*/; |
|---|
| 132 |
$metrics{$1} = [$2, 'i']; |
|---|
| 133 |
} |
|---|
| 134 |
close(MEMINFO); |
|---|
| 135 |
return \%metrics; |
|---|
| 136 |
} elsif ($osname eq 'freebsd') { |
|---|
| 137 |
my %metrics; |
|---|
| 138 |
open(SYSCTL, 'sysctl hw.physmem vm.stats.vm |') || die "Unable to read sysctl: $!\n"; |
|---|
| 139 |
while (<SYSCTL>) { |
|---|
| 140 |
/(.*)\:\s+(\d+).*/; |
|---|
| 141 |
$metrics{$1} = [$2, 'i']; |
|---|
| 142 |
} |
|---|
| 143 |
for my $page qw( cache inactive active wire free page ) { |
|---|
| 144 |
$metrics{"vm.stats.vm.v_${page}_count"}->[0] *= ($metrics{'vm.stats.vm.v_page_size'}->[0] / 1024); |
|---|
| 145 |
} |
|---|
| 146 |
close(SYSCTL); |
|---|
| 147 |
return \%metrics; |
|---|
| 148 |
} elsif ($osname eq 'openbsd') { |
|---|
| 149 |
my $output = run_command("$vmstat_path"); |
|---|
| 150 |
if ($output =~ /.*cs\s+us\s+sy\s+id\n\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\d+).*/) { |
|---|
| 151 |
return { |
|---|
| 152 |
'actv_mem' => [$1, 'i'], |
|---|
| 153 |
'free_mem' => [$2, 'i'] |
|---|
| 154 |
}; |
|---|
| 155 |
} else { |
|---|
| 156 |
die "Unable to extract statistics\n"; |
|---|
| 157 |
} |
|---|
| 158 |
} else { |
|---|
| 159 |
die "Unsupported platform: $osname\n"; |
|---|
| 160 |
} |
|---|
| 161 |
}; |
|---|
| 162 |
|
|---|
| 163 |
1; |
|---|