MongoDB Exporter 快速入门

Prometheus exporter for MongoDB server metrics.

https://github.com/percona/mongodb_exporter

https://github.com/dcu/mongodb_exporter

一、部署 mongodb_exporter

下载 mongodb_exporter

https://github.com/percona/mongodb_exporter/releases

解压并安装:

cd /usr/local/src && tar -zxvf mongodb_exporter-0.37.0.linux-amd64.tar.gz;
mv mongodb_exporter-0.37.0.linux-amd64 /usr/local/mongodb_exporter;
cd /usr/local/mongodb_exporter;

查看使用帮助:

[root@192 mongodb_exporter]# ./mongodb_exporter -h
Usage: mongodb_exporter

MongoDB Prometheus exporter

Flags:
  -h, --help                                                              Show context-sensitive help.
      --mongodb.collstats-colls=db1,db2.col2                              List of comma separared databases.collections to get $collStats
      --mongodb.indexstats-colls=db1.col1,db2.col2                        List of comma separared databases.collections to get $indexStats
      --mongodb.uri=mongodb://user:pass@127.0.0.1:27017/admin?ssl=true    MongoDB connection URI ($MONGODB_URI)
      --[no-]mongodb.global-conn-pool                                     Use global connection pool instead of creating new pool for each http request.
      --[no-]mongodb.direct-connect                                       Whether or not a direct connect should be made. Direct connections are not valid if multiple hosts are specified or an SRV URI is used.
      --web.listen-address=":9216"                                        Address to listen on for web interface and telemetry
      --web.telemetry-path="/metrics"                                     Metrics expose path
      --web.config=STRING                                                 Path to the file having Prometheus TLS config for basic auth
      --log.level="error"                                                 Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]
      --collector.diagnosticdata                                          Enable collecting metrics from getDiagnosticData
      --collector.replicasetstatus                                        Enable collecting metrics from replSetGetStatus
      --collector.dbstats                                                 Enable collecting metrics from dbStats
      --collector.topmetrics                                              Enable collecting metrics from top admin command
      --collector.indexstats                                              Enable collecting metrics from $indexStats
      --collector.collstats                                               Enable collecting metrics from $collStats
      --metrics.overridedescendingindex                                   Enable descending index name override to replace -1 with _DESC
      --collect-all                                                       Enable all collectors. Same as specifying all --collector.<name>
      --collector.collstats-limit=0                                       Disable collstats, dbstats, topmetrics and indexstats collector if there are more than <n> collections. 0=No limit
      --[no-]discovering-mode                                             Enable autodiscover collections
      --[no-]compatible-mode                                              Enable old mongodb-exporter compatible metrics
      --version                                                           Show version and exit

默认情况下,mongodb_exporter 在以下端口提供 0.0.0.0:9216 服务/metrics

./mongodb_exporter

访问监控度量信息地址:

http://localhost:9216/metrics

使用 systemd方式启动
# cat >/usr/lib/systemd/system/mongodb_exporter.service <<EOF

[Unit]
Description=Prometheus exporter for MongoDB server metrics.
Documentation=https://github.com/prometheus/mongodb_exporter
After=network.target

[Service]
#没开启安全认证
#ExecStart=/usr/local/mongodb_exporter/mongodb_exporter --mongodb.uri=mongodb://127.0.0.1:27017 --collect-all --web.listen-address=:9216
#开启安全认证
ExecStart=/usr/local/mongodb_exporter/mongodb_exporter --mongodb.uri=mongodb://root:AzpFpdyZRiJwaGj3@127.0.0.1:27017 --collect-all --web.listen-address=:9216
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

# systemctl enable mongodb_exporter
# systemctl restart mongodb_exporter
# systemctl status mongodb_exporter

设置随机启动:

[root@local ~]# systemctl daemon-reload
[root@local ~]# systemctl disable mongodb_exporter.service
[root@local ~]# systemctl enable mongodb_exporter.service
[root@local ~]# systemctl start mongodb_exporter.service
[root@local ~]# systemctl status mongodb_exporter.service
[root@local ~]# systemctl stop mongodb_exporter.service
[root@local ~]# systemctl restart mongodb_exporter.service

二、在 prometheus.yaml 中添加 mongodb_exporter 的配置

在 Prometheus 配置文件里,您可以按如下方式设置抓取配置

crape_configs:

  - job_name: "mongodb" # To get metrics about the mongodb exporter’s targets

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["192.168.1.111:9216"]

作者:Jeebiz  创建时间:2023-03-12 22:36
最后编辑:Jeebiz  更新时间:2024-01-24 21:48