Skip to content

Commit ab04936

Browse files
author
Sina Kashipazha
committed
Cloudstack Prometheus exporter: Add allocated capacity group by host tag.
1 parent 1b24280 commit ab04936

File tree

3 files changed

+84
-17
lines changed

3 files changed

+84
-17
lines changed

engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.cloud.capacity.CapacityVO;
2323
import com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity;
2424
import com.cloud.utils.Pair;
25+
import com.cloud.utils.Ternary;
2526
import com.cloud.utils.db.GenericDao;
2627

2728
public interface CapacityDao extends GenericDao<CapacityVO, Long> {
@@ -39,6 +40,8 @@ public interface CapacityDao extends GenericDao<CapacityVO, Long> {
3940

4041
Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(long id, long vmId, short capacityType, boolean isZone);
4142

43+
Ternary<Long, Long, Long> findCapacityByZoneAndHostTag(Long zoneId, String hostTag);
44+
4245
List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId);
4346

4447
List<Long> listPodsByHostCapacities(long zoneId, int requiredCpu, long requiredRam, short capacityType);

engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.cloud.dc.ClusterDetailsDao;
3838
import com.cloud.storage.Storage;
3939
import com.cloud.utils.Pair;
40+
import com.cloud.utils.Ternary;
4041
import com.cloud.utils.db.GenericDaoBase;
4142
import com.cloud.utils.db.GenericSearchBuilder;
4243
import com.cloud.utils.db.JoinBuilder.JoinType;
@@ -198,16 +199,18 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
198199
+
199200
"from op_host_capacity capacity where cluster_id = ? and capacity_type = ?;";
200201

201-
private static final String LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE = "SELECT v.data_center_id, SUM(cpu) AS cpucore, " +
202+
private static final String LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE_PART1 = "SELECT v.data_center_id, SUM(cpu) AS cpucore, " +
202203
"SUM(cpu * speed) AS cpu, SUM(ram_size * 1024 * 1024) AS memory " +
203204
"FROM (SELECT vi.data_center_id, (CASE WHEN ISNULL(service_offering.cpu) THEN custom_cpu.value ELSE service_offering.cpu end) AS cpu, " +
204205
"(CASE WHEN ISNULL(service_offering.speed) THEN custom_speed.value ELSE service_offering.speed end) AS speed, " +
205206
"(CASE WHEN ISNULL(service_offering.ram_size) THEN custom_ram_size.value ELSE service_offering.ram_size end) AS ram_size " +
206-
"FROM (((vm_instance vi LEFT JOIN service_offering ON(((vi.service_offering_id = service_offering.id))) " +
207-
"LEFT JOIN user_vm_details custom_cpu ON(((custom_cpu.vm_id = vi.id) AND (custom_cpu.name = 'CpuNumber')))) " +
208-
"LEFT JOIN user_vm_details custom_speed ON(((custom_speed.vm_id = vi.id) AND (custom_speed.name = 'CpuSpeed')))) " +
209-
"LEFT JOIN user_vm_details custom_ram_size ON(((custom_ram_size.vm_id = vi.id) AND (custom_ram_size.name = 'memory')))) " +
210-
"WHERE ISNULL(vi.removed) AND vi.state NOT IN ('Destroyed', 'Error', 'Expunging')";
207+
"FROM vm_instance vi LEFT JOIN service_offering ON(((vi.service_offering_id = service_offering.id))) " +
208+
"LEFT JOIN user_vm_details custom_cpu ON(((custom_cpu.vm_id = vi.id) AND (custom_cpu.name = 'CpuNumber'))) " +
209+
"LEFT JOIN user_vm_details custom_speed ON(((custom_speed.vm_id = vi.id) AND (custom_speed.name = 'CpuSpeed'))) " +
210+
"LEFT JOIN user_vm_details custom_ram_size ON(((custom_ram_size.vm_id = vi.id) AND (custom_ram_size.name = 'memory'))) ";
211+
212+
private static final String LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE_PART2 =
213+
"WHERE ISNULL(vi.removed) AND vi.state NOT IN ('Destroyed', 'Error', 'Expunging')";
211214

212215
public CapacityDaoImpl() {
213216
_hostIdTypeSearch = createSearchBuilder();
@@ -422,14 +425,50 @@ public List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacity
422425

423426
}
424427

428+
@Override
429+
public Ternary<Long, Long, Long> findCapacityByZoneAndHostTag(Long zoneId, String hostTag) {
430+
TransactionLegacy txn = TransactionLegacy.currentTxn();
431+
PreparedStatement pstmt = null;
432+
List<SummedCapacity> results = new ArrayList<SummedCapacity>();
433+
434+
StringBuilder allocatedSql = new StringBuilder(LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE_PART1);
435+
if (hostTag != null && ! hostTag.isEmpty()) {
436+
allocatedSql.append("LEFT JOIN vm_template ON vm_template.id = vi.vm_template_id ");
437+
}
438+
allocatedSql.append(LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE_PART2);
439+
if (zoneId != null){
440+
allocatedSql.append(" AND vi.data_center_id = ?");
441+
}
442+
if (hostTag != null && ! hostTag.isEmpty()) {
443+
allocatedSql.append(" AND (vm_template.template_tag = '").append(hostTag).append("'");
444+
allocatedSql.append(" OR service_offering.host_tag = '").append(hostTag).append("')");
445+
}
446+
allocatedSql.append(" ) AS v GROUP BY v.data_center_id");
447+
try {
448+
// add allocated capacity of zone in result
449+
pstmt = txn.prepareAutoCloseStatement(allocatedSql.toString());
450+
if (zoneId != null){
451+
pstmt.setLong(1, zoneId);
452+
}
453+
ResultSet rs = pstmt.executeQuery();
454+
if (rs.next()) {
455+
return new Ternary(rs.getLong(2), rs.getLong(3), rs.getLong(4)); // cpu cores, cpu, memory
456+
}
457+
return new Ternary(0L, 0L, 0L);
458+
} catch (SQLException e) {
459+
throw new CloudRuntimeException("DB Exception on: " + allocatedSql, e);
460+
}
461+
}
462+
425463
@Override
426464
public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId) {
427465

428466
TransactionLegacy txn = TransactionLegacy.currentTxn();
429467
PreparedStatement pstmt = null;
430468
List<SummedCapacity> results = new ArrayList<SummedCapacity>();
431469

432-
StringBuilder allocatedSql = new StringBuilder(LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE);
470+
StringBuilder allocatedSql = new StringBuilder(LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE_PART1);
471+
allocatedSql.append(LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE_PART2);
433472

434473
HashMap<Long, Long> sumCpuCore = new HashMap<Long, Long>();
435474
HashMap<Long, Long> sumCpu = new HashMap<Long, Long>();

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@
6262
import com.cloud.storage.VolumeVO;
6363
import com.cloud.storage.dao.VolumeDao;
6464
import com.cloud.utils.StringUtils;
65+
import com.cloud.utils.Ternary;
6566
import com.cloud.utils.component.Manager;
6667
import com.cloud.utils.component.ManagerBase;
6768
import com.cloud.vm.VirtualMachine.State;
6869
import com.cloud.vm.dao.VMInstanceDao;
69-
import org.apache.commons.lang3.StringUtils;
7070

7171
public class PrometheusExporterImpl extends ManagerBase implements PrometheusExporter, Manager {
7272
private static final Logger LOG = Logger.getLogger(PrometheusExporterImpl.class);
@@ -172,11 +172,11 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
172172
final String cpuFactor = String.valueOf(CapacityManager.CpuOverprovisioningFactor.valueIn(host.getClusterId()));
173173
final CapacityVO cpuCapacity = capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_CPU);
174174
if (cpuCapacity != null) {
175-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, cpuCapacity.getUsedCapacity(), hosttags));
176-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, cpuCapacity.getTotalCapacity(), hosttags));
175+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, cpuCapacity.getUsedCapacity(), isDedicated, hosttags));
176+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, cpuCapacity.getTotalCapacity(), isDedicated, hosttags));
177177
} else {
178-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, 0L, hosttags));
179-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, 0L, hosttags));
178+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, USED, 0L, isDedicated, hosttags));
179+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, host.getName(), host.getUuid(), host.getPrivateIpAddress(), cpuFactor, TOTAL, 0L, isDedicated, hosttags));
180180
}
181181

182182
final String memoryFactor = String.valueOf(CapacityManager.MemOverprovisioningFactor.valueIn(host.getClusterId()));
@@ -203,7 +203,7 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
203203

204204
final List<CapacityDaoImpl.SummedCapacity> cpuCapacity = capacityDao.findCapacityBy((int) Capacity.CAPACITY_TYPE_CPU, dcId, null, null);
205205
if (cpuCapacity != null && cpuCapacity.size() > 0) {
206-
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, null, null, null, null, ALLOCATED, cpuCapacity.get(0).getAllocatedCapacity() != null ? cpuCapacity.get(0).getAllocatedCapacity() : 0, ""));
206+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, null, null, null, null, ALLOCATED, cpuCapacity.get(0).getAllocatedCapacity() != null ? cpuCapacity.get(0).getAllocatedCapacity() : 0, 0, ""));
207207
}
208208

209209
final List<CapacityDaoImpl.SummedCapacity> memCapacity = capacityDao.findCapacityBy((int) Capacity.CAPACITY_TYPE_MEMORY, dcId, null, null);
@@ -234,6 +234,13 @@ private void addHostMetrics(final List<Item> metricsList, final long dcId, final
234234
metricsList.add(new ItemHost(zoneName, zoneUuid, OFFLINE, 0, tag));
235235
}
236236
}
237+
for (Map.Entry<String, Integer> entry : total_hosts.entrySet()) {
238+
String tag = entry.getKey();
239+
Ternary<Long, Long, Long> allocatedCapacityByTag = capacityDao.findCapacityByZoneAndHostTag(dcId, tag);
240+
metricsList.add(new ItemVMCore(zoneName, zoneUuid, null, null, null, ALLOCATED, allocatedCapacityByTag.first(), 0, tag));
241+
metricsList.add(new ItemHostCpu(zoneName, zoneUuid, null, null, null, null, ALLOCATED, allocatedCapacityByTag.second(),0, tag));
242+
metricsList.add(new ItemHostMemory(zoneName, zoneUuid, null, null, null, null, ALLOCATED, allocatedCapacityByTag.third(), 0, tag));
243+
}
237244
}
238245

239246
private void addVMMetrics(final List<Item> metricsList, final long dcId, final String zoneName, final String zoneUuid) {
@@ -512,6 +519,7 @@ public ItemHost(final String zn, final String zu, final String st, int cnt, fina
512519
@Override
513520
public String toMetricsString() {
514521
if (! Strings.isNullOrEmpty(hosttags)) {
522+
name = "cloudstack_hosts_total_by_tag";
515523
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %d", name, zoneName, state, hosttags, total);
516524
}
517525
return String.format("%s{zone=\"%s\",filter=\"%s\"} %d", name, zoneName, state, total);
@@ -547,7 +555,12 @@ public ItemVMCore(final String zn, final String zu, final String hn, final Strin
547555
@Override
548556
public String toMetricsString() {
549557
if (StringUtils.isAllEmpty(hostName, ip)) {
550-
return String.format("%s{zone=\"%s\",filter=\"%s\"} %d", name, zoneName, filter, core);
558+
if (Strings.isNullOrEmpty(hosttags)) {
559+
return String.format("%s{zone=\"%s\",filter=\"%s\"} %d", name, zoneName, filter, core);
560+
} else {
561+
name = "cloudstack_host_vms_cores_total_by_tag";
562+
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %d", name, zoneName, filter, hosttags, core);
563+
}
551564
}
552565
return String.format("%s{zone=\"%s\",hostname=\"%s\",ip=\"%s\",filter=\"%s\",dedicated=\"%d\",tags=\"%s\"} %d", name, zoneName, hostName, ip, filter, isDedicated, hosttags, core);
553566
}
@@ -562,9 +575,10 @@ class ItemHostCpu extends Item {
562575
String overProvisioningFactor;
563576
String filter;
564577
double mhertz;
578+
int isDedicated;
565579
String hosttags;
566580

567-
public ItemHostCpu(final String zn, final String zu, final String hn, final String hu, final String hip, final String of, final String fl, final double mh, final String tags) {
581+
public ItemHostCpu(final String zn, final String zu, final String hn, final String hu, final String hip, final String of, final String fl, final double mh, final int dedicated, final String tags) {
568582
super("cloudstack_host_cpu_usage_mhz_total");
569583
zoneName = zn;
570584
zoneUuid = zu;
@@ -574,13 +588,19 @@ public ItemHostCpu(final String zn, final String zu, final String hn, final Stri
574588
overProvisioningFactor = of;
575589
filter = fl;
576590
mhertz = mh;
591+
isDedicated = dedicated;
577592
hosttags = tags;
578593
}
579594

580595
@Override
581596
public String toMetricsString() {
582597
if (StringUtils.isAllEmpty(hostName, ip)) {
583-
return String.format("%s{zone=\"%s\",filter=\"%s\"} %.2f", name, zoneName, filter, mhertz);
598+
if (Strings.isNullOrEmpty(hosttags)) {
599+
return String.format("%s{zone=\"%s\",filter=\"%s\"} %.2f", name, zoneName, filter, mhertz);
600+
} else {
601+
name = "cloudstack_host_cpu_usage_mhz_total_by_tag";
602+
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %.2f", name, zoneName, filter, hosttags, mhertz);
603+
}
584604
}
585605
return String.format("%s{zone=\"%s\",hostname=\"%s\",ip=\"%s\",overprovisioningfactor=\"%s\",filter=\"%s\",tags=\"%s\"} %.2f", name, zoneName, hostName, ip, overProvisioningFactor, filter, hosttags, mhertz);
586606
}
@@ -615,7 +635,12 @@ public ItemHostMemory(final String zn, final String zu, final String hn, final S
615635
@Override
616636
public String toMetricsString() {
617637
if (StringUtils.isAllEmpty(hostName, ip)) {
618-
return String.format("%s{zone=\"%s\",filter=\"%s\"} %.2f", name, zoneName, filter, miBytes);
638+
if (Strings.isNullOrEmpty(hosttags)) {
639+
return String.format("%s{zone=\"%s\",filter=\"%s\"} %.2f", name, zoneName, filter, miBytes);
640+
} else {
641+
name = "cloudstack_host_memory_usage_mibs_total_by_tag";
642+
return String.format("%s{zone=\"%s\",filter=\"%s\",tags=\"%s\"} %.2f", name, zoneName, filter, hosttags, miBytes);
643+
}
619644
}
620645
return String.format("%s{zone=\"%s\",hostname=\"%s\",ip=\"%s\",overprovisioningfactor=\"%s\",filter=\"%s\",dedicated=\"%d\",tags=\"%s\"} %.2f", name, zoneName, hostName, ip, overProvisioningFactor, filter, isDedicated, hosttags, miBytes);
621646
}

0 commit comments

Comments
 (0)