在 Kibana 中创建 ILM 阶段分布的可视化图表,可以帮助你直观掌握集群中所有索引所处的生命周期阶段(如 hot、warm、cold),及时发现异常(如长期滞留 hot 阶段、未进入 warm 等)。
虽然 ILM 阶段信息不直接存储在文档数据中,但你可以通过以下两种方式实现可视化:
✅ 方法一:使用 Kibana Lens / TSVB + _ilm/explain 数据采集代理
由于 Kibana 无法直接查询 _ilm/explain 接口作为数据源,你需要 先将 ILM 状态写入一个监控索引,再基于该索引创建可视化。
步骤 1:编写脚本采集 ILM 状态并写入 ES
# ilm_monitor.py
import requests
from datetime import datetime
ES_URL = "http://localhost:9200"
MONITOR_INDEX = ".ilm-monitor-logs"
# 获取 ILM 解释信息
def fetch_ilm_state():
resp = requests.get(f"{ES_URL}/_ilm/explain/*")
data = resp.json().get("indices", {})
docs = []
for idx_name, state in data.items():
doc = {
"@timestamp": datetime.utcnow(),
"index": idx_name,
"ilm.phase": state.get("phase"),
"ilm.action": state.get("action"),
"ilm.step": state.get("step"),
"ilm.failure": state.get("failure"), # 失败时有值
"cluster.name": "prod-cluster"
}
docs.append(doc)
return docs
# 写入监控索引
def index_monitor_data(docs):
for doc in docs:
requests.post(f"{ES_URL}/{MONITOR_INDEX}/_doc", json=doc)
if __name__ == "__main__":
try:
ilm_data = fetch_ilm_state()
index_monitor_data(ilm_data)
print(f"Indexed {len(ilm_data)} ILM records.")
except Exception as e:
print("Error:", e)
📌 将此脚本配置为定时任务(如每小时一次):
0 * * * * /usr/bin/python3 /path/to/ilm_monitor.py >> /var/log/ilm-monitor.log 2>&1
步骤 2:在 Kibana 中创建 Index Pattern
- 打开 Kibana → Management → Stack Management → Index Patterns
- 创建新的 Index Pattern:
- 名称:
.ilm-monitor-* - 时间字段选择:
@timestamp
- 名称:
步骤 3:使用 Lens 创建“ILM 阶段分布”饼图
- 进入 Kibana → Visualize Library → Create visualization
- 选择 Lens
- 选择数据源
.ilm-monitor-* - 添加一个 Pie Chart(饼图)
- 配置如下:
- X-axis(切片):
ilm.phase.keyword- Aggregation: Top values of
- Size: 10
- Metrics: Count
- X-axis(切片):
- 标题设为:
ILM Phase Distribution
✅ 效果:显示当前各阶段(hot/warm/cold/delete)索引数量占比。
步骤 4:创建“失败 ILM 索引列表”表格
- 新建一个 TSVB Table
- 查询条件:
ilm.failure:* - 显示字段:
indexilm.phaseilm.actionilm.failure
可设置告警:当 failure 数 > 0 时高亮提醒。
步骤 5:构建 Dashboard 综合展示
创建名为 ILM Monitoring Dashboard 的仪表板,包含:
| 可视化组件 | 类型 | 说明 |
|---|---|---|
| ILM Phase Distribution | Pie Chart | 各阶段分布 |
| ILM Step Status Trend | Line Chart | 按时间统计各 step 趋势 |
| Failed ILM Indices | Table | 列出失败项及原因 |
| Average Age in Hot Phase | Metric | 平均停留时间(需扩展脚本记录) |
✅ 方法二:使用 Elastic Agent + Logs & Metrics UI(高级方案)
如果你使用 Elastic Stack 8.x + Fleet,可以结合:
- Elastic Agent 收集系统指标
- 自定义集成(Custom Integration)定期调用
_ilm/explain - 使用 Data Streams 存储结果
- 在 Logs Explorer / Metrics Explorer 中可视化
更适合大规模自动化部署场景。
✅ 补充技巧:增强监控能力
1. 添加标签区分业务类型
修改脚本,在写入时添加 log_type:
{
"index": "logs-app-000001",
"ilm.phase": "hot",
"log_type": "application"
}
便于按日志来源过滤分析。
2. 计算平均驻留时间
可在脚本中对比 creation_date 与当前时间,估算索引在 hot 阶段停留多久。
3. 设置告警规则(Alerting)
使用 Kibana Alerting 功能:
- 触发条件:
Count of documents where ilm.phase:"hot" and @timestamp < now-24h> 0 - 动作:发送邮件或 Slack 通知
✅ 示例:最终可视化效果描述
| 图表 | 内容 |
|---|---|
| 🟠 饼图 | 70% warm, 20% hot, 10% cold → 健康分布 |
| 🔴 表格 | 显示 logs-db-slow-000005 因磁盘满未能迁移到 warm |
| 📈 折线图 | 过去 7 天每日成功 rollover 数量趋势平稳 |
通过以上方法,你可以在 Kibana 中实现对 ILM 生命周期状态的 全面可视化监控,提升运维效率和系统鲁棒性。


1861

被折叠的 条评论
为什么被折叠?



