class Malsh::Notification::Slack

Public Class Methods

doit?() click to toggle source
# File lib/malsh/notification/slack.rb, line 66
def self.doit?
  %i(slack_webhook slack_channel).all? {|k| Malsh.options.key?(k) || ENV[k.to_s.upcase] }
end
notifier() click to toggle source
# File lib/malsh/notification/slack.rb, line 70
def self.notifier
  ::Slack::Notifier.new(
    ENV["SLACK_WEBHOOK"] || Malsh.options[:slack_webhook],
    channel: ENV["SLACK_CHANNEL"] || Malsh.options[:slack_channel],
    username: ENV["SLACK_USER"] || Malsh.options[:slack_user] || 'Mackerel-Check',
    link_names: 1
  )
end
notify_alert(subject, alerts) click to toggle source
# File lib/malsh/notification/slack.rb, line 20
def self.notify_alert(subject, alerts)
  org = Mackerel.org.name
  attachments = []
  alerts.each do |alert|
    color = case alert.status
            when 'CRITICAL'
              'danger'
            when 'WARNING'
              'warning'
            else
              ''
            end

    title = if Malsh.alert_has_host?(alert)
              alert.host.name
            else
              alert.monitor.name
            end

    author_name = if Malsh.alert_has_host?(alert)
                    alert.host.roles.map{|k, v| v.map{|r| "#{k}: #{r}"}}.flatten.join(" ")
                  else
                    ''
                  end

    attachments << {
        author_name: author_name,
        title: title,
        title_link: "https://mackerel.io/orgs/#{org}/alerts/#{alert.id}",
        text: alert.message,
        color: color,
        fields: [
            {
                title: 'Type',
                value: alert.type
            },
            {
                title: 'OpenedAt',
                value: Time.at(alert.openedAt).strftime("%Y/%m/%d %H:%M:%S")
            }
        ]
    }
  end
  notifier.ping "*#{subject}*", attachments: attachments
end
notify_host(subject, hosts) click to toggle source
# File lib/malsh/notification/slack.rb, line 4
def self.notify_host(subject, hosts)
  return unless doit?
  lists = if Malsh.options[:org]
            hosts.map do |h|
              "<https://mackerel.io/orgs/#{Malsh.options[:org]}/hosts/#{h.id}/-/setting|#{h.name}(#{h.roles.keys.join(",")})>"
            end
          else
            hosts.map(&:name)
          end
  note = {
    text: lists.join("\n"),
    color: "warning"
  }
  notifier.ping "*#{subject}*", attachments: [note]
end