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