Skip to content

第46章 利用Prometheus Grafana 搭建炫酷的MySQL监控平台

xiaoboluo768 edited this page Jun 18, 2020 · 4 revisions
  • 图 46-2 图 46-2

46.1.2 解压缩程序包

[root@localhost ~]# mkdir /data
[root@localhost ~]# tar xvf prometheus-2.1.0.linux-amd64.tar.gz -C /data/
 
[root@localhost ~]# tar xf node_exporter-0.15.2.linux-amd64.tar -C /root/

# 如果要监控MySQL,则继续解压缩mysqld_exporter
[root@localhost ~]# tar xf mysqld_exporter-0.10.0.linux-amd64.tar  -C /root/
  • 图 46-8 图 46-8

46.1.3 启动Prometheus

[root@localhost ~]# cd /data/
[root@localhost data]# mv prometheus-2.1.0.linux-amd64/ prometheus
[root@localhost ~]# cd /data/prometheus

[root@localhost data]# cat 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).

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- file_sd_configs:  # 注意,如果指定从某配置文件中加载监控目标,则在Prometheus启动之前需要确保该文件在Prometheus的工作目录下事先存在,否则在后续配置过程中可能会报错
  - files:
    - host.yml
  job_name: Host
  metrics_path: /metrics
  relabel_configs:
  - source_labels: [__address__]
    regex: (.*)
    target_label: instance
    replacement: $1
  - source_labels: [__address__]
    regex: (.*)
    target_label: __address__
    replacement: $1:9100
- file_sd_configs: # 注意,如果指定从某配置文件中加载监控目标,则在Prometheus启动之前需要确保该文件在Prometheus的工作目录下事先存在,否则在后续配置过程中可能会报错
  - files:
    - mysql.yml
  job_name: MySQL
  metrics_path: /metrics
  relabel_configs:
  - source_labels: [__address__]
    regex: (.*)
    target_label: instance
    replacement: $1
  - source_labels: [__address__]
    regex: (.*)
    target_label: __address__
    replacement: $1:9104


- job_name: prometheus
  static_configs:
  - targets:
    - localhost:9090

[root@localhost prometheus]# cd /data/prometheus
[root@localhost prometheus]# /data/prometheus/prometheus  --storage.tsdb.retention=\ 30d &

# 修改WorkingDirectory参数为Prometheus的工作目录
[root@localhost prometheus]# cat /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus instance
Wants=network-online.target
After=network-online.target
After=postgresql.service mariadb.service mysql.service

[Service]
User=root
Group=root
Type=simple
Restart=on-failure
WorkingDirectory=/data/prometheus/
RuntimeDirectory=prometheus
RuntimeDirectoryMode=0750
ExecStart=/data/prometheus/prometheus  --storage.tsdb.retention=30d --config.file=/data/ prometheus/prometheus.yml
LimitNOFILE=10000
TimeoutStopSec=20

[Install]
WantedBy=multi-user.target

46.2.2 解压缩程序包

[root@localhost ~]# tar xf grafana-4.6.3.linux-x64.tar.gz -C /data/prometheus/
[root@localhost ~]# cd /data/prometheus
[root@localhost prometheus]# mv grafana-4.6.3/ grafana

46.2.3 启动Grafana

[root@localhost ]# cd /data/prometheus/grafana
[root@localhost ]# ./bin/grafana-server &

[root@localhost service]# cat /usr/lib/systemd/system/grafana-server.service
[Unit]
Description=Grafana instance
Documentation=http://docs.grafana.org
Wants=network-online.target
After=network-online.target
After=postgresql.service mariadb.service mysql.service

[Service]
User=root
Group=root
Type=simple
Restart=on-failure
WorkingDirectory=/data/prometheus/grafana
RuntimeDirectory=grafana
RuntimeDirectoryMode=0750
ExecStart=/data/prometheus/grafana/bin/grafana-server
LimitNOFILE=10000
TimeoutStopSec=20

[Install]
WantedBy=multi-user.target
  • 图 46-12 图 46-12

46.2.4 在Grafana中导入grafana-dashboards

[root@localhost ~]# tar xvf grafana-dashboards-1.6.1.tar.gz 
[root@localhost ~]# cd grafana-dashboards-1.6.1
[root@localhost grafana-dashboards-1.6.1]# updatedb 
[root@localhost grafana-dashboards-1.6.1]# locate json |grep dashboards/
......
  • 图 46-13 图 46-13
  • 图 46-14 图 46-14
  • 图 46-15 图 46-15
  • 图 46-17 图 46-17
  • 图 46-18 图 46-18

46.3.1 添加主机监控

[root@localhost ~]# tar xf node_exporter-0.15.2.linux-amd64.tar 
[root@localhost ~]# mv node_exporter-0.15.2.linux-amd64 node_exporter

[root@localhost ~]# cd node_exporter
[root@localhost node_exporter]# nohup ./node_exporter &

[root@localhost node_exporter]# cat /data/prometheus/host.yml
- labels:
    service: test
  targets:
  - 10.10.30.165

[root@localhost mysqld_exporter]# cat /data/prometheus/host.yml
- labels:
    service: test
  targets:
  - 10.10.30.165
  - 10.10.20.14

46.3.2 添加MySQL监控

[root@localhost ~]# tar xf mysqld_exporter-0.10.0.linux-amd64.tar 
[root@localhost ~]# mv mysqld_exporter-0.10.0.linux-amd64 mysqld_exporter

[root@luoxiaobo-01 ~]# export DATA_SOURCE_NAME='admin:password@(10.10.20.14:3306)/'
[root@luoxiaobo-01 ~]# echo "export DATA_SOURCE_NAME='admin:password@(10.10.20.14:3306)\ /'" >> /etc/profile

# 由于目前最新的版本默认关闭了大量的MySQL采集项,需要显式使用相应的选项开启(截至本书写作时,最新的开发版本可以通过Prometheus端的配置项让Exporter端生效,而无须在Exporter中使用大量的启动选项开启)
[root@localhost ~]# cd mysqld_exporter
[root@localhost mysqld_exporter]# nohup ./mysqld_exporter --collect.info_schema.\ processlist --collect.info_schema.innodb_tablespaces --collect.info_schema.innodb_metrics\  --collect.perf_schema.tableiowaits --collect.perf_schema.indexiowaits --collect.perf_schema.\ tablelocks --collect.engine_innodb_status --collect.perf_schema.file_events --collect.info_\ schema.processlist --collect.binlog_size --collect.info_schema.clientstats --collect.perf_\ schema.eventswaits &
# 注意,新版本的mysqld_exporter可能不支持--collect.info_schema.processlist 选项,请自行使用./mysqld_exporter --help查看

[root@localhost mysqld_exporter]# cat /data/prometheus/mysql.yml
- labels:
    service: mysql_test
  targets:
 - 10.10.20.14

[root@localhost mysqld_exporter]# cat /data/prometheus/mysql.yml
- labels:
    service: mysql_test
  targets:
  - 10.10.30.165
  - 10.10.20.14

46.3.3

  • 图 46-22 图 46-22

上一篇:第45章 mysqladmin和innotop工具详解 | 下一篇:第47章 Percona Toolkit常用工具详解

Clone this wiki locally