阿里云主机折上折
  • 微信号
Current Site:Index > MongoDB's built-in monitoring tools (mongostat, mongotop)

MongoDB's built-in monitoring tools (mongostat, mongotop)

Author:Chuan Chen 阅读数:23444人阅读 分类: MongoDB

MongoDB provides two built-in monitoring tools, mongostat and mongotop, which can display the database's runtime status and performance metrics in real time. These tools do not require additional installation and can be used directly via the command line, making them suitable for quickly diagnosing database issues.

Basic Usage of mongostat

mongostat is similar to the Linux system's vmstat. It samples and returns MongoDB instance statistics at fixed intervals (default: 1 second). The basic command format is as follows:

mongostat --host <hostname>:<port> --username <user> --password <pass> --authenticationDatabase admin

If authentication information is not specified, it defaults to connecting to the local MongoDB instance on port 27017:

mongostat

Example output:

insert query update delete getmore command flushes mapped vsize res faults qr|qw ar|aw netIn netOut conn time
    *0    *0     *0     *0       0     1|0       0  80.0M 1.21G 50.0M      0   0|0   0|0   79b    10k    1 14:02:45
    *0    *0     *0     *0       0     1|0       0  80.0M 1.21G 50.0M      0   0|0   0|0   79b    10k    1 14:02:46

Detailed Explanation of mongostat Output Fields

Each field represents a statistical value for a specific time interval:

  1. Operation Counters (unit: operations/second)

    • insert/query/update/delete: Count of CRUD operations
    • getmore: Cursor batch retrieval operations
    • command: Count of administrative commands executed
  2. Storage Engine Metrics

    • flushes: WAL flush count (MMAPv1 engine)
    • vsize: Virtual memory usage
    • res: Physical memory usage
  3. Performance Metrics

    • faults: Page faults (MMAPv1 only)
    • qr|qw: Read/write queue length
    • netIn/netOut: Network traffic (bytes)

Advanced Usage of mongostat

Customize monitoring content with parameters:

# Sample every 5 seconds, display 20 times, then exit
mongostat --rowcount 20 --interval 5

# Display only specified fields
mongostat --discover -o "host=HOST,time=TIME,qr=QR,qw=QW"

# Monitor all members of a replica set
mongostat --host rs0/example1:27017,example2:27017 --discover

Basic Usage of mongotop

mongotop monitors collection-level read/write time consumption, similar to Linux's top command. Basic usage:

mongotop --host localhost:27017

Example output:

ns                      total        read        write
admin.system.roles      0ms         0ms         0ms  
test.users              105ms       30ms        75ms 
local.oplog.rs          50ms        50ms        0ms  

Interpreting mongotop Output

  1. ns: Namespace (database.collection)
  2. total: Total time consumed (milliseconds)
  3. read/write: Proportion of time spent on read/write operations

Advanced Configuration for mongotop

# Refresh every 3 seconds, repeat 10 times
mongotop --interval 3 --rowcount 10

# Output in JSON format
mongotop --json

# Monitor a specific database
mongotop --host localhost --username admin --password pass --authenticationDatabase admin test

Practical Application Scenarios

Scenario 1: Identifying Slow Queries

mongotop -n 10  # Display the top 10 collections by time consumption

Scenario 2: Diagnosing Write Bottlenecks

mongostat -o "insert,update,delete,dirty,used"  # Focus on write-related metrics

Scenario 3: Memory Pressure Analysis

mongostat --noheaders --rowcount 60 --interval 5 | grep -v "connected to" > stats.log

Integration with Third-Party Tools

Pipe monitoring data to other tools for processing:

// Node.js example for processing `mongostat` output
const { exec } = require('child_process');
const mongostat = exec('mongostat --json');

mongostat.stdout.on('data', (data) => {
  const stats = JSON.parse(data);
  console.log(`Current ops: ${stats.insert} inserts/s`);
});

Monitoring Sharded Clusters

Special handling is required for sharded clusters:

# Monitor all mongos instances
for shard in shard1:27017 shard2:27018; do
  mongostat --host $shard >> cluster_stats.log &
done

Security Considerations

  1. Monitoring tools require the clusterMonitor role permission.
  2. In production environments, use TLS-encrypted connections:
    mongostat --host dbserver --ssl --sslCAFile /path/to/ca.pem
    

Performance Impact Assessment

Resource consumption of monitoring tools at default sampling intervals:

  • CPU usage: < 1%
  • Network traffic: ~2KB/s (basic metrics)
  • Memory consumption: ~10MB

For long-term monitoring, adopt the following strategy:

# Generate one log file per day
mongostat --rowcount 86400 --interval 1 > $(date +%Y%m%d).log

Guide for Handling Abnormal Metrics

When specific metrics are abnormal, refer to the following actions:

Abnormal Metric Possible Cause Investigation Direction
Persistent qw > 0 Write blocking Check disk IOPS, journal performance
Sudden netOut spike Large data returns Check if queries lack index usage
Continuous res growth Memory leak Check if workingSet exceeds physical memory

Historical Data Analysis Techniques

Import monitoring data into MongoDB for analysis:

// Store `mongostat` output in a collection
use admin
db.createCollection("server_stats")
mongostat --json | mongo --eval '
  while (line=readline()) {
    db.server_stats.insert(JSON.parse(line))
  }
'

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.