Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility updates and new commands #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
hubot-elasticsearch
==

A Hubot script for interacting with an [elasticsearch](http://www.elasticsearch.org/x) cluster
A Hubot script for interacting with an [elasticsearch](http://www.elasticsearch.org/) cluster

Installation
---
Expand Down
116 changes: 90 additions & 26 deletions src/elasticsearch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
# Get ElasticSearch Cluster Information
#
# Commands:
# hubot: elasticsearch cluster health [cluster] - Gets the cluster health for the given server or alias
# hubot: elasticsearch cat nodes [cluster] - Gets the information from the cat nodes endpoint for the given server or alias
# hubot: elasticsearch cat indexes [cluster] - Gets the information from the cat indexes endpoint for the given server or alias
# hubot: elasticsearch cat allocation [cluster] - Gets the information from the cat allocation endpoint for the given server or alias
# hubot: elasticsearch clear cache [cluster] - Clears the cache for the specified cluster
# hubot: elasticsearch cluster settings [cluster] - Gets a list of all of the settings stored for the cluster
# hubot: elasticsearch index settings [cluster] [index] - Gets a list of all of the settings stored for a particular index
# hubot: elasticsearch disable allocation [cluster] - disables shard allocation to allow nodes to be taken offline
# hubot: elasticsearch enable allocation [cluster] - renables shard allocation
# hubot: elasticsearch show aliases - shows the aliases for the list of ElasticSearch instances
# hubot: elasticsearch add alias [alias name] [url] - sets the alias for a given url
# hubot: elasticsearch clear alias [alias name] - please note that this needs to include any port numbers as appropriate
# hubot: elasticsearch cluster health [cluster] - Gets the cluster health for the given server or alias
# hubot: elasticsearch cat nodes [cluster] - Gets the information from the cat nodes endpoint for the given server or alias
# hubot: elasticsearch cat indices <like logastsh*> [cluster] - Gets the information from the cat indexes endpoint for the given server or alias
# hubot: elasticsearch cat allocation [cluster] - Gets the information from the cat allocation endpoint for the given server or alias
# hubot: elasticsearch cat recovery [cluster] - Gets the information from the cat allocation endpoint for the given server or alias
# hubot: elasticsearch clear cache [cluster] - Clears the cache for the specified cluster
# hubot: elasticsearch cluster settings [cluster] - Gets a list of all of the settings stored for the cluster
# hubot: elasticsearch concurrent recoveries [number] - Sets the number of simultaneous shards to recover
# hubot: elasticsearch index settings [cluster] [index] - Gets a list of all of the settings stored for a particular index
# hubot: elasticsearch disable allocation [cluster] - disables shard allocation to allow nodes to be taken offline
# hubot: elasticsearch enable allocation [cluster] - renables shard allocation
# hubot: elasticsearch show aliases - shows the aliases for the list of ElasticSearch instances
# hubot: elasticsearch add alias [alias name] [url] - sets the alias for a given url
# hubot: elasticsearch clear alias [alias name] - please note that this needs to include any port numbers as appropriate
#
# Notes:
# The server must be a fqdn (with the port!) to get to the elasticsearch cluster
Expand All @@ -31,15 +33,23 @@ module.exports = (robot) ->
if robot.brain.data.elasticsearch_aliases?
_esAliases = robot.brain.data.elasticsearch_aliases

formattedSend = (text, msg) ->
if robot.adapterName == 'slack'
msg.send("```#{text}```")
else if robot.adapterName == 'shell'
msg.send("\n#{text}")
else
msg.send("/code #{text}")

clusterHealth = (msg, alias) ->
cluster_url = _esAliases[alias]

if cluster_url == "" || cluster_url == undefined
msg.send("Do not recognise the cluster alias: #{alias}")
else
msg.http("#{cluster_url}/_cat/health?pretty=true")
msg.http("#{cluster_url}/_cat/health?v")
.get() (err, res, body) ->
msg.send("/code #{body}")
formattedSend(body, msg)

catNodes = (msg, alias) ->
cluster_url = _esAliases[alias]
Expand All @@ -48,26 +58,30 @@ module.exports = (robot) ->
msg.send("Do not recognise the cluster alias: #{alias}")
else
msg.send("Getting the cat stats for the cluster: #{cluster_url}")
msg.http("#{cluster_url}/_cat/nodes?h=host,heapPercent,load,segmentsMemory,fielddataMemory,filterCacheMemory,idCacheMemory,percolateMemory,u,heapMax,nodeRole,master")
msg.http("#{cluster_url}/_cat/nodes?h=host,heapPercent,load,segmentsMemory,fielddataMemory,filterCacheMemory,idCacheMemory,percolateMemory,u,heapMax,nodeRole,master&v")
.get() (err, res, body) ->
lines = body.split("\n")
header = lines.shift()
list = [header].concat(lines.sort().reverse()).join("\n")
msg.send("/code #{list}")
formattedSend(list, msg)

catIndexes = (msg, alias) ->
catIndexes = (msg, alias, filter) ->
cluster_url = _esAliases[alias]

if cluster_url == "" || cluster_url == undefined
msg.send("Do not recognise the cluster alias: #{alias}")
else
msg.send("Getting the cat indices for the cluster: #{cluster_url}")
msg.http("#{cluster_url}/_cat/indices/logstash-*?h=idx,sm,fm,fcm,im,pm,ss,sc,dc&v")
if filter
index_url = "#{cluster_url}/_cat/indices/#{filter}?h=idx,sm,fm,fcm,im,pm,ss,sc,dc&v"
else
index_url = "#{cluster_url}/_cat/indices/?h=idx,sm,fm,fcm,im,pm,ss,sc,dc&v"
msg.http(index_url)
.get() (err, res, body) ->
lines = body.split("\n")
header = lines.shift()
list = [header].concat(lines.sort().reverse()).join("\n")
msg.send("/code #{list}")
formattedSend(list, msg)

catAllocation = (msg, alias) ->
cluster_url = _esAliases[alias]
Expand All @@ -81,7 +95,24 @@ module.exports = (robot) ->
lines = body.split("\n")
header = lines.shift()
list = [header].concat(lines.sort().reverse()).join("\n")
msg.send("/code #{list}")
formattedSend(list, msg)

recoveryIsDone = (recovery) ->
return !recovery.match(/.*\s+\d+\s+\d+\s+[a-z]*\s+done/i)

catRecovery = (msg, alias) ->
cluster_url = _esAliases[alias]

if cluster_url == "" || cluster_url == undefined
msg.send("Do not recognise the cluster alias: #{alias}")
else
msg.send("Getting the cat allocation for the cluster: #{cluster_url}")
msg.http("#{cluster_url}/_cat/recovery/?v")
.get() (err, res, body) ->
lines = body.split("\n")
header = lines.shift()
list = [header].concat(lines.filter(recoveryIsDone).sort().reverse()).join("\n")
formattedSend(list, msg)

clearCache = (msg, alias) ->
cluster_url = _esAliases[alias]
Expand All @@ -98,6 +129,25 @@ module.exports = (robot) ->
failure = json['_shards']['failed']
msg.send "Results: \n Total Shards: #{shards} \n Successful: #{successful} \n Failure: #{failure}"

concurrentRecoveries = (msg, count, alias) ->
cluster_url = _esAliases[alias]

if cluster_url == "" || cluster_url == undefined
msg.send("Do not recognise the cluster alias: #{alias}")
else
msg.send("Disabling Allocation for the cluster #{cluster_url}")

data = {
'transient': {
'cluster.routing.allocation.node_concurrent_recoveries': count
}
}

json = JSON.stringify(data)
msg.http("#{cluster_url}/_cluster/settings")
.put(json) (err, res, body) ->
formattedSend(body, msg)

disableAllocation = (msg, alias) ->
cluster_url = _esAliases[alias]

Expand All @@ -115,7 +165,7 @@ module.exports = (robot) ->
json = JSON.stringify(data)
msg.http("#{cluster_url}/_cluster/settings")
.put(json) (err, res, body) ->
msg.send("/code #{body}")
formattedSend(body, msg)

enableAllocation = (msg, alias) ->
cluster_url = _esAliases[alias]
Expand All @@ -134,7 +184,7 @@ module.exports = (robot) ->
json = JSON.stringify(data)
msg.http("#{cluster_url}/_cluster/settings")
.put(json) (err, res, body) ->
msg.send("/code #{body}")
formattedSend(body, msg)

showClusterSettings = (msg, alias) ->
cluster_url = _esAliases[alias]
Expand All @@ -145,7 +195,7 @@ module.exports = (robot) ->
msg.send("Getting the Cluster settings for #{cluster_url}")
msg.http("#{cluster_url}/_cluster/settings?pretty=true")
.get() (err, res, body) ->
msg.send("/code #{body}")
formattedSend(body, msg)

showIndexSettings = (msg, alias, index) ->
cluster_url = _esAliases[alias]
Expand All @@ -156,7 +206,7 @@ module.exports = (robot) ->
msg.send("Getting the Index settings for #{index} on #{cluster_url}")
msg.http("#{cluster_url}/#{index}/_settings?pretty=true")
.get() (err, res, body) ->
msg.send("/code #{body}")
formattedSend(body, msg)

showAliases = (msg) ->

Expand All @@ -183,11 +233,11 @@ module.exports = (robot) ->
catNodes msg, msg.match[1], (text) ->
msg.send text

robot.hear /elasticsearch cat indexes (.*)/i, (msg) ->
robot.hear /elasticsearch cat (indexes|indices)( like (.*))? (.*)/i, (msg) ->
if msg.message.user.id is robot.name
return

catIndexes msg, msg.match[1], (text) ->
catIndexes msg, msg.match[4], msg.match[3], (text) ->
msg.send text

robot.hear /elasticsearch cat allocation (.*)/i, (msg) ->
Expand All @@ -197,6 +247,13 @@ module.exports = (robot) ->
catAllocation msg, msg.match[1], (text) ->
msg.send text

robot.hear /elasticsearch cat recovery (.*)/i, (msg) ->
if msg.message.user.id is robot.name
return

catRecovery msg, msg.match[1], (text) ->
msg.send text

robot.hear /elasticsearch cluster settings (.*)/i, (msg) ->
if msg.message.user.id is robot.name
return
Expand Down Expand Up @@ -246,6 +303,13 @@ module.exports = (robot) ->
clearCache msg, msg.match[1], (text) ->
msg.send(text)

robot.respond /elasticsearch concurrent recoveries (\d*) (.*)/i, (msg) ->
if msg.message.user.id is robot.name
return

concurrentRecoveries msg, msg.match[1], msg.match[2], (text) ->
msg.send(text)

robot.respond /elasticsearch disable allocation (.*)/i, (msg) ->
if msg.message.user.id is robot.name
return
Expand Down