class Hawkular::Alerts::Client

Interface to use to talk to the Hawkular-Alerts component. @param entrypoint [String] base url of Hawkular-Alerts - e.g

http://localhost:8080/hawkular/alerts

@param credentials [Hash{String=>String}] Hash of username, password, token(optional) @param options [Hash{String=>String}] Additional rest client options

Public Class Methods

new(entrypoint, credentials = {}, options = {}) click to toggle source
Calls superclass method Hawkular::BaseClient::new
   # File lib/hawkular/alerts/alerts_api.rb
17 def initialize(entrypoint, credentials = {}, options = {})
18   entrypoint = normalize_entrypoint_url entrypoint, 'hawkular/alerts'
19   @entrypoint = entrypoint
20 
21   super(entrypoint, credentials, options)
22 end

Public Instance Methods

acknowledge_alert(alert_id, by = nil, comment = nil) click to toggle source

Mark one alert as acknowledged @param [String] alert_id Id of the alert to ack @param [String] by name of the user acknowledging the alert @param [String] comment A comment on the acknowledge

    # File lib/hawkular/alerts/alerts_api.rb
289 def acknowledge_alert(alert_id, by = nil, comment = nil)
290   sub_url = "/ack/#{alert_id}"
291   query = generate_query_params 'ackBy' => by, 'ackNotes' => comment
292   sub_url += query
293   http_put(sub_url, {})
294 
295   true
296 end
add_tags(alert_ids, tags) click to toggle source

Add tags to existing Alerts. @param [Array<numeric>] alert_ids List of alert IDs @param [Array<String>] tags List of tags. Each tag of format 'name|value'.

    # File lib/hawkular/alerts/alerts_api.rb
353 def add_tags(alert_ids, tags)
354   query = generate_query_params(alertIds: alert_ids, tags: tags)
355   http_put('/tags' + query, {})
356 end
alerts(criteria: {}, tenants: nil) click to toggle source

List fired alerts @param [Hash] criteria optional query criteria @param [Array] tenants optional list of tenants. The elements of the array can be any object

convertible to a string

@return [Array<Alert>] List of alerts in the system. Can be empty

    # File lib/hawkular/alerts/alerts_api.rb
254 def alerts(criteria: {}, tenants: nil)
255   query = generate_query_params(criteria)
256   uri = tenants ? '/admin/alerts/' : '/'
257   ret = http_get(uri + query, multi_tenants_header(tenants))
258   val = []
259   ret.each { |a| val.push(Alert.new(a)) }
260   val
261 end
bulk_import_triggers(hash) click to toggle source

Import multiple trigger or action definitions specified as a hash to the server. @param [Hash] hash The hash with the trigger and action definitions.

see the https://git.io/va5UO for more details about the structure

@return [Hash] The newly entities as hash

   # File lib/hawkular/alerts/alerts_api.rb
71 def bulk_import_triggers(hash)
72   http_post 'import/all', hash
73 end
create_action(plugin, action_id, properties = {}) click to toggle source

Creates the action. @param [String] plugin The id of action definition/plugin @param [String] action_id The id of action @param [Hash] properties Troperties of action @return [Action] The newly created action

    # File lib/hawkular/alerts/alerts_api.rb
198 def create_action(plugin, action_id, properties = {})
199   the_plugin = hawk_escape plugin
200   # Check if plugin exists
201   http_get("/plugins/#{the_plugin}")
202 
203   payload = { actionId: action_id, actionPlugin: plugin, properties: properties }
204   ret = http_post('/actions', payload)
205   Trigger::Action.new(ret)
206 end
create_event(id, category, text, extras) click to toggle source

Inject an event into Hawkular-alerts @param [String] id Id of the event must be unique @param [String] category Event category for further distinction @param [String] text Some text to the user @param [Hash<String,Object>] extras additional parameters

    # File lib/hawkular/alerts/alerts_api.rb
335 def create_event(id, category, text, extras)
336   event = {}
337   event['id'] = id
338   event['ctime'] = Time.now.to_i * 1000
339   event['category'] = category
340   event['text'] = text
341   event.merge!(extras) { |_key, v1, _v2| v1 }
342 
343   http_post('/events', event)
344 end
create_group_dampening(dampening) click to toggle source

Creates a dampening for a group trigger @param [Dampening] dampening the dampening to create @return [Dampening] the newly created dampening

    # File lib/hawkular/alerts/alerts_api.rb
150 def create_group_dampening(dampening)
151   ret = http_post "triggers/groups/#{dampening.trigger_id}/dampenings", dampening.to_h
152   Trigger::Dampening.new(ret)
153 end
create_group_trigger(trigger) click to toggle source

Creates the group trigger definition. @param [Trigger] trigger The group trigger to be created @return [Trigger] The newly created group trigger

   # File lib/hawkular/alerts/alerts_api.rb
96 def create_group_trigger(trigger)
97   ret = http_post 'triggers/groups', trigger.to_h
98   Trigger.new(ret)
99 end
create_member_trigger(group_member_info) click to toggle source

Creates a member trigger @param [GroupMemberInfo] group_member_info the group member to be added @return [Trigger] the newly created member trigger

    # File lib/hawkular/alerts/alerts_api.rb
125 def create_member_trigger(group_member_info)
126   ret = http_post 'triggers/groups/members', group_member_info.to_h
127   Trigger.new(ret)
128 end
create_trigger(trigger, conditions = [], dampenings = [], _actions = []) click to toggle source

Creates the trigger definition. @param [Trigger] trigger The trigger to be created @param [Array<Condition>] conditions Array of associated conditions @param [Array<Dampening>] dampenings Array of associated dampenings @return [Trigger] The newly created trigger

   # File lib/hawkular/alerts/alerts_api.rb
80 def create_trigger(trigger, conditions = [], dampenings = [], _actions = [])
81   full_trigger = {}
82   full_trigger[:trigger] = trigger.to_h
83   conds = []
84   conditions.each { |c| conds.push(c.to_h) }
85   full_trigger[:conditions] = conds
86   damps = []
87   dampenings.each { |d| damps.push(d.to_h) } unless dampenings.nil?
88   full_trigger[:dampenings] = damps
89 
90   http_post 'triggers/trigger', full_trigger
91 end
delete_action(plugin, action_id) click to toggle source

Deletes the action of given action plugin. @param [String] plugin Id of the action plugin @param [String] action_id Id of the action

    # File lib/hawkular/alerts/alerts_api.rb
222 def delete_action(plugin, action_id)
223   the_plugin = hawk_escape plugin
224   the_action_id = hawk_escape action_id
225   http_delete "/actions/#{the_plugin}/#{the_action_id}"
226 end
delete_event(id) click to toggle source
    # File lib/hawkular/alerts/alerts_api.rb
346 def delete_event(id)
347   http_delete "/events/#{id}"
348 end
delete_group_dampening(trigger_id, dampening_id) click to toggle source

Deletes the dampening of a group trigger @param [String] trigger_id ID of the group trigger @param [String] dampening_id ID

    # File lib/hawkular/alerts/alerts_api.rb
166 def delete_group_dampening(trigger_id, dampening_id)
167   http_delete "/triggers/groups/#{trigger_id}/dampenings/#{dampening_id}"
168 end
delete_group_trigger(trigger_id) click to toggle source

Deletes the group trigger definition. @param [String] trigger_id ID of the group trigger to delete

    # File lib/hawkular/alerts/alerts_api.rb
178 def delete_group_trigger(trigger_id)
179   http_delete "/triggers/groups/#{trigger_id}"
180 end
delete_trigger(trigger_id) click to toggle source

Deletes the trigger definition. @param [String] trigger_id ID of the trigger to delete

    # File lib/hawkular/alerts/alerts_api.rb
172 def delete_trigger(trigger_id)
173   http_delete "/triggers/#{trigger_id}"
174 end
events(criteria: {}, tenants: nil) click to toggle source

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)

@param [Hash] criteria optional query criteria @param [Array] tenants optional list of tenants. The elements of the array can be any object

convertible to a string

@return [Array<Event>] List of events. Can be empty

    # File lib/hawkular/alerts/alerts_api.rb
324 def events(criteria: {}, tenants: nil)
325   query = generate_query_params(criteria)
326   uri = tenants ? '/admin/events' : '/events'
327   http_get(uri + query, multi_tenants_header(tenants)).map { |e| Event.new(e) }
328 end
fetch_version_and_status() click to toggle source

Return version and status information for the used version of Hawkular-Alerting @return [Hash{String=>String}]

('Implementation-Version', 'Built-From-Git-SHA1', 'status')
   # File lib/hawkular/alerts/alerts_api.rb
27 def fetch_version_and_status
28   http_get('/status')
29 end
get_action(plugin, action_id) click to toggle source

Obtains one action of given action plugin from the server. @param [String] plugin Id of the action plugin @param [String] action_id Id of the action @return [Action] the selected trigger

    # File lib/hawkular/alerts/alerts_api.rb
212 def get_action(plugin, action_id)
213   the_plugin = hawk_escape plugin
214   the_action_id = hawk_escape action_id
215   ret = http_get "/actions/#{the_plugin}/#{the_action_id}"
216   Trigger::Action.new(ret)
217 end
get_action_definition(action_plugin = nil) click to toggle source

Obtains action definition/plugin from the server. @param [String] action_plugin Id of the action plugin to fetch. If nil, all the plugins are fetched

    # File lib/hawkular/alerts/alerts_api.rb
184 def get_action_definition(action_plugin = nil)
185   plugins = action_plugin.nil? ? http_get('plugins') : [action_plugin]
186   ret = {}
187   plugins.each do |p|
188     ret[p] = http_get("/plugins/#{p}")
189   end
190   ret
191 end
get_alerts_for_trigger(trigger_id) click to toggle source

Obtain the alerts for the Trigger with the passed id @param [String] trigger_id Id of the trigger that has fired the alerts @return [Array<Alert>] List of alerts for the trigger. Can be empty

    # File lib/hawkular/alerts/alerts_api.rb
231 def get_alerts_for_trigger(trigger_id)
232   # TODO: add additional filters
233   return [] unless trigger_id
234 
235   url = '/?triggerIds=' + trigger_id
236   ret = http_get(url)
237   val = []
238   ret.each { |a| val.push(Alert.new(a)) }
239   val
240 end
get_single_alert(alert_id) click to toggle source

Retrieve a single Alert by its id @param [String] alert_id id of the alert to fetch @return [Alert] Alert object retrieved

    # File lib/hawkular/alerts/alerts_api.rb
266 def get_single_alert(alert_id)
267   ret = http_get('/alert/' + alert_id)
268   val = Alert.new(ret)
269   val
270 end
get_single_trigger(trigger_id, full = false) click to toggle source

Obtains one Trigger definition from the server. @param [String] trigger_id Id of the trigger to fetch @param full If true then conditions and dampenings for the trigger are also fetched @return [Trigger] the selected trigger

   # File lib/hawkular/alerts/alerts_api.rb
52 def get_single_trigger(trigger_id, full = false)
53   the_trigger = '/triggers/' + trigger_id
54   ret = http_get(the_trigger)
55   trigger = Trigger.new(ret)
56 
57   if full
58     ret = http_get(the_trigger + '/conditions')
59     ret.each { |c| trigger.conditions.push(Trigger::Condition.new(c)) }
60     ret = http_get(the_trigger + '/dampenings')
61     ret.each { |c| trigger.dampenings.push(Trigger::Dampening.new(c)) }
62   end
63 
64   trigger
65 end
list_alerts(criteria = {}) click to toggle source

List fired alerts @param [Hash]criteria optional query criteria @return [Array<Alert>] List of alerts in the system. Can be empty

    # File lib/hawkular/alerts/alerts_api.rb
245 def list_alerts(criteria = {})
246   alerts(criteria: criteria)
247 end
list_events(criteria = {}) click to toggle source

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)

@param [Hash] criteria optional query criteria @return [Array<Event>] List of events. Can be empty

    # File lib/hawkular/alerts/alerts_api.rb
308 def list_events(criteria = {})
309   events(criteria: criteria)
310 end
list_members(trigger_id, orphans = false) click to toggle source

Lists members of a group trigger @param [String] trigger_id ID of the group trigger to list members @param [boolean] orphans flag to include orphans @return [Array<Trigger>] Members found

    # File lib/hawkular/alerts/alerts_api.rb
142 def list_members(trigger_id, orphans = false)
143   ret = http_get "triggers/groups/#{trigger_id}/members?includeOrphans=#{orphans}"
144   ret.collect { |t| Trigger.new(t) }
145 end
list_triggers(ids = [], tags = []) click to toggle source

Lists defined triggers in the system @param [Array] ids List of trigger ids. If provided, limits to the given triggers @param [Array] tags List of tags. If provided, limits to the given tags. Individual 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 @return [Array<Trigger>] Triggers found

   # File lib/hawkular/alerts/alerts_api.rb
37 def list_triggers(ids = [], tags = [])
38   query = generate_query_params 'triggerIds' => ids, 'tags' => tags
39   sub_url = '/triggers' + query
40 
41   ret = http_get(sub_url)
42 
43   val = []
44   ret.each { |t| val.push(Trigger.new(t)) }
45   val
46 end
orphan_member(trigger_id) click to toggle source

Detaches a member trigger from its group trigger @param [String] trigger_id ID of the member trigger to detach @return [Trigger] the orphan trigger

    # File lib/hawkular/alerts/alerts_api.rb
133 def orphan_member(trigger_id)
134   http_post "triggers/groups/members/#{trigger_id}/orphan", {}
135   get_single_trigger trigger_id, false
136 end
remove_tags(alert_ids, tag_names) click to toggle source

Remove tags from existing Alerts. @param [Array<numeric>] alert_ids List of alert IDs @param [Array<String>] tag_names List of tag names.

    # File lib/hawkular/alerts/alerts_api.rb
361 def remove_tags(alert_ids, tag_names)
362   query = generate_query_params(alertIds: alert_ids, tagNames: tag_names)
363   http_delete('/tags' + query)
364 end
resolve_alert(alert_id, by = nil, comment = nil) click to toggle source

Mark one alert as resolved @param [String] alert_id Id of the alert to resolve @param [String] by name of the user resolving the alert @param [String] comment A comment on the resolution

    # File lib/hawkular/alerts/alerts_api.rb
276 def resolve_alert(alert_id, by = nil, comment = nil)
277   sub_url = "/resolve/#{alert_id}"
278   query = generate_query_params 'resolvedBy' => by, 'resolvedNotes' => comment
279   sub_url += query
280   http_put(sub_url, {})
281 
282   true
283 end
set_group_conditions(trigger_id, trigger_mode, group_conditions_info) click to toggle source

Creates the group conditions definitions. @param [String] trigger_id ID of the group trigger to set conditions @param [String] trigger_mode Mode of the trigger where conditions are attached (:FIRING, :AUTORESOLVE) @param [GroupConditionsInfo] group_conditions_info the conditions to set into the group trigger with the mapping

with the data_id members map

@return [Array<Condition>] conditions Array of associated conditions

    # File lib/hawkular/alerts/alerts_api.rb
115 def set_group_conditions(trigger_id, trigger_mode, group_conditions_info)
116   ret = http_put "triggers/groups/#{trigger_id}/conditions/#{trigger_mode}", group_conditions_info.to_h
117   conditions = []
118   ret.each { |c| conditions.push(Trigger::Condition.new(c)) }
119   conditions
120 end
update_group_dampening(dampening) click to toggle source

Updates a dampening for a group trigger @param [Dampening] dampening the dampening to update @return [Dampening] the updated dampening

    # File lib/hawkular/alerts/alerts_api.rb
158 def update_group_dampening(dampening)
159   ret = http_put "triggers/groups/#{dampening.trigger_id}/dampenings/#{dampening.dampening_id}", dampening.to_h
160   Trigger::Dampening.new(ret)
161 end
update_group_trigger(trigger) click to toggle source

Updates a given group trigger definition @param [Trigger] trigger the group trigger to be updated @return [Trigger] The updated group trigger

    # File lib/hawkular/alerts/alerts_api.rb
104 def update_group_trigger(trigger)
105   http_put "triggers/groups/#{trigger.id}/", trigger.to_h
106   get_single_trigger trigger.id, false
107 end

Private Instance Methods

multi_tenants_header(tenants) click to toggle source

Builds the tenant HTTP header for multi-tenant operations @param [Array] tenants an array of tenant names. The elements of the array can

be any object convertible to a string. Can be nil

@return [Hash] The HTTP header for multi-tenant operations. An empty hash is returned

if tenants parameter is nil
    # File lib/hawkular/alerts/alerts_api.rb
373 def multi_tenants_header(tenants)
374   tenants = tenants.join(',') if tenants.respond_to?(:join)
375   tenants ? { 'Hawkular-Tenant': tenants } : {}
376 end