Class: Riemann::Tools::Opensearch

Inherits:
Object
  • Object
show all
Includes:
Riemann::Tools
Defined in:
lib/riemann/tools/opensearch.rb,
lib/riemann/tools/opensearch/version.rb

Defined Under Namespace

Classes: Allocation, Health, JsonMapper

Constant Summary collapse

HEALTH_STATUS_STATE =
{
  "green" => :ok,
  "yellow" => :warning,
  "red" => :critical
}
VERSION =
"1.0.0"

Instance Method Summary collapse

Instance Method Details

#allocationsObject



116
117
118
119
120
# File 'lib/riemann/tools/opensearch.rb', line 116

def allocations
  @allocations ||= @client.cat.allocation(format: "json", bytes: "b").map do |allocation|
    Allocation.new(allocation)
  end
end

#healthObject



122
123
124
125
126
# File 'lib/riemann/tools/opensearch.rb', line 122

def health
  @health ||= @client.cat.health(format: "json").map do |health|
    Health.new(health)
  end.first
end

#setting(name) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/riemann/tools/opensearch.rb', line 132

def setting(name)
  pieces = name.split(".")

  value = settings.dig(*(["persistent"] + pieces)) ||
    settings.dig(*(["transient"] + pieces)) ||
    settings.dig(*(["defaults"] + pieces))

  Integer(value)
rescue ArgumentError
  value
end

#settingsObject



128
129
130
# File 'lib/riemann/tools/opensearch.rb', line 128

def settings
  @settings ||= @client.cluster.get_settings(include_defaults: true)
end

#shard_allocation_state(count, limit) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/riemann/tools/opensearch.rb', line 144

def shard_allocation_state(count, limit)
  if count >= opts[:os_shard_allocation_error] * limit
    :critical
  elsif count >= opts[:os_shard_allocation_warning] * limit
    :warning
  else
    :ok
  end
end

#tickObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/riemann/tools/opensearch.rb', line 79

def tick
  invalidate_cache

  @client = ::OpenSearch::Client.new(
    host: opts[:os_host],
    transport_options: {
      ssl: {
        ca_file: opts[:os_ca_cert],
        client_cert: opts[:os_cert],
        client_key: opts[:os_key],
        verify: !opts[:os_insecure]
      }
    }
  )

  report({
    service: "#{health.cluster} cluster health",
    state: HEALTH_STATUS_STATE[health.status],
    description: health.status
  })

  max_shards_per_node = setting("cluster.max_shards_per_node")
  allocations.each do |allocation|
    report({
      service: "#{health.cluster} #{allocation.node} shard allocation",
      state: shard_allocation_state(allocation.shards, max_shards_per_node),
      metric: allocation.shards,
      description: "#{allocation.shards}/#{max_shards_per_node}"
    })

    report({
      service: "#{health.cluster} #{allocation.node} indices size",
      metric: allocation.disk.indices
    })
  end
end