Skip to content

Commit 9df23f9

Browse files
authored
Prometheus exporter fix cpu/memory usage labels (#7629)
1 parent dea1373 commit 9df23f9

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

plugins/integrations/prometheus/src/main/java/org/apache/cloudstack/metrics/PrometheusExporterImpl.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.cloud.configuration.dao.ResourceCountDao;
2929
import com.cloud.dc.DedicatedResourceVO;
3030
import com.cloud.dc.dao.DedicatedResourceDao;
31+
import com.cloud.host.HostStats;
3132
import com.cloud.user.Account;
3233
import com.cloud.user.dao.AccountDao;
3334
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
@@ -127,6 +128,8 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
127128
Map<String, Integer> upHosts = new HashMap<>();
128129
Map<String, Integer> downHosts = new HashMap<>();
129130

131+
HostStats hostStats;
132+
130133
for (final HostVO host : hostDao.listAll()) {
131134
if (host == null || host.getType() != Host.Type.Routing || host.getDataCenterId() != dcId) {
132135
continue;
@@ -144,6 +147,7 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
144147
metricsList.add(new ItemHostIsDedicated(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), isDedicated));
145148

146149
String hostTags = markTagMaps(host, totalHosts, upHosts, downHosts);
150+
hostStats = ApiDBUtils.getHostStatistics(host.getId());
147151

148152
// Get account, domain details for dedicated hosts
149153
if (isDedicated == 1) {
@@ -157,21 +161,27 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
157161

158162
final String cpuFactor = String.valueOf(CapacityManager.CpuOverprovisioningFactor.valueIn(host.getClusterId()));
159163
final CapacityVO cpuCapacity = capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_CPU);
164+
final double cpuUsedMhz = hostStats.getCpuUtilization() * host.getCpus() * host.getSpeed() / 100.0 ;
165+
160166
if (cpuCapacity != null && cpuCapacity.getCapacityState() == CapacityState.Enabled) {
161-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, cpuCapacity.getUsedCapacity(), isDedicated, hostTags));
167+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, ALLOCATED, cpuCapacity.getUsedCapacity(), isDedicated, hostTags));
168+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, cpuUsedMhz, isDedicated, hostTags));
162169
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, cpuCapacity.getTotalCapacity(), isDedicated, hostTags));
163170
} else {
171+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, ALLOCATED, 0L, isDedicated, hostTags));
164172
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, 0L, isDedicated, hostTags));
165173
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, 0L, isDedicated, hostTags));
166174
}
167175

168176
final String memoryFactor = String.valueOf(CapacityManager.MemOverprovisioningFactor.valueIn(host.getClusterId()));
169177
final CapacityVO memCapacity = capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_MEMORY);
170178
if (memCapacity != null && memCapacity.getCapacityState() == CapacityState.Enabled) {
171-
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, USED, memCapacity.getUsedCapacity(), isDedicated, hostTags));
179+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, ALLOCATED, memCapacity.getUsedCapacity(), isDedicated, hostTags));
180+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, USED, hostStats.getUsedMemory(), isDedicated, hostTags));
172181
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, TOTAL, memCapacity.getTotalCapacity(), isDedicated, hostTags));
173182
} else {
174-
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, USED, 0L, isDedicated, hostTags));
183+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, ALLOCATED, 0L, isDedicated, hostTags));
184+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, USED, 0, isDedicated, hostTags));
175185
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), memoryFactor, TOTAL, 0L, isDedicated, hostTags));
176186
}
177187

0 commit comments

Comments
 (0)