8 years ago I wrote a script to summarise Xen CPU use per domain [1]. Since then changes to Xen required changes to the script. I have new versions for Debian/Wheezy (Xen 4.1) and Debian/Jessie (Xen 4.4).
Here’s a new script for Debian/Wheezy:
#!/usr/bin/perl
use strict;
open(LIST, "xm list --long|") or die "Can't get list";
my $name = "Dom0";
my $uptime = 0.0;
my $cpu_time = 0.0;
my $total_percent = 0.0;
my $cur_time = time();
open(UPTIME, "</proc/uptime") or die "Can't open /proc/uptime";
my @arr = split(/ /, <UPTIME>);
$uptime = $arr[0];
close(UPTIME);
my %all_cpu;
while(<LIST>)
{
chomp;
if($_ =~ /^\)/)
{
my $cpu = $cpu_time / $uptime * 100.0;
if($name =~ /Domain-0/)
{
printf("%s uses %.2f%% of one CPU\n", $name, $cpu);
}
else
{
$all_cpu{$name} = $cpu;
}
$total_percent += $cpu;
next;
}
$_ =~ s/\).*$//;
if($_ =~ /start_time /)
{
$_ =~ s/^.*start_time //;
$uptime = $cur_time – $_;
next;
}
if($_ =~ /cpu_time /)
{
$_ =~ s/^.*cpu_time //;
$cpu_time = $_;
next;
}
if($_ =~ /\(name /)
{
$_ =~ s/^.*name //;
$name = $_;
next;
}
}
close(LIST);
sub hashValueDescendingNum {
$all_cpu{$b} <=> $all_cpu{$a};
}
my $key;
foreach $key (sort hashValueDescendingNum (keys(%all_cpu)))
{
printf("%s uses %.2f%% of one CPU\n", $key, $all_cpu{$key});
}
printf("Overall CPU use approximates %.1f%% of one CPU\n", $total_percent);
Here’s the script for Debian/Jessie:
#!/usr/bin/perl
use strict;
open(UPTIME, "xl uptime|") or die "Can't get uptime";
open(LIST, "xl list|") or die "Can't get list";
my %all_uptimes;
while(<UPTIME>)
{
chomp $_;
next if($_ =~ /^Name/);
$_ =~ s/ +/ /g;
my @split1 = split(/ /, $_);
my $dom = $split1[0];
my $uptime = 0;
my $time_ind = 2;
if($split1[3] eq "days,")
{
$uptime = $split1[2] * 24 * 3600;
$time_ind = 4;
}
my @split2 = split(/:/, $split1[$time_ind]);
$uptime += $split2[0] * 3600 + $split2[1] * 60 + $split2[2];
$all_uptimes{$dom} = $uptime;
}
close(UPTIME);
my $total_percent = 0;
while(<LIST>)
{
chomp $_;
my $dom = $_;
$dom =~ s/ .*$//;
if ( $_ =~ /(\d+)\.[0-9]$/ )
{
my $percent = $1 / $all_uptimes{$dom} * 100.0;
$total_percent += $percent;
printf("%s uses %.2f%% of one CPU\n", $dom, $percent);
}
else
{
next;
}
}
printf("Overall CPU use approximates %.1f%% of one CPU\n", $total_percent);