root / plugins / other / xen_cpu_v2 @ b809ad3e
Historique | Voir | Annoter | Télécharger (4,27 ko)
| 1 |
#!/usr/bin/perl -w |
|---|---|
| 2 |
# |
| 3 |
# xen_cpu_v2.pl 1.00 |
| 4 |
# Script to minitor the CPU usage of Xen domains |
| 5 |
# Zoltan HERPAI (c) 2009, wigyori@uid0.hu |
| 6 |
# |
| 7 |
# Based loosely on Adam Crews' xen_cpu script |
| 8 |
# |
| 9 |
# This script tries to measure the CPU usage of the Xen guests |
| 10 |
# accurately. |
| 11 |
# The problem with the current monitoring script is that these |
| 12 |
# scripts use the CPU output of xentop or xm list, which might be |
| 13 |
# inaccurate due to the resources used up at the time of the query by |
| 14 |
# the xm or xentop command. |
| 15 |
# |
| 16 |
# This script stores the previous value of the CPU sec value of the given |
| 17 |
# guests in a tempfile, then does some simple calculations to get the real |
| 18 |
# CPU usage percentage of the guests. |
| 19 |
# |
| 20 |
|
| 21 |
#%# family=auto |
| 22 |
#%# capabilities=autoconf |
| 23 |
|
| 24 |
use strict; |
| 25 |
use POSIX; |
| 26 |
|
| 27 |
# Define where to find xm tools |
| 28 |
my $XM = '/usr/sbin/xm'; |
| 29 |
my $XMTOP = '/usr/sbin/xentop'; |
| 30 |
my $curtime = time(); |
| 31 |
my $basename = `/usr/bin/basename $0`; chop ($basename); |
| 32 |
my $TEMPFILE = "/tmp/$basename"; |
| 33 |
|
| 34 |
my $debug = 0; |
| 35 |
|
| 36 |
############## |
| 37 |
# You should not need to edit anything below here |
| 38 |
# |
| 39 |
|
| 40 |
$ENV{PATH} = '/bin:/usr/bin:/usr/sbin';
|
| 41 |
|
| 42 |
my $arg; |
| 43 |
if ( defined($ARGV[0]) ) |
| 44 |
{
|
| 45 |
if ( $ARGV[0] eq 'config' ) |
| 46 |
{
|
| 47 |
$arg = 'config'; |
| 48 |
} |
| 49 |
if ( $ARGV[0] eq 'autoconf' ) |
| 50 |
{
|
| 51 |
$arg = 'autoconf'; |
| 52 |
} |
| 53 |
|
| 54 |
if ( $arg eq 'autoconf') |
| 55 |
{
|
| 56 |
if ( -e $XM && -e $XMTOP ) |
| 57 |
{
|
| 58 |
print "yes\n"; |
| 59 |
exit 0; |
| 60 |
} |
| 61 |
else |
| 62 |
{
|
| 63 |
print "no ($XM and/or $XMTOP not found\n"; |
| 64 |
exit 1; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if ( $arg eq 'config' ) |
| 69 |
{
|
| 70 |
my %cnf; |
| 71 |
%cnf = ( |
| 72 |
'graph_title' => 'Xen Domain CPU Usage v2', |
| 73 |
'graph_args' => '--base 1000 -l 0 --upper-limit 100 --rigid', |
| 74 |
'graph_vlabel' => 'Percent (%)', |
| 75 |
'graph_category' => 'xen', |
| 76 |
'graph_info' => 'Display the % of CPU Usage for each domain', |
| 77 |
); |
| 78 |
|
| 79 |
my @domains = `$XM list`; |
| 80 |
my $cnt = 0; |
| 81 |
shift(@domains); # we dont need the header line |
| 82 |
foreach my $domain ( @domains ) |
| 83 |
{
|
| 84 |
my ($dom,undef) = split(/\s/, $domain, 2); |
| 85 |
$dom =~ s/[-.]/_/g; |
| 86 |
$cnf{ "$dom" . '.label' } = "$dom";
|
| 87 |
$cnf{ "$dom" . '.draw' } = 'STACK';
|
| 88 |
$cnf{ "$dom" . '.min' } = '0';
|
| 89 |
$cnf{ "$dom" . '.max' } = '100';
|
| 90 |
$cnf{ "$dom" . '.info' } = '% CPU used for ' . "$dom";
|
| 91 |
# $cnf{ "$dom" . '.draw' } = 'AREA';
|
| 92 |
if ( $cnt == 0 ) |
| 93 |
{
|
| 94 |
$cnf{ "$dom" . '.draw' } = 'AREA';
|
| 95 |
} |
| 96 |
$cnt++; |
| 97 |
} |
| 98 |
foreach my $key (sort(keys(%cnf))) |
| 99 |
{
|
| 100 |
print "$key $cnf{$key}\n";
|
| 101 |
} |
| 102 |
exit 0; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
my @xmlist = `$XM list`; |
| 107 |
shift (@xmlist); |
| 108 |
|
| 109 |
my %dom; my $name; my $oldtime; |
| 110 |
# Read old data |
| 111 |
if ( -e $TEMPFILE ) |
| 112 |
{
|
| 113 |
open(FH, "<", $TEMPFILE) or die $!; |
| 114 |
$oldtime = <FH>; |
| 115 |
|
| 116 |
if ( $debug ) |
| 117 |
{
|
| 118 |
print "Oldtime: $oldtime\n"; |
| 119 |
} |
| 120 |
|
| 121 |
while (<FH>) |
| 122 |
{
|
| 123 |
# Get the guest name and its CPU usage, and store it in $dom |
| 124 |
$_ =~ /(\S+)\s+\S+\s+\S+\s+\d+\s+\S+\s+(\S+)/; |
| 125 |
$dom{$1}->{'oldtime'} = $2;
|
| 126 |
} |
| 127 |
|
| 128 |
close FH; |
| 129 |
} |
| 130 |
|
| 131 |
my $diff; my $cpusum = 0; |
| 132 |
foreach my $domain ( @xmlist ) |
| 133 |
{
|
| 134 |
# Get the domains' name and current CPU usage, store it in $dom |
| 135 |
$domain =~ /(\S+)\s+\S+\s+\S+\s+\d+\s+\S+\s+(\S+)/; |
| 136 |
$dom{$1}->{'newtime'} = $2;
|
| 137 |
|
| 138 |
$diff = $dom{$1}->{'newtime'} - $dom{$1}->{'oldtime'};
|
| 139 |
$diff = sprintf("%.2f", $diff);
|
| 140 |
|
| 141 |
# Calc the diff between old and new cputime, or reset the counter |
| 142 |
if ( $diff < 0 ) |
| 143 |
{
|
| 144 |
$diff = $dom{$1}->{'newtime'};
|
| 145 |
} |
| 146 |
$dom{$1}->{'diff'} = $diff;
|
| 147 |
|
| 148 |
# Calc a sum CPU usage |
| 149 |
$cpusum = $cpusum + $diff; |
| 150 |
} |
| 151 |
|
| 152 |
my $numcpus = `$XM info |grep nr_cpus |cut -d \: -f 2`; |
| 153 |
my $timediff = $curtime - $oldtime; |
| 154 |
my $tcpuavail = $numcpus * $timediff; |
| 155 |
|
| 156 |
if ( $debug ) |
| 157 |
{
|
| 158 |
print "UsedCPUtime sum: $cpusum\n"; |
| 159 |
print "CPUs: $numcpus\n"; |
| 160 |
print "Timediff: $timediff\n"; |
| 161 |
print "Total CPU time available: $tcpuavail\n"; |
| 162 |
} |
| 163 |
|
| 164 |
my $key; my $value; |
| 165 |
while (($key, $value) = each %dom) |
| 166 |
{
|
| 167 |
# Calc a percentage based on the used CPU time sum |
| 168 |
my $tmp = 0; |
| 169 |
$tmp = ( $dom{$key}->{'diff'} / $cpusum ) * 100;
|
| 170 |
$dom{$key}->{'pc_time'} = sprintf("%.2f", $tmp);
|
| 171 |
|
| 172 |
# Calc a percentage based on the _total_ available CPU time |
| 173 |
$tmp = 0; |
| 174 |
$tmp = ( $dom{$key}->{'diff'} / $tcpuavail ) * 100;
|
| 175 |
$dom{$key}->{'pc_tcpu'} = sprintf("%.2f", $tmp);
|
| 176 |
|
| 177 |
if ( $debug ) |
| 178 |
{
|
| 179 |
print "$key newtime: ".$dom{$key}->{'newtime'}.", oldtime: ".$dom{$key}->{'oldtime'}.", diff: ".$dom{$key}->{'diff'}.", pc_bytime ".$dom{$key}->{'pc_time'}.", pc_bytcpu ".$dom{$key}->{'pc_tcpu'}."\n";
|
| 180 |
} |
| 181 |
print "$key.value ".$dom{$key}->{'pc_tcpu'}."\n";
|
| 182 |
} |
| 183 |
|
| 184 |
# We'll need to log out the current "xm list" output, and the current time also. |
| 185 |
open(FH, ">", $TEMPFILE) or die $!; |
| 186 |
print FH $curtime."\n"; |
| 187 |
print FH @xmlist; |
| 188 |
close FH; |
| 189 |
|
