N1 SPS TIP : Using Public API to get information from N1SPS

Public API functionality available with N1 SPS can be very powerful to obtain certain information from the system. It's very simple to use and if you know JAVA, have a CLI installation of SPS and have JDK 5 to compile the code, then you're ready to have a go at it.

Below, I have documented a couple of uses of public API, which can be improvised based on your need. Instead of dumping th output to the standard output, you may dump it into a HTML file or an XML file which can later be transformed in any which way you want through an XSL template.


NOTE:

To compile and run these files, ensure that you have the following class path :
bash-3.00# echo $CLASSPATH
.:/opt/SUNWn1sps/N1_Service_Provisioning_System_6.0/cli/lib/sps-api.jar

Example 1: Get the number of hosts in a given host set, and list them.

The programming attempt below is a quick hack I put together, but it will give you a general idea...

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.n1.sps.client.CommandManager;
import com.sun.n1.sps.client.CommandManagerBuilder;
import com.sun.n1.sps.client.ConfigurationException;
import com.sun.n1.sps.model.host.Host;
import com.sun.n1.sps.model.host.HostID;
import com.sun.n1.sps.model.host.HostIDSet;
import com.sun.n1.sps.model.host.HostSet;
import com.sun.n1.sps.model.host.HostType;
import com.sun.n1.sps.model.util.ClientException;

public class HostsInHostSet {
public static void main(String args[]) {
CommandManagerBuilder builder = new CommandManagerBuilder();
try {
String path = args[0];
String uname = args[1];
String pass = args[2];
String hostSetName = args[3];
File cliDirectory = new File(path);
builder.setCLIInstallationDir(cliDirectory);
CommandManager commandManager = builder.build();
Map params = new HashMap();
params.put("u", uname);
params.put("p", pass);
params.put("ID", hostSetName);
HostSet hostset = (HostSet) commandManager.execute("hdb.hs.lo", params);
ArrayList <Host> results = new ArrayList<Host>();

HostIDSet hsIDSet = hostset.getMemberHostIDSet();
System.out.println(hsIDSet.size() + " host(s) found in " + hostSetName);
System.out.println(" \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* MEMBER HOSTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");

HostID[] hostIDs = hsIDSet.toIDArray();
params.clear();
System.out.println("| Host ID | Host Name | Description | Type |");
System.out.println("==============================");
for (HostID hID : hostIDs)
{
params.put("u", uname);
params.put("p", pass);
params.put("ID", hID.toString());
Host host = (Host) commandManager.execute("hdb.h.lo", params);
System.out.print(hID.toString() + " | ");
System.out.print(host.getName() + " | ");
System.out.print(host.getDescription() + " | ");
System.out.print(host.getHostType().getName() + "");
System.out.println();
}
System.out.println("================================");

} catch (ClientException ce) {
System.out.println("Exception message is: " + ce.getMessage() + ce.getCause().getMessage());
}
System.exit(-1);
} }

OUTPUT

bash-3.00# javac HostsInHostSet.java
bash-3.00# java HostsInHostSet "/opt/SUNWn1sps/N1_Service_Provisioning_System_6.0/cli" "admin" "admin" "NM:SIMPLE"

There were 3 host(s) found in NM:SIMPLE

 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* MEMBER HOSTS \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
| Host ID   | Host Name | Description | Type |
================================
010010001024-0000000000000-00001-0000000005 | masterserver | the master server host | system#crhost
010012236120-1237562688568-00200-1796609328 | Host1|  | TestHT
010012236120-1237562710331-00201-0028069883 | Host2 |  |
TestHT
================================

Example 2: Get number of hosts that are of a given Host Type

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.sun.n1.sps.client.CommandManager;
import com.sun.n1.sps.client.CommandManagerBuilder;
import com.sun.n1.sps.client.ConfigurationException;
import com.sun.n1.sps.model.host.Host;
import com.sun.n1.sps.model.host.HostType;
import com.sun.n1.sps.model.util.ClientException;

public class HostsByType {
public static void main(String args[]) {
CommandManagerBuilder builder = new CommandManagerBuilder();
try {
String path = args[0];
String uname = args[1];
String pass = args[2];
String hostType = args[3];
File cliDirectory = new File(path);
builder.setCLIInstallationDir(cliDirectory);
CommandManager commandManager = builder.build();
Map params = new HashMap();
params.put("u", uname);
params.put("p", pass);
Host[] hosts = (Host[]) commandManager.execute("hdb.h.la", params);
ArrayList <Host> results = new ArrayList<Host>();
for (Host host : hosts)
{
if (host.getHostType().getName().equals(hostType.trim()))
{
results.add(host);
}
}
System.out.println(results.size() + " host(s) of type "+hostType+ " found");
} catch (ClientException ce) {
System.out.println("Exception message is: " + ce.getMessage());
}
System.exit(-1);
}
}

OUTPUT

bash-3.00# javac HostsByType.java bash-3.00# java HostsByType "/opt/SUNWn1sps/N1_Service_Provisioning_System_6.0/cli" "admin" "admin" "TestHT" There were 2 host(s) of type TestHT found

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

sunnydays

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today