Class: Chef::Handler::Splunk

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Splunk

Creates a new Chef::Handler::Splunk object.

Parameters:

  • configuration (Hash)

    parameters: :path => the path for the splunk data - defaults to /var/chef/splunk :keep => the number of backup data files to keep - defaults to 10



29
30
31
32
33
34
# File 'lib/chef/handler/splunk.rb', line 29

def initialize(config={})
  @config = config
  @config[:path] ||= "/var/chef/splunk"
  @config[:keep] ||= 10 
  @config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/chef/handler/splunk.rb', line 22

def config
  @config
end

Instance Method Details

#build_report_dirObject

Creates the report directory, based on config



121
122
123
124
125
126
# File 'lib/chef/handler/splunk.rb', line 121

def build_report_dir
  d = Chef::Resource::Directory.new(config[:path], run_context)
  d.mode "0755"
  d.recursive true
  d.run_action(:create)
end

#reportObject

The body of the handler - creates run reports for Splunk. Results in three different files being created:

node.data - A k=v flattened list of node attributes, akin to search
run.data - Information about the last chef run 
resource.data - Information about every chef resource in the last run


43
44
45
46
47
48
49
50
51
# File 'lib/chef/handler/splunk.rb', line 43

def report
  Chef::Log.info("Creating Splunk run report")
  build_report_dir
  savetime = Time.new.utc.to_s
  save_node_data(savetime)
  save_run_data(savetime)
  save_resource_data(savetime)
  Chef::Log.info("Finished Splunk run report")
end

#save_node_data(savetime) ⇒ Object

Write out the node data

Parameters:

  • Takes (String)

    a Time.now.to_s as an argument



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/handler/splunk.rb', line 56

def save_node_data(savetime)
  f = Chef::Resource::File.new("#{config[:path]}/node.data", run_context)
  f.backup config[:keep].to_i
  f.mode '0644'
  node_data_content = ''
  flatten_and_expand(node).each do |k,v|
    if v.kind_of?(Array)
      v.each do |sv|
        node_data_content << "#{savetime} #{node.name} #{k}=#{sv}\n"
      end
    else
      node_data_content << "#{savetime} #{node.name} #{k}=#{v}\n"
    end
  end
  f.content(node_data_content)
  f.run_action(:create)
end

#save_resource_data(savetime) ⇒ Object

Write out the resource data

Parameters:

  • Takes (String)

    a Time.now.to_s as an argument



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/chef/handler/splunk.rb', line 101

def save_resource_data(savetime)
  f = Chef::Resource::File.new("#{config[:path]}/resource.data", run_context)
  f.backup config[:keep].to_i
  f.mode '0644'
  resource_data_content = ''
  all_resources.each do |r|
    resource_data_content << "#{savetime} #{node.name} type=#{r.resource_name} name=#{r.name} source_line=#{r.source_line} updated=#{r.updated?}"
    ivars = r.instance_variables.map { |ivar| ivar.to_sym } - Chef::Resource::HIDDEN_IVARS
    ivars.each do |ivar|
      if (value = r.instance_variable_get(ivar)) && !(value.respond_to?(:empty?) && value.empty?)
        resource_data_content << " #{ivar.to_s.sub(/^@/, '')}=#{value.inspect}"
      end
    end
    resource_data_content << "\n"
  end
  f.content resource_data_content
  f.run_action(:create)
end

#save_run_data(savetime) ⇒ Object

Write out the run data

Parameters:

  • Takes (String)

    a Time.now.to_s as an argument



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/chef/handler/splunk.rb', line 77

def save_run_data(savetime)
  f = Chef::Resource::File.new("#{config[:path]}/run.data", run_context)
  f.backup config[:keep].to_i
  f.mode '0644'
  run_data_content = ''
  run_data_content << "#{savetime} #{node.name} start_time=#{start_time}\n"
  run_data_content << "#{savetime} #{node.name} end_time=#{end_time}\n"
  run_data_content << "#{savetime} #{node.name} elapsed_time=#{elapsed_time}\n"
  run_data_content << "#{savetime} #{node.name} total_resources=#{all_resources.length}\n"
  run_data_content << "#{savetime} #{node.name} updated_resources=#{updated_resources.length}\n"
  if success?
    run_data_content << "#{savetime} #{node.name} successful=true\n"
  else
    run_data_content << "#{savetime} #{node.name} successful=false\n"
    run_data_content << "#{savetime} #{node.name} exception=#{exception}\n"
    run_data_content << "#{savetime} #{node.name} backtrace=#{backtrace}\n"
  end
  f.content(run_data_content)
  f.run_action(:create)
end