Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / cpu / cpu_by_process @ 17f78427

Historique | Voir | Annoter | Télécharger (2,94 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 where your CPU time
8
# is going without knowing what to monitor in advance, this can help;
9
# or in addition to one of the more specific CPU plugins to monitor
10
# just Apache or MySQL, for example.
11
#
12
# It's not obvious what the graph heights actually mean, even to me.
13
# Each counter is a DERIVE (difference since the last counter reading)
14
# of the CPU time usage (in seconds) accounted to each process, summed
15
# by the process name, so all Apache and all MySQL processes are grouped
16
# together. Processes with no CPU usage at all are ignored. Processes
17
# that die may not appear on the graph, and anyway their last chunk of
18
# CPU usage before they died is lost. You could modify this plugin to
19
# read SAR/psacct records if you care about that.
20
#
21
# This program is free software; you can redistribute it and/or
22
# modify it under the terms of the GNU General Public License
23
# as published by the Free Software Foundation; version 2 dated June,
24
# 1991.
25

    
26
#scriptname=`basename $0`
27
#vsname=`echo $scriptname | perl -ne '/^vserver_proc_VM_(.*)/ and print $1'`
28

    
29
#if [ "$1" = "suggest" ]; then
30
#	ls -1 /etc/vservers
31
#	exit 0
32
#elif [ -z "$vsname" ]; then
33
#	echo "Must be used with a vserver name; try '$0 suggest'" >&2
34
#	exit 2
35
#fi
36

    
37
use strict;
38
use warnings;
39

    
40
my $cmd = "ps -eo time,comm h";
41
open PS, "$cmd|" or die "Failed to run ps command: $cmd: $!";
42

    
43
# my $header_line = <PS>;
44
my %total_cpu_by_process;
45

    
46
while (<PS>)
47
{
48
	my @fields = split;
49
	my $cputime = $fields[0];
50
	my $process = $fields[1];
51

    
52
	# remove any / and everything after it from the process name,
53
	# e.g. kworker/0:2 -> kworker
54
	$process =~ s|/.*||;
55

    
56
	# remove any . at the end of the name (why does this appear?)
57
	# $process =~ s|\.$||;
58

    
59
	# change any symbol that's not allowed in a munin variable name to _
60
	$process =~ tr|a-zA-Z0-9|_|c;
61

    
62
	my @times = split /:/, $cputime;
63
	my @days = split /-/, $times[0];
64
	if ($days[1]) {
65
		$cputime = ((($days[0] * 24) + $days[1]) * 60 + $times[1]) * 60 + $times[2];
66
	} else {
67
		$cputime = (($times[0] * 60) + $times[1]) * 60 + $times[2];
68
	}
69
	$total_cpu_by_process{$process} += $cputime;
70
}
71

    
72
foreach my $process (keys %total_cpu_by_process)
73
{
74
	# remove all processes with 0 cpu time
75
	if (not $total_cpu_by_process{$process})
76
	{
77
		delete $total_cpu_by_process{$process};
78
	}
79
}
80

    
81
close(PS);
82

    
83
if (@ARGV and $ARGV[0] eq "config")
84
{
85
	print <<END;
86
graph_title CPU time by Process
87
graph_args --base 1000
88
graph_vlabel seconds
89
graph_category processes
90
graph_info Shows CPU time used by each process name
91
END
92

    
93
	my $stack = 0;
94
	sub draw() { return $stack++ ? "STACK" : "AREA" }
95
	print map
96
	{
97
		"$_.label $_\n" .
98
		"$_.min 0\n" .
99
		"$_.type DERIVE\n" .
100
		"$_.draw " . draw() . "\n"
101
	} sort keys %total_cpu_by_process;
102
}
103
else
104
{
105
	print map
106
	{
107
		"$_.value $total_cpu_by_process{$_}\n"
108
	} sort keys %total_cpu_by_process;
109
}
110

    
111
exit(0);