MongoDB: read-only user setup

What we scrape: Replica set health, oplog window, operation throughput, connections, page faults, cursor state, and WiredTiger cache pressure. The monitor-agent runs Percona `mongodb_exporter` locally and scrapes `127.0.0.1:9216`.

[Percona's `mongodb_exporter`](https://github.com/percona/mongodb_exporter) is baked into the monitor-agent image. You only create a scoped MongoDB user and paste a `mongodb://` connection URI (without embedded credentials) in Settings → Exporters. MongoDB's `clusterMonitor` role grants read access to `serverStatus`, `replSetGetStatus`, `currentOp`, and the system collections an exporter needs — nothing else.

  1. 1. Create the read-only MongoDB user

    Run as a user with `userAdminAnyDatabase` (the bootstrap admin). The two roles granted are: `clusterMonitor` (the cluster-wide metrics read role) plus `read` on the `local` DB (so the exporter can read the oplog window for replication-lag metrics). Nothing else — no `dbOwner`, no `userAdmin`, no per-database write.

    JSON
    // mongosh against your primary, authenticated as an admin:
    db.getSiblingDB("admin").createUser({
      user: "prometheus_exporter",
      pwd:  passwordPrompt(),   // prompts; never echoed to history
      roles: [
        { role: "clusterMonitor", db: "admin" },
        { role: "read",           db: "local" }
      ]
    })
  2. 2. Add the exporter in Settings

    On `/settings/exporters`, choose MongoDB and paste `mongodb://HOST:27017` (no userinfo in the URI) plus the scoped username/password — or use self-managed and set `EXPORTER_<id>_USER` / `EXPORTER_<id>_PASSWORD` on the agent host. The agent starts Percona mongodb_exporter on loopback `:9216` automatically.

    bash
    # Example managed connection URI (credentials go in the form fields, not the URI):
    mongodb://mongo.internal:27017/?authSource=admin
  3. 3. Verify the user reads metrics but cannot mutate data

    The first command must succeed (clusterMonitor lets the user run `serverStatus`). The second MUST fail with `not authorized on test to execute command insert` — proof the user is locked out of writes everywhere except the local oplog read path.

    bash
    # Read path — must return JSON, exit 0.
    mongosh "mongodb://prometheus_exporter:CHANGE_ME@MONGO_HOST:27017/?authSource=admin" \
      --quiet --eval 'JSON.stringify(db.adminCommand({ serverStatus: 1 }).ok)'
    
    # Write path — must fail with "not authorized".
    mongosh "mongodb://prometheus_exporter:CHANGE_ME@MONGO_HOST:27017/?authSource=admin" \
      --quiet --eval 'db.getSiblingDB("test").verify_t.insertOne({ x: 1 })'

Rotate the exporter's MongoDB password the same way you rotate any service account — update managed secrets in Settings (or the host env vars for self-managed) and the agent reloads on the next config poll. You never expose `:9216` on the host network.