RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  •  

    Returning HP Serial Numbers Using SNMP…

    I was recently asked if I knew of a automated way to get the serial numbers for our ESX hosts. Since they are Linux OS, the Win32_BIOS WMI namespace just went out the window. My next thought was to use HP SIM (Systems Insight Manager) alas, no serial numbers were stored for these hosts in the repository. It was starting to look like manually logging on to the System Management homepage for each server was going to be the way. Umm… no. I would rather use the time it would take to log on to each server (over 80 in this case) to automate the process for future use because we all know that someday someone else will ask for the same information.

    Enter SNMP. Since we are working with a Linux platform, I figured SNMP would be the best bet so I simply needed to find a way to connect. Net-SNMP to the rescue. The first order of business was to find the proper node in the SNMP tree where the serial number was stored. To do this, I used the snmpwalk.exe utility from Net-SNMP. The quickest way I found is to simply walk the entire tree; however now that I’ve done that and dumped a LOT of information, I’ll save you some time and target the proper object node with the following syntax:

    snmpwalk.exe -v 2c -c public servername enterprises > C:\snmpoutput.txt

    In this example, -v 2c tells snmpwalk to use SNMP version 2 and -c public stipulates the read community string to use when walking the OID tree. The enterprises OID is the starting point OID to begin walking all subtrees rooted underneath. Just for reference, the OID defined for enterprises by HP is .1.3.6.1.4.1; however, the MIB file has definitions that allow us to simply use the friendly name of enterprise.

    Once you have obtained the output file, search through the results for 2.2.2.5 which is the OID for the string that maintains the serial number of the server. Now that we have the entire OID for the node, we can use the snmpget.exe utility to directly target the OID tree node we want instead of enumerating everything under the enterprise node such as:

    snmpget.exe -v 2c -c public servername .1.3.6.1.4.1.232.2.2.2.5.0

    Another syntax that is a little easier on the eyes would be:

    snmpget.exe -v 2c -c public servername SNMPv2-SMI::enterprises.232.2.2.2.5.0

    which is the same as the previous command only using the defined enterprises name rather than the full OID. Now, this is all good and well for a single ESX host, but what about several? I wanted to use PowerShell to wrap up the automation functionality, but there were no .NET classes available for SNMP and using a custom assembly from CodePlex or CodeProject was a little bit of overkill just for a simple automation task so I decided to wrap it up in good old VBScript as shown below:

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objShell = CreateObject("WScript.Shell")
    Set infile = objFSO.OpenTextFile("C:\Scripts\VBScript\ESXSerials\ESXHosts.txt")
    Set outfile = objFSO.OpenTextFile("C:\Scripts\VBScript\ESXSerials\ESXSerials.csv", 2, True)
     
    Do While Not infile.AtEndOfStream
    	str = infile.ReadLine()
    	WScript.Echo "Querying " & str
    	output = str
    	Set objExecObject = objShell.Exec("cmd /c D:\Net-SNMP\bin\snmpget.exe -v 2c -c public " & str & " SNMPv2-SMI::enterprises.232.2.2.2.5.0")
    	Do While Not objExecObject.StdOut.AtEndOfStream
    	    strText = objExecObject.StdOut.ReadLine()
    	    If Instr(strText, "232.2.2.2.5.0") > 0 Then
    	        output = output & ", " & Right(strText, 12)
    	        i=1
    	        Exit Do
    	    End If
    	Loop
    	outfile.WriteLine output
    Loop

    In the above example, I installed Net-SNMP to D:\Net-SNMP on my workstation and the tools extract into the bin folder by default. I also used a list of all the ESX hosts I was targeting in a text infile. I found the easiest way to get this info was to simply export a list of all hosts from VCenter and then mangle the CSV into a text file with only the server names. The rest should be fairly obvious in the script.

    This was one of those weird requests that makes you do a little extra research to come up with a solution. I learned a bit more about SNMP during the process and hope this helps out some others who need to pull some data from ESX.

    The MIB containing the OID information for this particular script was CPQSINFO.MIB; cpqSystemInfo is the top variable. ByteSphere has a great MIB resource area to use for research.

    Leave a Reply