Class: Hawkular::Alerts::Client

Inherits:
BaseClient show all
Defined in:
lib/hawkular/alerts/alerts_api.rb

Overview

Interface to use to talk to the Hawkular-Alerts component.

Instance Attribute Summary

Attributes inherited from BaseClient

#tenants

Instance Method Summary collapse

Methods inherited from BaseClient

#admin_header, #base_64_credentials, #generate_query_params, #http_delete, #http_get, #http_post, #http_put, #normalize_entrypoint_url, #now, #url_with_websocket_scheme

Methods included from ClientUtils

#hawk_escape, #hawk_escape_id

Constructor Details

#initialize(entrypoint, credentials = {}, options = {}) ⇒ Client

Returns a new instance of Client



17
18
19
20
21
22
# File 'lib/hawkular/alerts/alerts_api.rb', line 17

def initialize(entrypoint, credentials = {}, options = {})
  entrypoint = normalize_entrypoint_url entrypoint, 'hawkular/alerts'
  @entrypoint = entrypoint

  super(entrypoint, credentials, options)
end

Instance Method Details

#acknowledge_alert(alert_id, by = nil, comment = nil) ⇒ Object

Mark one alert as acknowledged

Parameters:

  • alert_id (String)

    Id of the alert to ack

  • by (String) (defaults to: nil)

    name of the user acknowledging the alert

  • comment (String) (defaults to: nil)

    A comment on the acknowledge



292
293
294
295
296
297
298
299
# File 'lib/hawkular/alerts/alerts_api.rb', line 292

def acknowledge_alert(alert_id, by = nil, comment = nil)
  sub_url = "/ack/#{alert_id}"
  query = generate_query_params 'ackBy' => by, 'ackNotes' => comment
  sub_url += query
  http_put(sub_url, {})

  true
end

#add_tags(alert_ids, tags) ⇒ Object

Add tags to existing Alerts.

Parameters:

  • alert_ids (Array<numeric>)

    List of alert IDs

  • tags (Array<String>)

    List of tags. Each tag of format 'name|value'.



356
357
358
359
# File 'lib/hawkular/alerts/alerts_api.rb', line 356

def add_tags(alert_ids, tags)
  query = generate_query_params(alertIds: alert_ids, tags: tags)
  http_put('/tags' + query, {})
end

#alerts(criteria: {}, tenants: nil) ⇒ Array<Alert>

List fired alerts

Parameters:

  • criteria (Hash)

    optional query criteria

  • tenants (Array)

    optional list of tenants. The elements of the array can be any object convertible to a string

Returns:

  • (Array<Alert>)

    List of alerts in the system. Can be empty



257
258
259
260
261
262
263
264
# File 'lib/hawkular/alerts/alerts_api.rb', line 257

def alerts(criteria: {}, tenants:nil)
  query = generate_query_params(criteria)
  uri = tenants ? '/admin/alerts/' : '/'
  ret = http_get(uri + query, multi_tenants_header(tenants))
  val = []
  ret.each { |a| val.push(Alert.new(a)) }
  val
end

#bulk_import_triggers(hash) ⇒ Hash

Import multiple trigger or action definitions specified as a hash to the server.

Parameters:

  • hash (Hash)

    The hash with the trigger and action definitions. see the git.io/va5UO for more details about the structure

Returns:

  • (Hash)

    The newly entities as hash



71
72
73
# File 'lib/hawkular/alerts/alerts_api.rb', line 71

def bulk_import_triggers(hash)
  http_post 'import/all', hash
end

#create_action(plugin, action_id, properties = {}) ⇒ Action

Creates the action.

Parameters:

  • plugin (String)

    The id of action definition/plugin

  • action_id (String)

    The id of action

  • properties (Hash) (defaults to: {})

    Troperties of action

Returns:

  • (Action)

    The newly created action



202
203
204
205
206
207
208
209
210
# File 'lib/hawkular/alerts/alerts_api.rb', line 202

def create_action(plugin, action_id, properties = {})
  the_plugin = hawk_escape plugin
  # Check if plugin exists
  http_get("/plugins/#{the_plugin}")

  payload = { actionId: action_id, actionPlugin: plugin, properties: properties }
  ret = http_post('/actions', payload)
  Trigger::Action.new(ret)
end

#create_event(id, category, text, extras) ⇒ Object

Inject an event into Hawkular-alerts

Parameters:

  • id (String)

    Id of the event must be unique

  • category (String)

    Event category for further distinction

  • text (String)

    Some text to the user

  • extras (Hash<String,Object>)

    additional parameters



338
339
340
341
342
343
344
345
346
347
# File 'lib/hawkular/alerts/alerts_api.rb', line 338

def create_event(id, category, text, extras)
  event = {}
  event['id'] = id
  event['ctime'] = Time.now.to_i * 1000
  event['category'] = category
  event['text'] = text
  event.merge!(extras) { |_key, v1, _v2| v1 }

  http_post('/events', event)
end

#create_group_dampening(dampening) ⇒ Dampening

Creates a dampening for a group trigger

Parameters:

  • dampening (Dampening)

    the dampening to create

Returns:

  • (Dampening)

    the newly created dampening



150
151
152
153
# File 'lib/hawkular/alerts/alerts_api.rb', line 150

def create_group_dampening(dampening)
  ret = http_post "triggers/groups/#{dampening.trigger_id}/dampenings", dampening.to_h
  Trigger::Dampening.new(ret)
end

#create_group_trigger(trigger) ⇒ Trigger

Creates the group trigger definition.

Parameters:

  • trigger (Trigger)

    The group trigger to be created

Returns:

  • (Trigger)

    The newly created group trigger



96
97
98
99
# File 'lib/hawkular/alerts/alerts_api.rb', line 96

def create_group_trigger(trigger)
  ret = http_post 'triggers/groups', trigger.to_h
  Trigger.new(ret)
end

#create_member_trigger(group_member_info) ⇒ Trigger

Creates a member trigger

Parameters:

  • group_member_info (GroupMemberInfo)

    the group member to be added

Returns:

  • (Trigger)

    the newly created member trigger



125
126
127
128
# File 'lib/hawkular/alerts/alerts_api.rb', line 125

def create_member_trigger(group_member_info)
  ret = http_post 'triggers/groups/members', group_member_info.to_h
  Trigger.new(ret)
end

#create_trigger(trigger, conditions = [], dampenings = [], _actions = []) ⇒ Trigger

Creates the trigger definition.

Parameters:

  • trigger (Trigger)

    The trigger to be created

  • conditions (Array<Condition>) (defaults to: [])

    Array of associated conditions

  • dampenings (Array<Dampening>) (defaults to: [])

    Array of associated dampenings

Returns:

  • (Trigger)

    The newly created trigger



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hawkular/alerts/alerts_api.rb', line 80

def create_trigger(trigger, conditions = [], dampenings = [], _actions = [])
  full_trigger = {}
  full_trigger[:trigger] = trigger.to_h
  conds = []
  conditions.each { |c| conds.push(c.to_h) }
  full_trigger[:conditions] = conds
  damps = []
  dampenings.each { |d| damps.push(d.to_h) } unless dampenings.nil?
  full_trigger[:dampenings] = damps

  http_post 'triggers/trigger', full_trigger
end

#delete_action(plugin, action_id) ⇒ Object

Deletes the action of given action plugin.

Parameters:

  • plugin (String)

    Id of the action plugin

  • action_id (String)

    Id of the action



226
227
228
229
230
# File 'lib/hawkular/alerts/alerts_api.rb', line 226

def delete_action(plugin, action_id)
  the_plugin = hawk_escape plugin
  the_action_id = hawk_escape action_id
  http_delete "/actions/#{the_plugin}/#{the_action_id}"
end

#delete_event(id) ⇒ Object



349
350
351
# File 'lib/hawkular/alerts/alerts_api.rb', line 349

def delete_event(id)
  http_delete "/events/#{id}"
end

#delete_group_dampening(trigger_id, dampening_id) ⇒ Object

Deletes the dampening of a group trigger

Parameters:

  • trigger_id (String)

    ID of the group trigger

  • dampening_id (String)

    ID



166
167
168
# File 'lib/hawkular/alerts/alerts_api.rb', line 166

def delete_group_dampening(trigger_id, dampening_id)
  http_delete "/triggers/groups/#{trigger_id}/dampenings/#{dampening_id}"
end

#delete_group_trigger(trigger_id) ⇒ Object

Deletes the group trigger definition.

Parameters:

  • trigger_id (String)

    ID of the group trigger to delete



178
179
180
# File 'lib/hawkular/alerts/alerts_api.rb', line 178

def delete_group_trigger(trigger_id)
  http_delete "/triggers/groups/#{trigger_id}"
end

#delete_trigger(trigger_id) ⇒ Object

Deletes the trigger definition.

Parameters:

  • trigger_id (String)

    ID of the trigger to delete



172
173
174
# File 'lib/hawkular/alerts/alerts_api.rb', line 172

def delete_trigger(trigger_id)
  http_delete "/triggers/#{trigger_id}"
end

#events(criteria: {}, tenants: nil) ⇒ Array<Event>

List Events given optional criteria. Criteria keys are strings (not symbols):

startTime   numeric, milliseconds from epoch
endTime     numeric, milliseconds from epoch
eventIds    array of strings
triggerIds  array of strings
categories  array of strings
tags        array of strings, each tag of format 'name|value'. Specify '*' for value to match all values
thin        boolean, return lighter events (omits triggering data for trigger-generated events)

Parameters:

  • criteria (Hash)

    optional query criteria

  • tenants (Array)

    optional list of tenants. The elements of the array can be any object convertible to a string

Returns:

  • (Array<Event>)

    List of events. Can be empty



327
328
329
330
331
# File 'lib/hawkular/alerts/alerts_api.rb', line 327

def events(criteria: {}, tenants: nil)
  query = generate_query_params(criteria)
  uri = tenants ? '/admin/events' : '/events'
  http_get(uri + query, multi_tenants_header(tenants)).map { |e| Event.new(e) }
end

#fetch_version_and_statusHash{String=>String}

Return version and status information for the used version of Hawkular-Alerting

Returns:

  • (Hash{String=>String})

    ('Implementation-Version', 'Built-From-Git-SHA1', 'status')



27
28
29
# File 'lib/hawkular/alerts/alerts_api.rb', line 27

def fetch_version_and_status
  http_get('/status')
end

#get_action(plugin, action_id) ⇒ Action

Obtains one action of given action plugin from the server.

Parameters:

  • plugin (String)

    Id of the action plugin

  • action_id (String)

    Id of the action

Returns:

  • (Action)

    the selected trigger



216
217
218
219
220
221
# File 'lib/hawkular/alerts/alerts_api.rb', line 216

def get_action(plugin, action_id)
  the_plugin = hawk_escape plugin
  the_action_id = hawk_escape action_id
  ret = http_get "/actions/#{the_plugin}/#{the_action_id}"
  Trigger::Action.new(ret)
end

#get_action_definition(action_plugin = nil) ⇒ Object

Obtains action definition/plugin from the server.

Parameters:

  • action_plugin (String) (defaults to: nil)

    Id of the action plugin to fetch. If nil, all the plugins are fetched



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/hawkular/alerts/alerts_api.rb', line 184

def get_action_definition(action_plugin = nil)
  if action_plugin.nil?
    plugins = http_get('plugins')
  else
    plugins = [action_plugin]
  end
  ret = {}
  plugins.each do |p|
    ret[p] = http_get("/plugins/#{p}")
  end
  ret
end

#get_alerts_for_trigger(trigger_id) ⇒ Array<Alert>

Obtain the alerts for the Trigger with the passed id

Parameters:

  • trigger_id (String)

    Id of the trigger that has fired the alerts

Returns:

  • (Array<Alert>)

    List of alerts for the trigger. Can be empty



235
236
237
238
239
240
241
242
243
# File 'lib/hawkular/alerts/alerts_api.rb', line 235

def get_alerts_for_trigger(trigger_id) # TODO: add additional filters
  return [] unless trigger_id

  url = '/?triggerIds=' + trigger_id
  ret = http_get(url)
  val = []
  ret.each { |a| val.push(Alert.new(a)) }
  val
end

#get_single_alert(alert_id) ⇒ Alert

Retrieve a single Alert by its id

Parameters:

  • alert_id (String)

    id of the alert to fetch

Returns:

  • (Alert)

    Alert object retrieved



269
270
271
272
273
# File 'lib/hawkular/alerts/alerts_api.rb', line 269

def get_single_alert(alert_id)
  ret = http_get('/alert/' + alert_id)
  val = Alert.new(ret)
  val
end

#get_single_trigger(trigger_id, full = false) ⇒ Trigger

Obtains one Trigger definition from the server.

Parameters:

  • trigger_id (String)

    Id of the trigger to fetch

  • full (defaults to: false)

    If true then conditions and dampenings for the trigger are also fetched

Returns:

  • (Trigger)

    the selected trigger



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hawkular/alerts/alerts_api.rb', line 52

def get_single_trigger(trigger_id, full = false)
  the_trigger = '/triggers/' + trigger_id
  ret = http_get(the_trigger)
  trigger = Trigger.new(ret)

  if full
    ret = http_get(the_trigger + '/conditions')
    ret.each { |c| trigger.conditions.push(Trigger::Condition.new(c)) }
    ret = http_get(the_trigger + '/dampenings')
    ret.each { |c| trigger.dampenings.push(Trigger::Dampening.new(c)) }
  end

  trigger
end

#list_alerts(criteria = {}) ⇒ Array<Alert>

List fired alerts

Parameters:

  • criteria (Hash) (defaults to: {})

    optional query criteria

Returns:

  • (Array<Alert>)

    List of alerts in the system. Can be empty



248
249
250
# File 'lib/hawkular/alerts/alerts_api.rb', line 248

def list_alerts(criteria = {})
  alerts(criteria: criteria)
end

#list_events(criteria = {}) ⇒ Array<Event>

List Events given optional criteria. Criteria keys are strings (not symbols):

startTime   numeric, milliseconds from epoch
endTime     numeric, milliseconds from epoch
eventIds    array of strings
triggerIds  array of strings
categories  array of strings
tags        array of strings, each tag of format 'name|value'. Specify '*' for value to match all values
thin        boolean, return lighter events (omits triggering data for trigger-generated events)

Parameters:

  • criteria (Hash) (defaults to: {})

    optional query criteria

Returns:

  • (Array<Event>)

    List of events. Can be empty



311
312
313
# File 'lib/hawkular/alerts/alerts_api.rb', line 311

def list_events(criteria = {})
  events(criteria: criteria)
end

#list_members(trigger_id, orphans = false) ⇒ Array<Trigger>

Lists members of a group trigger

Parameters:

  • trigger_id (String)

    ID of the group trigger to list members

  • orphans (boolean) (defaults to: false)

    flag to include orphans

Returns:

  • (Array<Trigger>)

    Members found



142
143
144
145
# File 'lib/hawkular/alerts/alerts_api.rb', line 142

def list_members(trigger_id, orphans = false)
  ret = http_get "triggers/groups/#{trigger_id}/members?includeOrphans=#{orphans}"
  ret.collect { |t| Trigger.new(t) }
end

#list_triggers(ids = [], tags = []) ⇒ Array<Trigger>

Lists defined triggers in the system tags are of the format # key|value. Tags are OR'd together. If a tag-key shows up more than once, only the last one is accepted

Parameters:

  • ids (Array) (defaults to: [])

    List of trigger ids. If provided, limits to the given triggers

  • tags (Array) (defaults to: [])

    List of tags. If provided, limits to the given tags. Individual

Returns:

  • (Array<Trigger>)

    Triggers found



37
38
39
40
41
42
43
44
45
46
# File 'lib/hawkular/alerts/alerts_api.rb', line 37

def list_triggers(ids = [], tags = [])
  query = generate_query_params 'triggerIds' => ids, 'tags' => tags
  sub_url = '/triggers' + query

  ret = http_get(sub_url)

  val = []
  ret.each { |t| val.push(Trigger.new(t)) }
  val
end

#orphan_member(trigger_id) ⇒ Trigger

Detaches a member trigger from its group trigger

Parameters:

  • trigger_id (String)

    ID of the member trigger to detach

Returns:



133
134
135
136
# File 'lib/hawkular/alerts/alerts_api.rb', line 133

def orphan_member(trigger_id)
  http_post "triggers/groups/members/#{trigger_id}/orphan", {}
  get_single_trigger trigger_id, false
end

#remove_tags(alert_ids, tag_names) ⇒ Object

Remove tags from existing Alerts.

Parameters:

  • alert_ids (Array<numeric>)

    List of alert IDs

  • tag_names (Array<String>)

    List of tag names.



364
365
366
367
# File 'lib/hawkular/alerts/alerts_api.rb', line 364

def remove_tags(alert_ids, tag_names)
  query = generate_query_params(alertIds: alert_ids, tagNames: tag_names)
  http_delete('/tags' + query)
end

#resolve_alert(alert_id, by = nil, comment = nil) ⇒ Object

Mark one alert as resolved

Parameters:

  • alert_id (String)

    Id of the alert to resolve

  • by (String) (defaults to: nil)

    name of the user resolving the alert

  • comment (String) (defaults to: nil)

    A comment on the resolution



279
280
281
282
283
284
285
286
# File 'lib/hawkular/alerts/alerts_api.rb', line 279

def resolve_alert(alert_id, by = nil, comment = nil)
  sub_url = "/resolve/#{alert_id}"
  query = generate_query_params 'resolvedBy' => by, 'resolvedNotes' => comment
  sub_url += query
  http_put(sub_url, {})

  true
end

#set_group_conditions(trigger_id, trigger_mode, group_conditions_info) ⇒ Array<Condition>

Creates the group conditions definitions.

Parameters:

  • trigger_id (String)

    ID of the group trigger to set conditions

  • trigger_mode (String)

    Mode of the trigger where conditions are attached (:FIRING, :AUTORESOLVE)

  • group_conditions_info (GroupConditionsInfo)

    the conditions to set into the group trigger with the mapping with the data_id members map

Returns:

  • (Array<Condition>)

    conditions Array of associated conditions



115
116
117
118
119
120
# File 'lib/hawkular/alerts/alerts_api.rb', line 115

def set_group_conditions(trigger_id, trigger_mode, group_conditions_info)
  ret = http_put "triggers/groups/#{trigger_id}/conditions/#{trigger_mode}", group_conditions_info.to_h
  conditions = []
  ret.each { |c| conditions.push(Trigger::Condition.new(c)) }
  conditions
end

#update_group_dampening(dampening) ⇒ Dampening

Updates a dampening for a group trigger

Parameters:

  • dampening (Dampening)

    the dampening to update

Returns:

  • (Dampening)

    the updated dampening



158
159
160
161
# File 'lib/hawkular/alerts/alerts_api.rb', line 158

def update_group_dampening(dampening)
  ret = http_put "triggers/groups/#{dampening.trigger_id}/dampenings/#{dampening.dampening_id}", dampening.to_h
  Trigger::Dampening.new(ret)
end

#update_group_trigger(trigger) ⇒ Trigger

Updates a given group trigger definition

Parameters:

  • trigger (Trigger)

    the group trigger to be updated

Returns:

  • (Trigger)

    The updated group trigger



104
105
106
107
# File 'lib/hawkular/alerts/alerts_api.rb', line 104

def update_group_trigger(trigger)
  http_put "triggers/groups/#{trigger.id}/", trigger.to_h
  get_single_trigger trigger.id, false
end