6262import com .cloud .storage .VolumeVO ;
6363import com .cloud .storage .dao .VolumeDao ;
6464import com .cloud .utils .StringUtils ;
65+ import com .cloud .utils .Ternary ;
6566import com .cloud .utils .component .Manager ;
6667import com .cloud .utils .component .ManagerBase ;
6768import com .cloud .vm .VirtualMachine .State ;
6869import com .cloud .vm .dao .VMInstanceDao ;
69- import org .apache .commons .lang3 .StringUtils ;
7070
7171public 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