mongodb日志分析工具调研

93 阅读2分钟

背景

Mongodb 4.4+版本后,mongo的日志变成了json格式,由于之前日志分析一直使用的是mtools, mtools不支持json格式的日志,需要调研新的分析工具,推荐使用方案3




解决方案

方案1:

利用开源的日志转换工具将mongodb json格式的日志转换成之前的日志格式

github.com/ricojf-mong…

该方式的转换效率无法保障

方案3中的hatchet也可以操作转化,效果测试来看优于该方法



方案2:

利用jq根据日志关键字查询日志,做分析参考:www.mongodb.com/zh-cn/docs/…

启用慢操作日志记录后,以下内容仅返回用时超过 2000 毫秒的慢操作,以供进一步分析:

jq 'select(.attr.workingMillis>=2000)' /var/log/mongodb/mongod.log


方案3:(推荐)

网上开源的mongodb 慢日志分析工具

www.mydbops.com/blog/maximi…'

github.com/simagix/hat…

依赖:go (go1.21以上)

go1.21.0.linux-amd64.tar.gz

golang下载安装(目前测试用的是go1.21)

https://go.dev/dl/ 安装包官方地址

# 解压并加入/usr/local目录
$ tar -C /usr/local -zxf go1.21.1.linux-amd64.tar.gz

# 导出go环境变量
$ vim /etc/profile
  # 在文件最后加上下面这些
  export GOROOT=/usr/local/go
  export GOPATH=/root/go
  export GOPROXY=https://goproxy.cn,direct
  export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

# 刷新环境变量
$ source /etc/profile

# 查看go版本,检查是否安装成功
$ go version

安装运行 hatchet

hatchet-main.zip

源build.sh脚本依赖git, 不依赖git可修改脚本为

#! /bin/bash
# Copyright 2022-present Kuei-chun Chen. All rights reserved.
# build.sh

die() { echo "$*" 1>&2 ; exit 1; }
VERSION="v$(cat version)-$(date +'%Y%m%d')"
REPO="hatchet"
LDFLAGS="-X main.version=$VERSION -X main.repo=$REPO"
TAG="simagix/hatchet"
[[ "$(which go)" = "" ]] && die "go command not found"

gover=$(go version | cut -d' ' -f3)
[[ "$gover" < "go1.21" ]] && die "go version 1.21 or above it recommended."

if [ ! -f go.sum ]; then
    go mod tidy
fi

mkdir -p dist
if [ "$1" == "docker" ]; then
  BR="latest"
  docker build --no-cache -f Dockerfile -t ${TAG}:${BR} .
  docker run ${TAG}:${BR} /hatchet -version
elif [ "$1" == "dist" ]; then
  ofile="./dist/hatchet-$(uname|tr '[:upper:]' '[:lower:]')-$(uname -m)"
  go build -ldflags "$LDFLAGS" -o ${ofile} main/hatchet.go
else
  rm -f ./dist/hatchet
  go build -ldflags "$LDFLAGS" -o ./dist/hatchet main/hatchet.go
  if [[ -f ./dist/hatchet ]]; then
    ./dist/hatchet -version
  fi 
fi

安装

[root@fynn-test-mongo app]# cd hatchet-main
[root@fynn-test-mongo hatchet-main]# 
[root@fynn-test-mongo hatchet-main]# ./build.sh
go: downloading github.com/aws/aws-sdk-go v1.44.219
go: downloading github.com/brianvoe/gofakeit/v6 v6.24.0
go: downloading github.com/julienschmidt/httprouter v1.3.0
go: downloading github.com/mattn/go-sqlite3 v1.14.16
go: downloading github.com/simagix/gox v0.2.3
go: downloading go.mongodb.org/mongo-driver v1.14.0
go: downloading golang.org/x/text v0.14.0
go: downloading github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d
go: downloading github.com/golang/snappy v0.0.1
go: downloading github.com/klauspost/compress v1.13.6
go: downloading github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe
go: downloading golang.org/x/crypto v0.17.0
go: downloading github.com/xdg-go/scram v1.1.2
go: downloading github.com/xdg-go/stringprep v1.0.4
go: downloading golang.org/x/sync v0.1.0
go: downloading github.com/xdg-go/pbkdf2 v1.0.0
go: downloading github.com/jmespath/go-jmespath v0.4.0
hatchet v0.6.0-20241219

使用方法

  • 转换日志为老格式日志
./dist/hatchet -legacy /var/log/mongo/mongod.log > mongod_legacy.log

  • 使用以下命令处理日志文件 mongod.log.gz 并启动监听端口 3721 的 Web 服务器。默认数据库是 SQLite3。
./dist/hatchet -web /var/log/mongodb/mongod.log

访问ip:3721

  • 在定义的时间范围内加载文件:
./dist/hatchet -web -from "2023-09-23T20:25:00" -to "2023-09-23T20:26:00" logs/sample-mongod.log.gz
  • 加载多个文件并单独处理:
./dist/hatchet -web rs1/mongod.log rs2/mongod.log rs3/mongod.log
  • 加载多个文件并集体处理:
./dist/hatchet -web -merge rs1/mongod.log rs2/mongod.log rs3/mongod.log