Prometheus实战篇:Prometheus监控mongodb

本文涉及的产品
Serverless 应用引擎 SAE,800核*时 1600GiB*时
容器镜像服务 ACR,镜像仓库100个 不限时长
函数计算FC,每月15万CU 3个月
简介: Prometheus实战篇:Prometheus监控mongodb

Prometheus实战篇:Prometheus监控mongodb

准备环境

docker-compose安装mongodb

docker-compose.yaml

version: '3'
services:
   mongo:
  image: mongo:4.2.5
  container_name: mongo
   restart: always
   volumes:
    - /data/mongo/db: /data/db
   port:
   - 27017:27017
   command: [--auth]
   enviroment:
    MONGO_INITDB_ROOT_USERNAME: root
    MONGO_INITDB_ROOT_PASSWORD: 123456
docker-compose up -d

监控mongoDB

创建监控用户

登录MongoDB创建监控用户,权限为"readAnyDatabase",如果是cluster环境,需要有"clusterMonitor"

登录MongoDB(docker安装的mongo)

docker exec -it mongo mongo admin

创建监控用户

> db.auth('root','123456')
1
>db.createUser({ user: 'exporter',pwd : 'password',roles:[{role: 'readAnyDatabase',db : admin},{role: 'clusterMonitor',db : admin}]})
#测试 使用上面创建的用户信息进行连接
> db.auth('exporter','password')
1
#表示成功
> exit

docker安装exporter

docker直接运行

docker run -d  -p 9216:9216 -p 17001:17001 --restart=always  --name mongodb-exporter bitnami/mongodb_exporter :latest --collect-all --compatible-mode --mongodb.uri=mongodb://exporter:password@localhost:27017/admin?ssl=false

docker-compose方式

为了方便省事,我mongodb使用管理员账号,生产不建议使用

cat >docker-compose.yml << EOF
version: '3.3'
services:
 mongodb_exproter:
  image: bitnami/mongodb_exporter:latest
  container_name: mongodb_exproter
  restart: always
  environment:
    MONGODB_URI: "mongodb://exporter:password@localhost:27017/admin?ssl=false"
  command:
    - '--collect-all'
    - '--compatible-mode'
  port:
   - "9216:9216"
EOF

启动

docker-compose up -d

检查

查看正在运行的容器
docker ps
或者:
查看mongodb_exporter容器的运行日志
docker logs -f mongodb_exporter

参数解释

Environment variable

描述

collect-all

localhost:15672

rabbitmq管理插件的url(必须以http(2)开头)

compatible-mode

guest

rabbitmq管理插件的用户名

metrics地址

安装好Exporter后会暴露一个/metrics结尾的服务

名称

地址

mongodb_exporter

http://localhost:9216/metrics

Prometheus配置

配置Prometheus去采集(拉取)nginx_exporter的监控样本数据

cd /data/docker-prometheus
# 在scrapc_configs(搜刮配置):下面增加如下配置:
cat >prometheus/prometheus.yml <<FOF
 - job_name: 'mongodb_exporter'
   static_configs:
   - targets: ['localhost:9216']
     labels:
      instance: test服务器 
EOF

重新加载配置

curl -x POST http://localhost:9090/-/reload

检查


常用的mongodb监控指标

mongodb_ss_connections{conn_type="available"} 可用的连接数
mongodb_ss_mem_virtual
mongodb_ss_mem_residenl
#关于server status
mongodb_up  服务器是否在线
mongodb_ss_ok{cl_id="",cl_role="current",rs_state="0"}  服务器是否正常运行,取值为1,0.标签中记录了Cluster,ReplicaSet
mongodb_ss_uptime 服务器的运行时长,单位为秒
mongdb_ss_connections{conn_type="current"}  客户端连接数
# 关于主机
mongodb_sys_cpu_num_cpus  主机的CPU核数
# 关于 collection
mongodb_collstats_storageStats_count{database="xx",collection="xx"} collection 全部文档的数量
mongodb_collstats_storageStats_size collection全部文档的体积,单位bytes
mongodb_collstats_storageStats_storageSize  collection全部文档占用的磁盘空间,默认会压缩
delta(mongodb_collstats_latencyStats_reads_ops[1m]) collection读操作的数量(每分钟)
delta(mongodb_collstats_latencyStats_reads_latency[1m]) collection读操作的延迟(每分钟),单位为微秒
mongodb_collstats_latencyStats_write_ops
mongodb_collstats_latencyStats_write_latency

触发器配置

由于之前的触发器是全部写在了一个yml里面就是alert.yam,这样随着后面配置的触发器越来越多最终会变得难以维护.这里我们让它去读rules目录下所有的yml文件即可

Prometheus配置

rule_files:
  - "alert.yml"
  - "rules/*.yml"

配置mongdb触发器

因为是单机所以未配置集群的触发器

cat >prometheus/rules/mongodb.yml <<FOF
groups:
- name: PerconaMongodbExporter
  rules:
    - alert: MongodbDown
      expr: 'mongodb_up == 0'
      for: 0m
      labels:
        severity: critical
      annotations:
          summary: "MongoDB Down,容器:$labels.instance"
          description: "MongoDB 容器 is down,当前值{{ $value }}"
    - alert: MongodbNumberCursorsOpen
      expr: 'mongodb_ss_metrics_cursor_open{csr_type="total"}' > 10
      for: 2m
      labels:
        severity: warning
      annotations:
          summary: "MongoDB 数字游标打开告警 容器:{{$labels.instance }}"
          description: "MongoDB为客户端打开的游标过多>10k,当前值为:{{ $value }}"
    - alert: MongdbCursorsTimeouts
      expr: 'increase(mongodb_ss_metrics_cursor_timedOut[1m]) > 100'
      for: 2m
      labels:
        severity: warning
      annotations:
          summary: "MongDB 游标超时,容器:{{$labels.instance }}"
          description: "太多游标超时,当前值为:{{ $value }}"
    - alert: MongodbVirtualMemoryUsage
      expr: '(sum(mongodb_ssmem_virtual BY (instance) / sum(mongodb_ss_mem_resident) BY (instance)) > 3'
      for: 2m
      labels:
        severity: warning
      annotations:
          summary: "MongoDB虚拟内存使用告警,容器:{{$labels.instance }}"
          description: "虚拟内存使用过高,当前值为:{{ $value }}"
EOF

一定记住这里需要仔细校对yaml语法,最好是能去在线验证yaml语法的网站上看看.yaml语法还是比较严格的一点缩进错误都不能有

检查配置

docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml

重新加载配置

curl -x POST http://localhost:9090/-/reload

检查

http://localhost:9090/alerts?search=

或:

http://localhost:9090/rules

dashboard

这里需要从github上去下载对应的dashboard

https://github.com/percona/grafana-dashboards/blob/main/dashboards/MongoDB/MongoDB_Instances_Overview.json

选择导入刚刚从github上下载的json文件即可

可以发现最终的仪表盘上有这一块连接数是空的

只需要在插件中将polystat的插件下载即可,具体在设置中然后搜索这个插件下载即可.

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
1月前
|
存储 Prometheus 监控
性能监控之初识 Prometheus
【8月更文挑战第2天】性能监控之初识 Prometheus
62 17
|
1月前
|
Prometheus 监控 Cloud Native
性能监控之 Golang 应用接入 Prometheus 监控
【8月更文挑战第4天】性能监控之 Golang 应用接入 Prometheus 监控
83 0
性能监控之 Golang 应用接入 Prometheus 监控
|
1月前
|
Prometheus 监控 Cloud Native
性能监控之 node_exporter+Prometheus+Grafana 实现主机监控
【8月更文挑战第3天】性能监控之 node_exporter+Prometheus+Grafana 实现主机监控
80 0
|
3月前
|
Prometheus 监控 Cloud Native
性能监控神器Prometheus、Grafana、ELK 在springboot中的运用
【6月更文挑战第27天】在 Spring Boot 应用中,监控和日志管理是确保系统稳定性和性能的重要手段。
122 4
|
3月前
|
Prometheus 监控 Cloud Native
搭建服务端性能监控系统 Prometheus 详细指南
搭建Prometheus监控系统,涉及Ubuntu上Docker的安装,通过`docker run`命令启动Prometheus容器,并挂载配置文件。配置文件默认示例可以从GitHub获取,调整`scrape_interval`和`targets`以监控Prometheus自身及Node Exporter(提供系统指标)。Node Exporter以Docker容器形式运行在9100端口。完成配置后,重启Prometheus容器,通过Web界面查看监控数据。后续将介绍结合Grafana进行可视化。
82 0
|
4月前
|
Prometheus Kubernetes 监控
|
4月前
|
Prometheus 监控 Cloud Native
Prometheus实战篇:Prometheus监控docker
Prometheus实战篇:Prometheus监控docker
|
存储 Prometheus Kubernetes
Prometheus监控Kubernetes的3个配置挑战
Prometheus监控Kubernetes的3个配置挑战
181 0
Prometheus监控Kubernetes的3个配置挑战
|
4月前
|
SQL 运维 监控
关系型数据库性能监控工具
【5月更文挑战第21天】
78 2
|
19天前
|
监控 Java 开发者
揭秘Struts 2性能监控:选对工具与方法,让你的应用跑得更快,赢在起跑线上!
【8月更文挑战第31天】在企业级应用开发中,性能监控对系统的稳定运行至关重要。针对流行的Java EE框架Struts 2,本文探讨了性能监控的工具与方法,包括商用的JProfiler、免费的VisualVM以及Struts 2自带的性能监控插件。通过示例代码展示了如何在实际项目中实施这些监控手段,帮助开发者发现和解决性能瓶颈,确保应用在高并发、高负载环境下稳定运行。选择合适的监控工具需综合考虑项目需求、成本、易用性和可扩展性等因素。
25 0

相关产品

  • 可观测监控 Prometheus 版