#
# Cookbook:: irrigate-datadog
#

directory node['datadog']['neo4j']['path'] do
  owner 'dd-agent'
  group node['datadog']['group']
end

datadog_neo4j_wheel = if node['datadog']['neo4j']['version'].chr.to_i >= 4
                        "#{node['datadog']['neo4j']['path']}/datadog_neo4j-#{node['datadog']['neo4j']['wheel']['version']}-py3-none-any.whl"
                      else
                        "#{node['datadog']['neo4j']['path']}/datadog_neo4j-#{node['datadog']['neo4j']['wheel']['version']}-py2.py3-none-any.whl"
                      end

remote_file 'datadog-neo4j' do
  source node['datadog']['neo4j']['wheel']['source']
  path datadog_neo4j_wheel
  owner 'dd-agent'
  group node['datadog']['group']
  mode '0640'
end

# This installs the datadog neo4j integration, which is wiped out on datadog yum updates,
# so guard installation by checking for integration existence
execute 'install_datadog_neo4j_integration' do
  command "umask 0022 && /usr/bin/datadog-agent integration install -w #{datadog_neo4j_wheel}"
  cwd node['datadog']['neo4j']['path']
  not_if '/usr/bin/datadog-agent integration show datadog-neo4j'
  notifies :restart, 'service[datadog-agent]', :delayed if node['datadog']['agent_start']
  user 'dd-agent'
end

readable_directories = [
  '/etc/datadog-agent/conf.d/neo4j.d',
  '/var/log/neo4j',
]

readable_directories.each do |dir|
  directory dir do
    group node['datadog']['group']
  end
end

# Set Neo4j credentials
if node['datadog']['neo4j_credentials_data_bag_name'] && !node['datadog']['neo4j_credentials_data_bag_name'].empty?
  neo4j_credentials = data_bag_item('secrets', node['datadog']['neo4j_credentials_data_bag_name'])
  node.default['datadog']['neo4j']['user']     = neo4j_credentials['user'].chomp
  node.default['datadog']['neo4j']['password'] = neo4j_credentials['password'].chomp
end

# Overwrite default maximum size of single log message in bytes.
if node['datadog']['max_message_size_bytes'] && (node['datadog']['max_message_size_bytes'] || 0) > 0
  node.default['datadog']['extra_config']['logs_config'] = {
    'max_message_size_bytes' => node['datadog']['max_message_size_bytes'],
  }
end

neo4j_instance = [
  {
    'neo4j_url' => node['datadog']['neo4j']['url'],
    'host' => (node['datadog']['neo4j']['url'] if node['datadog']['neo4j']['version'].chr.to_i >= 4),
    'openmetrics_endpoint' => ("#{node['datadog']['neo4j']['url']}:#{node['datadog']['neo4j']['prometheus_port']}/metrics" if node['datadog']['neo4j']['version'].chr.to_i >= 4),
    'neo4j_dbs' => (node['datadog']['neo4j']['neo4j_dbs'] if node['datadog']['neo4j']['version'].chr.to_i >= 4),
    'extra_metrics' => (node['datadog']['neo4j']['extra_metrics'] if node['datadog']['neo4j']['version'].chr.to_i >= 4),
    'port' => node['datadog']['neo4j']['port'],
    'user' => node['datadog']['neo4j']['user'],
    'password' => node['datadog']['neo4j']['password'],
    'connect_timeout' => node['datadog']['neo4j']['connect_timeout'],
    'server_name' => node['datadog']['neo4j']['server_name'],
    'neo4j_version' => node['datadog']['neo4j']['version'],
    'tags' => [
      "neo4j_cluster:#{node['datadog']['neo4j']['cluster_name']}",
    ],
  }.compact,
]

neo4j_logs = node['datadog']['neo4j']['logs']

unless neo4j_logs.empty?
  # Enable logs agent if neo4j log files are specified
  node.default['datadog']['enable_logs_agent'] = true

  # Make sure datadog agent can read each specified neo4j log
  neo4j_logs.each do |log|
    file log['path'] do
      owner node['datadog']['neo4j']['server_user']
      group node['datadog']['group']
    end
  end
end

datadog_monitor 'neo4j' do
  instances neo4j_instance
  logs neo4j_logs
  action :add
  use_integration_template true
  notifies :restart, 'service[datadog-agent]' if node['datadog']['agent_start']
end

tag_array = []
node['datadog']['tags'].each do |k, v|
  tag_array.push("#{k}:#{v}")
end

template '/etc/datadog-agent/checks.d/custom_neo4j.py' do
  source 'custom_neo4j.py.erb'
  owner 'root'
  group node['datadog']['group']
  mode '0640'
  variables(
    tags: tag_array
  )
  sensitive true
end

directory '/etc/datadog-agent/conf.d/custom_neo4j.d' do
  owner node['datadog']['group']
  group node['datadog']['group']
  mode '0755'
  recursive true
  action :create
end

file '/etc/datadog-agent/conf.d/custom_neo4j.d/conf.yaml' do
  owner node['datadog']['group']
  group node['datadog']['group']
  mode '0640'
  sensitive true
end

custom_neo4j_instance = [
  {
    'min_collection_interval' => 30,
  },
]

datadog_monitor 'custom_neo4j' do
  instances custom_neo4j_instance
  action :add
  use_integration_template true
  notifies :restart, 'service[datadog-agent]' if node['datadog']['agent_start']
end
