利用Prometheus与Grafana对Mysql服务器的性能监控详解
概述
prometheus是一个开源的服务监控系统,它通过http协议从远程的机器收集数据并存储在本地的时序数据库上。它提供了一个简单的网页界面、一个功能强大的查询语言以及http接口等等。prometheus通过安装在远程机器上的exporter来收集监控数据,这里用到了以下两个exporter:
- node_exporter – 用于机器系统数据
- mysqld_exporter – 用于mysql服务器数据
grafana是一个开源的功能丰富的数据可视化平台,通常用于时序数据的可视化。它内置了以下数据源的支持:
并可以通过插件扩展支持的数据源。
架构图
下面是本次部署的架构图
安装并运行prometheus
1.在 monitor 上安装 prometheus
安装至/opt/prometheus
$ 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
vim /opt/prometheus/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). # 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
[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并解压:
$ 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
$ 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权限,我们为其创建用户并赋予所需的权限:
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:
$ 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段落下两个参数的值:
[dashboards.json] enabled = true path = /var/lib/grafana/dashboards
导入prometheus dashboard:
$ git clone https://github.com/percona/grafana-dashboards.git $ cp -r grafana-dashboards/dashboards /var/lib/grafana
启动:
$ /etc/init.d/grafana-server start
通过http://monitor_host:3000访问grafana web界面(缺省帐号/密码为admin/admin)
登录后,通过data sources页面添加数据源:
然后就可以通过选择不同的仪表盘(左上角)和时间段(右上角)来呈现图表了:
system overview:
mysql overview:
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: Python自动登录126邮箱的方法
下一篇: 通过实例讲解JS如何防抖动