4242import org .apache .log4j .Logger ;
4343
4444import com .cloud .alert .dao .AlertDao ;
45+ import com .cloud .api .ApiDBUtils ;
46+ import com .cloud .capacity .Capacity ;
4547import com .cloud .capacity .CapacityManager ;
4648import com .cloud .capacity .CapacityVO ;
4749import com .cloud .capacity .dao .CapacityDao ;
50+ import com .cloud .capacity .dao .CapacityDaoImpl .SummedCapacity ;
4851import com .cloud .configuration .Config ;
4952import com .cloud .configuration .dao .ConfigurationDao ;
53+ import com .cloud .dc .ClusterVO ;
5054import com .cloud .dc .DataCenterVO ;
5155import com .cloud .dc .HostPodVO ;
56+ import com .cloud .dc .dao .ClusterDao ;
5257import com .cloud .dc .dao .DataCenterDao ;
5358import com .cloud .dc .dao .DataCenterIpAddressDao ;
5459import com .cloud .dc .dao .HostPodDao ;
@@ -91,7 +96,8 @@ public class AlertManagerImpl implements AlertManager {
9196 @ Inject protected CapacityManager _capacityMgr ;
9297 @ Inject private CapacityDao _capacityDao ;
9398 @ Inject private DataCenterDao _dcDao ;
94- @ Inject private HostPodDao _podDao ;
99+ @ Inject private HostPodDao _podDao ;
100+ @ Inject private ClusterDao _clusterDao ;
95101 @ Inject private VolumeDao _volumeDao ;
96102 @ Inject private IPAddressDao _publicIPAddressDao ;
97103 @ Inject private DataCenterIpAddressDao _privateIPAddressDao ;
@@ -105,7 +111,8 @@ public class AlertManagerImpl implements AlertManager {
105111 private double _storageCapacityThreshold = 0.75 ;
106112 private double _storageAllocCapacityThreshold = 0.75 ;
107113 private double _publicIPCapacityThreshold = 0.75 ;
108- private double _privateIPCapacityThreshold = 0.75 ;
114+ private double _privateIPCapacityThreshold = 0.75 ;
115+ Map <Short ,Double > _capacityTypeThresholdMap = new HashMap <Short , Double >();
109116
110117 @ Override
111118 public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
@@ -166,8 +173,14 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
166173 }
167174 if (privateIPCapacityThreshold != null ) {
168175 _privateIPCapacityThreshold = Double .parseDouble (privateIPCapacityThreshold );
169- }
170-
176+ }
177+ _capacityTypeThresholdMap .put (Capacity .CAPACITY_TYPE_STORAGE , _storageCapacityThreshold );
178+ _capacityTypeThresholdMap .put (Capacity .CAPACITY_TYPE_STORAGE_ALLOCATED , _storageAllocCapacityThreshold );
179+ _capacityTypeThresholdMap .put (Capacity .CAPACITY_TYPE_CPU , _cpuCapacityThreshold );
180+ _capacityTypeThresholdMap .put (Capacity .CAPACITY_TYPE_MEMORY , _memoryCapacityThreshold );
181+ _capacityTypeThresholdMap .put (Capacity .CAPACITY_TYPE_PUBLIC_IP , _publicIPCapacityThreshold );
182+ _capacityTypeThresholdMap .put (Capacity .CAPACITY_TYPE_PRIVATE_IP , _privateIPCapacityThreshold );
183+
171184 String capacityCheckPeriodStr = configs .get ("capacity.check.period" );
172185 if (capacityCheckPeriodStr != null ) {
173186 _capacityCheckPeriod = Long .parseLong (capacityCheckPeriodStr );
@@ -462,6 +475,141 @@ public void run() {
462475
463476
464477 public void newAlertSystem (){
478+ recalculateCapacity ();
479+
480+ // abort if we can't possibly send an alert...
481+ if (_emailAlert == null ) {
482+ return ;
483+ }
484+
485+ //Get all datacenters, pods and clusters in the system.
486+ List <DataCenterVO > dataCenterList = _dcDao .listAll ();
487+ List <ClusterVO > clusterList = _clusterDao .listAll ();
488+ List <HostPodVO > podList = _podDao .listAll ();
489+ //Get capacity types at different levels
490+ List <Short > dataCenterCapacityTypes = getCapacityTypesAtZoneLevel ();
491+ List <Short > podCapacityTypes = getCapacityTypesAtPodLevel ();
492+ List <Short > clusterCapacityTypes = getCapacityTypesAtClusterLevel ();
493+
494+ // Generate Alerts for Zone Level capacities
495+ for (DataCenterVO dc : dataCenterList ){
496+ for (Short capacityType : dataCenterCapacityTypes ){
497+ List <SummedCapacity > capacity = _capacityDao .findCapacityBy (capacityType .intValue (), dc .getId (), null , null );
498+ double totalCapacity = capacity .get (0 ).getTotalCapacity ();
499+ double usedCapacity = capacity .get (0 ).getUsedCapacity ();
500+ if (totalCapacity != 0 && usedCapacity /totalCapacity > _capacityTypeThresholdMap .get (capacityType )){
501+ generateEmailAlert (dc , null , null , totalCapacity , usedCapacity , capacityType );
502+ }
503+ }
504+ }
505+
506+ // Generate Alerts for Pod Level capacities
507+ for ( HostPodVO pod : podList ){
508+ for (Short capacityType : podCapacityTypes ){
509+ List <SummedCapacity > capacity = _capacityDao .findCapacityBy (capacityType .intValue (), pod .getDataCenterId (), pod .getId (), null );
510+ double totalCapacity = capacity .get (0 ).getTotalCapacity ();
511+ double usedCapacity = capacity .get (0 ).getUsedCapacity ();
512+ if (totalCapacity != 0 && usedCapacity /totalCapacity > _capacityTypeThresholdMap .get (capacityType )){
513+ generateEmailAlert (ApiDBUtils .findZoneById (pod .getDataCenterId ()), pod , null ,
514+ totalCapacity , usedCapacity , capacityType );
515+ }
516+ }
517+ }
518+
519+ // Generate Alerts for Cluster Level capacities
520+ for ( ClusterVO cluster : clusterList ){
521+ for (Short capacityType : clusterCapacityTypes ){
522+ List <SummedCapacity > capacity = _capacityDao .findCapacityBy (capacityType .intValue (), cluster .getDataCenterId (), null , cluster .getId ());
523+ double totalCapacity = capacity .get (0 ).getTotalCapacity ();
524+ double usedCapacity = capacity .get (0 ).getUsedCapacity ();
525+ if (totalCapacity != 0 && usedCapacity /totalCapacity > _capacityTypeThresholdMap .get (capacityType )){
526+ generateEmailAlert (ApiDBUtils .findZoneById (cluster .getDataCenterId ()), ApiDBUtils .findPodById (cluster .getPodId ()), cluster ,
527+ totalCapacity , usedCapacity , capacityType );
528+ }
529+ }
530+ }
531+
532+ }
533+
534+ private void generateEmailAlert (DataCenterVO dc , HostPodVO pod , ClusterVO cluster , double totalCapacity , double usedCapacity , short capacityType ){
535+
536+ String msgSubject = null ;
537+ String msgContent = null ;
538+ String totalStr ;
539+ String usedStr ;
540+ String pctStr = formatPercent (usedCapacity /totalCapacity );
541+
542+ switch (capacityType ) {
543+ case CapacityVO .CAPACITY_TYPE_MEMORY :
544+ msgSubject = "System Alert: Low Available Memory in pod " +pod .getName ()+ " of availablity zone " + dc .getName ();
545+ totalStr = formatBytesToMegabytes (totalCapacity );
546+ usedStr = formatBytesToMegabytes (usedCapacity );
547+ msgContent = "System memory is low, total: " + totalStr + " MB, used: " + usedStr + " MB (" + pctStr + "%)" ;
548+ break ;
549+ case CapacityVO .CAPACITY_TYPE_CPU :
550+ msgSubject = "System Alert: Low Unallocated CPU in pod " +pod .getName ()+ " of availablity zone " + dc .getName ();
551+ totalStr = _dfWhole .format (totalCapacity );
552+ usedStr = _dfWhole .format (usedCapacity );
553+ msgContent = "Unallocated CPU is low, total: " + totalStr + " Mhz, used: " + usedStr + " Mhz (" + pctStr + "%)" ;
554+ break ;
555+ case CapacityVO .CAPACITY_TYPE_STORAGE :
556+ msgSubject = "System Alert: Low Available Storage in pod " +pod .getName ()+ " of availablity zone " + dc .getName ();
557+ totalStr = formatBytesToMegabytes (totalCapacity );
558+ usedStr = formatBytesToMegabytes (usedCapacity );
559+ msgContent = "Available storage space is low, total: " + totalStr + " MB, used: " + usedStr + " MB (" + pctStr + "%)" ;
560+ break ;
561+ case CapacityVO .CAPACITY_TYPE_STORAGE_ALLOCATED :
562+ msgSubject = "System Alert: Remaining unallocated Storage is low in pod " +pod .getName ()+ " of availablity zone " + dc .getName ();
563+ totalStr = formatBytesToMegabytes (totalCapacity );
564+ usedStr = formatBytesToMegabytes (usedCapacity );
565+ msgContent = "Unallocated storage space is low, total: " + totalStr + " MB, allocated: " + usedStr + " MB (" + pctStr + "%)" ;
566+ break ;
567+ case CapacityVO .CAPACITY_TYPE_PUBLIC_IP :
568+ msgSubject = "System Alert: Number of unallocated public IPs is low in availablity zone " + dc .getName ();
569+ totalStr = Double .toString (totalCapacity );
570+ usedStr = Double .toString (usedCapacity );
571+ msgContent = "Number of unallocated public IPs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)" ;
572+ break ;
573+ case CapacityVO .CAPACITY_TYPE_PRIVATE_IP :
574+ msgSubject = "System Alert: Number of unallocated private IPs is low in pod " +pod .getName ()+ " of availablity zone " + dc .getName ();
575+ totalStr = Double .toString (totalCapacity );
576+ usedStr = Double .toString (usedCapacity );
577+ msgContent = "Number of unallocated private IPs is low, total: " + totalStr + ", allocated: " + usedStr + " (" + pctStr + "%)" ;
578+ break ;
579+ }
580+
581+ try {
582+ _emailAlert .sendAlert (capacityType , dc .getId (), null , msgSubject , msgContent );
583+ } catch (Exception ex ) {
584+ s_logger .error ("Exception in CapacityChecker" , ex );
585+ }
586+ }
587+
588+ private List <Short > getCapacityTypesAtZoneLevel (){
589+
590+ List <Short > dataCenterCapacityTypes = new ArrayList <Short >();
591+ dataCenterCapacityTypes .add (Capacity .CAPACITY_TYPE_PUBLIC_IP );
592+ dataCenterCapacityTypes .add (Capacity .CAPACITY_TYPE_SECONDARY_STORAGE );
593+ return dataCenterCapacityTypes ;
594+
595+ }
596+
597+ private List <Short > getCapacityTypesAtPodLevel (){
598+
599+ List <Short > podCapacityTypes = new ArrayList <Short >();
600+ podCapacityTypes .add (Capacity .CAPACITY_TYPE_PRIVATE_IP );
601+ return podCapacityTypes ;
602+
603+ }
604+
605+ private List <Short > getCapacityTypesAtClusterLevel (){
606+
607+ List <Short > clusterCapacityTypes = new ArrayList <Short >();
608+ clusterCapacityTypes .add (Capacity .CAPACITY_TYPE_CPU );
609+ clusterCapacityTypes .add (Capacity .CAPACITY_TYPE_MEMORY );
610+ clusterCapacityTypes .add (Capacity .CAPACITY_TYPE_STORAGE );
611+ clusterCapacityTypes .add (Capacity .CAPACITY_TYPE_STORAGE_ALLOCATED );
612+ return clusterCapacityTypes ;
465613
466614 }
467615
0 commit comments