}
// Note: RP doesn't implement relative stats (cpu::usage or mem::usage), or mem::active, or net::anything
List<String> ids = new ArrayList<String>();
ids.add(refId);
Map<String, PerformanceSample> result = vcp.queryLatestPerformance(ids, PerformanceType.cpuAbs);
PerformanceSample sample= result.get(refId);
perf = (sample == null ? "unavailable" : sample.toString());
System.out.println(" latest cpu sample: " + perf);
} catch (Exception e) {
System.out.println(" perf query failed: " + e.getMessage());
}
}
}
// iterate datacenters to iterate virtual machines
for (Datacenter dc : getDatacenters()) {
System.out.println("DC " + dc);
for (VirtualMachine vm: getVirtualMachines(dc)) {
// Skip templates, which can't be powered on and have no associated performance data
if (vm.getConfig().isTemplate()) {
continue;
}
System.out.println("VM " + vm.getName() + ":");
String refId = MoUtil.morefToString(vm._getRef());
try {
String perf;
Integer[] intervals = vcp.getAllSamplingIntervals(refId);
int resolution = intervals[0]; // fastest sampling supported
//System.out.println(" supports sampling intervals: " + listToString(intervals));
// NB: if you pass times (relative to present, minutes ago) 0,0 (all recent samples): get lots, some are very recent
// if you pass 60,0 (all samples in last hour): tend to get 11 samples, missing recent one, newest sample can be 9 minutes old
// if you pass 30,0 (all samples in last half hour): tend to get 5 samples, missing recent one, newest sample can be 9 minutes old; optimization not kicking in
// if you pass 25,0 (all samples in last 25 min): get 5 samples, including most recent; the "optimization" apparently kicks in
// best realtime query might be for 5,0? But that involves knowing the interval; our API should have a way to get just newest sample without knowing interval.
// Anyway, note that 0,0, <30,0 and >30,0 are really different queries.
Calendar start = vcp.queryVcCurrentTime();
start.add(Calendar.MINUTE, -20);
Calendar end = null;
perf = samplesToString(vcp.queryPerformance(refId, PerformanceType.cpuAbs, resolution, 0, start, end));
System.out.println(" cpu performance samples: " + perf);
perf = samplesToString(vcp.queryPerformance(refId, PerformanceType.cpuRel, resolution, 0, start, end));
System.out.println(" cpu performance samples: " + perf);
perf = samplesToString(vcp.queryPerformance(refId, PerformanceType.memOccupiedAbs, resolution, 0, start, end));
System.out.println(" mem-occupy performance samples: " + perf);
perf = samplesToString(vcp.queryPerformance(refId, PerformanceType.memActiveAbs, resolution, 0, start, end));
System.out.println(" mem-active performance samples: " + perf);
perf = samplesToString(vcp.queryPerformance(refId, PerformanceType.memActiveRel, resolution, 0, start, end));
System.out.println(" mem-active performance samples: " + perf);
perf = samplesToString(vcp.queryPerformance(refId, PerformanceType.netAbs, resolution, 0, start, end));
System.out.println(" net performance samples: " + perf);
List<String> ids = new ArrayList<String>();
ids.add(refId);
Map<String, PerformanceSample> result = vcp.queryLatestPerformance(ids, PerformanceType.cpuAbs);
PerformanceSample sample= result.get(refId);
perf = (sample == null ? "unavailable" : sample.toString());
System.out.println(" latest cpu sample: " + perf);
} catch (Exception e) {
System.out.println(" perf query failed: " + e.getMessage());
}
}