module Malsh

Constants

VERSION

Public Class Methods

alert_has_host?(alert) click to toggle source
# File lib/malsh.rb, line 95
def alert_has_host?(alert)
  exclude_types = ['external', 'service']
  return false if exclude_types.include?(alert.type)
  true
end
alerts() click to toggle source
# 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) click to toggle source
# File lib/malsh.rb, line 67
def host_by_id(id)
  Mackerel.host(id)
end
host_metric_names(id) click to toggle source
# File lib/malsh.rb, line 87
def host_metric_names(id)
  begin
    Mackerel.host_metric_names(id)
  rescue => e
    puts e
  end
end
host_metrics(id, name, from, to) click to toggle source
# File lib/malsh.rb, line 79
def host_metrics(id, name, from, to)
  begin
    Mackerel.host_metrics(id, name: name, from: from, to: to)
  rescue => e
    puts e
  end
end
host_name(host) click to toggle source
# File lib/malsh.rb, line 63
def host_name(host)
  host.displayName || host.name
end
hosts(options = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/malsh.rb, line 22
def options(ops=nil)
  @_options = ops if ops
  @_options
end