aws-sdk-cloudwatch 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/core'
16
+ require 'aws/cloud_watch/config'
17
+
18
+ module AWS
19
+ class CloudWatch
20
+ AWS::register_autoloads(self, 'aws/cloud_watch') do
21
+ autoload :Client, 'client'
22
+ autoload :Errors, 'errors'
23
+ autoload :Metric, 'metric'
24
+ autoload :MetricBase, 'metric_base'
25
+ autoload :MetricCollection, 'metric_collection'
26
+ autoload :Request, 'request'
27
+ end
28
+
29
+ include Core::ServiceInterface
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/core'
16
+ require 'aws/core/option_grammar/double'
17
+ require 'yaml'
18
+
19
+ module AWS
20
+ class CloudWatch
21
+
22
+ class Client < Core::Client
23
+
24
+ # this is copied from 'aws/core/client'; it's needed,
25
+ # otherwise it searches in the original gem directory
26
+ def self.api_config
27
+ config_file =
28
+ File.dirname(File.dirname(__FILE__)) +
29
+ "/api_config/#{service_name}-#{self::API_VERSION}.yml"
30
+ YAML.load(File.read(config_file))
31
+ end
32
+
33
+ API_VERSION = '2010-08-01'
34
+
35
+ extend Core::Client::QueryXML
36
+
37
+ CACHEABLE_REQUESTS = Set[
38
+ # TODO
39
+ ]
40
+
41
+ define_client_method :delete_alarms, 'DeleteAlarms'
42
+ define_client_method :describe_alarm_history, 'DescribeAlarmHistory'
43
+ define_client_method :describe_alarms, 'DescribeAlarms'
44
+ define_client_method :describe_alarms_for_metric, 'DescribeAlarmsForMetric'
45
+ define_client_method :disable_alarm_actions, 'DisableAlarmActions'
46
+ define_client_method :enable_alarm_actions, 'EnableAlarmActions'
47
+ define_client_method :get_metric_statistics, 'GetMetricStatistics'
48
+ define_client_method :list_metrics, 'ListMetrics'
49
+ define_client_method :put_metric_alarm, 'PutMetricAlarm'
50
+ define_client_method :put_metric_data, 'PutMetricData'
51
+ define_client_method :set_alarm_state, 'SetAlarmState'
52
+
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,19 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ AWS::Core::Configuration.module_eval do
16
+
17
+ add_service 'CloudWatch', 'cloud_watch', 'monitoring.us-east-1.amazonaws.com'
18
+
19
+ end
@@ -0,0 +1,58 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/cloud_watch'
16
+ require 'aws/ec2'
17
+
18
+ module AWS
19
+ class CloudWatch
20
+ module EC2
21
+
22
+ def metrics
23
+ @metrics ||= MetricCollection.new :namespace => 'AWS/EC2', :instance_id => id, :config => config
24
+ end
25
+
26
+ def cpu_utilization *args
27
+ metric = metrics['CPUUtilization']
28
+ if args.length == 0
29
+ metric
30
+ else
31
+ metric.average *args
32
+ end
33
+ end
34
+
35
+ def method_missing name, *args
36
+ if [:disk_read_ops, :disk_write_ops, :network_out, :disk_read_bytes, :network_in, :disk_write_bytes].include?(name)
37
+ metric = metrics[name]
38
+ if args.length == 0
39
+ metric
40
+ else
41
+ metric.sum *args
42
+ end
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
51
+
52
+ require 'aws/ec2/resource'
53
+ require 'aws/ec2/tagged_item'
54
+ require 'aws/ec2/instance'
55
+
56
+ AWS::EC2::Instance.class_eval do
57
+ include AWS::CloudWatch::EC2
58
+ end
@@ -0,0 +1,23 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module AWS
16
+ class CloudWatch
17
+ module Errors
18
+
19
+ extend Core::LazyErrorClasses
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,97 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/core/inflection'
16
+ require 'time'
17
+
18
+ module AWS
19
+ class CloudWatch
20
+ class Metric < MetricBase
21
+
22
+ attr_accessor :unit
23
+
24
+ def options
25
+ super.merge(unit ? {:unit => unit} : {})
26
+ end
27
+
28
+ def attr_names
29
+ super + [:unit]
30
+ end
31
+
32
+ MAX_RECORDS = 1440
33
+
34
+ def get statistics, period, range = Time.now, opts = {}
35
+ statistics = [statistics] unless statistics.kind_of? Array
36
+ unless range.kind_of? Range
37
+ range = (range - period)..range
38
+ single_point = true
39
+ else
40
+ single_point = false
41
+ end
42
+ single_point &&= statistics.length == 1
43
+
44
+ def get_batch statistics, period, range, opts
45
+ options = self.options.merge({
46
+ :end_time => range.end.getutc.iso8601,
47
+ :start_time => range.begin.getutc.iso8601,
48
+ :period => period.to_i,
49
+ :statistics => statistics.map { |s| camel_case(s) } })
50
+ options[:unit] = camel_case(opts[:unit]) if opts[:unit]
51
+ return [] if options[:end_time] == options[:start_time]
52
+
53
+ data = client.get_metric_statistics(options).datapoints
54
+ end
55
+
56
+ if single_point
57
+ data = get_batch statistics, period, range, opts
58
+
59
+ if data.length == 0
60
+ return nil
61
+ else
62
+ return data[0][statistics[0]]
63
+ end
64
+ end
65
+
66
+ return [] if range.begin + period > range.end
67
+
68
+ max_period = MAX_RECORDS * period / statistics.length
69
+
70
+ ranges = [range]
71
+ while (ranges[-1].end - ranges[-1].begin) > max_period
72
+ range = ranges.pop
73
+ pivot = range.begin + max_period
74
+ ranges.push range.begin..pivot
75
+ ranges.push pivot..range.end
76
+ end
77
+
78
+ ranges.map{|range| get_batch statistics, period, range, opts}.flatten.sort{|a, b| a[:timestamp] <=> b[:timestamp]}
79
+ end
80
+
81
+ def method_missing name, *args
82
+ if [:average, :sum, :sample_count, :maximum, :minimum].include?(name)
83
+ data = get name, *args
84
+ if data.kind_of? Array
85
+ data.each do |point|
86
+ point[:value] = point[name]
87
+ end
88
+ end
89
+ data
90
+ else
91
+ super
92
+ end
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,100 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'aws/core/inflection'
16
+
17
+ module AWS
18
+ class CloudWatch
19
+
20
+ # @private
21
+ class MetricBase
22
+ include Core::Model
23
+
24
+ def initialize options
25
+ super
26
+ @dimensions = {}
27
+ set_attributes options
28
+ end
29
+
30
+ def set_attributes attrs
31
+ attrs.keys.each do |k|
32
+ key = k
33
+ key = :name if key == :metric_name
34
+ if key.kind_of? Symbol
35
+ send("#{key}=", attrs[k]) unless key == :config
36
+ else
37
+ dimensions[key] = attrs[k]
38
+ end
39
+ end
40
+ end
41
+
42
+ attr_accessor :name, :namespace
43
+ attr_reader :dimensions
44
+
45
+ def dimensions= value
46
+ if value.kind_of? Array
47
+ hash = {}
48
+ value.each do |entry|
49
+ hash[entry[:name]] = entry[:value]
50
+ end
51
+ value = hash
52
+ end
53
+
54
+ @dimensions = value
55
+ end
56
+
57
+ def attr_names
58
+ [:dimensions, :name, :namespace]
59
+ end
60
+
61
+ def method_missing name, *args
62
+ if args.length == 1 && name.to_s =~ /^(.*)=/ then
63
+ @dimensions[$1.to_sym] = args[0]
64
+ else
65
+ super
66
+ end
67
+ end
68
+
69
+ def options
70
+ opts = !dimensions.empty? ? {:dimensions =>
71
+ dimensions.keys.map {|key|
72
+ { :name => camel_case(key),
73
+ :value => dimensions[key] }}
74
+ } : {}
75
+
76
+ opts[:metric_name] = name if name
77
+ opts[:namespace] = namespace if namespace
78
+ opts
79
+ end
80
+
81
+ def camel_case s
82
+ Core::Inflection.class_name s.to_s
83
+ end
84
+
85
+ def to_sym s
86
+ Core::Inflection.ruby_name(s.to_s).to_sym
87
+ end
88
+
89
+ def inspect
90
+ attrs = []
91
+ attr_names.each do |attr|
92
+ val = self.send attr
93
+ attrs << "#{attr}:#{val.inspect}" if val
94
+ end
95
+
96
+ "<#{self::class} #{attrs.join ' '}>"
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,48 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module AWS
16
+ class CloudWatch
17
+ class MetricCollection < MetricBase
18
+
19
+ include Enumerable
20
+
21
+ def each &block
22
+
23
+ next_token = nil
24
+
25
+ begin
26
+
27
+ list_options = options.merge(next_token ? { :next_token => next_token } : {})
28
+ response = client.list_metrics(list_options)
29
+
30
+ response.metrics.each do |t|
31
+ metric = Metric.new(t.merge :config => config)
32
+ yield(metric)
33
+ end
34
+
35
+ end while(next_token = response.data[:next_token])
36
+ nil
37
+
38
+ end
39
+
40
+ def get_by_name name
41
+ Metric.new(options.merge(:name => camel_case(name), :config => config))
42
+ end
43
+
44
+ alias_method :[], :get_by_name
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ # Copyright 2012 Inscitiv
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module AWS
16
+ class CloudWatch
17
+
18
+ # @private
19
+ class Request < Core::Http::Request
20
+
21
+ include Core::Signature::Version4
22
+
23
+ def service
24
+ 'monitoring'
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end