You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Elastic Cloud Watcher中配置未分配分片告警?

解决Elastic Cloud中未分配分片告警的Watcher配置问题

首先明确:你提到的GET _cat/shards查询逻辑完全可以通过Elastic Watcher实现,但你的现有代码存在几个关键问题,导致Condition无法正常工作:

  1. Painless脚本里不能直接执行HTTP请求(你写的def nodes = GET _cat/shards...是错误写法)
  2. Input请求的是_cluster/stats,但Condition没有基于这个接口返回的数据做判断,逻辑完全脱节
  3. 用文本格式的_cat/shards统计未分配分片效率低,建议用返回JSON格式的API更易处理

修正后的两种实现方案

方案一:用_cluster/health快速获取未分配分片数(最简单)

这个接口直接返回集群的核心健康指标,其中unassigned_shards字段就是未分配分片的总数,不需要额外统计:

{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "http": {
      "request": {
        "scheme": "https",
        "host": "your-elastic-cloud-host",
        "port": 9243,
        "method": "get",
        "path": "/_cluster/health",
        "auth": {
          "basic": {
            "username": "your-username",
            "password": "your-password"
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": "ctx.payload.unassigned_shards > 300", // 这里替换为你的阈值x
      "lang": "painless"
    }
  },
  "actions": {
    "send_email_alert": {
      "email": {
        "profile": "standard",
        "to": ["me@examplemail.com"],
        "subject": "【告警】Elastic集群未分配分片数量超标",
        "body": {
          "text": "当前未分配分片数: {{ctx.payload.unassigned_shards}},已超过阈值300,请及时排查。"
        }
      }
    }
  }
}

方案二:用_cat/shards的JSON格式统计(适合需要详细分片信息的场景)

如果需要基于_cat/shards的结果统计(比如区分主分片/副本分片),可以请求其JSON格式输出,再统计state为UNASSIGNED的分片数量:

{
  "trigger": {
    "schedule": {
      "interval": "1m"
    }
  },
  "input": {
    "http": {
      "request": {
        "scheme": "https",
        "host": "your-elastic-cloud-host",
        "port": 9243,
        "method": "get",
        "path": "/_cat/shards",
        "params": {
          "format": "json",
          "h": "index,shard,prirep,state",
          "s": "state"
        },
        "auth": {
          "basic": {
            "username": "your-username",
            "password": "your-password"
          }
        }
      }
    }
  },
  "condition": {
    "script": {
      "source": "ctx.payload.stream().filter(shard -> shard.state == 'UNASSIGNED').count() > 300", // 替换为你的阈值x
      "lang": "painless"
    }
  },
  "actions": {
    "send_email_alert": {
      "email": {
        "profile": "standard",
        "to": ["me@examplemail.com"],
        "subject": "【告警】Elastic集群未分配分片数量超标",
        "body": {
          "text": "当前未分配分片数: {{ctx.payload.stream().filter(shard -> shard.state == 'UNASSIGNED').count()}},已超过阈值300,请及时排查。"
        }
      }
    }
  }
}

关键修正说明

  • Input与Condition关联:所有Condition的判断都基于ctx.payload,也就是Input请求返回的数据,不能在脚本里直接发起新的HTTP请求
  • API选择:优先用JSON格式的集群API,避免处理文本格式的_cat输出带来的额外复杂度
  • 权限注意:确保你的Watcher使用的账号拥有cluster:monitor/health/read或cluster:monitor/cat/shards/read等对应权限(Elastic Cloud默认的管理员账号通常具备)

内容的提问来源于stack exchange,提问作者Akhil Sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 17:55:08