Blogs

Showing 1 - 5 of 16 results.
Items per Page 5
of 4

Forums

« Back to Business Solutions

VMware SDK code along with UCS XML API

Combination View Flat View Tree View
Threads [ Previous | Next ]
Here's a little piece of code that combines VMware vSphere's Perl SDK with the UCS XML API. I grew tired of always wondering what UCS blade this or that ESX host in vCenter Server corresponds to. This Perl code queries vCenter for hosts in a given cluster and attempts to correlate the UUIDs it found with associated server profiles on a given UCS.
 
The result looks like this:
 
C:\DOCUME~1\cpaggen\Desktop\VMWARE\DEV\scrap>
C:\DOCUME~1\cpaggen\Desktop\VMWARE\DEV\scrap>perl clusters.pl --server 10.48.82.84 --username cpaggen --password cisco --cluster Production --ucs 10.48.58.12
Logged on to UCS 10.48.58.12 [cookie:1279115610/a3919e0d-0588-4b74-98d3-15534fdf399f]
Found 15 associated servers on UCS
Cluster Name: Production
Overall Status: yellow
Number of hosts: 3
Host esxi-prod-001 with deadbeef-0000-0000-1112-000000000000 (Cisco Systems Inc model N20-B6620-1) is  pnDn="sys/chassis-3/blade-1"
Host esx-prod-003 with deadbeef-0000-0000-1111-000000000000 (Cisco Systems Inc model N20-B6620-1) is  pnDn="sys/chassis-3/blade-5"
Host esxi-prod-002 with deadbeef-0000-0000-1113-000000000000 (Cisco Systems Inc model N20-B6620-1) is  pnDn="sys/chassis-1/blade-2"
Logged out of UCS

End Disconnect
 
Here's the source:
 
#!/bin/perl
#
# List actual location of UCS blades in a given VMware cluster
# Search key is UUID as returned by vCenter Server
#
# cpaggen@cisco.com Jul 2010
#
# v1.0 - quality level: "proof-of-concept"
#
#
# usage: perl clusters.pl --server <ip> --username <user> --password <pwd> --cluster <cluster> --ucs <ip>
#
# ensure vcenter and UCS have the same credentials (ideally use LDAP or any centralized cred. store)

use strict;
use VMware:IRuntime;
use VMware:ILib;
use Data:umper;
use LWP::UserAgent;
use HTTP::Request::Common;
use XML::LibXML;

my %opts = (
   cluster => {
     type => "=s",
     help => "Target Host",
     variable => "CLUSTER",
     required => 1,
   },
   ucs => {
     type => "=s",
     help => "UCS IP address",
     variable => "UCS",
     required => 1,
   },
);

Opts::add_options(%opts);
Opts::parse();
Util::connect();

my $cluster_name = Opts::get_option('cluster');
my $user = Opts::get_option('username');
my $password = Opts::get_option('password');
my $customFieldsManager = Vim::get_view(mo_ref => Vim::get_service_content()->customFieldsManager);
my $cluster_view = Vim::find_entity_view(view_type => 'ClusterComputeResource',filter => { name => $cluster_name });
if(!$cluster_view)
{
 die "Cluster: \"$cluster_name\" was not found on this vCenter Server\n";
}

# Log on to UCS and obtain a cookie

my $browser = LWP::UserAgent->new(agent => 'perl post');
my $xmlcmd = "<aaaLogin cookie='null' inName='".$user."' inPassword='".$password."'/>";
my $ucsip = Opts::get_option('ucs');
my $url = 'http://'.$ucsip.'/nuova';
my $xml_header = "<?xml version='1.0'?>";
my $request = HTTP::Request->new(POST => $url);
$request->content_type("text/xml; charset=utf-8");
$request->content($xmlcmd);

my $response = $browser->request($request);
if ($response->content =~ m/errorDescr=\"([a-zA-Z0-9_\-\. ]+)\".*/)
{
    die ("Login failure: ".$1."\n");
}

$response->content =~ m/outCookie=\"(\d{10}\/\w{8}\-\w{4}\-\w{4}\-\w{4}\-\w{12})\".*/;
my $ucscookie = $1;

# Retrieve list of associated server profiles on UCS

my $xmlcmd = "<configResolveClass cookie='" . $ucscookie . "' classId='lsServer'>
                <inFilter>
                  <eq class='lsServer' property='assocState' value='associated' />
                </inFilter>
              </configResolveClass>";
$request->content($xmlcmd);
$response = $browser->request($request);
my $parser = XML::LibXML->new();
my $xmlDoc = $parser->parse_string($response->content);
my $root = $xmlDoc->getDocumentElement();
my $topnode = $root->nodeName();
my @servers = $root->getElementsByLocalName("lsServer");
my %ucsuuids = ();

# Extract UUIDs from list of servers

foreach (@servers)
{
 # the XML attribute is actually uuid="...", we are extracting the actual UUID
 my $rawkey=$_->getAttributeNode("uuid")->toString(0);
 if ($rawkey =~ m/uuid=\"([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})\"/)
 {
 my $val=$_->getAttributeNode("pnDn")->toString(0);
 $ucsuuids{$1} = $val;
 }
}

print "Logged on to UCS " . $ucsip . " [cookie:" . $ucscookie . "]\n";
print "Found ". scalar(@servers) ." associated servers on UCS \n";
print "Cluster Name: " . $cluster_view->name ."\n";
print "Overall Status: " . $cluster_view->overallStatus->val ."\n";
print "Number of hosts: " . $cluster_view->summary->numHosts ."\n";

# Query vCenter Server - if uuid is found as hash key then value contains UCS blade location

my $hosts = Vim::get_views (mo_ref_array => $cluster_view->host);
foreach my $host (@$hosts)
{
  my $host_hardware = $host->hardware->systemInfo;
  my $uuid=$host_hardware->uuid;
  printf("Host %s (%s) (%s model %s) is at%s\n",$host->name, $uuid, $host_hardware->vendor, $host_hardware->model, $ucsuuids{$uuid});
}

# Logout of UCS

my $xmlcmd = "<aaaLogout inCookie='" . $ucscookie . "'/>";
$request->content($xmlcmd);
$response = $browser->request($request);
print "Logged out of UCS\n";

Collateral


No files available