<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>VMware SDK code along with UCS XML API</title>
  <link rel="alternate" href="http://developer.cisco.com/c/message_boards/find_recent_posts?p_l_id=" />
  <subtitle>VMware SDK code along with UCS XML API</subtitle>
  <id>http://developer.cisco.com/c/message_boards/find_recent_posts?p_l_id=</id>
  <updated>2013-05-21T13:57:27Z</updated>
  <dc:date>2013-05-21T13:57:27Z</dc:date>
  <entry>
    <title>VMware SDK code along with UCS XML API</title>
    <link rel="alternate" href="http://developer.cisco.com/c/message_boards/find_message?p_l_id=&amp;messageId=2357013" />
    <author>
      <name>Christophe Paggen</name>
    </author>
    <id>http://developer.cisco.com/c/message_boards/find_message?p_l_id=&amp;messageId=2357013</id>
    <updated>2010-07-14T19:23:55Z</updated>
    <published>2010-07-14T19:23:55Z</published>
    <summary type="html">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&gt;
C:\DOCUME~1\cpaggen\Desktop\VMWARE\DEV\scrap&gt;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 &lt;ip&gt; --username &lt;user&gt; --password &lt;pwd&gt; --cluster &lt;cluster&gt; --ucs &lt;ip&gt;
#
# ensure vcenter and UCS have the same credentials (ideally use LDAP or any centralized cred. store)

use strict;
use VMware:[img]http://mce_host/html/js/editor/http://developer.cisco.comhttp://developer.cisco.com/cisco2-theme/images/emoticons/pac_man.gif[/img]IRuntime;
use VMware:[img]http://mce_host/html/js/editor/http://developer.cisco.comhttp://developer.cisco.com/cisco2-theme/images/emoticons/pac_man.gif[/img]ILib;
use Data:[img]http://mce_host/html/js/editor/http://developer.cisco.comhttp://developer.cisco.com/cisco2-theme/images/emoticons/smile.gif[/img]umper;
use LWP::UserAgent;
use HTTP::Request::Common;
use XML::LibXML;

my %opts = (
   cluster =&gt; {
     type =&gt; "=s",
     help =&gt; "Target Host",
     variable =&gt; "CLUSTER",
     required =&gt; 1,
   },
   ucs =&gt; {
     type =&gt; "=s",
     help =&gt; "UCS IP address",
     variable =&gt; "UCS",
     required =&gt; 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 =&gt; Vim::get_service_content()-&gt;customFieldsManager);
my $cluster_view = Vim::find_entity_view(view_type =&gt; 'ClusterComputeResource',filter =&gt; { name =&gt; $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-&gt;new(agent =&gt; 'perl post');
my $xmlcmd = "&lt;aaaLogin cookie='null' inName='".$user."' inPassword='".$password."'/&gt;";
my $ucsip = Opts::get_option('ucs');
my $url = 'http://'.$ucsip.'/nuova';
my $xml_header = "&lt;?xml version='1.0'?&gt;";
my $request = HTTP::Request-&gt;new(POST =&gt; $url);
$request-&gt;content_type("text/xml; charset=utf-8");
$request-&gt;content($xmlcmd);

my $response = $browser-&gt;request($request);
if ($response-&gt;content =~ m/errorDescr=\"([a-zA-Z0-9_\-\. ]+)\".*/)
{
    die ("Login failure: ".$1."\n");
}

$response-&gt;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 = "&lt;configResolveClass cookie='" . $ucscookie . "' classId='lsServer'&gt;
                &lt;inFilter&gt;
                  &lt;eq class='lsServer' property='assocState' value='associated' /&gt;
                &lt;/inFilter&gt;
              &lt;/configResolveClass&gt;";
$request-&gt;content($xmlcmd);
$response = $browser-&gt;request($request);
my $parser = XML::LibXML-&gt;new();
my $xmlDoc = $parser-&gt;parse_string($response-&gt;content);
my $root = $xmlDoc-&gt;getDocumentElement();
my $topnode = $root-&gt;nodeName();
my @servers = $root-&gt;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=$_-&gt;getAttributeNode("uuid")-&gt;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=$_-&gt;getAttributeNode("pnDn")-&gt;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-&gt;name ."\n";
print "Overall Status: " . $cluster_view-&gt;overallStatus-&gt;val ."\n";
print "Number of hosts: " . $cluster_view-&gt;summary-&gt;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 =&gt; $cluster_view-&gt;host);
foreach my $host (@$hosts)
{
  my $host_hardware = $host-&gt;hardware-&gt;systemInfo;
  my $uuid=$host_hardware-&gt;uuid;
  printf("Host %s (%s) (%s model %s) is at%s\n",$host-&gt;name, $uuid, $host_hardware-&gt;vendor, $host_hardware-&gt;model, $ucsuuids{$uuid});
}

# Logout of UCS

my $xmlcmd = "&lt;aaaLogout inCookie='" . $ucscookie . "'/&gt;";
$request-&gt;content($xmlcmd);
$response = $browser-&gt;request($request);
print "Logged out of UCS\n";</summary>
    <dc:creator>Christophe Paggen</dc:creator>
    <dc:date>2010-07-14T19:23:55Z</dc:date>
  </entry>
</feed>

