atsd 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ require 'atsd/services/base_service'
2
+ require 'atsd/models/alert'
3
+ require 'atsd/models/alert_history'
4
+ require 'atsd/queries/alerts_query'
5
+ require 'atsd/queries/alerts_history_query'
6
+
7
+ module ATSD
8
+ class AlertsService < BaseService
9
+ # Create query builder for alerts.
10
+ #
11
+ # @param [Hash] options query parameters
12
+ # @return [AlertsQuery]
13
+ def query(options = {})
14
+ query = AlertsQuery.new @client
15
+ options.each { |option, value| query[option] = value }
16
+ query
17
+ end
18
+
19
+ # Acknowledge alerts
20
+ #
21
+ # @param [Array<Hash, Alert, Integer>, Hash, Alert, Integer] alerts
22
+ # @return [self]
23
+ # @raise [APIError]
24
+ def acknowledge(alerts)
25
+ alerts_acknowledge alerts, true
26
+ end
27
+
28
+ # De-acknowledge alerts
29
+ #
30
+ # @param [Array<Hash, Alert, Integer>, Hash, Alert, Integer] alerts
31
+ # @return [self]
32
+ # @raise [APIError]
33
+ def de_acknowledge(alerts)
34
+ alerts_acknowledge alerts, false
35
+ end
36
+
37
+ # Delete alerts
38
+ #
39
+ # @param [Array<Hash, Alert>, Hash, Alert] alerts
40
+ # @return [self]
41
+ # @raise [APIError]
42
+ def delete(alerts)
43
+ alerts = Utils.ensure_array(alerts).map do |alert|
44
+ { :id => id_for_alert(alert) }
45
+ end
46
+ return if alerts.count == 0
47
+ @client.alerts_update :action => 'delete',
48
+ :alerts => alerts
49
+ end
50
+
51
+ # Create query builder for alert history.
52
+ #
53
+ # @param [Hash] options query parameters
54
+ # @return [AlertsHistoryQuery]
55
+ def history_query(options = {})
56
+ query = AlertsHistoryQuery.new @client
57
+ options.each { |option, value| query[option] = value }
58
+ query
59
+ end
60
+
61
+ private
62
+
63
+ def alerts_acknowledge(alerts, acknowledge)
64
+ alerts = Utils.ensure_array(alerts).map do |alert|
65
+ alert[:acknowledged] = acknowledge if alert.is_a? Hash
66
+ { :id => id_for_alert(alert) }
67
+ end
68
+ return if alerts.count == 0
69
+ @client.alerts_update :action => 'update',
70
+ :fields => {:acknowledge => acknowledge},
71
+ :alerts => alerts
72
+ self
73
+ end
74
+
75
+ def id_for_alert(alert)
76
+ case alert
77
+ when Integer
78
+ alert
79
+ when Alert
80
+ alert.id
81
+ when Hash
82
+ alert[:id] || alert['id']
83
+ else
84
+ alert.id
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,15 @@
1
+ module ATSD
2
+ # Base class for all services
3
+ # @abstract
4
+ class BaseService
5
+
6
+ # @param [Client] client ATSD client
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ protected
12
+
13
+ attr_reader :client
14
+ end
15
+ end
@@ -0,0 +1,130 @@
1
+ require 'atsd/services/base_service'
2
+ require 'atsd/models/entity'
3
+ require 'atsd/models/metric'
4
+
5
+ module ATSD
6
+ # Entities service
7
+ class EntitiesService < BaseService
8
+
9
+ # Entities list
10
+ #
11
+ # @option options [Boolean] :active
12
+ # Filter entities by last insert time. If `active = true`,
13
+ # only entities with positive `last_insert_time` are included in the response
14
+ # @option options [String] :expression
15
+ # Use `name` variable for entity name. Use `*` placeholder in `like` expresions
16
+ # @option options [Array<String>] :tags
17
+ # Specify entity tags to be included in the response
18
+ # @option options [Integer] :limit
19
+ # Limit response to first N entities, ordered by name.
20
+ # @return [Array<Entity>]
21
+ # @raise [APIError]
22
+ def list(options = {})
23
+ options = options.camelize_keys
24
+ @client.entities_list(options).map do |json|
25
+ Entity.new json
26
+ end
27
+ end
28
+
29
+ # Displays entity properties and all tags.
30
+ #
31
+ # @param [String, Entity] entity
32
+ # @return [Entity]
33
+ # @raise [APIError]
34
+ def get(entity)
35
+ Entity.new(@client.entities_get(name_for_entity entity))
36
+ end
37
+
38
+ # Create an entity with specified properties and tags
39
+ # or replace an existing entity. This method creates a new
40
+ # entity or replaces an existing entity.
41
+ #
42
+ # @note If only a subset of fields is provided for an existing entity,
43
+ # the remaining properties will be set to default values and tags
44
+ # will be deleted.
45
+ #
46
+ # @param [Hash, Entity] entity
47
+ # @return [self]
48
+ # @raise [APIError]
49
+ def create_or_replace(entity)
50
+ entity = Entity.new(entity) if entity.is_a? Hash
51
+ name = name_for_entity entity
52
+ raise ArgumentError unless name
53
+ @client.entities_create_or_replace(name, entity.to_request_hash)
54
+ self
55
+ end
56
+
57
+ # Update specified properties and tags for the given entity.
58
+ # @note updates specified properties and tags for an existing entity.
59
+ # Properties and tags that are not specified are left unchanged.
60
+ #
61
+ # @param [Hash, Entity] entity
62
+ # @return [self]
63
+ # @raise [APIError]
64
+ def update(entity)
65
+ entity = Entity.new(entity) if entity.is_a? Hash
66
+ @client.entities_update(name_for_entity(entity), entity.to_request_hash)
67
+ self
68
+ end
69
+
70
+ # Delete the entity. Delete the entity from any Entity Groups that it
71
+ # belongs to. Data collected by the entity will be removed asynchronously
72
+ # in the background.
73
+ #
74
+ # @param [Hash, Entity, String] entity entity or name
75
+ # @return [self]
76
+ # @raise [APIError]
77
+ def delete(entity)
78
+ @client.entities_delete(name_for_entity entity)
79
+ self
80
+ end
81
+
82
+ # Returns an array of property types for the entity.
83
+ #
84
+ # @param [String, Hash, Entity] entity
85
+ # @param [Integer, Time] start_time
86
+ # Return only property types that have been collected after the specified time.
87
+ # @return [Array<String>]
88
+ # @raise [APIError]
89
+ def property_types(entity, start_time = nil)
90
+ start_time = start_time.to_i * 1000 if start_time.is_a? Time
91
+ params = start_time ? { :start_time => start_time } : {}
92
+ @client.entities_property_types(name_for_entity(entity), params)
93
+ end
94
+
95
+ # Returns a list of metrics collected by the entity.
96
+ # The list is based on memory cache which is rebuilt on ATSD restart.
97
+ #
98
+ # @param [String, Hash, Entity] entity
99
+ # @option options [Boolean] :active
100
+ # Filter metrics by last_insert_time. If active = true,
101
+ # only metrics with positive last_insert_time are included in the response
102
+ # @option options [Array<String>] :tags
103
+ # Specify metric tags to be included in the response
104
+ # @option options [Integer] :limit
105
+ # Limit response to first N metrics, ordered by name.
106
+ # @return [Array<Metric>]
107
+ # @raise [APIError]
108
+ def metrics(entity, options = {})
109
+ options = options.camelize_keys
110
+ @client.entities_metrics(name_for_entity(entity), options).map do |json|
111
+ Metric.new json
112
+ end
113
+ end
114
+
115
+ private
116
+
117
+ def name_for_entity(entity)
118
+ case entity
119
+ when String
120
+ entity
121
+ when Hash
122
+ entity[:name] || entity['name']
123
+ when Entity
124
+ entity.name? ? entity.name : nil
125
+ else
126
+ entity.name
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,186 @@
1
+ require 'atsd/services/base_service'
2
+ require 'atsd/models/entity_group'
3
+
4
+ module ATSD
5
+ class EntityGroupsService < BaseService
6
+ # Entity groups list.
7
+ #
8
+ # @option parameters [String] :expression
9
+ # Use name variable for entity group name. Use * placeholder
10
+ # in like expressions
11
+ # @option parameters [Array] :tags
12
+ # Specify entity group tags to be included in the response
13
+ # @option parameters [Integer] :limit
14
+ # Limit response to first N entity groups, ordered by name.
15
+ # @return [Array<EntityGroup>]
16
+ # @raise [APIError]
17
+ def list(parameters = {})
18
+ parameters = parameters.camelize_keys
19
+ @client.entity_groups_list(parameters).map do |json|
20
+ EntityGroup.new json
21
+ end
22
+ end
23
+
24
+ # Displays entity group properties and all tags.
25
+ #
26
+ # @param [String] entity_group
27
+ # @return [Array<EntityGroup>]
28
+ # @raise [APIError]
29
+ def get(entity_group)
30
+ EntityGroup.new(@client.entity_groups_get(name_for_entity_group entity_group))
31
+ end
32
+
33
+ # Create an entity group with specified properties and tags
34
+ # or replace an existing entity group. This method creates
35
+ # a new entity group or replaces an existing entity group.
36
+ #
37
+ # @note If only a subset of fields is provided for an
38
+ # existing entity group, the remaining properties and
39
+ # tags will be deleted.
40
+ #
41
+ # @param [Hash, EntityGroup, String] entity_group
42
+ # @return [self]
43
+ # @raise [APIError]
44
+ def create_or_replace(entity_group)
45
+ entity_group = EntityGroup.new(name: entity_group) if entity_group.is_a? String
46
+ entity_group = EntityGroup.new(entity_group) if entity_group.is_a? Hash
47
+ @client.entity_groups_create_or_replace(entity_group.name, entity_group.to_request_hash)
48
+ self
49
+ end
50
+
51
+ # Update specified properties and tags for the given entity
52
+ # group. This method updates specified properties and tags
53
+ # for an existing entity group.
54
+ #
55
+ # @note Properties and tags that are not specified are left
56
+ # unchanged.
57
+ #
58
+ # @param [Hash, EntityGroup] entity_group
59
+ # @return [self]
60
+ # @raise [APIError]
61
+ def update(entity_group)
62
+ entity_group = EntityGroup.new(entity_group) if entity_group.is_a? Hash
63
+ @client.entity_groups_update(entity_group.name, entity_group.to_request_hash)
64
+ self
65
+ end
66
+
67
+ # Delete the entity group.
68
+ #
69
+ # @note Entities that are members of the group are retained.
70
+ #
71
+ # @param [String, EntityGroup, Hash] entity_group
72
+ # @return [self]
73
+ # @raise [APIError]
74
+ def delete(entity_group)
75
+ @client.entity_groups_delete(name_for_entity_group(entity_group))
76
+ self
77
+ end
78
+
79
+ # Get entities for entity group
80
+ #
81
+ # @param [String, EntityGroup, Hash] entity_group
82
+ # @option parameters [Boolean] :active
83
+ # Filter entities by last_insert_time. If active = true, only entities
84
+ # with positive last_insert_time are included in the response
85
+ # @option parameters [String] :expression
86
+ # Use name variable for entity name. Use * placeholder in like expressions
87
+ # @option parameters [Array] :tags
88
+ # Specify entity tags to be included in the response
89
+ # @option parameters [Integer] :limit
90
+ # Limit response to first N entities, ordered by name.
91
+ # @return [Array<Entity>]
92
+ # @raise [APIError]
93
+ def entities(entity_group, parameters = {})
94
+ entity_group = name_for_entity_group(entity_group)
95
+ parameters = parameters.camelize_keys
96
+ @client.entity_groups_get_entities(entity_group, parameters).map do |json|
97
+ Entity.new json
98
+ end
99
+ end
100
+
101
+ # Add specified entities to entity group.
102
+ #
103
+ # @param [String, EntityGroup, Hash] entity_group
104
+ # @param [Hash, Entity, Array<Hash>, Array<Entity>] entities
105
+ # @option parameters [Boolean] :create_entities
106
+ # Automatically create new entities from the submitted list
107
+ # if such entities don’t already exist. Default value: true
108
+ # @return [self]
109
+ # @raise [APIError]
110
+ def add_entities(entity_group, entities, parameters = {})
111
+ entity_group = name_for_entity_group(entity_group)
112
+ parameters = parameters.camelize_keys
113
+ entities = Utils.ensure_array(entities).map do |e|
114
+ e = Entity.new(name: e) if e.is_a? String
115
+ e = Entity.new(e) if e.is_a? Hash
116
+ e = e.to_request_hash
117
+ end
118
+ @client.entity_groups_add_entities(entity_group, entities, parameters)
119
+ self
120
+ end
121
+
122
+ # Replace entities in the entity group with the specified collection.
123
+ #
124
+ # @note All existing entities that are not included in the collection
125
+ # will be removed. If the specified collection is empty, all entities
126
+ # are removed from the group (replace with empty collection).
127
+ #
128
+ # @param [String, EntityGroup, Hash] entity_group
129
+ # @param [Hash, Entity, Array<Hash>, Array<Entity>] entities
130
+ # @return [self]
131
+ # @raise [APIError]
132
+ def replace_entities(entity_group, entities)
133
+ entity_group = name_for_entity_group(entity_group)
134
+ entities = Utils.ensure_array(entities).map do |e|
135
+ e = Entity.new(name: e) if e.is_a? String
136
+ e = Entity.new(e) if e.is_a? Hash
137
+ e = e.to_request_hash
138
+ end
139
+ @client.entity_groups_replace_entities(entity_group, entities)
140
+ self
141
+ end
142
+
143
+ # Delete entities from entity group.
144
+ #
145
+ # @param [String, EntityGroup, Hash] entity_group
146
+ # @param [Hash, Entity, Array<Hash>, Array<Entity>] entities
147
+ # @return [self]
148
+ # @raise [APIError]
149
+ def delete_entities(entity_group, entities)
150
+ entity_group = name_for_entity_group(entity_group)
151
+ entities = Utils.ensure_array(entities).map do |e|
152
+ e = Entity.new(name: e) if e.is_a? String
153
+ e = Entity.new(e) if e.is_a? Hash
154
+ e = e.to_request_hash
155
+ end
156
+ @client.entity_groups_delete_entities(entity_group, entities)
157
+ self
158
+ end
159
+
160
+ # Delete all entities from entity group.
161
+ #
162
+ # @param [String, EntityGroup, Hash] entity_group
163
+ # @return [self]
164
+ # @raise [APIError]
165
+ def delete_all_entities(entity_group)
166
+ entity_group = name_for_entity_group(entity_group)
167
+ @client.entity_groups_delete_all_entities(entity_group)
168
+ self
169
+ end
170
+
171
+ private
172
+
173
+ def name_for_entity_group(entity_group)
174
+ case entity_group
175
+ when String
176
+ entity_group
177
+ when Hash
178
+ entity_group[:name] || entity_group['name']
179
+ when EntityGroup
180
+ entity_group.name? ? entity_group.name : nil
181
+ else
182
+ entity_group.name
183
+ end
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,122 @@
1
+ require 'atsd/services/base_service'
2
+ require 'atsd/models/metric'
3
+ require 'atsd/models/entity'
4
+
5
+ module ATSD
6
+ class MetricsService < BaseService
7
+ # Metrics list.
8
+ #
9
+ # @option parameters [String] :expression
10
+ # Use name variable for metric name. Use * placeholder
11
+ # in like expressions
12
+ # @option parameters [Boolean] :active
13
+ # Filter metrics by last_insert_time. If active = true,
14
+ # only metrics with positive last_insert_time are included in the response
15
+ # @option parameters [Array] :tags
16
+ # Specify metric tags to be included in the response
17
+ # @option parameters [Integer] :limit
18
+ # Limit response to first N metrics, ordered by name.
19
+ # @return [Array<Metric>]
20
+ # @raise [APIError]
21
+ def list(parameters = {})
22
+ parameters = parameters.camelize_keys
23
+ @client.metrics_list(parameters).map do |json|
24
+ Metric.new json
25
+ end
26
+ end
27
+
28
+ # Displays metric properties and its tags.
29
+ #
30
+ # @param [String] metric
31
+ # @return [Array<Metric>]
32
+ # @raise [APIError]
33
+ def get(metric)
34
+ Metric.new(@client.metrics_get(name_for_metric metric))
35
+ end
36
+
37
+ # Create a metric with specified properties and tags or replace
38
+ # an existing metric. This method creates a new metric or replaces
39
+ # an existing metric.
40
+ #
41
+ # @note If only a subset of fields is provided for an existing metric,
42
+ # the remaining properties and tags will be deleted.
43
+ #
44
+ # @param [Hash, Metric, String] metric
45
+ # @return [self]
46
+ # @raise [APIError]
47
+ def create_or_replace(metric)
48
+ metric = Metric.new(name: metric) if metric.is_a? String
49
+ metric = Metric.new(metric) if metric.is_a? Hash
50
+ @client.metrics_create_or_replace(metric.name, metric.to_request_hash)
51
+ self
52
+ end
53
+
54
+ # Update specified properties and tags for the given metric.
55
+ # This method updates specified properties and tags for an existing metric.
56
+ #
57
+ # @note Properties and tags that are not specified are left unchanged.
58
+ #
59
+ # @param [Hash, Metric] metric
60
+ # @return [self]
61
+ # @raise [APIError]
62
+ def update(metric)
63
+ metric = Metric.new(metric) if metric.is_a? Hash
64
+ @client.metrics_update(metric.name, metric.to_request_hash)
65
+ self
66
+ end
67
+
68
+ # Delete the metric. Data collected for the metric will be removed
69
+ # asynchronously in the background.
70
+ #
71
+ # @param [String, Metric, Hash] metric
72
+ # @return [self]
73
+ # @raise [APIError]
74
+ def delete(metric)
75
+ @client.metrics_delete(name_for_metric(metric))
76
+ self
77
+ end
78
+
79
+ # Returns a list of unique series tags for the metric. The list is
80
+ # based on data stored on disk for the last 24 hours.
81
+ #
82
+ # @param [Hash, Metric, String] metric
83
+ # @param [Hash, Entity, String] entity
84
+ # @return [Array<Entity>]
85
+ # @raise [APIError]
86
+ def entity_and_tags(metric, entity = nil)
87
+ metric = name_for_metric(metric)
88
+ params = {}
89
+ params[:entity] = name_for_entity(entity) if entity
90
+ result = @client.metrics_entity_and_tags(metric, params)
91
+ result.map { |json| Entity.new json }
92
+ end
93
+
94
+ private
95
+
96
+ def name_for_metric(metric)
97
+ case metric
98
+ when String
99
+ metric
100
+ when Hash
101
+ metric[:name] || metric['name']
102
+ when Metric
103
+ metric.name? ? metric.name : nil
104
+ else
105
+ metric.name
106
+ end
107
+ end
108
+
109
+ def name_for_entity(entity)
110
+ case entity
111
+ when String
112
+ entity
113
+ when Hash
114
+ entity[:name] || entity['name']
115
+ when Entity
116
+ entity.name? ? entity.name : nil
117
+ else
118
+ entity.name
119
+ end
120
+ end
121
+ end
122
+ end