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

Linux下用Grep筛选MongoDB日志中耗时超阈值的op_msg请求

MongoDB日志筛选:指定时间范围+耗时>100ms的op_msg请求

优化命令(两种可选)

方案1:sed+awk组合

sed -n '/2022-09-15T12:26/,/2022-09-15T14:03/p' mongod.log | awk '/protocol:op_msg/ {split($NF, ms, "ms"); if(ms[1] > 100) print $0}'

方案2:单awk命令(更高效)

awk '/2022-09-15T12:26/,/2022-09-15T14:03/ && /protocol:op_msg/ {split($NF, ms, "ms"); if(ms[1] > 100) print $0}' mongod.log

命令说明

  • 时间范围过滤:/2022-09-15T12:26/,/2022-09-15T14:03/ 精准提取该时间段内的所有日志行。
  • op_msg匹配:/protocol:op_msg/ 筛选出使用op_msg协议的请求日志。
  • 耗时判断:通过split($NF, ms, "ms")拆分日志末尾的耗时字段(如523ms),提取纯数值后判断是否大于100,满足条件则输出整行。

原命令问题

原命令仅能精确匹配耗时为523ms的请求,无法覆盖所有耗时>100ms的情况,因此需要结合数值判断逻辑实现范围筛选。

日志示例

7391:2022-11-22T09:23:23.047-0500 I COMMAND [conn26] command test.test appName: "MongoDB Shell" command: find { find: "test", filter: { creationDate: new Date(1663252936409) }, lsid: { id: UUID("7c1bb40c-5e99-4281-9351-893e3d23261d") }, $clusterTime: { clusterTime: Timestamp(1669126970, 1), signature: { hash: BinData(0, B141BFD0978167F8C023DFB4AB32BBB117B3CD80), keyId: 7136078726260850692 } }, $db: "test" } planSummary: COLLSCAN keysExamined:0 docsExamined:337738 cursorExhausted:1 numYields:2640 nreturned:1 queryHash:6F9DC23E planCacheKey:6F9DC23E reslen:304 locks:{ ReplicationStateTransition: { acquireCount: { w: 2641 } }, Global: { acquireCount: { r: 2641 } }, Database: { acquireCount: { r: 2641 } }, Collection: { acquireCount: { r: 2641 } }, Mutex: { acquireCount: { r: 1 } } } storage:{ data: { bytesRead: 28615999, timeReadingMicros: 288402 } } protocol:op_msg 523ms

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 13:05:41