MongoDB爱好者
垂直技术交流平台

MongoDB干货系列1-定期巡检之Mtools

大家好,我是E叔,好久不见,我来挖一个新坑《MongoDB干货系列》。
本文第一发分享,也是我将在9月中上海用户组见面会上的分享的先行版部分内容,希望有所帮助。

前言:

监控与troubleshooting永远是任何数据库最需要关注的地方之一。
任何数据库的DBA都应该对数据库情况进行定期的巡检,以清楚了解数据库的运行情况,健康状况,隐患等等。

本文主要讲述的是如何使用Mtools工具对MongoDB数据库进行巡检。
通过mtools并结合监控数据,如MMS,ZABBIX,或者自行绘制的监控图形,能够较好的对mongodb的运行情况,健康状况进行检查,并排除隐患。且在troubleshooting的时候也能提供很好的帮助。

Mtools简介:

Mtools是由MongoDB Inc 官方工程师所写,设计之初是为了方便自己的工作,但是随着MongoDB用户的增加,越来越多的朋友也开始使用Mtools,也越来越感受到Mtools带来的便捷。
Github地址如下:
Mtools github地址

Mtools主要有以下组件:

  
mlogfilter
mloginfo
mplotqueries
mlogvis
mlaunch
mgenerate

在此就不一一介绍了,本文中主要使用到的是 mlogfilter,mloginfo和mplotqueries.

前菜:

首先,我们来简单介绍下 mlogfilter,mloginfo和mplotqueries。
mlogfileter我们可以简单理解为日志的过滤器,参数如下,就不一一简介了

 
mlogfilter [-h] [--version] logfile [logfile ...]
           [--verbose] [--shorten [LENGTH]]
           [--human] [--exclude] [--json]
           [--timestamp-format {ctime-pre2.4, ctime, iso8601-utc, iso8601-local}]
           [--markers MARKERS [MARKERS ...]] [--timezone N [N ...]]
           [--namespace NS] [--operation OP] [--thread THREAD]
           [--slow [SLOW]]  [--fast [FAST]] [--scan]
           [--word WORD [WORD ...]]
           [--from FROM [FROM ...]] [--to TO [TO ...]]

mloginfo可以过滤总结出slow query的情况,以及为日志中各类最常常出现情况进行统计,参数如下:

 
mloginfo [-h] [--version] logfile
         [--verbose] 
         [--queries] [--restarts] [--distinct] [--connections] [--rsstate]

mplotqueries相对复杂一些,功能是可以根据需求画图,以便更直观的找出问题或者隐患所在。
具体用法参见github中的详情页,为了方便大家阅读,我也在这转发一个小伙伴翻译的中文版本
mplotqueries 中文翻译
特此感谢 吕明明的翻译。

参数如下:

 
mplotqueries [-h] [--version] logfile [logfile ...]
             [--group GROUP]
             [--logscale]
             [--type {nscanned/n,rsstate,connchurn,durline,histogram,range,scatter,event} ]
             [--overlay [ {add,list,reset} ]]
             [additional plot type parameters]

主菜(21道):

由于本文主要讲述的是通过Mtools来辅助我们定期巡检,且主打“干货”,我将在下面直接进行一系列的使用分享。

1.通过Mlogfilter 列出日志文件中所有的slow log(以json格式输出)

 
mlogfilter mongod.log-20150721 --slow --json

2.将通过mlogfilter输出的所有slow log入库

 
mlogfilter mongod.log-20150721 --slow --json |mongoimport -d test -c mycoll

3.通过mlogfilter查询日志中某个表的slow log(超过100ms的)

 
mlogfilter --namespace xxx.xx --slow 100 mongod.log-20150721

4.通过mloginfo统计日志中各类信息的distinct

 
mloginfo mongod.log-20150721 --distinct

5.通过mloginfo统计日志中connections的来源情况

 
mloginfo mongod.log-20150721 --connections

6.通过mloginfo查看日志中所记录的复制集状态变更(如果有的话)

 
mloginfo mongod.log-20150721 --rsstate

7.通过mloginfo统计查看日志中慢查询的分类

 
mloginfo --queries mongod.log-20150721

1-1

 

8.通过mplotqueries进行慢查询散点分布图绘制(请原谅我这个图打码技术有限)

 
mplotqueries mongod.log-20150721 --output-file 01-2.png

1-2

9.通过mplotqueries进行慢查询散点分布图绘制,且只返回前10个

 
mplotqueries mongod.log-20150721 --output-file 01-3.png --group-limit 10

1-3

10.上一个图中,我们可以发现底部的数据难以看清,我们可以使用对数模式

 
mplotqueries mongod.log-20150721 --output-file 01-4.png --logscale --group-limit 10

1-4

11.仅看日志中某一个表的慢查询散点分布情况

 
mlogfilter mongod.log-20150721 --namespace xx.xxx |mplotqueries --output-file 01-5.png

1-5

12.通过mplotqueries来对日志中的慢查询进行操作类型分布

 
mplotqueries mongod.log-20150721 --group operation --output-file 01-6.png

1-6

13.通过mplotqueries对日志中的慢查询进行扫表情况绘图

 
mplotqueries mongod.log-20150721 --type nscanned/n --output-file 01-7.png

1-7

14.自定义y轴内容,这里以w为例(nscanned, nupdated,ninserted, ntoreturn, nreturned, numYields, r (读锁), w (写锁))

 
mplotqueries mongod.log-20150721 --yaxis w --output-file 01-8.png

1-8

15.通过mplotqueries对连接情况进行分析,时间块单位1800(30min)

 
mplotqueries mongod.log-20150721 --type connchurn --bucketsize 1800 --output-file 01-9.png

1-9

16.通过mlogfileter过滤出xx.xxx的update,然后每30min时间块 以_id分布(这里可以引申为任何条件支持正则)

 
mlogfilter mongod.log-20150721 --operation update --namespace xx.xxx | mplotqueries --type histogram --group "_id:([^,}]*)" --bucketsize 1800 --output-file 01-10.png --group-limit 20

1-10

17.查看每小时的insert情况

 
mlogfilter mongod.log-20150721 --operation insert | mplotqueries --type histogram --bucketsize 3600 --output-file 01-11.png

1-11

18.对日志中的slow log进行分布绘图,(出现间隔超过10min时,显示间隔)

 
mplotqueries mongod.log-20150721 --type range --group operation --gap 600 --output-file 01-12.png

1-12

19.对日志中的事件进行绘图(事件图,显示出各类事件出现的时间位置等)。
由于测试数据中无getlasterror类型error,故暂无图示

 
grep "getlasterror" mongod.log-20150721 | mplotqueries --type event --output-file 01-13.png

20.通过log进行复制集状态的查看。(会绘制出复制集状态变动图形)

 
mplotqueries mongod.log-20150721 --type rsstate --output-file 01-14.png

21.overlay的使用

 
overlay参数可以将多组图进行重叠,便于troubleshooting时候的快速分析。
建立overlay仅需加参数--overlay
查看overlay可以使用--overlay list
清空overlay可以使用--overlay reset

饭后甜点:

有了这些数据我们如何进行分析呢?
首先需要知道,一切异于常态的状态都是值得关注的,如8图中有一些点完全高于其他点,这就是可以重点关注从此下手进行调查。
再者mloginfo查询出的slow log 类型结合我们mongo本身记录的 profile 也可以一起对业务语句进行很好的分析,查询执行计划,并进行优化。
结合mtools所绘的图片,并将系统数据(cpu,io,mem,进程资源情况)的数据监控图进行重叠比较,也能够为troubleshooting提供很好的帮助。

以上便是本次分享的内容了,如果有什么问题欢迎联系我,希望有所帮助。

关于作者

周李洋,社区常用ID eshujiushiwo,关注Mysql与MongoDB技术,数据架构,服务器架构等,现就职于DeNA,mongo-mopre,mongo-mload作者,任CSDN mongodb版主,MongoDB官方翻译组核心成员,MongoDB中文站博主,MongoDB Contribution Award获得者,MongoDB Days Beijing 2014演讲嘉宾。
联系方式:378013446
MongoDB上海用户组:192202324
欢迎交流。

赞(7)
未经允许不得转载:MongoDB中文社区 » MongoDB干货系列1-定期巡检之Mtools

评论 3

评论前必须登录!

 

  1. #1

    图片看不了

    xyyl3年前 (2021-06-23)
  2. #2

    我运行mloginfo mongod.log –connections
    总是报错Error: <mongod.log> does not appear to be a supported MongoDB log file format
    这是啥原因呢,这个和python是3.6有关吗?

    NoP2年前 (2022-03-08)
  3. #3

    还有运行
    mlaunch init –sharded 2 –mongos 2 –config 3 –replicaset –nodes 2 –arbiter –port 25001 –dir /data/mongodb/mcluster –auth –username admin –password admin1234 –auth-db admin后报
    Generating keyfile: /data/mongodb/mcluster/keyfile
    b"BadValue: security.keyFile is required when authorization is enabled with replica sets\ntry 'mongod –help' for more information\n"

    can't start process, return code 2. tried to launch: "mongod" –replSet shard01 –dbpath "/data/mongodb/mcluster/shard01/rs1/db" –logpath "/data/mongodb/mcluster/shard01/rs1/mongod.log" –port 25003 –fork –auth –shardsvr –wiredTigerCacheSizeGB 1

    NoP2年前 (2022-03-08)