Module: CloudwatchCommon

Includes:
Common
Defined in:
lib/sensu-plugins-aws/cloudwatch-common.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#aws_config, #initialize

Class Method Details

.parse_dimensions(dimension_string) ⇒ Object



86
87
88
89
90
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 86

def self.parse_dimensions(dimension_string)
  dimension_string.split(',')
                  .collect { |d| d.split '=' }
                  .collect { |a| { name: a[0], value: a[1] } }
end

Instance Method Details

#check(config) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 96

def check(config)
  resp = client.get_metric_statistics(metrics_request(config))

  no_data = resp_has_no_data(resp, config[:statistics])
  if no_data && config[:no_data_ok]
    ok "#{metric_desc} returned no data but that's ok"
  elsif no_data && !config[:no_data_ok]
    unknown "#{metric_desc} could not be retrieved"
  end

  value = read_value(resp, config[:statistics])
  base_msg = "#{metric_desc} is #{value}: comparison=#{config[:compare]}"

  if compare value, config[:critical], config[:compare]
    critical "#{base_msg} threshold=#{config[:critical]}"
  elsif config[:warning] && compare(value, config[:warning], config[:compare])
    warning "#{base_msg} threshold=#{config[:warning]}"
  else
    ok "#{base_msg}, will alarm at #{!config[:warning].nil? ? config[:warning] : config[:critical]}"
  end
end

#clientObject



5
6
7
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 5

def client
  Aws::CloudWatch::Client.new
end

#compare(value, threshold, compare_method) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 17

def compare(value, threshold, compare_method)
  case compare_method
  when 'greater'
    value > threshold
  when 'less'
    value < threshold
  when 'not'
    value != threshold
  else
    value == threshold
  end
end

#composite_check(config) ⇒ Object



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
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 56

def composite_check(config)
  fixed_time_now = Time.now
  numerator_metric_resp = client.get_metric_statistics(composite_metrics_request(config, :numerator_metric_name, fixed_time_now))
  denominator_metric_resp = client.get_metric_statistics(composite_metrics_request(config, :denominator_metric_name, fixed_time_now))

  no_data = resp_has_no_data(numerator_metric_resp, config[:statistics]) || \
            resp_has_no_data(denominator_metric_resp, config[:statistics])
  if no_data && config[:no_data_ok]
    ok "#{metric_desc} returned no data but that's ok"
  elsif no_data && !config[:no_data_ok]
    unknown "#{metric_desc} could not be retrieved"
  end

  denominator_value = read_value(denominator_metric_resp, config[:statistics]).to_f
  if denominator_value.zero?
    ok "#{metric_desc} denominator value is zero but that's ok"
  end
  numerator_value = read_value(numerator_metric_resp, config[:statistics]).to_f
  value = (numerator_value / denominator_value * 100).to_i
  base_msg = "#{metric_desc} is #{value}: comparison=#{config[:compare]}"

  if compare(value, config[:critical], config[:compare])
    critical "#{base_msg} threshold=#{config[:critical]}"
  elsif config[:warning] && compare(value, config[:warning], config[:compare])
    warning "#{base_msg} threshold=#{config[:warning]}"
  else
    ok "#{base_msg}, will alarm at #{!config[:warning].nil? ? config[:warning] : config[:critical]}"
  end
end

#composite_metrics_request(config, metric, fixed_time_now = Time.now) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 43

def composite_metrics_request(config, metric, fixed_time_now = Time.now)
  {
    namespace: config[:namespace],
    metric_name: config[metric],
    dimensions: config[:dimensions],
    start_time: fixed_time_now - config[:period] * 10,
    end_time: fixed_time_now,
    period: config[:period],
    statistics: [config[:statistics]],
    unit: config[:unit]
  }
end

#dimension_stringObject



92
93
94
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 92

def dimension_string
  config[:dimensions].map { |d| "#{d[:name]}=#{d[:value]}" }.join('&')
end

#metrics_request(config) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 30

def metrics_request(config)
  {
    namespace: config[:namespace],
    metric_name: config[:metric_name],
    dimensions: config[:dimensions],
    start_time: Time.now - config[:period] * 10,
    end_time: Time.now,
    period: config[:period],
    statistics: [config[:statistics]],
    unit: config[:unit]
  }
end

#read_value(resp, stats) ⇒ Object



9
10
11
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 9

def read_value(resp, stats)
  resp.datapoints.sort_by(&:timestamp).last.send(stats.downcase)
end

#resp_has_no_data(resp, stats) ⇒ Object



13
14
15
# File 'lib/sensu-plugins-aws/cloudwatch-common.rb', line 13

def resp_has_no_data(resp, stats)
  resp.datapoints.nil? || resp.datapoints.empty? || resp.datapoints.first.nil? || read_value(resp, stats).nil?
end