private static int findStatsIndex(List<PerfMetricId> midList, PerfMetricId mid) { int count = 0; for (PerfMetricId pmid : midList) { if ((pmid.getCounterId() == mid.getCounterId()) && pmid.getInstance().equals(mid.getInstance())) { return count; } ++count; } return -1; }
/** @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(); } }