Module: Utils::Sensu

Included in:
Lita::Handlers::Sensu2
Defined in:
lib/utils/sensu.rb

Overview

Utility methods for interacting with Sensu

Instance Method Summary collapse

Instance Method Details

#calculate_expiration(duration, units) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/utils/sensu.rb', line 4

def calculate_expiration(duration, units)
  if units
    expiration = case units
                 when 's'
                   duration
                 when 'm'
                   duration * 60
                 when 'h'
                   duration * 3600
                 when 'd'
                   duration * 3600 * 24
                 end
    human_duration = "#{duration}#{units}"
  else
    expiration = 3600
    human_duration = '1h'
  end
  [expiration, human_duration]
end

#check_alias(client, check) ⇒ Object



24
25
26
# File 'lib/utils/sensu.rb', line 24

def check_alias(client, check)
  "#{client}:#{check && !check.empty? ? check : '*'}"
end

#silence_post_data(user, expiration, client, check) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/utils/sensu.rb', line 40

def silence_post_data(user, expiration, client, check)
  data = {
    creator: user.name,
    expire: expiration,
    reason: 'Because Lita says so!',
    subscription: "client:#{client}"
  }
  data[:check] = check if !check.nil? && check != ''
  MultiJson.dump(data)
end

#sorted_by(body, attribute) ⇒ Object



28
29
30
31
32
# File 'lib/utils/sensu.rb', line 28

def sorted_by(body, attribute)
  MultiJson.load(body, symbolize_keys: true).sort do |a, b|
    a[attribute.to_sym] <=> b[attribute.to_sym]
  end
end

#sorted_events(body) ⇒ Object



34
35
36
37
38
# File 'lib/utils/sensu.rb', line 34

def sorted_events(body)
  MultiJson.load(body, symbolize_keys: true).sort do |a, b|
    a[:client][:name] <=> b[:client][:name]
  end
end