<commit_before>node["collectd"]["plugins"].each_pair do |plugin_key, definition|
  # Graphite auto-discovery
  if plugin_key.to_s == "write_graphite"
    if node["collectd"]["graphite_ipaddress"].empty?
      if Chef::Config[:solo]
        Chef::Application.fatal!("Graphite plugin enabled but no Graphite server configured.")
      end
      graphite_server_results = search(:node, "roles:#{node["collectd"]["graphite_role"]} AND chef_environment:#{node.chef_environment}")

      if graphite_server_results.empty?
        Chef::Application.fatal!("Graphite plugin enabled but no Graphite server found.")
      else
        definition["config"]["Host"] = graphite_server_results[0]["ipaddress"]
      end
    else
      definition["config"]["Host"] = node["collectd"]["graphite_ipaddress"]
    end
    definition["config"]["Port"] = 2003
  end

  collectd_plugin plugin_key.to_s do
    config definition["config"].to_hash if definition["config"]
    template definition["template"].to_s if definition["template"]
    cookbook definition["cookbook"].to_s if definition["cookbook"]
  end
end

conf_d  = "#{node["collectd"]["dir"]}/etc/conf.d"
keys    = node["collectd"]["plugins"].keys.collect { |k| k.to_s }

if File.exist?(conf_d)
  Dir.entries(conf_d).each do |entry|
    file "#{conf_d}/#{entry}" do
      backup false<commit_msg>attempting support at both Chef 11 and 10, as Chef 11 doesn't allow arbitrary setting of attributes (readonly) without a hierarchy modifier (here default)<commit_after>

# treat the graphite plugin specially: set address from search or attributes
write_graphite = node.default["collectd"]["plugins"]["write_graphite"]
if write_graphite
  if node["collectd"]["graphite_ipaddress"].empty?
    if Chef::Config[:solo]
      Chef::Application.fatal!("Graphite plugin enabled but no Graphite server configured.")
    end
    graphite_server_results = search(:node, "roles:#{node["collectd"]["graphite_role"]} AND chef_environment:#{node.chef_environment}")

    if graphite_server_results.empty?
      Chef::Application.fatal!("Graphite plugin enabled but no Graphite server found.")
    else
      write_graphite["config"]["Host"] = graphite_server_results[0]["ipaddress"]
    end
  else
    write_graphite["config"]["Host"] = node["collectd"]["graphite_ipaddress"]
  end
  write_graphite["config"]["Port"] = 2003
end


# flush all of configuration to conf.d/
node["collectd"]["plugins"].each_pair do |plugin_key, definition|
  # Graphite auto-discovery
  collectd_plugin plugin_key.to_s do
    config definition["config"].to_hash if definition["config"]
    template definition["template"].to_s if definition["template"]
    cookbook definition["cookbook"].to_s if definition["cookbook"]
  end
end


conf_d  = "#{node["collectd"]["dir"]}/etc/conf.d"
keys    = node["collectd"]["plugins"].keys.collect { |k| k.to_s }

if File.exist?(conf_d)
  Dir.entries(conf_d).each do |entry|
    file "#{conf_d}/#{entry}" do
      backup false
{'fraction_non_alphanumeric': 0.14334009465855307, 'fraction_numerical': 0.005409060175794456, 'mean_word_length': 4.989878542510121, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6245688', 'n_tokens_mistral': 907, 'n_tokens_neox': 916, 'n_words': 203}