上一篇【第34篇】Elasticsearch管道聚合与聚合实战——bucket_script与移动平均下一篇【第36篇】Elasticsearch集群API——节点类型、信息查询与动态配置摘要Elasticsearch提供了两套风格的API一套是返回JSON格式的标准REST API面向应用程序集成另一套是返回纯文本表格的CAT API面向运维人员的日常操作。CATCompact and TextAPI的设计理念就是让人用肉眼能快速扫读集群状态——不需要解析JSON也不依赖Kibana一条curl命令就能看到核心信息。本文系统讲解CAT API的通用参数v显示表头、help查看字段说明、h自定义列、s排序、format切换输出格式逐一详解十余种常用CAT命令health/indices/nodes/shards/segments/allocation/aliases/thread_pool/plugins/recovery/pending_tasks并通过实战案例演示多命令组合排查集群问题的标准工作流。关键词CAT API、集群健康、分片分布、运维诊断、节点管理。一、CAT API的设计理念1.1 为什么需要CAT APIElasticsearch的标准REST API返回JSON例如GET _cluster/health会返回{cluster_name:my-application,status:yellow,timed_out:false,number_of_nodes:3,number_of_data_nodes:2,active_primary_shards:150,active_shards:295,relocating_shards:0,initializing_shards:0,unassigned_shards:5}JSON适合程序解析但运维人员在终端里快速查看时表格形式要直观得多。CAT API正是为此而生curl-slocalhost:9200/_cat/health?v输出epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1716360000 00:00:00 my-application yellow 3 2 295 150 0 0 5 0 - 98.3%一目了然这就是CAT API的价值。1.2 CAT API的特点特性标准REST APICAT API输出格式JSON纯文本表格默认面向对象应用程序运维人员可读性需要格式化工具直接可读灵活性固定字段结构可自定义显示列典型用途Kibana/SDK集成SSH终端快速排查二、通用参数详解2.1 v参数——显示表头默认情况下CAT API只输出数据行不显示列名。加上vverbose参数后会在第一行显示表头# 不带v只看到数据不知道每列含义curl-slocalhost:9200/_cat/indices# 带v第一行显示列名curl-slocalhost:9200/_cat/indices?v输出示例health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open product abc123 5 1 12000 0 12.5mb 6.2mb yellow open logs def456 3 1 500000 1200 2.1gb 1gb建议日常使用时始终带上v参数除非你对每列含义烂熟于心。2.2 help参数——查看字段说明当表格列太多记不住含义时用help参数查看每个字段的解释curl-slocalhost:9200/_cat/indices?help输出示例epoch | s,seconds since epoch timestamp | t,time in HH:MM:SS health | h,current health status status | s,open/close status index | i,index name uuid | id,index uuid pri | p,number of primary shards rep | r,replica count docs.count | dc,number of docs docs.deleted | dd,deleted docs store.size | ss,store size (incl. tombstones) pri.store.size | primary store size每个字段都有全称和缩写缩写可用于h参数。2.3 h参数——自定义显示列用h参数精确控制要显示哪些列按需精简或扩展# 只显示索引名、健康状态、文档数和存储大小curl-slocalhost:9200/_cat/indices?vhindex,health,docs.count,store.size输出index health docs.count store.size product green 12000 12.5mb logs yellow 500000 2.1gb也可以用字段缩写curl-slocalhost:9200/_cat/indices?vhi,h,dc,ss2.4 s参数——按指定列排序s参数控制结果的排序方式支持升序和降序# 按索引大小降序排列最大的排前面curl-slocalhost:9200/_cat/indices?vsstore.size:desc# 按文档数升序排列curl-slocalhost:9200/_cat/indices?vsdocs.count:asc# 多列排序先按health再按store.size降序curl-slocalhost:9200/_cat/indices?vshealth,store.size:desc2.5 format参数——切换输出格式CAT API默认输出纯文本表格format参数可以切换为其他格式# JSON格式适合脚本处理curl-slocalhost:9200/_cat/indices?formatjson# YAML格式curl-slocalhost:9200/_cat/indices?formatyaml# 逗号分隔CSVcurl-slocalhost:9200/_cat/indices?formatcsvJSON输出示例[{health:green,status:open,index:product,uuid:abc123,pri:5,rep:1,docs.count:12000,store.size:12.5mb},{health:yellow,status:open,index:logs,uuid:def456,pri:3,rep:1,docs.count:500000,store.size:2.1gb}]2.6 通用参数速查表参数含义示例v显示表头列名?vhelp查看所有可用字段及说明?helph自定义显示哪些列?hindex,health,store.sizes按指定列排序:desc降序?sstore.size:descformat输出格式json/yaml/csv/text?formatjsonbytes字节显示单位b/kb/mb/gb?bytesgbtime时间显示格式ms/s/m/h/d?times三、常用CAT命令详解3.1 cat/health——集群健康状态这是最常用的CAT命令快速查看集群整体健康curl-slocalhost:9200/_cat/health?vepoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1716360000 00:00:00 my-application green 3 2 300 150 0 0 0 0 - 100.0%关键字段解读字段含义关注点status集群健康状态green全部正常yellow副本未分配red主分片未分配node.total总节点数与预期节点数对比node.data数据节点数是否有数据节点离线shards总分片数监控分片数量变化unassign未分配分片数大于0需排查pending_tasks待处理任务数持续大于0说明集群压力大active_shards_percent活跃分片百分比低于100%需要关注3.2 cat/indices——索引列表与状态curl-slocalhost:9200/_cat/indices?vsstore.size:deschindex,health,status,pri,rep,docs.count,store.size,pri.store.size输出index health status pri rep docs.count store.size pri.store.size logs yellow open 3 1 500000 2.1gb 1gb product green open 5 1 12000 12.5mb 6.2mb user green open 1 1 5000 3.2mb 1.6mb运维技巧按store.size降序排列可以立即发现占用空间最大的索引。3.3 cat/nodes——节点信息curl-slocalhost:9200/_cat/nodes?vhip,name,node.role,master,heap.percent,ram.percent,cpu,load_1m,disk.used_percent输出ip name node.role master heap.percent ram.percent cpu load_1m disk.used_percent 192.168.1.10 es-node-01 mdhilrst * 45 78 12 2.10 65 192.168.1.11 es-node-02 di - 62 82 8 1.50 72 192.168.1.12 es-node-03 di - 38 75 15 0.80 55node.role字段解读角色代码含义mMaster eligible node有资格竞选主节点dData node数据节点iIngest node摄取节点lMachine Learning node机器学习节点rRemote cluster client远程集群客户端sTransform node转换节点hHot tier热数据层wWarm tier温数据层cCold tier冷数据层tContent tier内容层*当前Master节点3.4 cat/shards——分片分布查看每个索引的分片在哪些节点上分布curl-slocalhost:9200/_cat/shards?vsindex输出示例index shard prirep state docs store ip node logs 0 p STARTED 50000 700mb 192.168.1.11 es-node-02 logs 0 r UNASSIGNED - - logs 1 p STARTED 50000 680mb 192.168.1.12 es-node-03 logs 1 r STARTED 50000 690mb 192.168.1.10 es-node-01 product 0 p STARTED 2400 2.5mb 192.168.1.10 es-node-01 product 0 r STARTED 2400 2.5mb 192.168.1.11 es-node-02prirep字段说明p表示主分片Primaryr表示副本分片Replica。UNASSIGNED状态是需要立即排查的问题可能原因节点离线导致副本无法分配磁盘空间不足分片分配过滤规则限制3.5 cat/segments——段信息查看索引内部的Lucene段信息curl-slocalhost:9200/_cat/segments/product?vhindex,shard,segment,generation,docs.count,deleted,size,searchable输出index shard segment generation docs.count deleted size searchable product 0 _0 0 2400 0 2.5mb true product 0 _1 1 800 0 0.8mb true product 0 _2 2 4000 200 4.2mb true当deleted列数值很大时说明有大量已删除文档等待合并需要考虑触发force merge。3.6 cat/allocation——磁盘分配查看每个节点的磁盘使用和分片分配情况curl-slocalhost:9200/_cat/allocation?v输出shards disk.indices disk.used disk.avail disk.total disk.percent host ip 100 12.5gb 45.2gb 18.8gb 64gb 70 192.168.1.10 192.168.1.10 95 11.2gb 42.1gb 12.9gb 55gb 76 192.168.1.11 192.168.1.11 105 13.8gb 48.5gb 25.5gb 74gb 65 192.168.1.12 192.168.1.12关键关注点disk.percent超过85%时Elasticsearch会停止向该节点分配新分片。3.7 cat/aliases——别名列表curl-slocalhost:9200/_cat/aliases?v输出alias index filter routing.index routing.search is_write_index current product - - - - all_logs logs - - - - v1 product - - - -3.8 cat/thread_pool——线程池状态curl-slocalhost:9200/_cat/thread_pool?vhname,active,queue,rejected,completed输出name active queue rejected completed search 12 3 0 150000 write 5 0 0 80000 generic 2 0 0 30000 snapshot 0 0 0 100 force_merge 0 0 0 5重点关注rejected列大于0线程池拒绝请求说明集群压力大queue列持续增长请求排队中系统可能出现瓶颈3.9 cat/plugins——已安装插件curl-slocalhost:9200/_cat/plugins?v输出name component version es-node-01 analysis-ik 8.12.0 es-node-01 repository-s3 8.12.0 es-node-02 analysis-ik 8.12.0 es-node-03 analysis-ik 8.12.03.10 cat/recovery——恢复状态当集群在恢复数据节点重启、副本重新分配等时查看进度curl-slocalhost:9200/_cat/recovery?vhindex,shard,stage,source_node,target_node,files_percent输出index shard stage source_node target_node files_percent logs 0 index es-node-01 es-node-03 85% logs 1 done es-node-02 es-node-03 100% product 0 done - - 100%3.11 cat/pending_tasks——待处理任务队列curl-slocalhost:9200/_cat/pending_tasks?v输出insertOrder createTime priority source node 1234 14:30:00 HIGH create-index es-node-01 1235 14:30:01 URGENT shard-started es-node-01正常情况下此表为空。出现记录时说明集群管理任务在排队可能导致延迟。3.12 CAT命令速查表命令用途运维频率_cat/health集群健康状态每日必查_cat/indices索引列表与状态高频使用_cat/nodes节点信息与资源高频使用_cat/shards分片分布详情排查分片问题时_cat/segmentsLucene段信息性能调优时_cat/allocation磁盘分配情况每日巡检_cat/aliases索引别名列表按需查看_cat/thread_pool线程池状态性能排查时_cat/plugins已安装插件变更时查看_cat/recovery恢复进度节点恢复时_cat/pending_tasks待处理任务集群压力大时四、进阶技巧4.1 支持模式匹配CAT API支持通配符匹配索引名方便批量操作# 查看所有以logs开头的索引curl-slocalhost:9200/_cat/indices/logs*?v# 查看所有索引curl-slocalhost:9200/_cat/indices/*?v4.2 bytes和time参数控制数值的显示单位让数据更易读# 磁盘大小统一以GB显示curl-slocalhost:9200/_cat/allocation?vbytesgb# 时间统一以秒显示curl-slocalhost:9200/_cat/thread_pool?vtimes4.3 查看更多隐藏命令CAT API还包含一些不常用的命令可通过_cat根路径查看所有可用命令curl-slocalhost:9200/_cat五、实战用CAT API快速诊断集群问题场景一集群状态为yellow当_cat/health显示yellow时表示有副本分片未分配。标准排查流程第一步查看哪个索引有未分配分片curl-slocalhost:9200/_cat/shards?v|grepUNASSIGNEDlogs 0 r UNASSIGNED - - logs 2 r UNASSIGNED - -第二步查看磁盘分配判断是否是空间不足curl-slocalhost:9200/_cat/allocation?v第三步查看节点状态确认是否有节点离线curl-slocalhost:9200/_cat/nodes?v场景二查询响应变慢第一步检查线程池是否有rejectedcurl-slocalhost:9200/_cat/thread_pool/search?vhname,active,queue,rejected第二步检查节点负载curl-slocalhost:9200/_cat/nodes?vhip,name,heap.percent,cpu,load_1m第三步检查是否有分片恢复消耗资源curl-slocalhost:9200/_cat/recovery?vhindex,stage,files_percent|grep-vdone场景三磁盘空间告警第一步查看各节点磁盘使用率curl-slocalhost:9200/_cat/allocation?vhip,disk.used,disk.avail,disk.percentbytesgb第二步查看各索引大小找到最大的索引curl-slocalhost:9200/_cat/indices?vsstore.size:deschindex,store.size,pri,repbytesgb第三步清理过期的日志索引或执行force merge释放空间场景四日常巡检脚本将常用CAT命令组合成一个巡检脚本快速获取集群概况#!/bin/bashES_HOSTlocalhost:9200echo 集群健康 curl-s${ES_HOST}/_cat/health?vechoecho 节点资源 curl-s${ES_HOST}/_cat/nodes?vhip,name,heap.percent,ram.percent,cpu,disk.used_percent,load_1mechoecho 索引概况Top 10 curl-s${ES_HOST}/_cat/indices?vsstore.size:deschindex,health,status,pri,rep,docs.count,store.sizeechoecho 线程池rejected curl-s${ES_HOST}/_cat/thread_pool?vhname,active,queue,rejected|grep-v 0 echoecho 磁盘分配 curl-s${ES_HOST}/_cat/allocation?vbytesgb六、总结与最佳实践核心要点回顾CAT API是运维人员的快速查看工具用表格替代JSON终端友好v参数是日常使用的基础——没有表头的表格几乎没有可读性help参数是学习CAT命令的最佳入口——不清楚某个字段含义时随时查看h和s参数组合是最常用的模式——只看需要的列、按关注维度排序formatjson让CAT API也能被脚本处理兼具可读性和可解析性最佳实践建议日常巡检优先用CAT API比调用Cluster Health API再解析JSON高效得多编写巡检脚本将多个CAT命令组合成脚本定时执行记录巡检结果关注rejected和pending_tasks这两个指标为零是集群健康的底线用bytes和time参数统一单位避免不同单位换算的困扰shards命令加grep是排查利器curl .../_cat/shards | grep UNASSIGNED是排查yellow/red状态的第一步定期检查磁盘分配disk.used_percent超过85%就要预警上一篇【第34篇】Elasticsearch管道聚合与聚合实战——bucket_script与移动平均下一篇【第36篇】Elasticsearch集群API——节点类型、信息查询与动态配置
【Elasticsearch从入门到精通】第35篇:Elasticsearch CAT API完全指南——集群状态可视化查看
上一篇【第34篇】Elasticsearch管道聚合与聚合实战——bucket_script与移动平均下一篇【第36篇】Elasticsearch集群API——节点类型、信息查询与动态配置摘要Elasticsearch提供了两套风格的API一套是返回JSON格式的标准REST API面向应用程序集成另一套是返回纯文本表格的CAT API面向运维人员的日常操作。CATCompact and TextAPI的设计理念就是让人用肉眼能快速扫读集群状态——不需要解析JSON也不依赖Kibana一条curl命令就能看到核心信息。本文系统讲解CAT API的通用参数v显示表头、help查看字段说明、h自定义列、s排序、format切换输出格式逐一详解十余种常用CAT命令health/indices/nodes/shards/segments/allocation/aliases/thread_pool/plugins/recovery/pending_tasks并通过实战案例演示多命令组合排查集群问题的标准工作流。关键词CAT API、集群健康、分片分布、运维诊断、节点管理。一、CAT API的设计理念1.1 为什么需要CAT APIElasticsearch的标准REST API返回JSON例如GET _cluster/health会返回{cluster_name:my-application,status:yellow,timed_out:false,number_of_nodes:3,number_of_data_nodes:2,active_primary_shards:150,active_shards:295,relocating_shards:0,initializing_shards:0,unassigned_shards:5}JSON适合程序解析但运维人员在终端里快速查看时表格形式要直观得多。CAT API正是为此而生curl-slocalhost:9200/_cat/health?v输出epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1716360000 00:00:00 my-application yellow 3 2 295 150 0 0 5 0 - 98.3%一目了然这就是CAT API的价值。1.2 CAT API的特点特性标准REST APICAT API输出格式JSON纯文本表格默认面向对象应用程序运维人员可读性需要格式化工具直接可读灵活性固定字段结构可自定义显示列典型用途Kibana/SDK集成SSH终端快速排查二、通用参数详解2.1 v参数——显示表头默认情况下CAT API只输出数据行不显示列名。加上vverbose参数后会在第一行显示表头# 不带v只看到数据不知道每列含义curl-slocalhost:9200/_cat/indices# 带v第一行显示列名curl-slocalhost:9200/_cat/indices?v输出示例health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open product abc123 5 1 12000 0 12.5mb 6.2mb yellow open logs def456 3 1 500000 1200 2.1gb 1gb建议日常使用时始终带上v参数除非你对每列含义烂熟于心。2.2 help参数——查看字段说明当表格列太多记不住含义时用help参数查看每个字段的解释curl-slocalhost:9200/_cat/indices?help输出示例epoch | s,seconds since epoch timestamp | t,time in HH:MM:SS health | h,current health status status | s,open/close status index | i,index name uuid | id,index uuid pri | p,number of primary shards rep | r,replica count docs.count | dc,number of docs docs.deleted | dd,deleted docs store.size | ss,store size (incl. tombstones) pri.store.size | primary store size每个字段都有全称和缩写缩写可用于h参数。2.3 h参数——自定义显示列用h参数精确控制要显示哪些列按需精简或扩展# 只显示索引名、健康状态、文档数和存储大小curl-slocalhost:9200/_cat/indices?vhindex,health,docs.count,store.size输出index health docs.count store.size product green 12000 12.5mb logs yellow 500000 2.1gb也可以用字段缩写curl-slocalhost:9200/_cat/indices?vhi,h,dc,ss2.4 s参数——按指定列排序s参数控制结果的排序方式支持升序和降序# 按索引大小降序排列最大的排前面curl-slocalhost:9200/_cat/indices?vsstore.size:desc# 按文档数升序排列curl-slocalhost:9200/_cat/indices?vsdocs.count:asc# 多列排序先按health再按store.size降序curl-slocalhost:9200/_cat/indices?vshealth,store.size:desc2.5 format参数——切换输出格式CAT API默认输出纯文本表格format参数可以切换为其他格式# JSON格式适合脚本处理curl-slocalhost:9200/_cat/indices?formatjson# YAML格式curl-slocalhost:9200/_cat/indices?formatyaml# 逗号分隔CSVcurl-slocalhost:9200/_cat/indices?formatcsvJSON输出示例[{health:green,status:open,index:product,uuid:abc123,pri:5,rep:1,docs.count:12000,store.size:12.5mb},{health:yellow,status:open,index:logs,uuid:def456,pri:3,rep:1,docs.count:500000,store.size:2.1gb}]2.6 通用参数速查表参数含义示例v显示表头列名?vhelp查看所有可用字段及说明?helph自定义显示哪些列?hindex,health,store.sizes按指定列排序:desc降序?sstore.size:descformat输出格式json/yaml/csv/text?formatjsonbytes字节显示单位b/kb/mb/gb?bytesgbtime时间显示格式ms/s/m/h/d?times三、常用CAT命令详解3.1 cat/health——集群健康状态这是最常用的CAT命令快速查看集群整体健康curl-slocalhost:9200/_cat/health?vepoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1716360000 00:00:00 my-application green 3 2 300 150 0 0 0 0 - 100.0%关键字段解读字段含义关注点status集群健康状态green全部正常yellow副本未分配red主分片未分配node.total总节点数与预期节点数对比node.data数据节点数是否有数据节点离线shards总分片数监控分片数量变化unassign未分配分片数大于0需排查pending_tasks待处理任务数持续大于0说明集群压力大active_shards_percent活跃分片百分比低于100%需要关注3.2 cat/indices——索引列表与状态curl-slocalhost:9200/_cat/indices?vsstore.size:deschindex,health,status,pri,rep,docs.count,store.size,pri.store.size输出index health status pri rep docs.count store.size pri.store.size logs yellow open 3 1 500000 2.1gb 1gb product green open 5 1 12000 12.5mb 6.2mb user green open 1 1 5000 3.2mb 1.6mb运维技巧按store.size降序排列可以立即发现占用空间最大的索引。3.3 cat/nodes——节点信息curl-slocalhost:9200/_cat/nodes?vhip,name,node.role,master,heap.percent,ram.percent,cpu,load_1m,disk.used_percent输出ip name node.role master heap.percent ram.percent cpu load_1m disk.used_percent 192.168.1.10 es-node-01 mdhilrst * 45 78 12 2.10 65 192.168.1.11 es-node-02 di - 62 82 8 1.50 72 192.168.1.12 es-node-03 di - 38 75 15 0.80 55node.role字段解读角色代码含义mMaster eligible node有资格竞选主节点dData node数据节点iIngest node摄取节点lMachine Learning node机器学习节点rRemote cluster client远程集群客户端sTransform node转换节点hHot tier热数据层wWarm tier温数据层cCold tier冷数据层tContent tier内容层*当前Master节点3.4 cat/shards——分片分布查看每个索引的分片在哪些节点上分布curl-slocalhost:9200/_cat/shards?vsindex输出示例index shard prirep state docs store ip node logs 0 p STARTED 50000 700mb 192.168.1.11 es-node-02 logs 0 r UNASSIGNED - - logs 1 p STARTED 50000 680mb 192.168.1.12 es-node-03 logs 1 r STARTED 50000 690mb 192.168.1.10 es-node-01 product 0 p STARTED 2400 2.5mb 192.168.1.10 es-node-01 product 0 r STARTED 2400 2.5mb 192.168.1.11 es-node-02prirep字段说明p表示主分片Primaryr表示副本分片Replica。UNASSIGNED状态是需要立即排查的问题可能原因节点离线导致副本无法分配磁盘空间不足分片分配过滤规则限制3.5 cat/segments——段信息查看索引内部的Lucene段信息curl-slocalhost:9200/_cat/segments/product?vhindex,shard,segment,generation,docs.count,deleted,size,searchable输出index shard segment generation docs.count deleted size searchable product 0 _0 0 2400 0 2.5mb true product 0 _1 1 800 0 0.8mb true product 0 _2 2 4000 200 4.2mb true当deleted列数值很大时说明有大量已删除文档等待合并需要考虑触发force merge。3.6 cat/allocation——磁盘分配查看每个节点的磁盘使用和分片分配情况curl-slocalhost:9200/_cat/allocation?v输出shards disk.indices disk.used disk.avail disk.total disk.percent host ip 100 12.5gb 45.2gb 18.8gb 64gb 70 192.168.1.10 192.168.1.10 95 11.2gb 42.1gb 12.9gb 55gb 76 192.168.1.11 192.168.1.11 105 13.8gb 48.5gb 25.5gb 74gb 65 192.168.1.12 192.168.1.12关键关注点disk.percent超过85%时Elasticsearch会停止向该节点分配新分片。3.7 cat/aliases——别名列表curl-slocalhost:9200/_cat/aliases?v输出alias index filter routing.index routing.search is_write_index current product - - - - all_logs logs - - - - v1 product - - - -3.8 cat/thread_pool——线程池状态curl-slocalhost:9200/_cat/thread_pool?vhname,active,queue,rejected,completed输出name active queue rejected completed search 12 3 0 150000 write 5 0 0 80000 generic 2 0 0 30000 snapshot 0 0 0 100 force_merge 0 0 0 5重点关注rejected列大于0线程池拒绝请求说明集群压力大queue列持续增长请求排队中系统可能出现瓶颈3.9 cat/plugins——已安装插件curl-slocalhost:9200/_cat/plugins?v输出name component version es-node-01 analysis-ik 8.12.0 es-node-01 repository-s3 8.12.0 es-node-02 analysis-ik 8.12.0 es-node-03 analysis-ik 8.12.03.10 cat/recovery——恢复状态当集群在恢复数据节点重启、副本重新分配等时查看进度curl-slocalhost:9200/_cat/recovery?vhindex,shard,stage,source_node,target_node,files_percent输出index shard stage source_node target_node files_percent logs 0 index es-node-01 es-node-03 85% logs 1 done es-node-02 es-node-03 100% product 0 done - - 100%3.11 cat/pending_tasks——待处理任务队列curl-slocalhost:9200/_cat/pending_tasks?v输出insertOrder createTime priority source node 1234 14:30:00 HIGH create-index es-node-01 1235 14:30:01 URGENT shard-started es-node-01正常情况下此表为空。出现记录时说明集群管理任务在排队可能导致延迟。3.12 CAT命令速查表命令用途运维频率_cat/health集群健康状态每日必查_cat/indices索引列表与状态高频使用_cat/nodes节点信息与资源高频使用_cat/shards分片分布详情排查分片问题时_cat/segmentsLucene段信息性能调优时_cat/allocation磁盘分配情况每日巡检_cat/aliases索引别名列表按需查看_cat/thread_pool线程池状态性能排查时_cat/plugins已安装插件变更时查看_cat/recovery恢复进度节点恢复时_cat/pending_tasks待处理任务集群压力大时四、进阶技巧4.1 支持模式匹配CAT API支持通配符匹配索引名方便批量操作# 查看所有以logs开头的索引curl-slocalhost:9200/_cat/indices/logs*?v# 查看所有索引curl-slocalhost:9200/_cat/indices/*?v4.2 bytes和time参数控制数值的显示单位让数据更易读# 磁盘大小统一以GB显示curl-slocalhost:9200/_cat/allocation?vbytesgb# 时间统一以秒显示curl-slocalhost:9200/_cat/thread_pool?vtimes4.3 查看更多隐藏命令CAT API还包含一些不常用的命令可通过_cat根路径查看所有可用命令curl-slocalhost:9200/_cat五、实战用CAT API快速诊断集群问题场景一集群状态为yellow当_cat/health显示yellow时表示有副本分片未分配。标准排查流程第一步查看哪个索引有未分配分片curl-slocalhost:9200/_cat/shards?v|grepUNASSIGNEDlogs 0 r UNASSIGNED - - logs 2 r UNASSIGNED - -第二步查看磁盘分配判断是否是空间不足curl-slocalhost:9200/_cat/allocation?v第三步查看节点状态确认是否有节点离线curl-slocalhost:9200/_cat/nodes?v场景二查询响应变慢第一步检查线程池是否有rejectedcurl-slocalhost:9200/_cat/thread_pool/search?vhname,active,queue,rejected第二步检查节点负载curl-slocalhost:9200/_cat/nodes?vhip,name,heap.percent,cpu,load_1m第三步检查是否有分片恢复消耗资源curl-slocalhost:9200/_cat/recovery?vhindex,stage,files_percent|grep-vdone场景三磁盘空间告警第一步查看各节点磁盘使用率curl-slocalhost:9200/_cat/allocation?vhip,disk.used,disk.avail,disk.percentbytesgb第二步查看各索引大小找到最大的索引curl-slocalhost:9200/_cat/indices?vsstore.size:deschindex,store.size,pri,repbytesgb第三步清理过期的日志索引或执行force merge释放空间场景四日常巡检脚本将常用CAT命令组合成一个巡检脚本快速获取集群概况#!/bin/bashES_HOSTlocalhost:9200echo 集群健康 curl-s${ES_HOST}/_cat/health?vechoecho 节点资源 curl-s${ES_HOST}/_cat/nodes?vhip,name,heap.percent,ram.percent,cpu,disk.used_percent,load_1mechoecho 索引概况Top 10 curl-s${ES_HOST}/_cat/indices?vsstore.size:deschindex,health,status,pri,rep,docs.count,store.sizeechoecho 线程池rejected curl-s${ES_HOST}/_cat/thread_pool?vhname,active,queue,rejected|grep-v 0 echoecho 磁盘分配 curl-s${ES_HOST}/_cat/allocation?vbytesgb六、总结与最佳实践核心要点回顾CAT API是运维人员的快速查看工具用表格替代JSON终端友好v参数是日常使用的基础——没有表头的表格几乎没有可读性help参数是学习CAT命令的最佳入口——不清楚某个字段含义时随时查看h和s参数组合是最常用的模式——只看需要的列、按关注维度排序formatjson让CAT API也能被脚本处理兼具可读性和可解析性最佳实践建议日常巡检优先用CAT API比调用Cluster Health API再解析JSON高效得多编写巡检脚本将多个CAT命令组合成脚本定时执行记录巡检结果关注rejected和pending_tasks这两个指标为零是集群健康的底线用bytes和time参数统一单位避免不同单位换算的困扰shards命令加grep是排查利器curl .../_cat/shards | grep UNASSIGNED是排查yellow/red状态的第一步定期检查磁盘分配disk.used_percent超过85%就要预警上一篇【第34篇】Elasticsearch管道聚合与聚合实战——bucket_script与移动平均下一篇【第36篇】Elasticsearch集群API——节点类型、信息查询与动态配置