Class: Fluent::Plugin::MattermostOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_mattermost.rb

Instance Method Summary collapse

Instance Method Details

#build_message(record) ⇒ Object



102
103
104
# File 'lib/fluent/plugin/out_mattermost.rb', line 102

def build_message(record)
  @message % record.to_json
end

#check_config_paramsObject



106
107
108
109
110
111
# File 'lib/fluent/plugin/out_mattermost.rb', line 106

def check_config_params()
  if @webhook_url.nil? || @channel_id.nil? || @message.nil?
    raise "Check in your Mattermost config, that all parameters in the configuration file are filled"
    abort
  end
end

#configure(conf) ⇒ Object



29
30
31
# File 'lib/fluent/plugin/out_mattermost.rb', line 29

def configure(conf)
  super
end

#get_infos(chunk) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/fluent/plugin/out_mattermost.rb', line 92

def get_infos(chunk) 
  messages = []
  messages << "\n"
  chunk.msgpack_each do |time, record|
    messages << "#{build_message(record)}\n"
  end

  return messages
end

#message(record) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fluent/plugin/out_mattermost.rb', line 77

def message(record)
  payload = [{
              "author_name": "Fluentd",
              "thumb_url": "https://coralogix.com/wp-content/uploads/2020/04/fluentd-guide-700x430.png",
              "color": @message_color,
              "fields": [
              {
                "short": false,
                "title": @message_title,
                "value": record
              }]
            }]
  return payload
end

#post(payload) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fluent/plugin/out_mattermost.rb', line 52

def post(payload)

  url = URI(@webhook_url)

  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = @enable_tls
  if !@ca_path.nil? and @enable_tls == true
      https.ca_path = @ca_path
  end

  request = Net::HTTP::Post.new(url)
  request["Content-Type"] = "application/json"
  request.body = JSON.dump({
    "channel_id": @channel_id,
    "attachments": message(payload)
  })
  response = https.request(request)

  if response.read_body != "ok"
    log.error "response from mattermost: ", response.read_body
  else 
    puts response.read_body
  end
end

#startObject



33
34
35
36
# File 'lib/fluent/plugin/out_mattermost.rb', line 33

def start
  super
  check_config_params
end

#write(chunk) ⇒ Object



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

def write(chunk)
  begin
    message = get_infos(chunk)

    post(message)
  rescue Timeout::Error => e
    log.warn "out_mattermost:", :error => e.to_s, :error_class => e.class.to_s
    raise e # let Fluentd retry
  rescue => e
    log.error "out_mattermost:", :error => e.to_s, :error_class => e.class.to_s
    log.warn_backtrace e.backtrace
  end
end