UCS API Ruby Script

Yesterday I completed my first vFoglight Script Agent. There was nothing special about the entire process just making sure to do everything the right way. A colleague of mine was instrumental in getting the integrating with vFoglight. The script on the other hand I got working pretty easily. Ruby proved quite useful and I actually installed ruby on Windows and it worked like a charm. Here is the script I used in case anyone wants to dabble a bit.

On windows I downloaded the windows installer (1.8.7-p352) - http://rubyinstaller.org/downloads/ After Ruby is install I loaded just one GEM
gem install rest-client
Here is the code for the script - ucsmblades.rb
#!/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 '', File.read('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 '', computeBladesXML, :content_type => 'text/xml'

# Create an XML doc out of ucsResp and parse it for stuff
ucsBladesDoc = Document.new(ucsResp)

#print out the results
ucsBladesDoc.elements.each("configResolveClass/outConfigs/*") {
  bladeName = blade.attributes["dn"]
  bladePower = blade.attributes["operPower"]
  bladeDiscovery = blade.attributes["discovery"]
  bladeAssociated = blade.attributes["association"]
  bladeState = blade.attributes["operState"]
  bladeCheck = blade.attributes["checkPoint"]
  bladeConnPath = blade.attributes["connPath"]
  bladeConnStatus = blade.attributes["connStatus"]
  bladeAvailability = blade.attributes["availability"]

  bladeAdminState = blade.attributes["adminState"]
  bladeSerial = blade.attributes["serial"]
#Location information
  bladeChassis = blade.attributes["chassisId"]
  bladeSlotID = blade.attributes["slotId"]
#Blade Inventory
  bladeModel = blade.attributes["model"]
  bladeTotMemory = blade.attributes["totalMemory"]
  bladeMemory = blade.attributes["availableMemory"]   
  bladeAdaptors = blade.attributes["numOfAdaptors"] 
  bladeCpus = blade.attributes["numOfCpus"]
  bladeCores = blade.attributes["numOfCores"]
  bladeCoreEnabled = blade.attributes["numOfCoresEnabled"]
  bladeNics = blade.attributes["numOfEthHostIfs"]
  bladeFcs = blade.attributes["numOfFcHostIfs"]
  bladeThreads = blade.attributes["numOfThreads"]

#Start to output
  puts "TABLE "+ bladeSerial

  puts "BladeName.StringObservation.obs= " + bladeName
  puts "BladeModel.StringObservation.obs= " + bladeModel
  puts "BladeChassis.StringObservation.obs= " + bladeChassis
  puts "BladeSlotID.StringObservation.obs= " + bladeSlotID
  puts "BladeAdminState.StringObservation.obs= " + bladeAdminState
  puts "BladeAvailability.StringObservation.obs= " + bladeAvailability
  puts "BladeConnStatus.StringObservation.obs= " + bladeConnStatus
  puts "BladeConnPath.StringObservation.obs= " + bladeConnPath
  puts "BladeCheck.StringObservation.obs= " + bladeCheck
  puts "BladeState.StringObservation.obs= " + bladeState
  puts "BladeAssociated.StringObservation.obs= " + bladeAssociated
  puts "BladeDiscovery.StringObservation.obs= " + bladeDiscovery
  puts "BladePower.StringObservation.obs= " + bladePower
  puts "BladeTotMemory:megabyte= " + bladeTotMemory
  puts "BladeMemory:megabyte= " + bladeMemory
  puts "BladeAdaptors:count= " + bladeAdaptors
  puts "BladeCPUs:count= " + bladeCpus
  puts "BladeCores:count= " + bladeCores
  puts "BladeCoresEnabled:count= " + bladeCoreEnabled
  puts "BladeNics:count= " + bladeNics
  puts "BladeFcs:count= " + bladeFcs
  puts "BladeThreads:count= " + bladeThreads

  puts "END_TABLE"

#end the output
Here is the code for the initiate.xml
Put everything in a folder and just run it from command line. Make sure and change the UCS address and username and password
ruby -W0 ucsmblades.rb


Popular posts from this blog

Console over SSH (ONTAP)

Brocade Zone Setup

OpenSolaris ZFS Setup