利用Prometheus与Grafana对Mysql服务器的性能监控详解

利用Prometheus与Grafana对Mysql服务器的性能监控详解

转载  更新时间:2017年03月04日 08:37:38   做者:LoyaChen    我要评论php

 

Prometheus是源于 Google Borgmon的一个开源监控系统,用 Golang开发。被不少人称为下一代监控系统。Grafana是一个开源的图表可视化系统,简单说图表配置比较方便、生成的图表比较漂亮。下面就介绍了利用Prometheus与Grafana对Mysql服务器性能监控的方法。html

概述node

Prometheus是一个开源的服务监控系统,它经过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上。它提供了一个简单的网页界面、一个功能强大的查询语言以及HTTP接口等等。Prometheus经过安装在远程机器上的exporter来收集监控数据,这里用到了如下两个exporter:mysql

  1. node_exporter – 用于机器系统数据
  2. mysqld_exporter – 用于Mysql服务器数据

Grafana是一个开源的功能丰富的数据可视化平台,一般用于时序数据的可视化。它内置了如下数据源的支持:linux

并能够经过插件扩展支持的数据源。git

架构图github

下面是本次部署的架构图web

安装并运行Prometheussql

1.在 Monitor 上安装 Prometheus数据库

安装至/opt/prometheus

?

1

2

3

$ wget https://github.com/prometheus/prometheus/releases/download/v1.5.2/prometheus-1.5.2.linux-amd64.tar.gz

$ tar zxvf prometheus-1.5.2.linux-amd64.tar.gz

$ mv prometheus-1.5.2.linux-amd64 /opt/prometheus

2.在安装目下编辑配置文件 prometheus.yml

?

1

vim /opt/prometheus/prometheus.yml

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

# 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).

 # Attach these labels to any time series or alerts when communicating with

 # external systems (federation, remote storage, Alertmanager).

 external_labels:

 monitor: 'codelab-monitor'

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.

rule_files:

 # - "first.rules"

 # - "second.rules"

# 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

 static_configs:

 - targets: ['172.30.251.214:9100']

 labels:

 instance: db1

 - job_name: mysql

 static_configs:

 - targets: ['172.30.251.214:9104']

 labels:

 instance: db1

其中:172.30.251.214是MySQL的IP,端口则是对应的exporter的监听端口。

3.启动Prometheus

?

1

2

3

4

5

6

7

8

[loya@centos6 prometheus]$ ./prometheus

INFO[0000] Starting prometheus (version=1.5.2, branch=master, revision=bd1182d29f462c39544f94cc822830e1c64cf55b) source=main.go:75

INFO[0000] Build context (go=go1.7.5, user=root@a8af9200f95d, date=20170210-14:41:22) source=main.go:76

INFO[0000] Loading configuration file prometheus.yml source=main.go:248

INFO[0000] Loading series map and head chunks... source=storage.go:373

INFO[0000] 0 series loaded. source=storage.go:378

INFO[0000] Starting target manager... source=targetmanager.go:61

INFO[0000] Listening on :9090 source=web.go:259

Prometheus内置了一个web界面,能够经过http://monitor_host:9090进行访问:

在Status->Targets页面下,能够看到咱们配置的两个Target,它们的State为DOWN。

4.接下来须要在被监控机器上安装并运行exporter

下载exporters并解压:

?

1

2

3

4

5

6

7

8

9

10

11

$ mkdir /opt/prometheus/prometheus_exporters

$ cd /opt/prometheus/prometheus_exporters

$ wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0-rc.1/node_exporter-0.14.0-rc.1.linux-amd64.tar.gz

$ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.9.0/mysqld_exporter-0.9.0.linux-amd64.tar.gz

$ tar zxvf node_exporter-0.14.0-rc.1.linux-amd64.tar.gz

$ tar zxvf mysqld_exporter-0.9.0.linux-amd64.tar.gz

# 将各自的可执行文件移动到/opt/prometheus/prometheus_exporters

$ ll /opt/prometheus/prometheus_exporters

total 24464

-rwxr-xr-x 1 root root 12182376 Feb 23 19:01 mysqld_exporter

-rwxr-xr-x 1 root root 12862209 Feb 23 19:01 node_exporter

运行node_exporter

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

$ cd /opt/prometheus/prometheus_exporters

$ ./node_exporter

INFO[0000] Starting node_exporter (version=0.14.0-rc.1, branch=master, revision=5a07f4173d97fa0dd307db5bd3c2e6da26a4b16e) source="node_exporter.go:136"

INFO[0000] Build context (go=go1.7.4, user=root@ed143c8f2fcd, date=20170116-16:00:03) source="node_exporter.go:137"

INFO[0000] No directory specified, see --collector.textfile.directory source="textfile.go:57"

INFO[0000] Enabled collectors: source="node_exporter.go:156"

INFO[0000] - entropy source="node_exporter.go:158"

INFO[0000] - loadavg source="node_exporter.go:158"

INFO[0000] - stat source="node_exporter.go:158"

INFO[0000] - diskstats source="node_exporter.go:158"

INFO[0000] - textfile source="node_exporter.go:158"

INFO[0000] - vmstat source="node_exporter.go:158"

INFO[0000] - meminfo source="node_exporter.go:158"

INFO[0000] - filefd source="node_exporter.go:158"

INFO[0000] - filesystem source="node_exporter.go:158"

INFO[0000] - mdadm source="node_exporter.go:158"

INFO[0000] - netdev source="node_exporter.go:158"

INFO[0000] - sockstat source="node_exporter.go:158"

INFO[0000] - time source="node_exporter.go:158"

INFO[0000] - zfs source="node_exporter.go:158"

INFO[0000] - edac source="node_exporter.go:158"

INFO[0000] - hwmon source="node_exporter.go:158"

INFO[0000] - netstat source="node_exporter.go:158"

INFO[0000] - uname source="node_exporter.go:158"

INFO[0000] - conntrack source="node_exporter.go:158"

INFO[0000] Listening on :9100 source="node_exporter.go:176"

mysqld_exporter须要链接到MySQL,因此须要MySQL权限,咱们为其建立用户并赋予所需的权限:

?

1

2

mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'prom'@'localhost' identified by 'abc123';

mysql> GRANT SELECT ON performance_schema.* TO 'prom'@'localhost';

建立.my.cnf文件并运行mysqld_exporter:

?

1

2

3

4

5

6

7

8

9

10

11

12

$ cd /opt/prometheus/prometheus_exporters

$

$ cat << EOF > .my.cnf

[client]

user=prom

password=abc123

EOF

$

$ ./mysqld_exporter -config.my-cnf=".my.cnf"

INFO[0000] Starting mysqld_exporter (version=0.9.0, branch=master, revision=8400af20ccdbf6b5e0faa2c925c56c48cd78d70b) source=mysqld_exporter.go:432

INFO[0000] Build context (go=go1.6.3, user=root@2c131c66ca20, date=20160926-18:28:09) source=mysqld_exporter.go:433

INFO[0000] Listening on :9104 source=mysqld_exporter.go:451

回到Prometheus web界面Status->Targets,能够看到两个Target的状态已经变成UP了:

安装和运行Grafana

安装:

Grafana 的安装官方文档比较清晰,这里再也不赘述。

配置:

编辑配置文件/etc/grafana/grafana.ini,修改dashboards.json段落下两个参数的值:

?

1

2

3

[dashboards.json]

enabled = true

path = /var/lib/grafana/dashboards

导入Prometheus Dashboard:

?

1

2

$ git clone https://github.com/percona/grafana-dashboards.git

$ cp -r grafana-dashboards/dashboards /var/lib/grafana

启动:

?

1

$ /etc/init.d/grafana-server start

经过http://monitor_host:3000访问Grafana Web界面(缺省账号/密码为admin/admin)

登陆后,经过Data Sources页面添加数据源:

而后就能够经过选择不一样的仪表盘(左上角)和时间段(右上角)来呈现图表了:

System Overview:

MySQL Overview:

总结

以上就是这篇文章的所有内容了,但愿本文的内容对你们的学习或者工做能带来必定的帮助,若是有疑问你们能够留言交流,谢谢你们对脚本之家的支持。

您可能感兴趣的文章:

原文连接:http://qingkang.me/Grafana-Prometheus-Monitor.html

相关文章
相关标签/搜索