Projet

Général

Profil

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

root / plugins / other / xen-cpu @ dffaa52a

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

1 6355d3d2 Adam Crews
#!/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 origional 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
#%# family=auto
17
#%# capabilities=autoconf
18
19
# Define where to find xm tools
20
my $XM = '/usr/sbin/xm';
21
my $XMTOP = '/usr/sbin/xentop';
22
23
##############
24
# You should not need to edit anything below here
25
#
26
27
use strict;
28
29
$ENV{PATH} = '/bin:/usr/bin:/usr/sbin';
30
31
my $arg; undef($arg);
32
if (defined($ARGV[0])) {
33
	$arg = 'config' if ($ARGV[0] eq 'config');
34
	$arg = 'autoconf' if ($ARGV[0] eq 'autoconf');
35
36
	if ( "$arg" eq 'autoconf') {
37
		if ( -e $XM && -e $XMTOP ) {
38
			print "yes\n";
39
			exit 0;
40
		} else {
41
			print "no ($XM and/or $XMTOP not found\n";
42
			exit 1;
43
		}
44
	}
45
46
	if ( "$arg" eq 'config') {
47
		my %cnf; undef(%cnf);
48
		%cnf = (
49
			'graph_title' => 'Xen Domain CPU Usage',
50
			'graph_args' => '--base 1000 -l 0 --upper-limit 100 --rigid',
51
			'graph_vlabel' => 'Percent (%)',
52
			'graph_category' => 'xen',
53
			'graph_info' => 'Display the % of CPU Usage for each domain',
54
		);	
55
56
		my @domains = `$XM list`;
57
		shift(@domains); # we dont need the header line
58
		my $cnt = "0";
59
		foreach my $domain ( @domains ) {
60
			my ($dom,undef) = split(/\s/, $domain, 2);
61
			# we need to change - and . to _ or things get weird with the graphs
62
			# some decent quoting would probably fix this, but this works for now
63
			$dom =~ s/[-.]/_/g;
64
65
			$cnf{ "$dom" . '.label' } = "$dom";
66
			$cnf{ "$dom" . '.draw' } = 'STACK';
67
			$cnf{ "$dom" . '.min' } = '0';
68
			$cnf{ "$dom" . '.max' } = '100';
69
			$cnf{ "$dom" . '.info' } = '% CPU used for ' . "$dom";
70
71
			if ( "$cnt" == "0") { $cnf{$dom.'.draw'} = 'AREA'; }
72
			$cnt++;
73
		}
74
	
75
		foreach my $key (sort(keys(%cnf))) {
76
			print "$key $cnf{$key}\n";
77
		}
78
		exit 0;
79
	}
80
}
81
82
# Nothing was passed as an argument, so let's just return the proper values
83
84
my @chunks; undef(@chunks);
85
86
{ 
87
	# run the xentop command a few times because the first reading is not always accurate
88
	local $/ = undef;
89
	@chunks = split(/^xentop - .*$/m, `$XMTOP -b -i2 -d2`);
90
}
91
92
# Take only the last run of xentop
93
my @stats = split (/\n/,pop(@chunks));
94
95
# remove the first 4 items that are junk that we don't need.
96
shift(@stats); 
97
shift(@stats); 
98
shift(@stats); 
99
shift(@stats); 
100
101
my %vals; undef(%vals);
102
103
foreach my $domain (@stats) {
104
	# trim the leading whitespace
105
	$domain =~ s/^\s+//;
106
	my @v_tmp = split(/\s+/, $domain);
107
108
	# we need to change - and . to _ or things get weird with the graphs
109
	# some decent quoting would probably fix this, but this works for now
110
	$v_tmp[0] =~ s/[-.]/_/g;
111
112
	$vals{$v_tmp[0]}{'cpu_percent'} = $v_tmp[3];
113
	$vals{$v_tmp[0]}{'vcpu'} = $v_tmp[8];
114
	if ( $vals{$v_tmp[0]}{'vcpu'} =~ m/n\/a/ ) {
115
		my $cpu = `grep -c "processor" < /proc/cpuinfo`;
116
		if ( $cpu =~ m/^(\d+)$/ ) {
117
			$vals{$v_tmp[0]}{'vcpu'} = $1;
118
		}
119
	}
120
}
121
122
foreach my $key (sort(keys(%vals))) {
123
	print "$key.value " . ($vals{$key}{'cpu_percent'}/$vals{'Domain_0'}{'vcpu'}), "\n";
124
}