Class: Chef::Handler::Logstash
- Inherits:
-
Chef::Handler
- Object
- Chef::Handler
- Chef::Handler::Logstash
- Defined in:
- lib/chef/handler/chef_logstash.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
writeonly
Sets the attribute host.
-
#port ⇒ Object
writeonly
Sets the attribute port.
-
#tags ⇒ Object
writeonly
Sets the attribute tags.
-
#timeout ⇒ Object
writeonly
Sets the attribute timeout.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Logstash
constructor
A new instance of Logstash.
- #report ⇒ Object
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( = {}) [:tags] ||= Array.new [:timeout] ||= 15 @tags = [:tags] @timeout = [:timeout] @host = [:host] @port = [:port] end |
Instance Attribute Details
#host=(value) ⇒ Object (writeonly)
Sets the attribute host
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
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
9 10 11 |
# File 'lib/chef/handler/chef_logstash.rb', line 9 def (value) @tags = value end |
#timeout=(value) ⇒ Object (writeonly)
Sets the attribute timeout
9 10 11 |
# File 'lib/chef/handler/chef_logstash.rb', line 9 def timeout=(value) @timeout = value end |
Instance Method Details
#report ⇒ Object
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.}") end end |