# Copyright:: 2011-Present, Datadog
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

include_recipe '::dd-agent'

# Monitor elasticsearch
#
# Assuming you have 2 clusters "test" and "prod",
# one with and one without authentication
# you need to set up the following attributes
# node['datadog']['elasticsearch']['instances'] = [
#   {
#     :url => 'http://localhost:9200',
#     ::tags => ['env:test']
#   },
#   {
#     :url => 'http://localhost:19200',
#     :username => 'myuser',
#     :password => 'mypass',
#     :tags => ['env:prod']
#   }
# ]
#
# Additional flags:
#
# If you enable the "pshard_stats" flag, statistics over primary shards
# will be collected by the check and sent to the backend with the
# 'elasticsearch.primary' prefix. It is particularly useful if you want to
# get certain metrics without taking replicas into account. For instance,
# 'elasticsearch.primaries.docs.count` will give you the total number of
# documents in your indexes WITHOUT counting duplicates due to the existence
# of replica shards in your ES cluster
#
# If you enable the "index_stats" flag, the agent will send index level metrics.
# This means that you will be able to get the "elasticsearch.index.*" metrics. For
# more details, visit https://docs.datadoghq.com/integrations/elastic/
#
# The "shard_level_metrics" flag enables metrics and service checks on a per-
# shard basis (all the information is fetched under the /_stats?level=shards
# endpoint). The metrics and service check sent for each shard are named as
# such: elasticsearch.shard.metric.name .
# The shard name is computed according to elasticsearch's documentation. Each
# primary shard is named Pi (ex: P0, P1, P2...) and every replica shards will
# be named Ri (R0, R1, R2). In case the number of replicas is superior to 1,
# we stick to the following convention for shard names : Rx_y where x is the
# number of the associated primary shard while y is the replica number.
#
# Plase note that shard-level metrics will get the following extra tags:
# es_node:<node_name>, es_shard:<shard_name>, es_index:<index_name> and
# es_role:(primary|replica). They will also carry a "shard_specific" tag. It
# should enable you to slice and dice as you please in your DatadogHQ.
#
# Example:
#
# node['datadog']['elasticsearch']['instances'] = [
#   {
#     :url => 'http://localhost:9200',
#     :tags => ['env:test'],
#     :pshard_stats => true,
#     :index_stats => true,
#     :shard_level_metrics: true
#   }
# ]
#

datadog_monitor 'elastic' do
  instances node['datadog']['elasticsearch']['instances']
  logs node['datadog']['elasticsearch']['logs']
  action :add
  notifies :restart, 'service[datadog-agent]' if node['datadog']['agent_start']
end
