use of com.vmware.vim.binding.vim.PerformanceManager.ProviderSummary in project flowgate by vmware.
the class VCDataServiceTest method testFeedHostUsageData.
@Test
public void testFeedHostUsageData() {
Asset hostMappingAsset = Mockito.mock(Asset.class);
HostSystem host = Mockito.mock(HostSystem.class);
when(hostMappingAsset.getId()).thenReturn("hostid");
PerformanceManager performanceManager = Mockito.mock(PerformanceManager.class);
when(vsphereClient.getPerformanceManager()).thenReturn(performanceManager);
CounterInfo[] counterInfos = new CounterInfo[4];
counterInfos[0] = Mockito.mock(CounterInfo.class);
when(counterInfos[0].getKey()).thenReturn(1);
ElementDescription edGroup = Mockito.mock(ElementDescription.class);
when(counterInfos[0].getGroupInfo()).thenReturn(edGroup);
when(edGroup.getKey()).thenReturn(VCConstants.HOST_CPU_GROUP);
ElementDescription edName = Mockito.mock(ElementDescription.class);
when(counterInfos[0].getNameInfo()).thenReturn(edName);
when(edName.getKey()).thenReturn(VCConstants.HOST_METRIC_USAGE);
counterInfos[1] = Mockito.mock(CounterInfo.class);
when(counterInfos[1].getKey()).thenReturn(2);
ElementDescription edGroup1 = Mockito.mock(ElementDescription.class);
when(counterInfos[1].getGroupInfo()).thenReturn(edGroup1);
when(edGroup1.getKey()).thenReturn(VCConstants.HOST_MEMORY_GROUP);
ElementDescription edName1 = Mockito.mock(ElementDescription.class);
when(counterInfos[1].getNameInfo()).thenReturn(edName1);
when(edName1.getKey()).thenReturn(VCConstants.HOST_METRIC_USAGE);
counterInfos[2] = Mockito.mock(CounterInfo.class);
when(counterInfos[2].getKey()).thenReturn(3);
ElementDescription edGroup2 = Mockito.mock(ElementDescription.class);
when(counterInfos[2].getGroupInfo()).thenReturn(edGroup2);
when(edGroup2.getKey()).thenReturn(VCConstants.HOST_POWER_GROUP);
ElementDescription edName2 = Mockito.mock(ElementDescription.class);
when(counterInfos[2].getNameInfo()).thenReturn(edName2);
when(edName2.getKey()).thenReturn(VCConstants.HOST_METRIC_POWER_ENERGY);
counterInfos[3] = Mockito.mock(CounterInfo.class);
when(counterInfos[3].getKey()).thenReturn(4);
ElementDescription edGroup3 = Mockito.mock(ElementDescription.class);
when(counterInfos[3].getGroupInfo()).thenReturn(edGroup3);
when(edGroup3.getKey()).thenReturn(VCConstants.HOST_POWER_GROUP);
ElementDescription edName3 = Mockito.mock(ElementDescription.class);
when(counterInfos[3].getNameInfo()).thenReturn(edName3);
when(edName3.getKey()).thenReturn(VCConstants.HOST_METRIC_POWER_POWER);
when(performanceManager.getPerfCounter()).thenReturn(counterInfos);
MetricId[] metrics = new MetricId[4];
metrics[0] = Mockito.mock(MetricId.class);
metrics[1] = Mockito.mock(MetricId.class);
metrics[2] = Mockito.mock(MetricId.class);
metrics[3] = Mockito.mock(MetricId.class);
when(metrics[0].getCounterId()).thenReturn(1);
when(metrics[1].getCounterId()).thenReturn(2);
when(metrics[2].getCounterId()).thenReturn(3);
when(metrics[3].getCounterId()).thenReturn(4);
when(metrics[0].getInstance()).thenReturn(null);
when(metrics[1].getInstance()).thenReturn(null);
when(metrics[2].getInstance()).thenReturn(null);
when(metrics[3].getInstance()).thenReturn(null);
when(performanceManager.queryAvailableMetric(host._getRef(), null, null, new Integer(20))).thenReturn(metrics);
ProviderSummary summary = Mockito.mock(ProviderSummary.class);
when(summary.getRefreshRate()).thenReturn(1);
when(performanceManager.queryProviderSummary(host._getRef())).thenReturn(summary);
EntityMetric[] metricBase = new EntityMetric[4];
metricBase[0] = Mockito.mock(EntityMetric.class);
long[] values = { 1L, 2L, 3L, 4L };
IntSeries[] metricSerieses1 = new IntSeries[4];
metricSerieses1[0] = Mockito.mock(IntSeries.class);
MetricId metricID = Mockito.mock(MetricId.class);
when(metricSerieses1[0].getId()).thenReturn(metricID);
when(metricID.getCounterId()).thenReturn(1);
IntSeries intSeries1 = Mockito.mock(IntSeries.class);
intSeries1 = metricSerieses1[0];
when((intSeries1.getValue())).thenReturn(values);
when(metricID.getCounterId()).thenReturn(1);
metricSerieses1[1] = Mockito.mock(IntSeries.class);
when(metricSerieses1[1].getId()).thenReturn(metricID);
when(metricID.getCounterId()).thenReturn(1);
IntSeries intSeries2 = Mockito.mock(IntSeries.class);
intSeries2 = metricSerieses1[1];
when(intSeries2.getValue()).thenReturn(values);
MetricId metricID2 = Mockito.mock(MetricId.class);
metricSerieses1[2] = Mockito.mock(IntSeries.class);
when(metricSerieses1[2].getId()).thenReturn(metricID2);
when(metricID2.getCounterId()).thenReturn(3);
IntSeries intSeries3 = Mockito.mock(IntSeries.class);
intSeries3 = metricSerieses1[2];
when(intSeries3.getValue()).thenReturn(values);
MetricId metricID3 = Mockito.mock(MetricId.class);
metricSerieses1[3] = Mockito.mock(IntSeries.class);
when(metricSerieses1[3].getId()).thenReturn(metricID3);
when(metricID3.getCounterId()).thenReturn(4);
IntSeries intSeries4 = Mockito.mock(IntSeries.class);
intSeries4 = metricSerieses1[3];
when(intSeries4.getValue()).thenReturn(values);
when(metricBase[0].getValue()).thenReturn(metricSerieses1);
SampleInfo[] sampleInfos1 = new SampleInfo[4];
sampleInfos1[0] = Mockito.mock(SampleInfo.class);
Calendar time = Calendar.getInstance();
when(sampleInfos1[0].getTimestamp()).thenReturn(time);
sampleInfos1[1] = Mockito.mock(SampleInfo.class);
when(sampleInfos1[1].getTimestamp()).thenReturn(time);
sampleInfos1[2] = Mockito.mock(SampleInfo.class);
when(sampleInfos1[2].getTimestamp()).thenReturn(time);
sampleInfos1[3] = Mockito.mock(SampleInfo.class);
when(sampleInfos1[3].getTimestamp()).thenReturn(time);
when(metricBase[0].getSampleInfo()).thenReturn(sampleInfos1);
metricBase[1] = Mockito.mock(EntityMetric.class);
when(metricBase[1].getValue()).thenReturn(metricSerieses1);
when(metricBase[1].getSampleInfo()).thenReturn(sampleInfos1);
metricBase[2] = Mockito.mock(EntityMetric.class);
when(metricBase[2].getValue()).thenReturn(metricSerieses1);
when(metricBase[2].getSampleInfo()).thenReturn(sampleInfos1);
metricBase[3] = Mockito.mock(EntityMetric.class);
when(metricBase[3].getValue()).thenReturn(metricSerieses1);
when(metricBase[3].getSampleInfo()).thenReturn(sampleInfos1);
when(performanceManager.queryStats(any())).thenReturn(metricBase);
service.feedHostUsageData(vsphereClient, hostMappingAsset.getId(), host._getRef());
}
use of com.vmware.vim.binding.vim.PerformanceManager.ProviderSummary in project flowgate by vmware.
the class VCDataService method feedHostUsageData.
public void feedHostUsageData(VsphereClient vsphereClient, String assetId, ManagedObjectReference hostRef) {
List<RealTimeData> realTimeDatas = new ArrayList<RealTimeData>();
RealTimeData realTimeData = new RealTimeData();
realTimeData.setAssetID(assetId);
List<ValueUnit> valueUnits = new ArrayList<ValueUnit>();
PerformanceManager performanceManager = vsphereClient.getPerformanceManager();
Map<Integer, String> counters = getMetricsCounters(performanceManager);
if (counters == null || counters.isEmpty()) {
logger.error("Asset: {} failed to get performance counters", assetId);
return;
}
List<MetricId> metricIdList = getPerformenceMetricsIds(performanceManager, hostRef, counters);
if (metricIdList == null || metricIdList.isEmpty()) {
logger.error("Asset: {} failed to get performance metricIds", assetId);
return;
}
// fill spec info
ProviderSummary summary = performanceManager.queryProviderSummary(hostRef);
int perfInterval = summary.getRefreshRate();
QuerySpec[] specs = new QuerySpecImpl[1];
specs[0] = new QuerySpecImpl();
specs[0].setEntity(hostRef);
specs[0].setMetricId(metricIdList.toArray(new MetricId[metricIdList.size()]));
specs[0].setIntervalId(perfInterval);
specs[0].setMaxSample(15);
// get metrics value
EntityMetricBase[] metricBase = performanceManager.queryStats(specs);
if (metricBase == null || metricBase.length <= 0) {
logger.error("Asset: {} failed to get performance metricBase", assetId);
return;
}
List<ValueUnit> powerValueUnits = new ArrayList<>(15);
for (EntityMetricBase entityMetricBase : metricBase) {
/*
(vim.EntityMetric) {
dynamicType = null,
dynamicProperty = null,
entity = ManagedObjectReference: type = HostSystem, value = host-65, serverGuid = null,
sampleInfo = (vim.SampleInfo) [
(vim.SampleInfo) {
dynamicType = null,
dynamicProperty = null,
timestamp = java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+00:00",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=11,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=10,DAY_OF_YEAR=1,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=2,MINUTE=12,SECOND=20,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0],
interval = 20
}...
],
value = (vim.MetricSeries) [
(vim.IntSeries) {
dynamicType = null,
dynamicProperty = null,
id = (vim.MetricId) {
dynamicType = null,
dynamicProperty = null,
counterId = 125,
instance =
},
value = (LONG) [
667,
251,
96,
298,
99,
119,
492,
318,
98,
218,
128,
141,
1224,
358,
133
]
}...
*/
EntityMetric entityMetric = (EntityMetric) entityMetricBase;
/*
(vim.IntSeries) {
dynamicType = null,
dynamicProperty = null,
id = (vim.MetricId) {
dynamicType = null,
dynamicProperty = null,
counterId = 125,
instance =
},
value = (LONG) [101,134,667,282,90,224,99,129,667,251,96,298,99,119,492]
}
*/
MetricSeries[] metricSeries = entityMetric.getValue();
/*
(vim.SampleInfo) {
dynamicType = null,
dynamicProperty = null,
timestamp = java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+00:00",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2020,MONTH=11,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=10,DAY_OF_YEAR=1,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=2,MINUTE=7,SECOND=40,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0],
interval = 20
}
*/
SampleInfo[] sampleInfos = entityMetric.getSampleInfo();
for (MetricSeries metricSerie : metricSeries) {
if (metricSerie instanceof IntSeries) {
IntSeries intSeries = (IntSeries) metricSerie;
long[] values = intSeries.getValue();
int counterId = metricSerie.getId().getCounterId();
for (int index = 0; index < values.length; index++) {
long timeStamp = sampleInfos[index].getTimestamp().getTimeInMillis();
ValueUnit valueUnit = new ValueUnit();
long value = values[index];
switch(counters.get(counterId)) {
case VCConstants.HOST_CPU_GROUP + VCConstants.HOST_METRIC_USAGE:
valueUnit.setKey(MetricName.SERVER_CPUUSAGE);
valueUnit.setValueNum(value / 100.0);
valueUnit.setUnit(ValueUnit.MetricUnit.percent.name());
break;
case VCConstants.HOST_CPU_GROUP + VCConstants.HOST_METRIC_USAGEMHZ:
valueUnit.setKey(MetricName.SERVER_CPUUSEDINMHZ);
valueUnit.setValueNum(value);
valueUnit.setUnit(ValueUnit.MetricUnit.Mhz.name());
break;
case VCConstants.HOST_MEMORY_GROUP + VCConstants.HOST_METRIC_USAGE:
valueUnit.setKey(MetricName.SERVER_MEMORYUSAGE);
valueUnit.setValueNum(value / 100.0);
valueUnit.setUnit(ValueUnit.MetricUnit.percent.name());
break;
case VCConstants.HOST_MEMORY_GROUP + VCConstants.HOST_METRIC_MEM_ACTIVE:
valueUnit.setKey(MetricName.SERVER_ACTIVEMEMORY);
valueUnit.setValueNum(value);
valueUnit.setUnit(ValueUnit.MetricUnit.kB.name());
break;
case VCConstants.HOST_MEMORY_GROUP + VCConstants.HOST_METRIC_MEM_SHARED:
valueUnit.setKey(MetricName.SERVER_SHAREDMEMORY);
valueUnit.setValueNum(value);
valueUnit.setUnit(ValueUnit.MetricUnit.kB.name());
break;
case VCConstants.HOST_MEMORY_GROUP + VCConstants.HOST_METRIC_MEM_CONSUMED:
valueUnit.setKey(MetricName.SERVER_CONSUMEDMEMORY);
valueUnit.setValueNum(value);
valueUnit.setUnit(ValueUnit.MetricUnit.kB.name());
break;
case VCConstants.HOST_MEMORY_GROUP + VCConstants.HOST_METRIC_MEM_SWAP:
valueUnit.setKey(MetricName.SERVER_SWAPMEMORY);
valueUnit.setValueNum(value);
valueUnit.setUnit(ValueUnit.MetricUnit.kB.name());
break;
case VCConstants.HOST_MEMORY_GROUP + VCConstants.HOST_METRIC_MEM_BALLON:
valueUnit.setKey(MetricName.SERVER_BALLOONMEMORY);
valueUnit.setValueNum(value);
valueUnit.setUnit(ValueUnit.MetricUnit.kB.name());
break;
case VCConstants.HOST_DISK_GROUP + VCConstants.HOST_METRIC_USAGE:
valueUnit.setKey(MetricName.SERVER_STORAGEIORATEUSAGE);
valueUnit.setValueNum(value);
valueUnit.setUnit(ValueUnit.MetricUnit.kBps.name());
break;
case VCConstants.HOST_NETWORK_GROUP + VCConstants.HOST_METRIC_USAGE:
valueUnit.setKey(MetricName.SERVER_NETWORKUTILIZATION);
valueUnit.setValueNum(value);
valueUnit.setUnit(ValueUnit.MetricUnit.kBps.name());
break;
case VCConstants.HOST_POWER_GROUP + VCConstants.HOST_METRIC_POWER_POWER:
valueUnit.setKey(MetricName.SERVER_POWER);
valueUnit.setValueNum(valueUnit.translateUnit(value, ValueUnit.MetricUnit.W, ValueUnit.MetricUnit.kW));
valueUnit.setUnit(ValueUnit.MetricUnit.kW.name());
powerValueUnits.add(valueUnit);
break;
case VCConstants.HOST_POWER_GROUP + VCConstants.HOST_METRIC_POWER_ENERGY:
valueUnit.setKey(MetricName.SERVER_ENERGY_CONSUMPTION);
// This extraidentifier is the start time
valueUnit.setExtraidentifier(String.valueOf(timeStamp - (perfInterval * 1000)));
valueUnit.setValueNum(value * JOULETOKWHRATE);
valueUnit.setUnit(ValueUnit.MetricUnit.kWh.name());
break;
}
valueUnit.setTime(timeStamp);
valueUnits.add(valueUnit);
}
}
}
}
if (valueUnits == null || valueUnits.isEmpty()) {
logger.error("ValueUnits of asset: {} is empty.", assetId);
return;
}
valueUnits.addAll(getMinMaxAvgValueUnit(powerValueUnits));
realTimeData.setValues(valueUnits);
realTimeData.setTime(valueUnits.get(0).getTime());
realTimeDatas.add(realTimeData);
restClient.saveRealTimeData(realTimeDatas);
}
Aggregations