Xen host can be monitored and controlled via Xen API ( it uses XML-RPC communications). Here is a sample how to program using XenAPI. For XenAPI to work, you should have the below enty in /etc/xen/xend-config.sxp. You can also control which hosts can connect through API.
— /etc/xen/xend-config.sxp–
(xen-api-server ((9363 none)
(unix none)))
—
Here is a sample perl script which can query and get details from Xen host using Xen API.
x——————————————–x
#!/usr/bin/perl
# Xen API Programming.
# unixfoo – http://unixfoo.blogspot.com/
use Net::Ping;
use RPC::XML;
use RPC::XML::Client;
my $xenhost = $ARGV[0];
my $user = $ARGV[1];
my $passwd = $ARGV[2];
my $xenport = 9363;
my $state = {
“Running” => “ON”,
“Halted” => “OFF”,
“Suspended” => “SUSPENDED”,
“Paused” => “SUSPENDED”,
“Unknown” => “UNKNOWN”
};
my %hash=();
# Check ping status
my $ping = Net::Ping->new();
if ( $ping->ping($xenhost,2) ne ‘1’ )
{
print “$xenhost is downn”;
exit;
}
else
{
# Establish the XEN API connection
my $xen = RPC::XML::Client->new(“http://$xenhost:$xenport”);
my $session = extractvalue($xen->simple_request(“session.login_with_password”,
$user,$passwd));
if (! $session)
{
print “connection failure : $xenhostn”;
exit;
}
my ($host_ref,$domU_ref,$hostname) = ();
$host_ref = extractvalue($xen->simple_request(“session.get_this_host”,
$session,$session));
# Get the reference for all the VMs on the xen dom0 host.
$domU_ref = extractvalue($xen->simple_request(“host.get_resident_VMs”,
$session, $host_ref));
my ($vmtype,@domUarr) = ();
print “dom0 host – $xenhostn”;
foreach (@$domU_ref)
{
my $vm_ref = $_;
# General VM info
my $vmrecord = extractvalue($xen->simple_request(“VM.get_record”,$session,$vm_ref));
if ( $vmrecord->{domid} eq 0 ) { next; };
my $vmname = $vmrecord->{name_label};
my $vmstate = $vmrecord->{power_state};
$vmstate = $state->{$vmstate};
my $xenmemmax = $vmrecord->{memory_dynamic_max};
my $xenmemmin = $vmrecord->{memory_dynamic_min};
$xenmemmax = $xenmemmax/1024/1024;
$xenmemmin = $xenmemmin/1024/1024;
if ( $vmrecord->{HVM_boot_policy} eq ” )
{
$vmtype = ‘Para-Virt’;
}
else
{
$vmtype = ‘Full-Virt’;
}
print “$vmname – $vmstate – $xenmemmax – $xenmemmin – $vmtypen”;
}
}
sub extractvalue
{
my ($val) = @_;
if ($val->{‘Status’} eq “Success”)
{
return $val->{‘Value’};
}
else
{
return undef;
}
}
x——————————————–x
