root / plugins / system / pagefaults_by_process @ 17f78427
Historique | Voir | Annoter | Télécharger (2,77 ko)
| 1 |
#!/usr/bin/perl |
|---|---|
| 2 |
# |
| 3 |
# Copyright 2012 Chris Wilson |
| 4 |
# Copyright 2006 Holger Levsen |
| 5 |
# |
| 6 |
# This plugin monitors ALL processes on a system. No exceptions. It can |
| 7 |
# produce very big graphs! But if you want to know which processes are |
| 8 |
# killing your system by page faulting, without knowing what to monitor |
| 9 |
# in advance, this can help; or in addition to one of the more specific |
| 10 |
# plugins to monitor just Apache or MySQL, for example. |
| 11 |
# |
| 12 |
# Each counter is a DERIVE (difference since the last counter reading) |
| 13 |
# of the number of major page faults, usually 4k each, read in by a |
| 14 |
# process. Memory mapped files probably contribute to this. The process |
| 15 |
# cannot continue until the page fault is served, so this is a |
| 16 |
# high-priority read that usually indicates memory starvation. |
| 17 |
# Processes with no page faults at all are ignored. Processes |
| 18 |
# that die may not appear on the graph, and anyway their last chunk of |
| 19 |
# CPU usage before they died is lost. You could modify this plugin to |
| 20 |
# read SAR/psacct records if you care about that. |
| 21 |
# |
| 22 |
# This program is free software; you can redistribute it and/or |
| 23 |
# modify it under the terms of the GNU General Public License |
| 24 |
# as published by the Free Software Foundation; version 2 dated June, |
| 25 |
# 1991. |
| 26 |
|
| 27 |
#scriptname=`basename $0` |
| 28 |
#vsname=`echo $scriptname | perl -ne '/^vserver_proc_VM_(.*)/ and print $1'` |
| 29 |
|
| 30 |
#if [ "$1" = "suggest" ]; then |
| 31 |
# ls -1 /etc/vservers |
| 32 |
# exit 0 |
| 33 |
#elif [ -z "$vsname" ]; then |
| 34 |
# echo "Must be used with a vserver name; try '$0 suggest'" >&2 |
| 35 |
# exit 2 |
| 36 |
#fi |
| 37 |
|
| 38 |
use strict; |
| 39 |
use warnings; |
| 40 |
|
| 41 |
my $cmd = "ps -eo maj_flt,comm h"; |
| 42 |
open PS, "$cmd|" or die "Failed to run ps command: $cmd: $!"; |
| 43 |
|
| 44 |
# my $header_line = <PS>; |
| 45 |
my %total_by_process; |
| 46 |
|
| 47 |
while (<PS>) |
| 48 |
{
|
| 49 |
my @fields = split; |
| 50 |
my $value = $fields[0]; |
| 51 |
my $process = $fields[1]; |
| 52 |
|
| 53 |
# remove any / and everything after it from the process name, |
| 54 |
# e.g. kworker/0:2 -> kworker |
| 55 |
$process =~ s|/.*||; |
| 56 |
|
| 57 |
# remove any . at the end of the name (why does this appear?) |
| 58 |
# $process =~ s|\.$||; |
| 59 |
|
| 60 |
# change any symbol that's not allowed in a munin variable name to _ |
| 61 |
$process =~ tr|a-zA-Z0-9|_|c; |
| 62 |
|
| 63 |
$total_by_process{$process} += $value;
|
| 64 |
} |
| 65 |
|
| 66 |
foreach my $process (keys %total_by_process) |
| 67 |
{
|
| 68 |
# remove all processes with 0 faults |
| 69 |
if (not $total_by_process{$process})
|
| 70 |
{
|
| 71 |
delete $total_by_process{$process};
|
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
close(PS); |
| 76 |
|
| 77 |
if (@ARGV and $ARGV[0] eq "config") |
| 78 |
{
|
| 79 |
print <<END; |
| 80 |
graph_title Page faults by Process |
| 81 |
graph_args --base 1000 |
| 82 |
graph_vlabel major page faults |
| 83 |
graph_category processes |
| 84 |
graph_info Shows number of major page faults caused by each process name |
| 85 |
END |
| 86 |
|
| 87 |
my $stack = 0; |
| 88 |
sub draw() { return $stack++ ? "STACK" : "AREA" }
|
| 89 |
print map |
| 90 |
{
|
| 91 |
"$_.label $_\n" . |
| 92 |
"$_.min 0\n" . |
| 93 |
"$_.type DERIVE\n" . |
| 94 |
"$_.draw " . draw() . "\n" |
| 95 |
} sort keys %total_by_process; |
| 96 |
} |
| 97 |
else |
| 98 |
{
|
| 99 |
print map |
| 100 |
{
|
| 101 |
"$_.value $total_by_process{$_}\n"
|
| 102 |
} sort keys %total_by_process; |
| 103 |
} |
| 104 |
|
| 105 |
exit(0); |
