ElasticSearch fix number of replicas
problem: some service was configured in a such way that its index number of replicas was bigger than number of servers, because of that cluster become "yellow"
to prevent such things and autome its healing following script may be used:
$max = 2
$hostname = $env:ES_HOSTNAME
$headers = @{Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($env:ES_USERNAME):$($env:ES_PASSWORD)"))}
$indices = Invoke-RestMethod "https://$hostname/_cat/indices?v&health=yellow&h=index,rep" -Headers $headers | ForEach-Object { $_ -replace ' +', "`t" } | ConvertFrom-Csv -Delimiter "`t" | Where-Object rep -GT $max | Select-Object -ExpandProperty index
foreach($index in $indices) {
Invoke-RestMethod -Method Put "https://$hostname/$index/_settings" -ContentType 'application/json' -Headers $headers -Body (@{number_of_replicas = $max} | ConvertTo-Json)
Invoke-RestMethod -Method Post "https://slack.com/api/chat.postMessage" -ContentType 'application/json' -Headers @{ Authorization = "Bearer $($env:SLACK_TOKEN)" } -Body (@{channel = '#demo'; text = "fixed number_of_replicas for $index index in $hostname elastic"} | ConvertTo-Json)
}