MongoDB · Monitoring Basics

Where alert signals come from:
five MongoDB built-in commands worth watching

High CPU and rising memory are only symptoms. What actually tells you what's going on inside the database is a handful of status commands MongoDB ships with. This post picks the five best suited to drive alerts, and explains what each one shows and what rule it can become.

MongoDB commandSignal it revealsAlert rule it enables
rs.status()
→
Member state & replication lag
→
Sustained lag / unexpected member state
db.currentOp()
→
Operations in flight
→
Operations running too long
Database Profiler
→
Operations over a time limit
→
Slow-query count or ratio rising
serverStatus().globalLock
→
Reads/writes waiting to run
→
Queue persistently non-empty
serverStatus().connections
→
Current connections & headroom
→
Connection usage too high

One command, one kind of signal, one rule you can actually deploy

01 · Why Look Inside the Database

External metrics say "it hurts"; internal commands say "where"

Host-level CPU, memory and disk metrics are essential, but they can't answer "which query is eating resources" or "how far behind is that node". Bringing the database's internal state into your alerting makes rules specific and troubleshooting shorter.

External metrics

CPU · Memory · Disk IO

Broad coverage, collectable on any system — but they only say "resources are tight", not why.

Database internal state

Replication · Operations · Queues · Connections

Straight from MongoDB itself, pointing to whether the cause is replication lag, a stuck operation or a backed-up queue.

SIGNAL 01

rs.status(): member state & replication lag

First-hand health information for a replica set lives here. Each member's stateStr (PRIMARY / SECONDARY / RECOVERING and so on), health and optimeDate can all be read from it.

Subtract each secondary's optimeDate from the primary's and you get the replication lag. For a quick look, you can also use rs.printSecondaryReplicationInfo().

RULE IDEAAlert immediately if a member isn't in its expected state; alert on replication lag only when it exceeds a threshold for a sustained period.
Read each member's state and latest sync time
rs.status().members.map(m => ({
  name:   m.name,
  state:  m.stateStr,
  health: m.health,
  optime: m.optimeDate
}))
See how far each secondary is behind the primary
rs.printSecondaryReplicationInfo()
SIGNAL 02

db.currentOp(): operations in flight

To find out "what is the database busy with right now", use currentOp. It returns every operation currently executing, including how many seconds it has been running, which namespace it touches and which client it came from.

Filtering on secs_running pulls out the operations that have been running unusually long — often long transactions, queries missing an index, or something a client forgot to finish.

RULE IDEAThe number of operations running longer than N seconds is above 0 for a sustained period — a hint that something may be stuck.
NOTEcurrentOp requires the right privileges. After spotting a long operation, work out what it is before deciding to end it — don't killOp on sight.
Find active operations running over 30 seconds
db.currentOp({
  active: true,
  secs_running: { $gt: 30 }
})
SIGNAL 03

Database Profiler: slow-operation records

The profiler records operations that exceed a time threshold into the system.profile collection of the current database. Set the level to 1 and give it a slowms value, and only operations slower than that many milliseconds are recorded.

That turns "slow queries" from a vague feeling into data you can count: how many in a period, which collections they cluster on, how many documents they scanned.

RULE IDEASlow-operation count per unit time, or their share of total requests, exceeds baseline for a sustained period.
NOTEsystem.profile is a fixed-size capped collection, so older records get overwritten; the profiler itself also has overhead, so don't set the threshold too low in production.
Record operations slower than 100 ms
db.setProfilingLevel(1, { slowms: 100 })
View the 5 most recent slow operations
db.system.profile
  .find()
  .sort({ ts: -1 })
  .limit(5)
SIGNAL 04 · 05

serverStatus(): queues & connections

serverStatus is where MongoDB's status information is most concentrated. Two parts of it are especially good as alert inputs.

globalLock.currentQueue: how many reads and writes are queued, waiting to run. An occasional non-zero queue is normal; a queue that is persistently non-zero means the database can't keep up.

connections: current is the number of open connections and available is how many more can be accepted. Add them for the total capacity — when usage approaches the limit, act early.

RULE IDEARead/write queue persistently non-empty; connection usage (current ÷ (current + available)) above a set ratio.
Read/write queue
db.serverStatus().globalLock.currentQueue
// { total: .., readers: .., writers: .. }
Connections and remaining capacity
db.serverStatus().connections
// { current: .., available: .., ... }
02 · Summary

Turning signals into rules: a starter checklist

The thresholds below are starting-point examples only. Every cluster has its own baseline — observe normal levels for a while first, then adjust.

Signal sourceWhat to watchExample ruleSeverity
rs.status() Member state Any member not in its expected state P1
rs.status() Replication lag Over 10 s, for 2 min P2
db.currentOp() Long-running op count Operations over 30 s keep showing up P2
Profiler Slow-op count / ratio Clearly above own baseline and still rising P3
globalLock Read/write queue Queue persistently non-empty P2
connections Connection usage Above 80% of total capacity P2
WAP Note

Signals like replication lag, connection count and slow-query ratio are exactly what WAP collects continuously, so they can feed alert rules directly — no need to log in and run commands by hand every time. The commands above are best used after an alert fires, to confirm what's happening on the scene.

ONE LAST THING

Understand the signals first,
then talk about alerts

How good a rule is depends on how well you understand MongoDB's internal state. Get these five commands down and most common problems can be spotted before they affect the business.