root / plugins / apache / apache_smaps @ 17f78427
Historique | Voir | Annoter | Télécharger (3,51 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
|
| 3 |
# This plugin watches the Linux kernel SMAPS memory usage statistics |
| 4 |
# available in kernel revisions later than 2.6.14. It watches only the |
| 5 |
# Apache 2 processes, and creates averages, maximums and minimums for |
| 6 |
# a given moment in time for the shared size and the virtual size of |
| 7 |
# the processes. This is useful for estimating the constraints to give |
| 8 |
# to Apache2::SizeLimit. |
| 9 |
|
| 10 |
# Author: Kjetil Kjernsmo <kjetilk@opera.com>, based on work by William Viker |
| 11 |
# Copyright (C) 2007 Opera Software ASA |
| 12 |
# |
| 13 |
# Contibutors: Earle Nietzel <earle.nietzel@gmail.com> |
| 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 |
| 27 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 28 |
|
| 29 |
use strict; |
| 30 |
|
| 31 |
my $ret = undef; |
| 32 |
if (!eval "require Linux::Smaps;") {
|
| 33 |
$ret = "Linux::Smaps not found, fix with cpan -i Linux::Smaps"; |
| 34 |
} |
| 35 |
|
| 36 |
# allow process name and process user to be specified |
| 37 |
my $PNAME = exists $ENV{'pname'} ? $ENV{'pname'} : "httpd";
|
| 38 |
my $PUSER = exists $ENV{'puser'} ? $ENV{'puser'} : "apache";
|
| 39 |
|
| 40 |
if (defined(@ARGV) && ($ARGV[0] eq 'config')) {
|
| 41 |
print "graph_title Apache Smaps\n"; |
| 42 |
print "graph_args --base 1024 -l 0\n"; |
| 43 |
print "graph_vlabel Bytes\n"; |
| 44 |
print "graph_category webserver\n"; |
| 45 |
print "graph_info This graph shows memory usage for each given process.\n"; |
| 46 |
|
| 47 |
print "shr_max.label Shared memory max\n"; |
| 48 |
print "shr_max.draw LINE2\n"; |
| 49 |
print "shr_max.info Max shared memory.\n"; |
| 50 |
print "shr_min.label Shared memory min\n"; |
| 51 |
print "shr_min.draw LINE2\n"; |
| 52 |
print "shr_min.info Minimum shared memory.\n"; |
| 53 |
print "shr_avg.label Shared memory average\n"; |
| 54 |
print "shr_avg.draw LINE2\n"; |
| 55 |
print "shr_avg.info Average shared memory.\n"; |
| 56 |
|
| 57 |
print "vsz_max.label Virtual memory max\n"; |
| 58 |
print "vsz_max.draw LINE2\n"; |
| 59 |
print "vsz_max.info Max Virtual memory.\n"; |
| 60 |
print "vsz_min.label Virtual memory min\n"; |
| 61 |
print "vsz_min.draw LINE2\n"; |
| 62 |
print "vsz_min.info Minimum Virtual memory.\n"; |
| 63 |
print "vsz_avg.label Virtual memory avg\n"; |
| 64 |
print "vsz_avg.draw LINE2\n"; |
| 65 |
print "vsz_avg.info Average Virtual memory.\n"; |
| 66 |
exit(0); |
| 67 |
} |
| 68 |
|
| 69 |
my $i = 0; |
| 70 |
my $max_shared = 0; |
| 71 |
my $sum_shared = 0; |
| 72 |
my $min_shared = 1171541713117116171; # An insanely high number |
| 73 |
my $max_virt = 0; |
| 74 |
my $sum_virt = 0; |
| 75 |
my $min_virt = 1171541713117116171; # An insanely high number |
| 76 |
for my $pid (split(/\n/,`pgrep -x $PNAME -u $PUSER`)) {
|
| 77 |
my $map = Linux::Smaps->new($pid); |
| 78 |
$i++; |
| 79 |
my $shared = $map->shared_clean + $map->shared_dirty; |
| 80 |
my $size = $map->size; |
| 81 |
$sum_shared += $shared; |
| 82 |
$max_shared = $shared if ($shared > $max_shared); |
| 83 |
$min_shared = $shared if ($shared < $min_shared); |
| 84 |
$sum_virt += $size; |
| 85 |
$max_virt = $size if ($size > $max_virt); |
| 86 |
$min_virt = $size if ($size < $min_virt); |
| 87 |
} |
| 88 |
|
| 89 |
# if no processes were found prevents divide by 0 |
| 90 |
if ($i gt 0) {
|
| 91 |
print "shr_max.value $max_shared\n"; |
| 92 |
print "shr_min.value $min_shared\n"; |
| 93 |
print "shr_avg.value ".$sum_shared/$i."\n"; |
| 94 |
print "vsz_max.value $max_virt\n"; |
| 95 |
print "vsz_min.value $min_virt\n"; |
| 96 |
print "vsz_avg.value ".$sum_virt/$i."\n"; |
| 97 |
} |
