private static void refreshStats() { try { PerfCompositeMetric metric = vimPort.queryPerfComposite(perfManager, querySpec); XMLGregorianCalendar latestTs = displayStats(querySpec.getMetricId(), metric); if (latestTs != null) { querySpec.setStartTime(latestTs); } } catch (SOAPFaultException sfe) { printSoapFaultException(sfe); } catch (Exception ex) { ex.printStackTrace(); } }
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); }
@Override public void run() { try { while (true) { URL url = new URL(SJSULabConfig.getvCenterURL()); try { ServiceInstance si = new ServiceInstance( url, SJSULabConfig.getvCenterUsername(), SJSULabConfig.getPassword(), true); HostSystem host = (HostSystem) new InventoryNavigator(si.getRootFolder()) .searchManagedEntity("HostSystem", ip); // System.out.println("host value" + host); PerformanceManager perfMgr = si.getPerformanceManager(); PerfProviderSummary summary = perfMgr.queryPerfProviderSummary(host); // vm int perfInterval = summary.getRefreshRate(); PerfMetricId[] queryAvailablePerfMetric = perfMgr.queryAvailablePerfMetric(host, null, null, perfInterval); PerfCounterInfo[] pci = perfMgr.getPerfCounter(); ArrayList<PerfMetricId> list = new ArrayList<PerfMetricId>(); for (int i2 = 0; i2 < queryAvailablePerfMetric.length; i2++) { PerfMetricId perfMetricId = queryAvailablePerfMetric[i2]; if (SELECTED_COUNTER_ID == perfMetricId.getCounterId()) { list.add(perfMetricId); } } PerfMetricId[] pmis = list.toArray(new PerfMetricId[list.size()]); PerfQuerySpec qSpec = new PerfQuerySpec(); qSpec.setEntity(host.getMOR()); qSpec.setMetricId(pmis); qSpec.intervalId = perfInterval; PerfEntityMetricBase[] pembs = perfMgr.queryPerf(new PerfQuerySpec[] {qSpec}); for (int i = 0; pembs != null && i < pembs.length; i++) { PerfEntityMetricBase val = pembs[i]; PerfEntityMetric pem = (PerfEntityMetric) val; PerfMetricSeries[] vals = pem.getValue(); PerfSampleInfo[] infos = pem.getSampleInfo(); Date date1 = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy h:mm:ss"); String formattedDate = sdf.format(date1); infoList.put(aName[counter], formattedDate); for (int j = 0; vals != null && j < vals.length; ++j) { PerfMetricIntSeries val1 = (PerfMetricIntSeries) vals[j]; long[] longs = val1.getValue(); System.out.println("Host CPU :" + longs[5]); infoList.put(aName[0], String.valueOf(longs[5])); /* for (int k : a) { infoList.put(aName[counter+1], String.valueOf(longs[k])); counter++; } counter = 0; */ } } si.getServerConnection().logout(); } catch (InvalidProperty e) { e.printStackTrace(); } catch (RuntimeFault e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } // infoList.put("vmIP", ip); counter = 0; try { StringBuilder sb = new StringBuilder(); sb.append("vmname:" + ip + ","); int countTest = 0; for (String str : aName) { sb.append(aName[countTest] + ":" + infoList.get(str).toString() + ","); countTest++; } String out = sb.toString(); out = out.substring(0, out.length() - 1); writer.write(out + "\n"); System.out.println(out); } catch (Exception e) { e.printStackTrace(); } Thread.sleep(5000); } } catch (Exception e) { e.printStackTrace(); } }