private static PerfQuerySpec createPerfQuerySpec( ManagedEntity me, PerfMetricId[] metricIds, int maxSample, int interval) { PerfQuerySpec qSpec = new PerfQuerySpec(); qSpec.setEntity(me.getMOR()); // set the maximum of metrics to be return // only appropriate in real-time performance collecting qSpec.setMaxSample(new Integer(maxSample)); // qSpec.setMetricId(metricIds); // optionally you can set format as "normal" qSpec.setFormat("normal"); // set the interval to the refresh rate for the entity qSpec.setIntervalId(new Integer(interval)); return qSpec; }
/** * This method queries performance values for a given managed entity. * * @param managedEntity the managed entity to query * @return the perfomance values * @throws RemoteException */ public VmwarePerformanceValues queryPerformanceValues(ManagedEntity managedEntity) throws RemoteException { VmwarePerformanceValues vmwarePerformanceValues = new VmwarePerformanceValues(); int refreshRate = getPerformanceManager().queryPerfProviderSummary(managedEntity).getRefreshRate(); PerfQuerySpec perfQuerySpec = new PerfQuerySpec(); perfQuerySpec.setEntity(managedEntity.getMOR()); perfQuerySpec.setMaxSample(Integer.valueOf(1)); perfQuerySpec.setIntervalId(refreshRate); PerfEntityMetricBase[] perfEntityMetricBases = getPerformanceManager().queryPerf(new PerfQuerySpec[] {perfQuerySpec}); if (perfEntityMetricBases != null) { for (int i = 0; i < perfEntityMetricBases.length; i++) { PerfMetricSeries[] perfMetricSeries = ((PerfEntityMetric) perfEntityMetricBases[i]).getValue(); for (int j = 0; perfMetricSeries != null && j < perfMetricSeries.length; j++) { if (perfMetricSeries[j] instanceof PerfMetricIntSeries) { long[] longs = ((PerfMetricIntSeries) perfMetricSeries[j]).getValue(); if (longs.length == 1) { PerfCounterInfo perfCounterInfo = getPerfCounterInfoMap().get(perfMetricSeries[j].getId().getCounterId()); String instance = perfMetricSeries[j].getId().getInstance(); String name = getHumanReadableName(perfCounterInfo); if (instance != null && !"".equals(instance)) { vmwarePerformanceValues.addValue(name, instance, longs[0]); } else { vmwarePerformanceValues.addValue(name, longs[0]); } } } } } } return vmwarePerformanceValues; }
/** @throws Exception */ private static void displayStats() throws Exception { String[][] statsList = getCounters(); ManagedObjectReference hostmor = getHostByHostName(hostname); if (hostmor == null) { System.out.println("Host Not Found"); return; } Object property; ArrayList props = (ArrayList) getDynamicProperty(perfManager, "perfCounter"); if (props != null && props.size() > 0) { property = props.get(0); } else { property = null; } // ArrayOfPerfCounterInfo arrayCounter = (ArrayOfPerfCounterInfo) property; ArrayList counterInfoList = new ArrayList(); counterInfoList.add(property); List<PerfMetricId> midVector = new ArrayList<PerfMetricId>(); List<String> statNames = new ArrayList<String>(); for (int i = 0; i < statsList.length; i++) { PerfCounterInfo counterInfo = getCounterInfo(counterInfoList, statsList[i][0], statsList[i][1]); if (counterInfo == null) { System.out.println( "Warning: Unable to find stat " + statsList[i][0] + " " + statsList[i][1]); continue; } String counterName = counterInfo.getNameInfo().getLabel(); statNames.add(counterName); PerfMetricId pmid = new PerfMetricId(); pmid.setCounterId(counterInfo.getKey()); pmid.setInstance(""); midVector.add(pmid); } List<PerfMetricId> midList = new ArrayList<PerfMetricId>(midVector); Collections.copy(midList, midVector); PerfQuerySpec spec = new PerfQuerySpec(); spec.setEntity(hostmor); GregorianCalendar startTime = new GregorianCalendar(); startTime.add(Calendar.SECOND, -60); XMLGregorianCalendar starttimexmlgc = DatatypeFactory.newInstance().newXMLGregorianCalendar(startTime); // spec.setStartTime(starttimexmlgc); spec.getMetricId().addAll(midList); spec.setIntervalId(new Integer(20)); querySpec = spec; final List<String> statNames2 = statNames; javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() { createAndShowGUI("VM Name", statNames2); } }); Timer timer = new Timer(true); timer.schedule( new TimerTask() { public void run() { refreshStats(); } }, 1000, 21000); }