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,找到自己需要的儀表盤就可以了,后續在自己改造。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM