Class: Fluent::ImKayacOutput

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

Instance Method Summary collapse

Constructor Details

#initializeImKayacOutput

Returns a new instance of ImKayacOutput.



4
5
6
7
8
9
10
# File 'lib/fluent/plugin/out_imkayac.rb', line 4

def initialize
  super
  require 'net/http'
  require 'digest/sha1'
  require 'json'
  require 'erb'
end

Instance Method Details

#configure(conf) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/fluent/plugin/out_imkayac.rb', line 19

def configure(conf)
  super

  if @username.nil?
    raise Fluent::ConfigError, "missing username"
  end

end

#emit(tag, es, chain) ⇒ Object



67
68
69
70
71
72
# File 'lib/fluent/plugin/out_imkayac.rb', line 67

def emit(tag, es, chain)
  es.each do |time,record|
    post(tag, time, record)
  end
  chain.next
end

#post(tag, time, record) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluent/plugin/out_imkayac.rb', line 36

def post(tag, time, record)
  url = URI.parse( @api_url + @username )
  params = {
    "message" => ERB.new(@template).result(binding),
  }
  params["handler"]  = @handler  if @handler
  params["password"] = @password if @password
  params["sig"]      = Digest::SHA1.hexdigest(params["message"] + @secret_key) if @secret_key

  begin
    res = Net::HTTP.post_form(url, params)
  rescue IOError, EOFError, SystemCallError
    # server didn't respond
    $log.warn "out_imkayac: Net::HTTP.post_form raises exception: #{$!.class}, '#{$!.message}'"
    res = nil
  end
  unless res and res.is_a?(Net::HTTPSuccess)
    $log.warn "out_imkayac: failed to post to im.kayac.com, code: #{res && res.code}"
    return
  end
  begin
    result = JSON.load(res.body)
  rescue
    $log.warn "out_imkayac: response body is not valid JSON format: #{$!.class}, '#{$!.message}' #{res.body}"
    result = nil
  end
  if result and result["error"] != ""
    $log.warn "out_imkayac: error from im.kayac.com: #{result['error']}"
  end
end

#shutdownObject



32
33
34
# File 'lib/fluent/plugin/out_imkayac.rb', line 32

def shutdown
  super
end

#startObject



28
29
30
# File 'lib/fluent/plugin/out_imkayac.rb', line 28

def start
  super
end