grafana使用Prometheus数据源监控mongo数据库

数据库改用mongo后,监控需求就需要整合进grafana里,由于一直在坚持docker化部署,那么此次也不例外。

1. 安装Prometheus:

What is Prometheus?

Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud. Since its inception in 2012, many companies and organizations have adopted Prometheus, and the project has a very active developer and user community. It is now a standalone open source project and maintained independently of any company. To emphasize this, and to clarify the project's governance structure, Prometheus joined the Cloud Native Computing Foundation in 2016 as the second hosted project, after Kubernetes.

Prometheus is a monitoring platform that collects metrics from monitored targets by scraping metrics HTTP endpoints on these targets.

主要的意思就是Prometheus是一个开源的系统监控和告警工具包,通过HTTP endpoints来收集要监控的系统的指标。跟着官网的first step,只需要下载最新版本,配置prometheus.yml,启动即可,比较的简单, 因此我们docker化安装也较为容易。

  1) 安装镜像

[root@vhost18 prometheus]# docker search prometheus
NAME DESCRIPTION STARS OFFICIAL AUTOMATED prom
/prometheus 605 [OK] basi/prometheus-swarm A sample image that can be used as a base fo… 7 [OK] infinityworks/prometheus-rancher-exporter Exposes Service/Stack/Host status from the R… 7 [OK] linuxtips/prometheus_alpine Image to run Prometheus on Alpine Linux. #VA… 6 [OK] sscaling/jmx-prometheus-exporter A docker image containing a released version… 5 [OK] argussecurity/cassandra-prometheus Docker Official Cassandra image, with Promet… 4 [OK]

  安装STARS最多的那个就是了。

  2)编写配置文件,参考官网

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

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

    static_configs:
    - targets: ['localhost:9090']

  3)启动容器

docker run -d --name prom -p 9090:9090 -v /data/prometheus/:/etc/prometheus/ prom/prometheus

  4)看下日志是否启动成功

docker logs -f prom

level=info ts=2018-12-11T11:39:08.73690446Z caller=main.go:244 msg="Starting Prometheus" version="(version=2.5.0, branch=HEAD, revision=67dc912ac8b24f94a1fc478f352d25179c94ab9b)" level=info ts=2018-12-11T11:39:08.737024426Z caller=main.go:245 build_context="(go=go1.11.1, user=root@578ab108d0b9, date=20181106-11:40:44)" level=info ts=2018-12-11T11:39:08.737054274Z caller=main.go:246 host_details="(Linux 3.10.0-693.5.2.el7.x86_64 #1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64 72be1c67c847 (none))" level=info ts=2018-12-11T11:39:08.737077772Z caller=main.go:247 fd_limits="(soft=1048576, hard=1048576)" level=info ts=2018-12-11T11:39:08.737095504Z caller=main.go:248 vm_limits="(soft=unlimited, hard=unlimited)" level=info ts=2018-12-11T11:39:08.757770232Z caller=main.go:562 msg="Starting TSDB ..." level=info ts=2018-12-11T11:39:08.757939887Z caller=web.go:399 component=web msg="Start listening for connections" address=0.0.0.0:9090 level=info ts=2018-12-11T11:39:08.769272192Z caller=main.go:572 msg="TSDB started" level=info ts=2018-12-11T11:39:08.769349241Z caller=main.go:632 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml level=info ts=2018-12-11T11:39:08.772044947Z caller=main.go:658 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml level=info ts=2018-12-11T11:39:08.77209973Z caller=main.go:531 msg="Server is ready to receive web requests."

  5) 访问http://ip:9090

    成功的话会出现以下页面

prmetheus

2. 安装mongodb_exporter

刚说到Prometheus是根据endpoints来获取指标数据的,由于grafana没有直接的插件来获取mongo数据库的指标数据,这时候就可以用得上mongodb_exporter了,参考1参考2

  1)安装镜像

可以直接build刚刚参考1和参考2的Dockerfile,也可以像安装Prometheus一样直接search,我这边为了简单采用直接search的方式,方式重复不累赘描述。

  2)启动镜像

docker run -d --name mongo-explorer -p 9104:9104 eses/mongodb_exporter --mongodb.uri mongodb://monitor:monitor@ip:port

参数需要的mongo地址直接命令传入,无需export 。

如何知道端口和参数呢,一种是看官方说明,另一种直接启动一个空镜像看日志就知道了。

  3) 检查下日志

[root@vhost18 prometheus]# docker logs -f mongo-explorer
### Warning: the exporter is in beta/experimental state and field names are very
### likely to change in the future and features may change or get removed!
### See: https://github.com/percona/mongodb_exporter for updates
mongodb_exporter version: unknown, git commit hash: unknown
Listening on :9104

  4)访问http://ip:9090

    成功的话会出现以下页面

3. mongodb_exporter整合进Prometheus

  1)  修改配置

    只需要在prometheus.yml上添加如下框

  2) 重启prometheus

docker restart prom

  3) 检查

    访问http://172.28.64.10:9090/targets,能看到数据源就表示成功了。

4. grafana中添加数据源

5. 安装仪表盘

  到grafana官网直接搜mongo,找到自己需要的仪表盘就可以了,后续在自己改造。

猜你喜欢

转载自www.cnblogs.com/zqyx/p/10107233.html