Ruby Ruby Ruby Ruby!

Ruby Ruby Ruby Ruby!

Whilst all the big systems management companies are integrating with UCS, the inquisitive amongst us are surely intrigued by the open API offered by UCS.

We all know how popular the vCenter API has been, including the amazing success of Powershell, so imagine if the same happened to UCS… or hang on, vCenter + UCS… hmmm, the mind boggles.

I’ve been doing some research (ok, foolin’ around) with the UCS API today and it has taken me just a few hours, from scratch, to get a complete system working.  Amazing.  No way would this have been possible without VMware Workstation, Ubuntu, Ruby, the amazing Ruby libraries (GEM, rest-client, REXML) and the open Cisco UCS API.

So what did I find?  Well, only having a few hours today I just managed to get a few odds and ends working.. but now my mind is abuzz with “vmkusage for UCS”… :-)

I’ll let the script and output do the talking, suffice to say I asked UCS what blades were in there, what they were called and their power status.  Through the API I can get every value, I can also set values, and I can listen (callbacks) for events.  S W E E T!

Here’s the script (download)

#!/usr/bin/ruby -v
#
# Steve Chambers rough'n'ready RUCS script (Ruby UCS)
# No error checking or anything clever, just plain
# code to demonstrate how easy this is
#
require 'rubygems' # rest_client is a GEM addon, so need this to use it
require 'rest_client' # hides all the hard stuff for connecting to RESTful service
require 'rexml/document' # makes XML work dead easy
include REXML # means I don't have to put REXML:: before every Document class

# Login to UCS
# initiate.xml is simply the <aaaLogin /> code from page 2-2 of the UCS API pdf
ucsResp = RestClient.post 'http://beltac/nuova', File.read('../xml/initiate.xml'), :content_type => 'text/xml'

# Create an XML doc and parse it, especially taking out the cookie.
ucsLoginDoc = Document.new(ucsResp)
ucsLoginRoot = ucsLoginDoc.root
ucsCookie = ucsLoginRoot.attributes['outCookie']

#
# Now we've got a session and a cookie, the UCS XML API is at our disposal!
#

# The UCS XML-API has a powerful query system, so let's see the blades

computeBladesXML = '<configResolveClass cookie="' + ucsCookie + '" inHierarchical="false" classId="computeBlade"/>'
ucsResp = RestClient.post 'http://beltac/nuova', computeBladesXML, :content_type => 'text/xml'

# Create an XML doc out of ucsResp and parse it for stuff
ucsBladesDoc = Document.new(ucsResp)
ucsBladesDoc.elements.each("configResolveClass/outConfigs/*") {
 |blade|
 bladeName = blade.attributes["dn"]
 bladePower = blade.attributes["operPower"]
 puts "Blade " + bladeName + " is currently powered " + bladePower
}

And here’s the output:

stechamb@ubuntu:~/dev/ruby/scripts$ ./test.rb
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
Blade sys/chassis-2/blade-6 is currently powered on
Blade sys/chassis-1/blade-1 is currently powered off
Blade sys/chassis-1/blade-2 is currently powered on
Blade sys/chassis-2/blade-3 is currently powered on
Blade sys/chassis-2/blade-1 is currently powered off
Blade sys/chassis-2/blade-2 is currently powered off
Blade sys/chassis-2/blade-5 is currently powered off
Blade sys/chassis-2/blade-7 is currently powered off
Blade sys/chassis-2/blade-4 is currently powered off
Blade sys/chassis-2/blade-8 is currently powered off
Blade sys/chassis-1/blade-3 is currently powered off
Blade sys/chassis-1/blade-4 is currently powered off
Blade sys/chassis-1/blade-5 is currently powered off
Blade sys/chassis-1/blade-6 is currently powered off
Blade sys/chassis-1/blade-7 is currently powered off
Blade sys/chassis-1/blade-8 is currently powered off
Blade sys/chassis-4/blade-1 is currently powered off
Blade sys/chassis-3/blade-2 is currently powered off
Blade sys/chassis-3/blade-3 is currently powered off
Blade sys/chassis-3/blade-4 is currently powered off
Blade sys/chassis-3/blade-5 is currently powered off
Blade sys/chassis-3/blade-1 is currently powered off
Blade sys/chassis-3/blade-6 is currently powered off
Blade sys/chassis-4/blade-2 is currently powered off

Nice…. :-)

Related posts:

  1. Cisco 6100XP aint Switches like VMware ESX aint Linux
  2. The stars are aligning: VMware, Cisco and ESG!
  3. Cisco UCS dog food tastes nice
  4. Cisco Solutions for a VMware View 4.0 Environment Design Guide