id: elastic-ilm-phase-stuck-remediation-guard
namespace: company.team
inputs:
- id: elasticsearch_url
type: STRING
defaults: "http://localhost:9200"
description: Base URL of the Elasticsearch or OpenSearch cluster.
- id: elasticsearch_username
type: STRING
defaults: "{{ secret('ELASTICSEARCH_USERNAME') }}"
description: Username for basic authentication.
- id: elasticsearch_password
type: STRING
defaults: "{{ secret('ELASTICSEARCH_PASSWORD') }}"
description: Password for basic authentication.
- id: index_pattern
type: STRING
defaults: "*"
description: Index name or wildcard pattern to inspect for ILM or ISM status.
Defaults to '*'.
- id: auto_remediate
type: BOOL
defaults: true
description: When true, clear conflicting index settings and dispatch automated
ILM retries.
- id: dry_run
type: BOOL
defaults: false
description: When true, evaluate and report stuck indices without applying
mutating settings or retries.
- id: max_retry_threshold
type: INT
defaults: 10
description: Maximum failed step retry count before flagging index as an
escalated repeated failure.
triggers:
- id: hourly_ilm_guard
type: io.kestra.plugin.core.trigger.Schedule
description: Periodic scheduled audit scanning managed indices for stuck ILM phases.
cron: "0 * * * *"
tasks:
- id: fetch_ilm_explain
type: io.kestra.plugin.core.http.Request
description: Query the Elasticsearch or OpenSearch ILM explain API for targeted indices.
uri: "{{ inputs.elasticsearch_url }}/{{ inputs.index_pattern }}/_ilm/explain"
method: GET
headers:
Authorization: "Basic {{ (inputs.elasticsearch_username ~ ':' ~
inputs.elasticsearch_password) | base64Encode }}"
Accept: "application/json"
options:
allowFailed: true
retry:
type: exponential
interval: PT2S
maxInterval: PT15S
maxAttempts: 3
- id: check_explain_status
type: io.kestra.plugin.core.flow.If
description: Fail early if the cluster explain API returned a non-200 HTTP status code.
condition: "{{ outputs.fetch_ilm_explain.code != 200 }}"
then:
- id: fail_unreachable_cluster
type: io.kestra.plugin.core.execution.Fail
description: Abort execution because the ILM explain endpoint could not be reached.
errorMessage: >-
Failed to query ILM explain API at {{ inputs.elasticsearch_url }}/{{
inputs.index_pattern }}/_ilm/explain. HTTP status: {{
outputs.fetch_ilm_explain.code }}. Response: {{
outputs.fetch_ilm_explain.body }}.
- id: triage_ilm_failures
type: io.kestra.plugin.scripts.python.Script
description: Parse ILM explain API response, detect stuck error phases,
categorize root causes, and prepare remediation plans.
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
containerImage: python:3.11-slim
inputFiles:
explain_response.json: "{{ outputs.fetch_ilm_explain.body ?? '{}' }}"
env:
MAX_RETRY_THRESHOLD: "{{ inputs.max_retry_threshold }}"
script: |
import json
import os
from datetime import datetime, timezone
try:
from kestra import Kestra
except ImportError:
Kestra = None
max_retry_threshold = int(os.environ.get("MAX_RETRY_THRESHOLD", "10"))
try:
with open("explain_response.json", "r", encoding="utf-8") as f:
raw_text = f.read().strip()
data = json.loads(raw_text) if raw_text else {}
except Exception:
data = {}
indices_dict = data.get("indices", data) if isinstance(data, dict) else {}
stuck_indices = []
total_inspected = 0
for idx_name, info in indices_dict.items():
if not isinstance(info, dict):
continue
is_managed = info.get("managed", True) and info.get("enabled", True)
if not is_managed:
continue
total_inspected += 1
step = str(info.get("step") or "")
failed_step = str(info.get("failed_step") or "")
ilm_status = str(info.get("is_ilm_status") or "")
phase = str(info.get("phase") or "unknown")
action = str(info.get("action") or "unknown")
policy = str(info.get("policy") or info.get("index.plugins.index_state_management.policy_id") or "unspecified")
retry_count = int(info.get("failed_step_retry_count") or 0)
step_info = info.get("step_info", {})
if not isinstance(step_info, dict):
step_info = {"reason": str(step_info)}
if not step_info and "info" in info and isinstance(info["info"], dict):
step_info = {
"type": "opensearch_ism_error",
"reason": info["info"].get("cause") or info["info"].get("message") or ""
}
step_type = str(step_info.get("type") or "")
step_reason = str(step_info.get("reason") or "")
is_error = (
step.upper() == "ERROR" or
ilm_status.upper() == "ERROR" or
(failed_step and failed_step != "None" and failed_step != "") or
"ILLEGAL_ARGUMENT_EXCEPTION" in step_type.upper() or
"failed" in step.lower()
)
if not is_error:
continue
text_context = f"{action} {step} {failed_step} {step_type} {step_reason}".lower()
if "rollover" in text_context and ("alias" in text_context or "does not point" in text_context):
category = "ROLLOVER_ALIAS_MISSING"
recommended_action = "Clear read-only block and re-evaluate rollover alias target"
has_settings_remediation = True
settings_payload = {"index": {"blocks.read_only_allow_delete": None}}
elif "shrink" in text_context and any(k in text_context for k in ["shard", "routing", "allocation", "cannot be shrunk", "node"]):
category = "SHRINK_SHARD_ROUTING_MISMATCH"
recommended_action = "Enforce read-only write block for shrink shard routing"
has_settings_remediation = True
settings_payload = {"index": {"blocks.write": "true"}}
elif any(k in phase.lower() for k in ["frozen", "cold"]) or any(k in text_context for k in ["snapshot", "mount", "timeout", "timed out", "repository"]):
category = "FROZEN_TIER_TIMEOUT"
recommended_action = "Retry mount step after storage snapshot repository clears"
has_settings_remediation = False
settings_payload = None
else:
category = "GENERIC_PHASE_ERROR"
recommended_action = "Clear conflicting blocks and dispatch ILM retry"
has_settings_remediation = True
settings_payload = {"index": {"blocks.read_only_allow_delete": None}}
exceeded_threshold = retry_count >= max_retry_threshold
stuck_indices.append({
"index": idx_name,
"policy": policy,
"phase": phase,
"action": action,
"step": step,
"failed_step": failed_step or step,
"retry_count": retry_count,
"retry_threshold_exceeded": exceeded_threshold,
"error_type": step_type or "unknown_error",
"error_reason": step_reason or "No error reason returned by cluster explain API",
"failure_category": category,
"recommended_action": recommended_action,
"has_settings_remediation": has_settings_remediation,
"settings_payload": settings_payload
})
lines = []
for item in stuck_indices:
thresh_alert = " :warning: *[RETRY THRESHOLD EXCEEDED]*" if item["retry_threshold_exceeded"] else ""
lines.append(
f"• *{item['index']}* (Policy: `{item['policy']}`, Phase: `{item['phase']}`, Action: `{item['action']}`){thresh_alert}\n"
f" Category: *{item['failure_category']}* | Failed Step: `{item['failed_step']}` (Retries: {item['retry_count']})\n"
f" Error: `{item['error_type']}`: {item['error_reason']}\n"
f" Action: {item['recommended_action']}"
)
slack_summary = "\n\n".join(lines) if lines else "No stuck indices detected."
kv_payload = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"total_inspected": total_inspected,
"stuck_count": len(stuck_indices),
"status": "ERROR" if stuck_indices else "HEALTHY",
"stuck_indices": stuck_indices
}
output_data = {
"total_inspected": total_inspected,
"stuck_count": len(stuck_indices),
"has_stuck_indices": len(stuck_indices) > 0,
"stuck_indices": stuck_indices,
"slack_summary": slack_summary,
"kv_payload": kv_payload
}
if Kestra:
Kestra.outputs(output_data)
else:
print("::" + json.dumps({"outputs": output_data}) + "::")
- id: persist_telemetry_kv
type: io.kestra.plugin.core.kv.Set
description: Persist the latest ILM health manifest and stuck index history in
Kestra KV store.
key: "elastic_ilm_stuck_remediation_history"
kvType: JSON
overwrite: true
value: "{{ outputs.triage_ilm_failures.vars.kv_payload | toJson }}"
- id: gate_stuck_indices
type: io.kestra.plugin.core.flow.If
description: Evaluate if stuck indices require Slack triage notification and
remediation actions.
condition: "{{ outputs.triage_ilm_failures.vars.has_stuck_indices }}"
then:
- id: dispatch_slack_triage
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Post immediate operational triage alert to Slack with exact step
error details.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: >-
{{ {
"text": "*Elasticsearch / OpenSearch ILM Phase Stuck Alert*\nFound " ~ outputs.triage_ilm_failures.vars.stuck_count ~ " stuck index(es) on `" ~ inputs.index_pattern ~ "` in cluster `" ~ inputs.elasticsearch_url ~ "`.\n\n" ~ outputs.triage_ilm_failures.vars.slack_summary ~ "\n\n*Remediation Mode:* auto_remediate=" ~ inputs.auto_remediate ~ ", dry_run=" ~ inputs.dry_run ~ " (Execution: `" ~ execution.id ~ "`)"
} | toJson }}
- id: evaluate_remediation_mode
type: io.kestra.plugin.core.flow.If
description: Execute automated index remediation when auto_remediate is true and
dry_run is disabled.
condition: "{{ inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: remediate_indices
type: io.kestra.plugin.core.flow.Loop
description: Iterate sequentially over each stuck index to clear conflicting
settings and dispatch retries.
values: "{{ outputs.triage_ilm_failures.vars.stuck_indices }}"
concurrencyLimit: 1
tasks:
- id: check_settings_fix
type: io.kestra.plugin.core.flow.If
description: Check if conflicting settings need to be cleared or applied before
retrying ILM.
condition: "{{ item.value.has_settings_remediation }}"
then:
- id: apply_settings
type: io.kestra.plugin.core.http.Request
description: Clear conflicting index settings or enforce required routing.
uri: "{{ inputs.elasticsearch_url }}/{{ item.value.index }}/_settings"
method: PUT
headers:
Authorization: "Basic {{ (inputs.elasticsearch_username ~ ':' ~
inputs.elasticsearch_password) | base64Encode }}"
Content-Type: "application/json"
body: "{{ item.value.settings_payload | toJson }}"
options:
allowFailed: true
- id: retry_ilm_phase
type: io.kestra.plugin.core.http.Request
description: Dispatch retry request to re-execute the failed ILM lifecycle step.
uri: "{{ inputs.elasticsearch_url }}/{{ item.value.index }}/_ilm/retry"
method: POST
headers:
Authorization: "Basic {{ (inputs.elasticsearch_username ~ ':' ~
inputs.elasticsearch_password) | base64Encode }}"
Accept: "application/json"
options:
allowFailed: true
- id: log_remediation_result
type: io.kestra.plugin.core.log.Log
description: Log remediation attempt for the index.
message: >-
Remediation dispatched for index '{{ item.value.index }}'
(failure: {{ item.value.failure_category }}). Retry response
HTTP code: {{ outputs.retry_ilm_phase.code }}.
else:
- id: log_simulation_mode
type: io.kestra.plugin.core.log.Log
description: Record that remediation actions were bypassed due to dry_run or
auto_remediate flags.
message: >-
Remediation skipped: dry_run={{ inputs.dry_run }},
auto_remediate={{ inputs.auto_remediate }}. Found {{
outputs.triage_ilm_failures.vars.stuck_count }} stuck index(es).
else:
- id: log_cluster_healthy
type: io.kestra.plugin.core.log.Log
description: Log confirmation when all inspected indices are healthy in their
expected lifecycle phase.
message: >-
ILM audit completed cleanly. Inspected {{
outputs.triage_ilm_failures.vars.total_inspected }} index(es) on
pattern '{{ inputs.index_pattern }}'. No stuck phases found.
errors:
- id: alert_on_flow_error
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Dispatch emergency alert to Slack if an unexpected execution error occurs.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: >-
{{ {
"text": ":rotating_light: *Flow Execution Failed*: `elastic-ilm-phase-stuck-remediation-guard` (Execution: `" ~ execution.id ~ "`). Failed to complete ILM scan or remediation pass. Check cluster health and network reachability."
} | toJson }}