Ubuntu 上 MongoDB 监控实践指南
一 监控方案总览
二 快速上手 系统与服务层监控
#!/usr/bin/env bash
if systemctl is-active --quiet mongod; then
echo "MongoDB is running."
exit 0
else
echo "MongoDB is down. Restarting..."
sudo systemctl restart mongod
exit 1
fi
chmod +x /usr/local/bin/check_mongo.sh
crontab -e
*/5 * * * * /usr/local/bin/check_mongo.sh
三 数据库自带与命令行监控
四 Prometheus Grafana MongoDB Exporter 监控与告警
use admin
db.createUser({
user: "exporter",
pwd: "StrongPassw0rd",
roles: [
{ role: "readAnyDatabase", db: "admin" },
{ role: "clusterMonitor", db: "admin" }
]
})
version: "3.8"
services:
mongodb_exporter:
image: bitnami/mongodb-exporter:latest
environment:
MONGODB_URI: "mongodb://exporter:StrongPassw0rd@mongo:27017/admin?ssl=false"
ports:
- "9216:9216"
restart: always
scrape_configs:
- job_name: 'mongodb'
static_configs:
- targets: ['<mongo_host_or_ip>:9216']
groups:
- name: mongodb
rules:
- alert: MongoDBDown
expr: mongodb_up == 0
for: 0m
labels: severity: critical
annotations:
summary: "MongoDB Down on {{ $labels.instance }}"
description: "MongoDB instance is unreachable."
- alert: MongoDBTooManyOpenCursors
expr: mongodb_ss_metrics_cursor_open{csr_type="total"} > 10000
for: 2m
labels: severity: warning
annotations:
summary: "MongoDB open cursors high on {{ $labels.instance }}"
description: "Open cursors: {{ $value }}"
五 关键指标与告警建议