Module: AsCombinedMetrics::Cli::Stats

Defined in:
lib/as-combined-metrics/stats.rb

Instance Method Summary collapse

Instance Method Details

#aggregate_instances_per_as_group(metric) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/as-combined-metrics/stats.rb', line 39

def aggregate_instances_per_as_group(metric)
  logger.progname = "#{Module.nesting.first.to_s} #{__method__}"
  logger.info { set_color "Aggregating metrics accross instances for autoscale group: #{@config[:autoscale_group_name]}" , :white}
  begin

    instances =  @as.describe_auto_scaling_groups({auto_scaling_group_names: [@config[:autoscale_group_name]], max_records: 1}).auto_scaling_groups.first.instances.collect {|i| i[:instance_id]}

    logger.info { set_color "Found #{instances.size} Instances for autoscale group: #{@config['autoscale_group_name']}", :magenta }
    
    instances_aggregated_data = []
    
    metric[:dimensions] = [{name: "InstanceId"}]

    instances.each do |instance_id|
      metric[:dimensions][0][:value] = instance_id

      logger.info {"aggregated metric: #{metric}"}
      metric_result = fetch_metric(metric)

      logger.info { set_color "Metric result: #{metric_result}", :bold }

      if metric_result == -1
        instances_aggregated_data << metric_result
        break
        return # check removal of break
      else
        instances_aggregated_data << metric_result
      end
    end

    logger.info { "Instances_aggregated_data: #{instances_aggregated_data}"  }
    
    if !instances_aggregated_data.empty?
      case metric[:statistics][0].downcase
      when 'maximum'
        logger.info { set_color "Instances_aggregated_data [Maximum]: #{instances_aggregated_data.max}", :yellow }
        instances_aggregated_data.max
      when 'minimum'
        logger.info { set_color "Instances_aggregated_data [Minimum]: #{instances_aggregated_data.min}", :yellow }
        instances_aggregated_data.min
      else
        avg_array(instances_aggregated_data)
      end
    else
      # Do not scale down - we don't have all the metric data to scale down
    end
  rescue Exception => e
    logger.info { set_color "Error aggregating metrics #{e.message}", :red }
  end
end

#avg_array(data) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/as-combined-metrics/stats.rb', line 90

def avg_array(data)
  logger.progname = "#{Module.nesting.first.to_s} #{__method__}"

  avg = (data.inject {|sum, i| sum + i }) / data.size
  self.logger.info { set_color "Instances_aggregated_data [Average]: #{avg.round}", :white }
  avg.round 
end

#check_combined_metrics(mode) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/as-combined-metrics/stats.rb', line 2

def check_combined_metrics(mode)
  logger.progname = "#{Module.nesting.first.to_s} #{__method__}"

  logger.info { set_color "Checking combined metrics for #{mode}:", :white }
  
  # prepare an array to hold a true / false result for each metric
  results_array = []
  
  # checking each metric by its comparison operator for true / flase
  @combined_metrics.each do |name, values|
    # The line below checks => measure comparison operator value (a <= b) without the use of eval! (it turn the string into evalutation)
    # measure.method(b['comparison_operator']).(threshold)
    if values[:measure] == -1
      evaluation = false
    else
      evaluation = values[:measure].method(values[:comparison_operator]).(values[:threshold])
    end
    results_array << evaluation
    logger.info { set_color "Results for combined metrics for #{name} with #{values}: #{results_array.last}", :magenta }
  end

  # a short hand if statement =>  if all array elements true set combined_metric_value to 1 else to 0
  case mode 
  when :ScaleIn then
    results_array.all? ?  combined_metric_value = 1 :  combined_metric_value  = 0
  when :ScaleOut then
    results_array.any? ?  combined_metric_value = 1 :  combined_metric_value  = 0
  else
    logger.fatal { set_color "Could not find mode #{mode} - check the config file, exiting now...", :red }
    exit
  end
    
  logger.info { set_color "Combined results of #{results_array} for #{mode} is: #{combined_metric_value}", :magenta }

  return combined_metric_value
end