class Notifu::Actors::Slack

Public Instance Methods

act() click to toggle source
# File lib/notifu/actors/slack.rb, line 91
def act
  self.contacts.each do |contact|
    if contact.slack_id != nil
      data = self.post_data(contact.slack_template, contact.slack_rich).merge({ channel: contact.slack_id })
    else
      data = self.post_data(contact.slack_template, contact.slack_rich)
    end

    begin
      Excon.post(Notifu::CONFIG[:actors][:slack][:url],
        tcp_nodelay: true,
        headers: { "ContentType" => "application/json" },
        body: data.to_json,
        expects: [ 200 ],
        idempotent: true
      )
    rescue Exception => e
      log "error", "Failed to send message to Slack - #{e.class}: #{e.message}"
    end
  end
end
color() click to toggle source
# File lib/notifu/actors/slack.rb, line 57
def color
  case self.issue.code.to_i
  when 0
    "good"
  when 1
    "warning"
  when 2
    "danger"
  else
    "#999999"
  end
end
default_template() click to toggle source
# File lib/notifu/actors/slack.rb, line 48
def default_template
  "#{self.status_icon} *<<%= uchiwa_url %>|<%= datacenter %>/<%= host %>>* <%= address %> #{self.service_icon} *<%= service %>* - <%= message %> _<%= duration %>_"
end
fallback_template() click to toggle source
# File lib/notifu/actors/slack.rb, line 52
def fallback_template
  "<%= status %> | <%= datacenter%>/<%= host %>/<%= service %> - <%= message %>"
end
post_data(template, rich) click to toggle source
# File lib/notifu/actors/slack.rb, line 11
def post_data(template, rich)
  return {
    username: "notifu",
    icon_emoji: ":loudspeaker:",
    attachments: [
      {
        fallback: self.apply_template(self.fallback_template),
        color: self.color,
        title: "#{self.issue.host} - #{self.issue.service}",
        title_link: "#{Notifu::CONFIG[:uchiwa_url]}/#/client/#{self.issue.datacenter}/#{self.issue.host}?check=#{self.issue.service}",
        text: self.issue.message,
        fields: [
          {
            title: "duration",
            value: (Time.now.to_i - self.issue.time_created.to_i).duration,
            short: true
          },
          {
            title: "notifu ID",
            value: self.issue.notifu_id,
            short: true
          }
        ]
      }
    ]
  } if rich != nil

  template ||= self.default_template
  return { username: "notifu", icon_emoji: ":loudspeaker:", text: self.text }
end
service_icon() click to toggle source
# File lib/notifu/actors/slack.rb, line 85
def service_icon
  return @service_icon if defined? @service_icon
  @service_icon = ":computer:" if self.issue.service == "keepalive"
  @service_icon ||= ":gear:"
end
status_icon() click to toggle source
# File lib/notifu/actors/slack.rb, line 70
def status_icon
  return @status_icon if defined? @status_icon
  case self.issue.code.to_i
  when 0
    @status_icon = ":white_check_mark:"
  when 1
    @status_icon = ":exclamation:"
  when 2
    @status_icon = ":bangbang:"
  else
    @status_icon = ":question: _#{self.issue.code.to_s}_"
  end
  @status_icon
end
text(c) click to toggle source
# File lib/notifu/actors/slack.rb, line 42
def text(c)
  t = c.slack_template
  t ||= self.default_template
  self.apply_template t
end