Class: Chef::Handler::Logstash

Inherits:
Chef::Handler show all
Defined in:
lib/chef/handler/chef_logstash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logstash

Returns a new instance of Logstash.



11
12
13
14
15
16
17
18
# File 'lib/chef/handler/chef_logstash.rb', line 11

def initialize(options = {})
  options[:tags] ||= Array.new
  options[:timeout] ||= 15
  @tags = options[:tags]
  @timeout = options[:timeout]
  @host = options[:host]
  @port = options[:port]
end

Instance Attribute Details

#host=(value) ⇒ Object (writeonly)

Sets the attribute host

Parameters:

  • value

    the value to set the attribute host to.



9
10
11
# File 'lib/chef/handler/chef_logstash.rb', line 9

def host=(value)
  @host = value
end

#port=(value) ⇒ Object (writeonly)

Sets the attribute port

Parameters:

  • value

    the value to set the attribute port to.



9
10
11
# File 'lib/chef/handler/chef_logstash.rb', line 9

def port=(value)
  @port = value
end

#tags=(value) ⇒ Object (writeonly)

Sets the attribute tags

Parameters:

  • value

    the value to set the attribute tags to.



9
10
11
# File 'lib/chef/handler/chef_logstash.rb', line 9

def tags=(value)
  @tags = value
end

#timeout=(value) ⇒ Object (writeonly)

Sets the attribute timeout

Parameters:

  • value

    the value to set the attribute timeout to.



9
10
11
# File 'lib/chef/handler/chef_logstash.rb', line 9

def timeout=(value)
  @timeout = value
end

Instance Method Details

#reportObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/handler/chef_logstash.rb', line 20

def report
  # A logstash json_event looks like this:
  # {
  #   "@source":"typicall determined by logstash input def",
  #   "@type":"determined by logstash input def",
  #   "@tags":[],
  #   "@fields":{},
  #   "@timestamp":"ISO8601 of report seen by logstash",
  #   "@source_host":"host.foo.com",
  #   "@source_path":"typically the name of the log file",
  #   "@message":"escaped representation of report"
  # }
  #
  # When sending an report in native `json_event` format
  # - You are required to set everything EXCEPT @type and @timestamp
  # - @type CAN be overridden
  # - @timestamp will be ignored
  @updated_resources = []
  @updated_resources_count = 0
  if run_status.updated_resources
    run_status.updated_resources.each do |r|
      @updated_resources << r.to_s
      @updated_resources_count += 1
    end
  end
  report = Hash.new
  report["@source"] = "chef://#{run_status.node.name}/handler/logstash"
  report["@source_path"] = "#{__FILE__}"
  report["@source_host"] = run_status.node.name
  report["@tags"] = @tags
  report["@fields"] = Hash.new
  report["@fields"]["environment"] = run_status.node.chef_environment
  report["@fields"]["run_list"] = run_status.node.run_list
  report["@fields"]["updated_resources"] = @updated_resources
  report["@fields"]["updated_resources_count"] = @updated_resources_count
  report["@fields"]["elapsed_time"] = run_status.elapsed_time
  report["@fields"]["success"] = run_status.success?
  # (TODO) Convert to ISO8601
  report["@fields"]["start_time"] = run_status.start_time.to_time.iso8601
  report["@fields"]["end_time"] = run_status.end_time.to_time.iso8601
  if run_status.backtrace
    report["@fields"]["backtrace"] = run_status.backtrace.join("\n")
  else
    report["@fields"]["backtrace"] = ""
  end
  if run_status.exception
    report["@fields"]["exception"] = run_status.exception
  else
    report["@fields"]["exception"] = ""
  end
  report["@message"] = run_status.exception || "Chef client run completed in #{run_status.elapsed_time}"

  begin
    Timeout::timeout(@timeout) do
      json = report.to_json
      ls = TCPSocket.new "#{@host}" , @port
      ls.puts json
      ls.close
    end
  rescue Exception => e
    Chef::Log.info("Failed to write to #{@host} on port #{@port}: #{e.message}")
  end
end