Example: Performance Monitoring Infrastructure client (Version 4.0)

The following is a list of example Performance Monitoring Infrastructure (PMI) client code from (V4.0)

/**
 * This is a sample code to show how to use PmiClient to collect PMI data.
 * You will need to use adminconsole to set instrumentation level (a level other
 * than NONE) first.
 * 
 * <p>
 * End-to-end code path in 4.0:
 *     PmiTester -> PmiClient -> AdminServer -> appServer
 */


package com.ibm.websphere.pmi;

import com.ibm.websphere.pmi.*;
import com.ibm.websphere.pmi.server.*;
import com.ibm.websphere.pmi.client.*;
import com.ibm.ws.pmi.server.*;
import com.ibm.ws.pmi.perfServer.*;
import com.ibm.ws.pmi.server.modules.*;
import com.ibm.ws.pmi.wire.*; 
import java.util.ArrayList;


/** 
 * Sample code to use PmiClient API (old API in 4.0) and get CpdData/CpdCollection objects.
 * 
 */
public class PmiTester implements PmiConstants {

    /** a test driver:
     * @param args[0] - node name
     * @param args[1] - port number, optional, default is 2809
     * @param args[2] - connector type, default is RMI
     * @param args[3] - verion (AE, AEs, WAS50), default is WAS50     
     * 
     */
    public static void main(String[] args) {
        String hostName = null;
        String portNumber = "2809";
        String connectorType = "RMI";
        String version = "WAS50";

        if (args.length < 1) {
            System.out.println("Usage: <host> [<port>] [<connectorType>] 
[<version>]");
            return;
        }

        if(args.length >= 1)
            hostName = args[0];
        if(args.length >= 2)
            portNumber = args[1];
        if (args.length >=3)
            connectorType = args[2];
        if (args.length >=4)
            version = args[3];

        try {
            PmiClient pmiClnt = new PmiClient(hostName, portNumber, 
version, false, connectorType);
            
            // uncomment it if you want debug info
            //pmiClnt.setDebug(true);

            // get all the node PerfDescriptor in the domain
            PerfDescriptor[] nodePds = pmiClnt.listNodes();

            if(nodePds == null) {
                System.out.println("no nodes");
                return;
            }

            // get the first node 
            String nodeName = nodePds[0].getName();
            System.out.println("after listNodes: " + nodeName);

            //list all the servers on the node
            PerfDescriptor[] serverPds = pmiClnt.listServers(nodePds[0].getName());
            if(serverPds == null || serverPds.length == 0) {
                System.out.println("NO app server in node");
                return;
            }

            // print out all the servers on that node
            for(int j=0; j<serverPds.length; j++) {
                System.out.println("server " + j + ": " + serverPds[j].getName());
            }

            for(int j=0; j<serverPds.length; j++) {
                System.out.println("server " + j + ": " + serverPds[j].getName());                                 

                // Option: one can call createPerfLevelSpec and then 
setInstrumentationLevel to set the level
                // for each server if you want. For example, to set all 
the modules to be LEVEL_HIGH for the server j,
                // uncomment the following. 
                // PerfLevelSpec[] plds = new PerfLevelSpec[1];
                // plds[0] = pmiClnt.createPerfLevelSpec(null, LEVEL_HIGH);
                // pmiClnt.setInstrumentationLevel(serverPds[j].getNodeName(), 
serverPds[j].getServerName(), plds, true);

                // First, list the PerfDescriptor in the server
                PerfDescriptor[] myPds = pmiClnt.listMembers(serverPds[j]);         

                // check returned PerfDescriptor
                if(myPds == null) {
                    System.out.println("null from listMembers");
                    continue;
                }

                // one can  add the pds in which you are interested to PerfDescriptorList
                PerfDescriptorList pdList = new PerfDescriptorList();
                for(int i=0; i<myPds.length; i++) {
                    // Option 1: one can recursively call listMembers for each myPds
                    //               and find the one you are interested. We can call 
listMembers
                    //               until individual data level and after that level 
you will null from listMembers.
                    // e.g., PerfDescriptor[] nextPds = pmiClnt.listMembers(myPds[i]);

                    // Option 2: one can filter these pds  before adding to pdList
                    System.out.println("add to pdList: " + myPds[i].getModuleName());   
                    pdList.addDescriptor(myPds[i]);
                    if( i % 2 == 0)
                        pmiClnt.add(myPds[i]);
                }

                // call gets method to get the CpdCollection[] corresponding to pdList
                CpdCollection[] cpdCols = pmiClnt.gets(pdList, true);

                if(cpdCols == null) {
                    // check error
                    if(pmiClnt.getErrorCode() >0)
                        System.out.println(pmiClnt.getErrorMessage());
                    continue;
                }

                for(int i=0; i<cpdCols.length; i++) {
                    // simple print them
                    //System.out.println(cpdCols[i].toString());

                    // Or call processCpdCollection to get each data
                    processCpdCollection(cpdCols[i], "");
                }

                // Or call gets() method to add the CpdCollection[] for whatever 
there by calling pmiClnt.add().
                System.out.println("\n\n\n ---- get data using gets(true) ----- ");
                cpdCols = pmiClnt.gets(true);

                if(cpdCols == null) {
                    // check error
                    if(pmiClnt.getErrorCode() >0)
                        System.out.println(pmiClnt.getErrorMessage());
                    continue;
                }

                for(int i=0; i<cpdCols.length; i++) {
                    // simple print out the whole collection
                    System.out.println(cpdCols[i].toString());

                    // Option: refer processCpdCollection to get each data        
                }           
            }

        }
        catch(Exception ex) {
            System.out.println("Exception calling CollectorAE");
            ex.printStackTrace();
        }
    }

    /** 
     * show the methods to retrieve individual data
     */
    private static void processCpdCollection(CpdCollection cpdCol, String indent) {
        CpdData[] dataList = cpdCol.dataMembers();
        String myindent = indent;

        System.out.println("\n" + myindent + "--- CpdCollection " 
+ cpdCol.getDescriptor().getName() + " ---");
        myindent += "   ";
        for(int i=0; i<dataList.length; i++) {
            if (dataList[i] == null)
                continue;

            // if you want to get static info like name, description, etc
            PmiDataInfo dataInfo = dataList[i].getPmiDataInfo();
            // call getName(), getDescription() on dataInfo;

            CpdValue cpdVal = dataList[i].getValue();
            if(cpdVal.getType() == TYPE_STAT) {
                CpdStat cpdStat = (CpdStat)cpdVal;
                double mean = cpdStat.mean(); 
                double sumSquares = cpdStat.sumSquares();
                int count = cpdStat.count();
                double total = cpdStat.total();
                System.out.println(myindent + "CpdData id=" + dataList[i].getId() 
                                   + " type=stat mean=" + mean);
                // one can print more values like sumSquares, count,etc here
            }
            else if(cpdVal.getType() == TYPE_LOAD) {
                CpdLoad cpdLoad = (CpdLoad)cpdVal;
                long time = cpdLoad.getTime();
                double mean = cpdLoad.mean();
                double currentLevel = cpdLoad.getCurrentLevel();
                double integral = cpdLoad.getIntegral();
                double timeWeight = cpdLoad.getWeight();
                System.out.println(myindent + "CpdData id=" + dataList[i].getId() 
                                   + " type=load mean=" + mean + " currentLevel=" 
+ currentLevel);
                // one can print more values like sumSquares, count,etc here
            }
            else if(cpdVal.getType() == TYPE_LONG) {
                CpdValue cpdLong = (CpdValue)cpdVal;
                long value = (long)cpdLong.getValue();
                System.out.println(myindent + "CpdData id=" + dataList[i].getId() 
                                   + " type=long value=" + value);
            }
            else if(cpdVal.getType() == TYPE_DOUBLE) {
                CpdValue cpdDouble = (CpdValue)cpdVal;
                double value = cpdDouble.getValue();
                System.out.println(myindent + "CpdData id=" + dataList[i].getId() 
                                   + " type=double value=" + value);
            }
            else if(cpdVal.getType() == TYPE_INT) {
                CpdValue cpdInt = (CpdValue)cpdVal;
                int value = (int)cpdInt.getValue();
                System.out.println(myindent + "CpdData id=" + dataList[i].getId() 
                                   + " type=int value=" + value);
            }
        }

        // recursively go through the subcollection
        CpdCollection[] subCols = cpdCol.subcollections();
        for(int i=0; i<subCols.length; i++) {
            processCpdCollection(subCols[i], myindent);
        }
    }

    /**
     * show the methods to navigate CpdCollection
     */
    private static void report(CpdCollection col) {
        System.out.println("\n\n");
        if(col==null) {
            System.out.println("report: null CpdCollection");
            return;
        }
        System.out.println("report - CpdCollection ");
        printPD(col.getDescriptor());
        CpdData[] dataMembers = col.dataMembers();
        if(dataMembers != null) {
            System.out.println("report CpdCollection: dataMembers is " 
+ dataMembers.length);
            for(int i=0; i<dataMembers.length; i++) {
                CpdData data = dataMembers[i];
                printPD(data.getDescriptor());
            }
        }
        CpdCollection[] subCollections = col.subcollections();
        if(subCollections != null) {
            for(int i=0; i<subCollections.length; i++) {
                report(subCollections[i]);
            }
        }
    }

    private static void printPD(PerfDescriptor pd) {
        System.out.println(pd.getFullName());
    }

}


 

Related Tasks


Using PMI client to develop your monitoring application (deprecated)