root/lib/Core/Memstat.pm

Revision b8708d6f4067cf4246d5353bd1eccc82d33d0e46, 4.0 kB (checked in by Jason Dixon <jdixon@omniti.com>, 4 years ago)

remove debugging, fix docs

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