Projet

Général

Profil

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

root / plugins / xen / xen-cpu @ 17f78427

Historique | Voir | Annoter | Télécharger (3,18 ko)

1
#!/usr/bin/perl -wT
2
#
3
# Script to minitor the cpu usage of Xen domains
4
#
5
# Author: Adam Crews <doo <at> shroom <dot> com>
6
#
7
# License: GPL
8
# Based on the original xen script from Matthias Pfafferodt, syntron at web.de
9
#
10
# Note: Your munin config must run this as root.
11
#
12
# Parameters
13
#   config 	(required)
14
#   autoconf	(optional - used by munin-config)
15
#
16
# Changelog:
17
# Properly ignore the xentop output header line.
18
# Ward Vandewege (ward@gnu.org), 2011-04-20
19
#
20
#%# family=auto
21
#%# capabilities=autoconf
22

    
23
# Define where to find xm tools
24
my $XM = '/usr/sbin/xm';
25
my $XMTOP = '/usr/sbin/xentop';
26

    
27
##############
28
# You should not need to edit anything below here
29
#
30

    
31
use strict;
32

    
33
$ENV{PATH} = '/bin:/usr/bin:/usr/sbin';
34

    
35
my $arg; undef($arg);
36
if (defined($ARGV[0])) {
37
	$arg = 'config' if ($ARGV[0] eq 'config');
38
	$arg = 'autoconf' if ($ARGV[0] eq 'autoconf');
39

    
40
	if ( "$arg" eq 'autoconf') {
41
		if ( -e $XM && -e $XMTOP ) {
42
			print "yes\n";
43
			exit 0;
44
		} else {
45
			print "no ($XM and/or $XMTOP not found\n";
46
			exit 1;
47
		}
48
	}
49

    
50
	if ( "$arg" eq 'config') {
51
		my %cnf; undef(%cnf);
52
		%cnf = (
53
			'graph_title' => 'Xen Domain CPU Usage',
54
			'graph_args' => '--base 1000 -l 0 --upper-limit 100 --rigid',
55
			'graph_vlabel' => 'Percent (%)',
56
			'graph_category' => 'virtualization',
57
			'graph_info' => 'Display the % of CPU Usage for each domain',
58
		);
59

    
60
		my @domains = `$XM list`;
61
		shift(@domains); # we dont need the header line
62
		my $cnt = "0";
63
		foreach my $domain ( @domains ) {
64
			my ($dom,undef) = split(/\s/, $domain, 2);
65
			# we need to change - and . to _ or things get weird with the graphs
66
			# some decent quoting would probably fix this, but this works for now
67
			$dom =~ s/[-.]/_/g;
68

    
69
			$cnf{ "$dom" . '.label' } = "$dom";
70
			$cnf{ "$dom" . '.draw' } = 'STACK';
71
			$cnf{ "$dom" . '.min' } = '0';
72
			$cnf{ "$dom" . '.max' } = '100';
73
			$cnf{ "$dom" . '.info' } = '% CPU used for ' . "$dom";
74

    
75
			if ( "$cnt" == "0") { $cnf{$dom.'.draw'} = 'AREA'; }
76
			$cnt++;
77
		}
78

    
79
		foreach my $key (sort(keys(%cnf))) {
80
			print "$key $cnf{$key}\n";
81
		}
82
		exit 0;
83
	}
84
}
85

    
86
# Nothing was passed as an argument, so let's just return the proper values
87

    
88
my @chunks; undef(@chunks);
89

    
90
{
91
	# run the xentop command a few times because the first reading is not always accurate
92
	local $/ = undef;
93
	@chunks = split(/^xentop - .*$/m, `$XMTOP -b -i2 -d2`);
94
}
95

    
96
# Take only the last run of xentop
97
my @stats = split (/\n/,pop(@chunks));
98

    
99
# remove the first 4 items that are junk that we don't need.
100
shift(@stats);
101
shift(@stats);
102
shift(@stats);
103
shift(@stats);
104

    
105
my %vals; undef(%vals);
106

    
107
foreach my $domain (@stats) {
108
	# trim the leading whitespace
109
	$domain =~ s/^\s+//;
110
	my @v_tmp = split(/\s+/, $domain);
111
	next if ($v_tmp[0] eq 'NAME');
112

    
113
	# we need to change - and . to _ or things get weird with the graphs
114
	# some decent quoting would probably fix this, but this works for now
115
	$v_tmp[0] =~ s/[-.]/_/g;
116

    
117
	$vals{$v_tmp[0]}{'cpu_percent'} = $v_tmp[3];
118
	$vals{$v_tmp[0]}{'vcpu'} = $v_tmp[8];
119
	if ( $vals{$v_tmp[0]}{'vcpu'} =~ m/n\/a/ ) {
120
		my $cpu = `grep -c "processor" < /proc/cpuinfo`;
121
		if ( $cpu =~ m/^(\d+)$/ ) {
122
			$vals{$v_tmp[0]}{'vcpu'} = $1;
123
		}
124
	}
125
}
126

    
127
foreach my $key (sort(keys(%vals))) {
128
	print "$key.value " . ($vals{$key}{'cpu_percent'}/$vals{'Domain_0'}{'vcpu'}), "\n";
129
}