Class: ZabbixCloudwatch::GetCloudwatchMetric

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix-cloudwatch.rb

Defined Under Namespace

Classes: AwsAccessKeyMissingException, AwsSecretKeyMissingException, BadAWSAccessKeysException, DimensionArgumentMissingException, MetricnameArgumentMissingException, MonitoringTypeArgumentException, NamespaceArgumentMissingException, StatisticTypeArgumentException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ GetCloudwatchMetric

Returns a new instance of GetCloudwatchMetric.



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

def initialize options
  self.options = options
  usage if options.key?"help"
  raise NamespaceArgumentMissingException unless options.key?"namespace"
  raise MetricnameArgumentMissingException unless options.key?"metricname"
  raise DimensionArgumentMissingException unless options.key?"dimension-name"
  raise DimensionArgumentMissingException unless options.key?"dimension-value"
  self.aws = AWS::CloudWatch.new(get_aws_options).client
  test_aws_connectivity
  set_time_range
  set_statistic
end

Instance Attribute Details

#awsObject

Returns the value of attribute aws.



15
16
17
# File 'lib/zabbix-cloudwatch.rb', line 15

def aws
  @aws
end

#end_timeObject

Returns the value of attribute end_time.



15
16
17
# File 'lib/zabbix-cloudwatch.rb', line 15

def end_time
  @end_time
end

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/zabbix-cloudwatch.rb', line 15

def options
  @options
end

#periodObject

Returns the value of attribute period.



15
16
17
# File 'lib/zabbix-cloudwatch.rb', line 15

def period
  @period
end

#start_timeObject

Returns the value of attribute start_time.



15
16
17
# File 'lib/zabbix-cloudwatch.rb', line 15

def start_time
  @start_time
end

#statisticObject

Returns the value of attribute statistic.



15
16
17
# File 'lib/zabbix-cloudwatch.rb', line 15

def statistic
  @statistic
end

Instance Method Details

#get_aws_optionsObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zabbix-cloudwatch.rb', line 30

def get_aws_options
  raise AwsAccessKeyMissingException unless options.key?"aws-access-key"
  raise AwsSecretKeyMissingException unless options.key?"aws-secret-key"
  if options.key?"aws-region" && options['aws-region'] != ''
    region = options["aws-region"]
  else
    region = 'us-east-1'
  end
  puts options.inspect
  {:access_key_id => options["aws-access-key"], :secret_access_key => options["aws-secret-key"], :region => region}
end

#run!Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/zabbix-cloudwatch.rb', line 89

def run!
  ret = aws.get_metric_statistics({
          :namespace => options["namespace"],
          :metric_name => options["metricname"],
          :dimensions => [{:name => options["dimension-name"],:value => options["dimension-value"]}],
          :period => period,
          :start_time => start_time,
          :end_time => end_time,
          :statistics => [statistic]})
  begin
    symbol = statistic.downcase.to_sym
    symbol = :sample_count if symbol == :samplecount
    puts ret[:datapoints][0][symbol]
  rescue
    exit 1
  end
end

#set_statisticObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zabbix-cloudwatch.rb', line 42

def set_statistic
  unless options.key?"statistic"
    self.statistic = "Average"
  else
    if options["statistic"] =~ /Minimum|Maximum|Average|Sum|SampleCount/
      self.statistic = options["statistic"]
    else
      raise StatisticTypeArgumentException, "Statistic type must be one of: Minimum, Maximum, Average, Sum, SampleCount. "
    end
  end
end

#set_time_rangeObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zabbix-cloudwatch.rb', line 54

def set_time_range
  unless options.key?"monitoring-type"
    detailed = false
  else
    if options["monitoring-type"] =~ /detailed|basic/
      detailed = true if options["monitoring-type"] == 'detailed'
      detailed = false if options["monitoring-type"] == 'basic'
    else
      raise MonitoringTypeArgumentException, "Monitoring type must be either 'detailed' or 'basic'. "
    end
  end
  if detailed
    self.start_time = time_one_minute_ago
    self.period = 60
  else
    self.start_time = time_five_minutes_ago
    self.period = 360
  end
  self.end_time = time_now
end

#test_aws_connectivityObject



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/zabbix-cloudwatch.rb', line 107

def test_aws_connectivity
  begin
    aws.describe_alarms(:max_records => 1)
  rescue
    raise BadAWSAccessKeysException, <<-EOF
  
  You cannot access AWS due to one of the following reasons:
- The AWS keys provided do not have access to Cloudwatch
- your server is not synced with NTP
- The Region setting is missing or incorrect
      EOF
  end
end

#time_five_minutes_agoObject



84
85
86
87
# File 'lib/zabbix-cloudwatch.rb', line 84

def time_five_minutes_ago
  # Not really 5 minutes ago, but adds a bit of buffer for amazon's silliness 
  (Time.now - (60*7+30)).utc.iso8601
end

#time_nowObject



75
76
77
# File 'lib/zabbix-cloudwatch.rb', line 75

def time_now
  Time.now.utc.iso8601
end

#time_one_minute_agoObject



79
80
81
82
# File 'lib/zabbix-cloudwatch.rb', line 79

def time_one_minute_ago
  # Not really 1 minute ago, but adds a bit of buffer for amazon's silliness 
  (Time.now - 90).utc.iso8601
end