root / plugins / system / total_by_process_ @ c86e4ab2
Historique | Voir | Annoter | Télécharger (3,06 ko)
| 1 | ef86c469 | Chris Wilson | #!/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 | use strict; |
||
| 28 | use warnings; |
||
| 29 | |||
| 30 | my $scriptname = $0; |
||
| 31 | $scriptname =~ s|.*/||; |
||
| 32 | my $fieldname = ($scriptname =~ /^total_by_process_(.*)_(.*)/) ? $1 : undef; |
||
| 33 | my $fieldtype = ($scriptname =~ /^total_by_process_(.*)_(.*)/) ? $2 : undef; |
||
| 34 | |||
| 35 | if (@ARGV and $ARGV[1] eq "suggest") |
||
| 36 | {
|
||
| 37 | system("ps L | cut -d' ' -f1");
|
||
| 38 | exit(0); |
||
| 39 | } |
||
| 40 | |||
| 41 | if (!$fieldname) |
||
| 42 | {
|
||
| 43 | print STDERR "Must be used with a PS format specifier name; try '$0 suggest'"; |
||
| 44 | exit(2); |
||
| 45 | } |
||
| 46 | |||
| 47 | unless ($fieldtype =~ /^(GAUGE|DERIVE)$/) |
||
| 48 | {
|
||
| 49 | print STDERR "Unknown field type $fieldtype: should be GAUGE or DERIVE"; |
||
| 50 | exit(2); |
||
| 51 | } |
||
| 52 | |||
| 53 | my $cmd = "ps -eo $fieldname,comm h"; |
||
| 54 | open PS, "$cmd|" or die "Failed to run ps command: $cmd: $!"; |
||
| 55 | |||
| 56 | # my $header_line = <PS>; |
||
| 57 | my %total_by_process; |
||
| 58 | |||
| 59 | while (<PS>) |
||
| 60 | {
|
||
| 61 | my @fields = split; |
||
| 62 | my $value = $fields[0]; |
||
| 63 | my $process = $fields[1]; |
||
| 64 | |||
| 65 | # remove any / and everything after it from the process name, |
||
| 66 | # e.g. kworker/0:2 -> kworker |
||
| 67 | $process =~ s|/.*||; |
||
| 68 | |||
| 69 | # remove any . at the end of the name (why does this appear?) |
||
| 70 | # $process =~ s|\.$||; |
||
| 71 | |||
| 72 | # change any symbol that's not allowed in a munin variable name to _ |
||
| 73 | $process =~ tr|a-zA-Z0-9|_|c; |
||
| 74 | |||
| 75 | $total_by_process{$process} += $value;
|
||
| 76 | } |
||
| 77 | |||
| 78 | foreach my $process (keys %total_by_process) |
||
| 79 | {
|
||
| 80 | # remove all processes with 0 faults |
||
| 81 | if (not $total_by_process{$process})
|
||
| 82 | {
|
||
| 83 | delete $total_by_process{$process};
|
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | close(PS); |
||
| 88 | |||
| 89 | if (@ARGV and $ARGV[1] == "config") |
||
| 90 | {
|
||
| 91 | print <<END; |
||
| 92 | graph_title $fieldname by Process |
||
| 93 | graph_category system |
||
| 94 | graph_info Shows total of $fieldname (reported by ps) for each process name |
||
| 95 | 73239ed2 | Chris Wilson | graph_vlabel $fieldname (from ps) |
| 96 | ef86c469 | Chris Wilson | END |
| 97 | |||
| 98 | # graph_args --base 1000 |
||
| 99 | # graph_vlabel seconds |
||
| 100 | |||
| 101 | my $stack = 0; |
||
| 102 | sub draw() { return $stack++ ? "STACK" : "AREA" }
|
||
| 103 | print map |
||
| 104 | {
|
||
| 105 | "$_.label $_\n" . |
||
| 106 | "$_.min 0\n" . |
||
| 107 | "$_.type $fieldtype\n" . |
||
| 108 | "$_.draw " . draw() . "\n" |
||
| 109 | } sort keys %total_by_process; |
||
| 110 | } |
||
| 111 | else |
||
| 112 | {
|
||
| 113 | print map |
||
| 114 | {
|
||
| 115 | "$_.value $total_by_process{$_}\n"
|
||
| 116 | } sort keys %total_by_process; |
||
| 117 | } |
||
| 118 | |||
| 119 | exit(0); |
