Module: Malsh

Defined in:
lib/malsh.rb,
lib/malsh/cli.rb,
lib/malsh/version.rb,
lib/malsh/host_metrics.rb,
lib/malsh/notification.rb

Defined Under Namespace

Modules: HostMetrics, Notification Classes: CLI

Constant Summary collapse

VERSION =
'0.3.6'

Class Method Summary collapse

Class Method Details

.alert_has_host?(alert) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
# File 'lib/malsh.rb', line 91

def alert_has_host?(alert)
  exclude_types = %w[external service expression]
  return false if exclude_types.include?(alert.type)

  true
end

.alertsObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/malsh.rb', line 52

def alerts
  @_alerts ||= Mackerel.alerts.map do |alert|
    if alert_has_host?(alert)
      alert['host'] = Malsh.host_by_id(alert.hostId)
    else
      alert['monitor'] = Mackerel.monitor(alert.monitorId)
    end
    alert
  end
end

.host_by_id(id) ⇒ Object



67
68
69
# File 'lib/malsh.rb', line 67

def host_by_id(id)
  Mackerel.host(id)
end

.host_metric_names(id) ⇒ Object



85
86
87
88
89
# File 'lib/malsh.rb', line 85

def host_metric_names(id)
  Mackerel.host_metric_names(id)
rescue StandardError => e
  puts e
end

.host_metrics(id, name, from, to) ⇒ Object



79
80
81
82
83
# File 'lib/malsh.rb', line 79

def host_metrics(id, name, from, to)
  Mackerel.host_metrics(id, name: name, from: from, to: to)
rescue StandardError => e
  puts e
end

.host_name(host) ⇒ Object



63
64
65
# File 'lib/malsh.rb', line 63

def host_name(host)
  host.displayName || host.name
end

.hosts(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/malsh.rb', line 39

def hosts(options = {})
  @_hosts ||= Mackerel.hosts(options).reject do |h|
    Malsh.options[:invert_match] && Malsh.options[:invert_match].find { |v| host_name(h).match(/#{v}/) }
  end.reject do |h|
    Malsh.options[:regexp] && Malsh.options[:regexp].all? { |r| !host_name(h).match(/#{r}/) }
  end.reject do |h|
    Malsh.options[:invert_role] && Malsh.options[:invert_role].find do |r|
      service, role = r.split(/:/)
      h.roles[service] && h.roles[service].include?(role)
    end
  end
end

.init(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/malsh.rb', line 27

def init(options)
  if !ENV['MACKEREL_APIKEY'] && !options[:api_key]
    puts "must set be mackerel api key <--api-key> or ENV['MACKEREL_APIKEY']"
    exit
  end

  options options
  Mackerel.configure do |config|
    config.api_key = ENV['MACKEREL_APIKEY'] || options[:api_key]
  end
end

.metrics(name) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/malsh.rb', line 71

def metrics(name)
  hash = {}
  hosts.map(&:id).each_slice(200) do |ids|
    hash.merge!(Mackerel.latest_tsdb({ hostId: ids, name: name }))
  end
  hash
end

.notify_alert(subject, alerts) ⇒ Object



16
17
18
19
20
# File 'lib/malsh.rb', line 16

def notify_alert(subject, alerts)
  Malsh::Notification.constants.each do |c|
    Object.const_get("Malsh::Notification::#{c}").notify_alert(options[:subject] || subject, alerts)
  end
end

.notify_host(subject, host) ⇒ Object



10
11
12
13
14
# File 'lib/malsh.rb', line 10

def notify_host(subject, host)
  Malsh::Notification.constants.each do |c|
    Object.const_get("Malsh::Notification::#{c}").notify_host(options[:subject] || subject, host)
  end
end

.options(ops = nil) ⇒ Object



22
23
24
25
# File 'lib/malsh.rb', line 22

def options(ops = nil)
  @_options = ops if ops
  @_options
end