root/lib/Core/Memstat.pm

Revision 5728f16816bc1a8ed99f37de367a4d818ef591f9, 4.0 kB (checked in by Mark Harrison <mark@omniti.com>, 4 years ago)

Make the Memstat docs a little clearer

git-svn-id: https://labs.omniti.com/resmon/branches/resmon2@336 8c0face9-b7db-6ec6-c4b3-d5f7145c7d55

  • Property mode set to 100644
Line 
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 =back
43
44 =head1 METRICS
45
46 The metrics returned by this module vary by OS and method used:
47
48 =head2 VMSTAT METRICS
49
50 The default/fallback method used is vmstat, which returns the following
51 metrics:
52
53 =over
54
55 =item actv_mem
56
57 Active virtual pages.
58
59 =item free_mem
60
61 Free real memory.
62
63 =back
64
65 =head2 KSTAT METRICS
66
67 Solaris provides an interface to numerous kernel statistics.  If the
68 Perl Sun::Solaris::Kstat library is locally available, this module will
69 prefer that method first, bypassing vmstat collection.  Otherwise, this
70 module falls back on the standard vmstat collection method.
71
72 =head2 PROC METRICS
73
74 Linux configurations use /proc/meminfo for memory statistics.
75
76 =head2 SYSCTL METRICS
77
78 FreeBSD configurations use sysctl to extract the most common memory
79 statistics.  With the exception of hw.physmem, all metrics are pulled
80 from the vm.stats.vm branch.
81
82 =cut
83
84 sub handler {
85     my $self = shift;
86     my $disk = $self->{'check_name'};
87     my $config = $self->{'config'};
88     my $vmstat_path = $config->{'vmstat_path'} || 'vmstat';
89     my $osname = $^O;
90
91     if ($osname eq 'solaris') {
92         my $usekstat = 0;
93         my $pagesize = run_command('pagesize');
94         my $kstat;
95         eval "use Sun::Solaris::Kstat";
96         unless ($@) {
97             $usekstat = 1;
98             $kstat = Sun::Solaris::Kstat->new();
99         }
100         if ($usekstat && $pagesize) {
101             my %metrics;
102             my $syspages = $kstat->{'unix'}->{0}->{'system_pages'};
103
104             foreach (keys %$syspages) {
105                 $metrics{"kstat_${_}"} = [int($syspages->{$_} * $pagesize / 1024), 'i'] unless ($_ eq 'class');
106             }
107             $metrics{'kstat_cache_mem'} = int($kstat->{'zfs'}->{0}->{'arcstats'}->{'size'} / 1024);
108             return \%metrics;
109         } else {
110             my $output = run_command("$vmstat_path");
111             if ($output =~ /.*cs\s+us\s+sy\s+id\n\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\d+).*/) {
112                 return {
113                     'actv_mem' => [$1, 'i'],
114                     'free_mem' => [$2, 'i']
115                 };
116             } else {
117                 die "Unable to extract statistics\n";
118             }
119         }
120     } elsif ($osname eq 'linux') {
121         my %metrics;
122         open(MEMINFO, '/proc/meminfo') || die "Unable to read proc: $!\n";
123         while (<MEMINFO>) {
124             /(\w+)\:\s+(\d+).*/;
125             $metrics{$1} = [$2, 'i'];
126         }
127         close(MEMINFO);
128         return \%metrics;
129     } elsif ($osname eq 'freebsd') {
130         my %metrics;
131         open(SYSCTL, 'sysctl hw.physmem vm.stats.vm |') || die "Unable to read sysctl: $!\n";
132         while (<SYSCTL>) {
133             /(.*)\:\s+(\d+).*/;
134             $metrics{$1} = [$2, 'i'];
135         }
136         for my $page qw( cache inactive active wire free page ) {
137             $metrics{"vm.stats.vm.v_${page}_count"}->[0] *= ($metrics{'vm.stats.vm.v_page_size'}->[0] / 1024);
138         }
139         close(SYSCTL);
140         return \%metrics;
141     } elsif ($osname eq 'openbsd') {
142         my $output = run_command("$vmstat_path");
143         if ($output =~ /.*cs\s+us\s+sy\s+id\n\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(\d+).*/) {
144             return {
145                 'actv_mem' => [$1, 'i'],
146                 'free_mem' => [$2, 'i']
147             };
148         } else {
149             die "Unable to extract statistics\n";
150         }
151     } else {
152         die "Unsupported platform: $osname\n";
153     }
154 };
155
156 1;
Note: See TracBrowser for help on using the browser.