使用Prometheus+Grafana 監控MySQL/MONGODB

使用Prometheus+Grafana 監控MySQL/MONGODB
之前就久仰 Prometheus 大名,因爲有用zabbix 進行監控,就沒去安裝它。現在正好用上監控MONGO+MYSQL。

服務器端組件: 
prometheus      #服務端
alertmanager    #預警

Grafana         #前端展示,



客戶端組件:
    node_exporter
    mongodb_exporter
    mysqld_exporter

mongodb01 192.168.0.11
mongodb02 192.168.0.12
mysql01 192.168.0.13
mysql02 192.168.0.14

prometheus Grafana 安裝在mongodb02
mongodb01,mongodb02 安裝了 node_exporter ,mongodb_exporter
mysql01,mysql02 安裝了 node_exporter ,mysql_exporter

+

1.安裝 GO
golang-bin.x86_64 0:1.8.3-1.el7

1.下載安裝 Prometheus (https://prometheus.io/download/)

1.1 下載安裝

[root@mongodb01 download]# tar xvf prometheus-2.0.0.linux-amd64.tar.gz
-C /usr/local/
mv /usr/local/prometheus-2.0.0.linux-amd64 /usr/local/prometheus2.0

1.2 配置文件
 [root@mongodb01 download]#vi /usr/local/prometheus2.0/prometheus.yml 
# 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']


      - job_name: linux_mongo1
        static_configs:
          - targets: ['192.168.11.190:9100']
            labels:
              instance: linux_mongo1

      - job_name: mongodb1
        static_configs:
          - targets: ['192.168.11.190:9104']
            labels:
              instance: mongodb1


      - job_name: linux_mongo2
        static_configs:
          - targets: ['192.168.11.191:9100']
            labels:
              instance: linux_mongo2

      - job_name: mongodb2
        static_configs:
          - targets: ['192.168.11.191:9104']
            labels:
              instance: mongodb2


      - job_name: linux_mysql1
        static_configs:
          - targets: ['192.168.11.192:9100']
            labels:
              instance: linux_mysql1

      - job_name: mysqldb1
        static_configs:
          - targets: ['192.168.11.192:9104']
            labels:
              instance: mysqldb1


      - job_name: linux_mysql2
        static_configs:
          - targets: ['192.168.11.193:9100']
            labels:
              instance: linux_mysql2

      - job_name: mysqldb2
        static_configs:
          - targets: ['192.168.11.193:9104']
            labels:
              instance: mysqldb2

    1.3 啓動 prometheus
        [root@mongodb01 prometheus2.0]# ./prometheus --config.file=prometheus.yml &
        [root@mongodb01 prometheus2.0]# level=info ts=2017-12-07T05:56:43.220942048Z caller=main.go:215 msg="Starting Prometheus" version="(version=2.0.0, branch=HEAD, revision=0a74f98628a0463dddc90528220c94de5032d1a0)"
        level=info ts=2017-12-07T05:56:43.221007739Z caller=main.go:216 build_context="(go=go1.9.2, user=root@615b82cb36b6, date=20171108-07:11:59)"
        level=info ts=2017-12-07T05:56:43.221024785Z caller=main.go:217 host_details="(Linux 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 mongodb01 (none))"
        level=info ts=2017-12-07T05:56:43.223677457Z caller=web.go:380 component=web msg="Start listening for connections" address=0.0.0.0:9090
        level=info ts=2017-12-07T05:56:43.223707498Z caller=main.go:314 msg="Starting TSDB"
        level=info ts=2017-12-07T05:56:43.223717423Z caller=targetmanager.go:71 component="target manager" msg="Starting target manager..."
        level=info ts=2017-12-07T05:56:43.229958353Z caller=main.go:326 msg="TSDB started"
        level=info ts=2017-12-07T05:56:43.230020283Z caller=main.go:394 msg="Loading configuration file" filename=prometheus.yml
        level=info ts=2017-12-07T05:56:43.231843003Z caller=main.go:371 msg="Server is ready to receive requests."

2.下載 EXPORTER: MONGODB/MYSQL/NODE

https://github.com/percona/mongodb_exporter
https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz

3.安裝 node_exporter

[root@mysql01 app]# tar -xzvf
node_exporter-0.15.2.linux-amd64.tar.gz -C /usr/local/
node_exporter-0.15.2.linux-amd64/
node_exporter-0.15.2.linux-amd64/LICENSE
node_exporter-0.15.2.linux-amd64/NOTICE
node_exporter-0.15.2.linux-amd64/node_exporter
[root@mysql01 app]# nohup /usr/local/node_exporter-0.15.2.linux-amd64/node_exporter &
[1] 6795
[root@mysql01 app]# nohup: ignoring input and appending output to ‘nohup.out’

4.安裝MYSQLD_EXPORTER

4.1 mysqld_exporter
[root@mysql01 app]# tar xvf mysqld_exporter-0.10.0.linux-amd64.tar.gz -C /usr/local
mysqld_exporter-0.10.0.linux-amd64/
mysqld_exporter-0.10.0.linux-amd64/LICENSE
mysqld_exporter-0.10.0.linux-amd64/NOTICE
mysqld_exporter-0.10.0.linux-amd64/mysqld_exporter

4.2 添加監控用戶及設置權限
root@master:mysql>GRANT REPLICATION CLIENT,PROCESS ON *.* TO 'mysql_monitor'@'localhost' identified by 'mysql_monitor';
    Query OK, 0 rows affected (0.00 sec)

    root@master:mysql>GRANT SELECT ON *.* TO 'mysql_monitor'@'localhost';
    Query OK, 0 rows affected (0.00 sec)
4.3配置文件添加MYSQL訪問信息
$ vi /usr/local/mysqld_exporter-0.10.0.linux-amd64/.my.cnf
    [client]
    port=3306
    user=mysql_monitor
    password=mysql_monitor
4.4 啓動

[root@mysql01 app]#
/usr/local/mysqld_exporter-0.10.0.linux-amd64/mysqld_exporter
-config.my-cnf=”/usr/local/mysqld_exporter-0.10.0.linux-amd64/.my.cnf” &

4.5 Mongodb_exporter 安裝 
    download:
        軟件包: prometheus-mongodb-exporter 1.0.0-1 
        https://packages.debian.org/stretch/prometheus-mongodb-exporter


    1.yum install -y golang glide
    2.定義一個gopath的環境變量
    export GOPATH=/opt/go/
    3.把exporter包拷到$GOPATH/src/github.com/dcu/mongodb_exporter
    4.cd $GOPATH/src/github.com/dcu/mongodb_exporter   && make build

    5.啓動 登錄到mongodb server.
    ./mongodb_exporter -mongodb.uri mongodb://root:psd@localhost:2001/admin


5.安裝  grafana
    [root@mongodb02 download]# yum install initscripts
    [root@mongodb02 download]# yum install fontconfig
    [root@mongodb02 grafana]# yum install freetype*
    [root@mongodb02 grafana]# yum install urw-fonts

    [root@mongodb02 download]# rpm -Uvh grafana-4.6.2-1.x86_64.rpm
    warning: grafana-4.6.2-1.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 24098cb6: NOKEY
    Preparing...                          ################################# [100%]
    Updating / installing...
       1:grafana-4.6.2-1                  ################################# [100%]
    ### NOT starting on installation, please execute the following statements to configure grafana to start automatically using systemd
     sudo /bin/systemctl daemon-reload
     sudo /bin/systemctl enable grafana-server.service
    ### You can start grafana-server by executing
     sudo /bin/systemctl start grafana-server.service
    POSTTRANS: Running script


    [database]
    # You can configure the database connection by specifying type, host, name, user and password
    # as seperate properties or as on string using the url propertie.

    # Either "mysql", "postgres" or "sqlite3", it's your choice
    type = mysql
    host = 127.0.0.1:3306
    name = grafana
    user = grafana
    # If the password contains # or ; you have to wrap it with trippel quotes. Ex """#password;"""
    password =

    # Use either URL or the previous fields to configure the database
    # Example: mysql://user:secret@host:port/database
    url =mysql://grafana:5SCti04n0wXunsj2@localhost:3106/grafana

    # For "postgres" only, either "disable", "require" or "verify-full"
    ;ssl_mode = disable

    # For "sqlite3" only, path relative to data_path setting
    ;path = grafana.db

    # Max idle conn setting default is 2
    ;max_idle_conn = 2

    # Max conn setting default is 0 (mean not set)
    ;max_open_conn =
5.1 編輯配置文件/etc/grafana/grafana.ini,修改dashboards.json段落下兩個參數的值:
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards

5.2 下載安裝儀表盤  grafana-dashboards

gwet
https://codeload.github.com/percona/grafana-dashboards/zip/master
unzip grafana-dashboards-master.zip
cp -r grafana-dashboards-master/dashboards /var/lib/grafana

5.3運行Grafana服務
  $ systemctl daemon-reload
    $ systemctl start grafana-server
    $ systemctl status grafana-server
5.4 可以通過端口3000打開 頁面 http://47.95.47.18:3000/ (默認用戶 admin/admin)

5.5 mysql 數據庫中添加庫名及用戶賬號
grant all privileges on grafana.* to 'grafana'@'192.168.%' identified by 'grafana' ;
    CREATE DATABASE IF NOT EXISTS grafana DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
5.6 安裝plugins
[root@mongodb02 mysql]# grafana-cli plugins list-remote
id: abhisant-druid-datasource version: 0.0.4
id: adremsoft-netcrunch-app version: 1.0.0
id: alexanderzobnin-zabbix-app version: 3.7.0
id: ayoungprogrammer-finance-datasource version: 1.0.0
#下載安裝 zabbix
# git clone https://github.com/linglong0820/grafana-zabbix

# mkdir /usr/share/grafana/public/app/plugins/datasource/ -pv

# cp -r grafana-zabbix/zabbix/ /usr/share/grafana/public/app/plugins/datasource/
使用grafana-cli 安裝插件

# grafana-cli plugins install alexanderzobnin-zabbix-app 安裝zabbix插件

#grafana-cli plugins install grafana-worldmap-panel 安裝世界地圖插件

#grafana-cli plugins install grafana-clock-panel 安裝時間插件

#grafana-cli plugins install grafana-piechart-panel 安裝圓餅插件

# /etc/init.d/grafana-server restart
5.7 導入 dashboard
 unzip grafana-dashboards-master.zip
    cp grafana-dashboards-master/dashboards /var/lib/grafana/
prometheus


grafana


http://39.106.152.0:3000/login

參考URL

客戶端下載
https://github.com/percona/mongodb_exporter

官方文檔
https://prometheus.io/docs/operating/configuration/

https://prometheus.io/blog/2015/06/01/advanced-service-discovery/

http://docs.grafana.org/features/datasources/prometheus/

軟件包: prometheus-mongodb-exporter (1.0.0-1 以及其他的) download
https://packages.debian.org/stretch/prometheus-mongodb-exporter

“mongodb://root:123456@localhost:24001/admin”

PMM INSTALL
http://www.ywnds.com/?p=9713
http://www.jianshu.com/p/97233bac2964
https://www.linuxba.com/archives/7915#36_pmm_client

grafana + cloudwatch
http://www.mamicode.com/info-detail-1750659.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章