XenServer API from Perl

I’m adding a plugin to my monitoring package for checking various resources on XenServer but Citrix doesn’t provide a nifty Perl wrapper for their XenAPI.  Didn’t find much online when digging but was able to piece together a solution.  Pretty simple.

use Frontier::Client;

my $host = 'xen';    # hostname or address
my $user = 'root';   # login name
my $pass = 'secret'; # password

# login
my $client = Frontier::Client->new(url=>'https://'.$host);
my $ret = $client->call('session.login_with_password',
                        $user, $pass, '1.0.0');
die(join(', ', @{$ret->{ErrorDescription}}) 
    unless $ret->{Status} eq 'Success';
my $session = $ret->{Value};

# list all hosts
$ret = $client->call('hosts.get_all', $session);
foreach (@{$ret->{Value}}) {
    $ret = $client->call('host.get_name_label', $session, $_);
    print $ret->{Value}, "\n";
}

# logout
$client->call('session.logout', $session);

See here for details of the objects, properties, and methods available.  In the first $client->call() above, I’m calling the login_with_password method on the session object.  The $ret returned, assuming the credentials are correct, is a hashref with Status and Value keys.  The Status value will be Success and the Value value will be a string that refers to a session object.  Pass this value to subsequent calls to authenticate like the second $client->call() where I’m getting the list of hosts.

You’ll want to check $ret each time to make sure $ret->{Status} eq 'Success' and handle errors sanely.