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

使用jq从Redfish API返回数据筛选故障磁盘位置失败求助

问题排查:jq筛选Redfish磁盘数据无输出

问题背景

通过iLO接口的Redfish API获取到服务器磁盘JSON数据,需要筛选出StatusIndicator为"Fail"的磁盘,并输出其PhysicalLocation.PartLocation.ServiceLabel,执行指定jq命令后无任何输出。

原始JSON数据

{"Server":"my-pc.org","ArrayController":"DE0080000","Health":"OK","NumDrives":"2","Drives":[{"@odata.id":"/redfish/v1/Systems/1/Storage/DE0080000/Drives/0","@odata.type":"#Drive.v1_14_0.Drive","Id":"0","Name":"15TB 22.5G SAS SSD","Status":{"State":"UnavailableOffline","Health":"Critical"},"LocationIndicatorActive":false,"Model":"VO001920RZWUV","Revision":"HPD0","Protocol":"SAS","MediaType":"SSD","CapacityBytes":1920383410176,"BlockSizeBytes":512,"SerialNumber":"12BA08LTL08","FailurePredicted":false,"PredictedMediaLifeLeftPercent":100,"StatusIndicator":"Fail","Identifiers":[{"DurableName":"58CE38EE21D51FE","DurableNameFormat":"NAA"}],"PhysicalLocation":{"PartLocation":{"LocationType":"Bay","ServiceLabel":"Slot=1:Port=2E:Box=1:Bay=1","LocationOrdinalValue":1}},"HotspareType":"None","EncryptionAbility":"None","WriteCacheEnabled":false,"CapableSpeedGbs":22.5,"NegotiatedSpeedGbs":12,"Operations":[],"Multipath":true,"Links":{"Volumes@odata.count":1,"Volumes":[{"@odata.id":"/redfish/v1/Systems/1/Storage/DE0080000/Volumes/1"}]},"@odata.etag":"\"63838C5C\""},{"@odata.id":"/redfish/v1/Systems/1/Storage/DE0080000/Drives/1","@odata.type":"#Drive.v1_14_0.Drive","Id":"1","Name":"15TB 22.5G SAS SSD","Status":{"State":"Enabled","Health":"OK"},"LocationIndicatorActive":false,"Model":"VO001920RZWUV","Revision":"HPD0","Protocol":"SAS","MediaType":"SSD","CapacityBytes":1920383410176,"BlockSizeBytes":512,"SerialNumber":"12BA08LTL08","FailurePredicted":false,"PredictedMediaLifeLeftPercent":100,"StatusIndicator":"OK","Identifiers":[{"DurableName":"58CE38EE21D51FE","DurableNameFormat":"NAA"}],"PhysicalLocation":{"PartLocation":{"LocationType":"Bay","ServiceLabel":"Slot=1:Port=2E:Box=1:Bay=2","LocationOrdinalValue":2}},"HotspareType":"None","EncryptionAbility":"None","WriteCacheEnabled":false,"CapableSpeedGbs":22.5,"NegotiatedSpeedGbs":12,"Operations":[],"Multipath":true,"Links":{"Volumes@odata.count":1,"Volumes":[{"@odata.id":"/redfish/v1/Systems/1/Storage/DE0080000/Volumes/1"}]},"@odata.etag":"\"6383FE34\""}]}

执行的jq命令

jq -r '.Drives[] | select(.StatusIndicator=="Fail") | .PhysicalLocation.PartLocation.ServiceLabel' json-file

排查步骤与解决方法

1. 验证JSON文件有效性与路径

  • 确认JSON文件路径正确,若文件不在当前工作目录,需使用绝对路径。
  • 执行jq . json-file验证JSON格式是否合法,若能正常格式化输出,说明文件无语法问题。

2. 简化命令定位问题

  • 先执行简化版命令,确认筛选条件是否能匹配到数据:
    jq -r '.Drives[] | select(.StatusIndicator=="Fail")' json-file
    
    • 若此命令能输出第一个磁盘的完整数据,说明筛选条件有效,问题出在后续的字段路径;
    • 若无输出,需检查StatusIndicator字段的拼写、大小写,或是否存在隐藏字符(如空格)。

3. 处理潜在的字段异常

  • 若StatusIndicator值存在隐藏空格,可使用trim处理后再匹配:
    jq -r '.Drives[] | select(.StatusIndicator | trim == "Fail") | .PhysicalLocation.PartLocation.ServiceLabel' json-file
    
  • 为避免嵌套字段缺失导致无输出,可使用可选链操作符(jq 1.6+支持):
    jq -r '.Drives[] | select(.StatusIndicator=="Fail") | .PhysicalLocation?.PartLocation?.ServiceLabel' json-file
    

4. 正确命令验证

若JSON文件路径正确、格式合法,原命令应能输出:Slot=1:Port=2E:Box=1:Bay=1。若仍无输出,需检查终端环境的jq版本(推荐使用1.6及以上版本)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 00:30:31