Master MongoDB Monitoring with Mtools: Practical Commands & Visual Insights
Learn how to use the open‑source Mtools suite to inspect MongoDB logs, extract slow queries, generate performance charts, and overlay system metrics, enabling effective regular health checks, troubleshooting, and optimization for database administrators.
Monitoring and troubleshooting are critical for any database, and DBAs should perform regular inspections to understand health, performance, and potential issues.
This article explains how to use the Mtools toolkit to audit MongoDB databases and assist performance optimization.
1. Mtools Overview
Mtools is an open‑source toolkit created by MongoDB engineers to simplify log analysis and performance monitoring.
Mtools GitHub: https://github.com/rueckstiess/mtools
Mtools components include:
mlogfilter
mloginfo
mplotqueries
mlogvis
mlaunch
mgenerate
In this article we mainly use mlogfilter, mloginfo, and mplotqueries .
2. Basics
Brief introduction of the three tools.
mlogfilter can be understood as a log filter; its parameters are omitted for brevity.
mloginfo aggregates slow‑query information and provides statistics for common log patterns.
mplotqueries creates visual charts to help pinpoint problems.
3. Practical Commands (21 items)
1. List all slow logs in JSON format
<code>mlogfilter mongod.log-20150721 --slow --json</code>2. Import slow logs into a collection
<code>mlogfilter mongod.log-20150721 --slow --json | mongoimport -d test -c mycoll</code>3. Show slow logs for a specific namespace (over 100 ms)
<code>mlogfilter --namespace xxx.xx --slow 100 mongod.log-20150721</code>4. List distinct values in the log
<code>mloginfo mongod.log-20150721 --distinct</code>5. Show connection sources
<code>mloginfo mongod.log-20150721 --connections</code>6. Display replica set state changes
<code>mloginfo mongod.log-20150721 --rsstate</code>7. Categorize slow queries
<code>mloginfo --queries mongod.log-20150721</code>8. Plot slow‑query scatter diagram
<code>mplotqueries mongod.log-20150721 --output-file 01-2.png</code>9. Plot top 10 slow‑query points
<code>mplotqueries mongod.log-20150721 --output-file 01-3.png --group-limit 10</code>10. Use logarithmic scale for better visibility
<code>mplotqueries mongod.log-20150721 --output-file 01-4.png --logscale --group-limit 10</code>11. Plot slow‑query distribution for a single collection
<code>mlogfilter mongod.log-20150721 --namespace xx.xxx | mplotqueries --output-file 01-5.png</code>12. Plot operation‑type distribution
<code>mplotqueries mongod.log-20150721 --group operation --output-file 01-6.png</code>13. Visualize collection scans
<code>mplotqueries mongod.log-20150721 --type nscanned/n --output-file 01-7.png</code>14. Customize Y‑axis (example: write lock "w")
<code>mplotqueries mongod.log-20150721 --yaxis w --output-file 01-8.png</code>15. Analyze connection churn in 30‑minute buckets
<code>mplotqueries mongod.log-20150721 --type connchurn --bucketsize 1800 --output-file 01-9.png</code>16. Histogram of updates by _id every 30 minutes
<code>mlogfilter mongod.log-20150721 --operation update --namespace xx.xxx | mplotqueries --type histogram --group "_id:([^,}]*)" --bucketsize 1800 --output-file 01-10.png --group-limit 20</code>17. Hourly insert histogram
<code>mlogfilter mongod.log-20150721 --operation insert | mplotqueries --type histogram --bucketsize 3600 --output-file 01-11.png</code>18. Plot intervals between slow logs (>10 min gaps)
mplotqueries mongod.log-20150721 --type range --group operation --gap 600 --output-file 01-12.png19. Event timeline (example for getlasterror)
<code>grep "getlasterror" mongod.log-20150721 | mplotqueries --type event --output-file 01-13.png</code>20. Visualize replica set state changes
<code>mplotqueries mongod.log-20150721 --type rsstate --output-file 01-14.png</code>21. Overlay multiple charts for quick troubleshooting
Add overlay with
--overlayList overlays with
--overlay listReset overlays with
--overlay reset4. Analysis Tips
Identify outliers in charts (e.g., points far above the norm) as potential problem areas.
Combine slow‑log data from
mloginfowith MongoDB profiling to analyze query plans and optimize statements.
Overlay Mtools visualizations with system metrics (CPU, I/O, memory) for comprehensive troubleshooting.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.