Class: Fluent::HipchatOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_hipchat.rb

Constant Summary collapse

COLORS =
%w(yellow red green purple gray random)
FORMAT =
%w(html text)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHipchatOutput

Returns a new instance of HipchatOutput.



24
25
26
27
# File 'lib/fluent/plugin/out_hipchat.rb', line 24

def initialize
  super
  require 'hipchat-api'
end

Instance Attribute Details

#hipchatObject (readonly)

Returns the value of attribute hipchat.



22
23
24
# File 'lib/fluent/plugin/out_hipchat.rb', line 22

def hipchat
  @hipchat
end

Instance Method Details

#configure(conf) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fluent/plugin/out_hipchat.rb', line 29

def configure(conf)
  super

  @hipchat = HipChat::API.new(conf['api_token'])
  @default_room = conf['default_room']
  @default_from = conf['default_from'] || 'fluentd'
  @default_notify = conf['default_notify'] || 0
  @default_color = conf['default_color'] || 'yellow'
  @default_format = conf['default_format'] || 'html'
  @default_timeout = conf['default_timeout']
  if conf['http_proxy_host']
    HipChat::API.http_proxy(
      conf['http_proxy_host'],
      conf['http_proxy_port'],
      conf['http_proxy_user'],
      conf['http_proxy_pass'])
  end
end

#format(tag, time, record) ⇒ Object



48
49
50
# File 'lib/fluent/plugin/out_hipchat.rb', line 48

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#send_message(record) ⇒ Object

Raises:

  • (StandardError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fluent/plugin/out_hipchat.rb', line 63

def send_message(record)
  room = record['room'] || @default_room
  from = record['from'] || @default_from
  message = record[@key_name]
  if record['notify'].nil?
    notify = @default_notify
  else
    notify = record['notify'] ? 1 : 0
  end
  color = COLORS.include?(record['color']) ? record['color'] : @default_color
  message_format = FORMAT.include?(record['format']) ? record['format'] : @default_format
  @hipchat.set_timeout(@default_timeout.to_i) unless @default_timeout.nil?
  response = @hipchat.rooms_message(room, from, message, notify, color, message_format)
  raise StandardError, response['error'][@key_name].to_s if defined?(response['error'][@key_name])
end

#set_topic(record) ⇒ Object

Raises:

  • (StandardError)


79
80
81
82
83
84
85
# File 'lib/fluent/plugin/out_hipchat.rb', line 79

def set_topic(record)
  room = record['room'] || @default_room
  from = record['from'] || @default_from
  topic = record['topic']
  response = @hipchat.rooms_topic(room, topic, from)
  raise StandardError, response['error'][@key_name].to_s if defined?(response['error'][@key_name])
end

#write(chunk) ⇒ Object



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

def write(chunk)
  chunk.msgpack_each do |(tag,time,record)|
    begin
      send_message(record) if record[@key_name]
      set_topic(record) if record['topic']
    rescue => e
      $log.error("HipChat Error:", :error_class => e.class, :error => e.message)
    end
  end
end