Using ServerIron ADX XML API with Microsoft Visual Studio 2008
Part 2 – Working with the AdcSysInfo API
This article will cover many of the methods that are available in the AdcSysInfo API. Using that API you can retrieve the ServerIron ADX configuration file, send CLI commands and retrieve system information such as host name, flash memory content, current software version, memory and CPU utilization, chassis and model information, etc. The complete description of the API can be found at
We will use MS Visual Basic to create a Windows application with three tabs: System, Configuration and Monitor (this document will cover the System tab only). We will also add a menu bar with a few additional functions. We assume the reader has average programming skills, including knowledge of MS Visual Basic and Visual Studio. It is not the intent of this document to teach visual basic programming or how to use MS Visual Studio. There are many tutorials in the Internet that can help the reader with that. However, we will explain in detail how to use the objects and functions available in the SysInfo API.
Main Form
Open up the API Demo project that we started in Part 1. In the Solution Explorer, rename Form1 form to Main, then select View Designer, change the form text property to “ServerIron ADX API Demo”, resize it and add the controls to create a layout like Figure 1. Start with one TabControl control and MenuStrip control, then add the other controls.
Figure 1 – Main form layout
The following objects need to be added to the Main form:
Name / Type / Text / Other PropertiestabControl1 / tabcontrol / 3 TabPages, named System, Configuration and Monitor
Dock=Fill
menuStrip1 / menutrip / 4 DropDownItems (see Fig.2)
btnGetHostName / button / Get Host Name
btnGetVersion / button / Get Version
BtnGetFlash / button / Get Flash
BtnGetCpu / button / Get CPUs
btnGetChassis / button / Get Chassis
BtnGetTemp / button / Get Temperature
btnGetMemory / button / Get DRAM
LblAddress / label / IP Address:
TxtIpAddress / textbox
txtHostName / textbox
txtVersion / textbox
txtFlash / textbox / Multiline=True
txtChassis / textbox / Multiline=True
txtTemp / textbox
gridCpu / datagridview / AllowUserToAddRows, AllowUserToDeleteRows, RowHeadersVisible=False,
Scrollbars=None
gridMemory / datagridview / AllowUserToAddRows, AllowUserToDeleteRows, RowHeadersVisible=False,
Scrollbars=None
Table A – Main form controls
Figure 2 – DropDownItems at the MenuStrip control
Three additional forms will be used in this application: Authentication, ConfigFile and FrontPanel. Add the new forms to the API Demo Project (from the Solution Explorer pane, right click on API Demo, select Add, New Item, Windows Form). Please refer to Figures 3, 4 and 5, plus Tables B, C and D to create the forms.
Authentication Form
This form will be used to save the credentials to access the ServerIron ADX.
Figure 3 – Authentication form layout
Name / Type / Text / Other Propertieslabel1 / label / User Name:
label2 / label / Password:
label3 / label / Re-Type Password:
TxtUserName / textbox
TxtPassword / textbox / UseSystemPasswordChar=True
txtRetypePassword / textbox / UseSystemPasswordChar=True
BtnOK / button / OK
BtnCancel / button / Cancel
Table B – Authentication form controls
Configuration File Form
This form will be used to retrieve the ServerIron ADX configuration file. Although it looks like it has no controls, it does have one: a textbox control, in multiline mode, which is docked to fill up the ConfigFile form.
Figure 4 – ConfigFile form layout
Name / Type / Text / Other PropertiestxtConfigFile / Textbox / Multiline=True, Dock=Fill, ScrollBars=Vertical
Table C – ConfigFile form controls
Front Panel Form
The Front Panel will show a picture of the ServerIron ADX, depending on the ServerIron model accessed by the application. The front panel feature in this application is a little crude – there are only three different pictures and the same picture will show for any ServerIron sub-model. The app can be easily changed to reflect every hardware configuration. To simplify the app design even further, this form uses three PictureBox controls, each using a different image (ServerIron ADX 1000, ServerIron ADX 4000 and ServerIron ADX 10000). The pictures are superimposed in this form. The form code-behind will show or hide each picture depending on the ServerIron ADX model accessed by the application.
First, get pictures for the ServerIron ADX 1000, ServerIron ADX 4000 and ServerIron ADX 10000. To add a picture to the PictureBox control (and to the application), use the Properties Pane and go to the Image property of the PictureBox. At the Select Resource window, click on Project Resource File, then click on Import to import the picture into the application and the PictureBox control. Repeat the process for the three PictureBox controls. The figures below show each individual PictureBox. Remember that they are superimposed in the same form.
Figure 5 – FrontPanel form layout
Name / Type / Text / Other PropertiesPicADX1K / picturebox / SizeMode=StretchImage, Visible=False
PicADX4K / picturebox / SizeMode=StretchImage, Visible=False
PicADX10K / picturebox / SizeMode=StretchImage, Visible=False
Table D – FrontPanel form controls
Retrieving and saving the ServerIron ADX IP address
Now let´s work on the code. The following code needs to be added to the Main.vb file.
regKey is an object used to access the Windows Registry in the machine where the application is running. We will use the registry to save application parameters such as the ServerIron ADX ip address and the user credentials.
The SetCurrentAddressAsDefaultIPAddres subroutine will save the currrent ip address in the registry. Before that, we will use the IsValidIPAddress function to verify if the ip address typed in the textbox is valid.
The SetAuthenticationCredential subroutine will save the user credentials in the registry.
Imports System.Text
PublicClass Main
'define object types
Dim regKey As Microsoft.Win32.RegistryKey 'registry key to save application parameters
Dim username, password AsString'credentials to access the ServerIron
PrivateSub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\AdxDemo", True)
'check if registry contains subkey "AdxDemo"
If regKey IsNothingThen'create subkey and key to store default ip address and user credentials
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE", True)
regKey.CreateSubKey("AdxDemo")
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\AdxDemo", True)
regKey.SetValue("defaultIPAddress", "0.0.0.0")
regKey.SetValue("username", "")
regKey.SetValue("password", "")
EndIf
txtIpAddress.Text = regKey.GetValue("defaultIPAddress") 'read the current ip address
EndSub
PrivateSub SetCurrentAddressAsDefaultIPAddressToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetCurrentAddressAsDefaultIPAddressToolStripMenuItem.Click
'saves default ip address in the registry
If IsValidIPAddress(txtIpAddress.Text) = TrueThen
regKey.SetValue("defaultIPAddress", txtIpAddress.Text)
Else
MessageBox.Show("Invalid IP Address", "Warning!")
EndIf
EndSub
PrivateSub SetAuthenticationCredentialsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetAuthenticationCredentialsToolStripMenuItem.Click
Authentication.Show()
EndSub
PublicFunction IsValidIPAddress(ByVal addr AsString) AsBoolean
'create the match pattern
Dim pattern AsString = "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\." & _
"([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"
'create the Regular Expression object
Dim check AsNew System.Text.RegularExpressions.Regex(pattern)
'boolean variable to hold the status
Dim valid AsBoolean = False
'check to make sure an ip address was provided and it does not ends with ".0"
If addr = ""Or txtIpAddress.Text.EndsWith(".0") Then
valid = False
Else
valid = check.IsMatch(addr, 0)
EndIf
Return valid
EndFunction
EndClass
Saving the user credentials
We need to provide the means to save the user credentials in the Windows Registry. The Authentication form will be used for that. The following code needs to be added to the Authentication.vb file.
PublicClass Authentication
Dim regKey As Microsoft.Win32.RegistryKey
PrivateSub Authentication_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\AdxDemo", True)
txtuserName.Text = regKey.GetValue("username")
txtPassword.Text = regKey.GetValue("password")
txtRetypePassword.Text = regKey.GetValue("password")
EndSub
PrivateSub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
If txtPassword.Text > txtRetypePassword.Text Then
MessageBox.Show("The passwords don't match!", "Warning!")
Else
regKey.SetValue("username", txtuserName.Text)
regKey.SetValue("password", txtPassword.Text)
Me.Close()
EndIf
EndSub
PrivateSub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
EndSub
EndClass
Using the Get Version method
ADXSYS is the object that will handle all calls to AdcSysInfo API.
The code shown below contains enhancements to the “Get Version” subroutine that was used in Part 1. The “Authenticate” subroutine was also changed to retrieve the ip address and user credentials from the registry.
Add the following code to the Main.vb file:
The ADXSYS object definition must be added close to the other object types defined at the start of the Main Class:
'define object types
Dim ADXSYS AsNew Adcsysinfo.AdcSysInfo 'Adcsysinfo API object
The following sections are revised subroutines that should replace the ones created at Part 1:
PrivateSub btnGetVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetVersion.Click
If IsValidIPAddress(txtIpAddress.Text) = TrueThen
Call Authenticate()
txtVersion.Text = ADXSYS.getVersion()
Else
MessageBox.Show("Invalid IP Address", "Warning!")
EndIf
EndSub
PrivateSub Authenticate()
username = regKey.GetValue("username")
password = regKey.GetValue("password")
ADXSYS.Url = " + txtIpAddress.Text + "/WS/SYS"'form the URL to access the ADX. The AdcSysInfo API resides at /WS/SYS
ADXSYS.SetRequestHeader("Authorization", "Basic " + Convert.ToBase64String(New ASCIIEncoding().GetBytes(username + ":" + password))) 'add the HTTP authentication header
ADXSYS.PreAuthenticate = True
EndSub
Now you can run the application. First thing is to add the authentication credentials. Go to the Tools menu and click on Set Authentication Credentials. Type in the User Name and Password and hit the OK button. Now type in the ServerIron ADX ip address and click on the Get Version button. You can also save the ip address that is typed in the textbox (the same ip address will show when you run the application a second time) using the Set Current IP Address as Default IP Address in the Tools menu.
Retrieving the Configuration File
The menuStrip contains a tool to retrieve the ServerIron ADX configuration file. The method getRunningConfig is used to retrieve the configuration file. The following code needs to be added to the Main.vb file:
PrivateSub GetConfigurationFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetConfigurationFileToolStripMenuItem.Click
If IsValidIPAddress(txtIpAddress.Text) = TrueThen
ConfigFile.Show() 'show configuration file form
Else
MessageBox.Show("Invalid IP Address", "Warning!")
EndIf
EndSub
And the following code needs to be added to the ConfigFile.vb file:
Imports System.Text
PublicClass ConfigFile
Dim ADXSYS AsNew Adcsysinfo.AdcSysInfo 'Adcsysinfo API object
Dim regKey As Microsoft.Win32.RegistryKey
Dim username, password AsString
PrivateSub ConfigFile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\AdxDemo", True)
Try
Call Authenticate()
txtConfigFile.Text = ADXSYS.getRunningConfig.Replace(Chr(10), vbCrLf) 'get config file. Correct linefeed character
txtConfigFile.Focus()
txtConfigFile.SelectionStart = 0 'take cursor to start of text
Catch ex As System.Net.WebException
If ex.Message.Contains("404") = TrueThen
MessageBox.Show("The SOAP XML API is not enabled on the ServerIron!", "Warning!")
Else
MessageBox.Show("Cannot connect to the ServerIron!", "Warning!")
EndIf
Catch ex As System.Web.Services.Protocols.SoapException
MessageBox.Show("Cannot authenticate, please check user credentials!", "Warning!")
EndTry
EndSub
PrivateSub Authenticate()
username = regKey.GetValue("username")
password = regKey.GetValue("password")
ADXSYS.Url = " + Main.txtIpAddress.Text + "/WS/SYS"'form the URL to access the ADX. The AdcSysInfo API resides at /WS/SYS
ADXSYS.SetRequestHeader("Authorization", "Basic " + Convert.ToBase64String(New ASCIIEncoding().GetBytes(username + ":" + password))) 'add the HTTP authentication header
ADXSYS.PreAuthenticate = True
EndSub
End Class
Now run the application and test this function by going to the Tools Menus and click on Get Configuration File.
Show the ServerIron ADX Front Panel
The trick is to show a picture that represents the ServerIron ADX model accessed by the application. As mentioned before, this application simply unhides a picture depending on the ServerIron ADX model. A more polished application would determine which modules are installed in the chassis, and would show a picture representing the actual device.
The chassis object is created to gather all chassis-related information. When the getChassis method is invoked, the chassis object is filled up with information about fans, identification, power, temperature and wattage. Inside the identification key you will find more keys: bootprommac, model, serialnumber. We will use the model information to determine which picture will be shown.
The following code needs to be added to the Main.vb file:
PrivateSub GetFrontPanelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetFrontPanelToolStripMenuItem.Click
If IsValidIPAddress(txtIpAddress.Text) = TrueThen
FrontPanel.Show()
Else
MessageBox.Show("Invalid IP Address", "Warning!")
EndIf
EndSub
And the following code needs to be added to the FrontPanel.vb file:
Imports System.Text
PublicClass FrontPanel
Dim ADXSYS AsNew Adcsysinfo.AdcSysInfo
Dim username, password AsString
Dim regKey As Microsoft.Win32.RegistryKey
PrivateSub FrontPanel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim chassis AsNew Adcsysinfo.chassis 'define a chassis object
Call Authenticate()
'get chassis data
Try
chassis = ADXSYS.getChassis
SelectCase chassis.identification.model.ToString()
Case"SI-1216-4-PREM"'other ADX1K models will return different strings
Me.Text = "ServerIron ADX 1000"
Me.Size = New Size(Me.Size.Width, 150)
PicADX1K.Visible = True
PicADX4K.Visible = False
PicADX10K.Visible = False
Case"SI-4000-PREM"'not tested
Me.Text = "ServerIron ADX 4000"
PicADX1K.Visible = False
PicADX4K.Visible = True
PicADX10K.Visible = False
Case"SI-10000-PREM"'not tested
Me.Text = "ServerIron ADX 10000"
PicADX1K.Visible = False
PicADX4K.Visible = False
PicADX10K.Visible = True
EndSelect
Catch ex As System.Net.WebException
If ex.Message.Contains("404") = TrueThen
MessageBox.Show("The SOAP XML API is not enabled on the ServerIron!", "Warning!")
Else
MessageBox.Show("Cannot connect to the ServerIron!", "Warning!")
EndIf
Catch
MessageBox.Show("Cannot authenticate, please check user credentials!", "Warning!")
EndTry
EndSub
PrivateSub Authenticate()
regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\AdxDemo", True)
username = regKey.GetValue("username")
password = regKey.GetValue("password")
ADXSYS.Url = " + Main.txtIpAddress.Text + "/WS/SYS"'form the URL to access the ADX. The AdcSysInfo API resides at /WS/SYS
ADXSYS.SetRequestHeader("Authorization", "Basic " + Convert.ToBase64String(New ASCIIEncoding().GetBytes(username + ":" + password))) 'add the HTTP authentication header
ADXSYS.PreAuthenticate = True
EndSub
EndClass
Run the application and test this function by going to the Tools Menus and click on Get Front Panel.
Get Host Name
Use the getHostName method to retrieve the hostname configured in the ServerIron ADX.
The following code needs to be added to the Main.vb file:
PrivateSub btnGetHostName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetHostName.Click
If IsValidIPAddress(txtIpAddress.Text) = TrueThen
Call Authenticate()
'get the hostname
Try
txtHostName.Text = ADXSYS.getHostName
Catch ex As System.Net.WebException
If ex.Message.Contains("404") = TrueThen
MessageBox.Show("The SOAP XML API is not enabled on the ServerIron!", "Warning!")
Else
MessageBox.Show("Cannot connect to the ServerIron!", "Warning!")
EndIf
Catch ex As System.Web.Services.Protocols.SoapException
MessageBox.Show("Cannot authenticate, please check user credentials!", "Warning!")
EndTry
Else
MessageBox.Show("Invalid IP Address", "Error")
EndIf
EndSub
Get Flash
A flash object needs to be defined to store information about the primary and secondary flash, which includes size, version name and used bytes. The getFlash method retrieves all information from the ServerIron ADX. The relevant fields are concatenated and assigned to the txtFlash textbox.
The following code needs to be added to the Main.vb file:
PrivateSub btnGetFlash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetFlash.Click
If IsValidIPAddress(txtIpAddress.Text) = TrueThen
Dim flash AsNew Adcsysinfo.flash 'create flash object
Call Authenticate()
'get all flash data with a single fetch
Try
flash = ADXSYS.getFlash
txtFlash.Text = "Primary flash:" + vbCrLf + "version: " + flash.primaryImage.version + vbCrLf + "label: " + flash.primaryImage.label + vbCrLf + vbCrLf + "Secondary flash:" + vbCrLf + "version: " + flash.secondaryImage.version + vbCrLf + "label: " + flash.secondaryImage.label 'create string and print
Catch ex As System.Net.WebException
If ex.Message.Contains("404") = TrueThen
MessageBox.Show("The SOAP XML API is not enabled on the ServerIron!", "Warning!")
Else
MessageBox.Show("Cannot connect to the ServerIron!", "Warning!")
EndIf
Catch ex As System.Web.Services.Protocols.SoapException
MessageBox.Show("Cannot authenticate, please check user credentials!", "Warning!")
EndTry
Else
MessageBox.Show("Invalid IP Address", "Error")
EndIf
EndSub
Get CPUs
The Get CPUs button will run code to populate a datagridview control.
cpu is a generic object that will hold raw data for all BPs and the MP. By invoking the getCPU method just once, all cpu data will be loaded into the cpu object. The resulting cpu object structure looks like a matrix, with each index containing data about one cpu.