Skip to content

Instantly share code, notes, and snippets.

@xcsrz
Created January 7, 2015 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcsrz/cd081af4b29189a241d2 to your computer and use it in GitHub Desktop.
Save xcsrz/cd081af4b29189a241d2 to your computer and use it in GitHub Desktop.
Prints out how how many primary and secondary shards on each node in an Elasticsearch Cluster.
#!/usr/bin/env ruby
require "json"
state = JSON.parse `curl localhost:9200/_cluster/state`
dets = {}
state["nodes"].each do |name, details|
dets[name] = { "name" => details["name"], "address" => details["transport_address"] }
end
nodes = {}
state["routing_nodes"]["nodes"].each do |node,shards|
nodes[node] = {"primary" => 0, "replica" => 0 }
shards.each do |shard|
if shard["primary"]
nodes[node]["primary"] += 1
else
nodes[node]["replica"] += 1
end
end
end
nodes.each do |name, status|
puts "#{name} (#{dets[name]["name"]} - #{dets[name]["address"].gsub(/[^\d\.]/,'').sub(/930\d/,'')}):"
puts "\tPrimary Shards: #{status["primary"]}"
puts "\tReplica Shards: #{status["replica"]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment