写的比较粗糙。等整个项目上生产环境的时候,再补充完善这篇博文。


官方网站

https://prometheus.io/

https://github.com/prometheus

https://github.com/prometheus/mysqld_exporter

https://prometheus.io/docs/


参考文档:

http://www.cnblogs.com/sfnz/p/6566951.html

http://www.jb51.net/article/107386.htm

https://www.iamle.com/archives/2130.html



新建目录:

mkdir -p /root/prometheus

然后把prometheus、grafana的包都丢到这个目录下,这是我们的工作目录(当然生产环境下不要这个路径规划,建议生产环境下丢到/opt目录下去)。



编写用于安装mysqld_exporter的yml文件:

- hosts: 10.0.10.100
  tasks:
    - name: copy install file
      copy: src=files/mysqld_exporter-0.10.0.linux-amd64.tar.gz dest=/tmp/mysqld_exporter-0.10.0.linux-amd64.tar.gz
    - name: install mysqld_exporter
      shell: cd /tmp && tar xf mysqld_exporter-0.10.0.linux-amd64.tar.gz -C /opt && cd /opt/mysqld_exporter-0.10.0.linux-amd64 && echo -e "[client]\nuser=prom\npassword=123456\nhost=$(/sbin/ip a|egrep "10.[0|1].[1-9]+.[1-9]+*" | awk '{print $2}'| cut -d '/' -f 1)" > .my.cnf && cd /opt/mysqld_exporter-0.10.0.linux-amd64 && nohup ./mysqld_exporter -config.my-cnf=".my.cnf" &

只要使用ansible批量推送这个软件到mysqld服务器兵启动即可开始采集数据了。


在mysql上加prom采集数据的授权:

#!/bin/bash
# 添加prom监控账号 以便mysqld_exporter 采集数据
# Date: 2017/08/01

source /etc/profile

MYSQLCMD="/usr/local/mysql/bin/mysql -uroot -p111111"

IP=$(ip a|egrep "10.[0|1].[1-9]+.[1-9]+*" | awk '{print $2}'| cut -d '/' -f 1)

# 只添加本机的prom账户,因此这里不写binlog 防止同步到从库去
$MYSQLCMD -e "set session sql_log_bin=OFF;grant process,replication client,select on *.* to 'prom'@'$IP' identified by '123456'; "



在prometheus.yml里面添加相关监控host,类似如下:

wKioL1mF1GOQGPc1AAAaypX0tR4866.png


然后,使用 ./prometheus 来启动prometheus。



然后浏览器访问prometheus的服务器的9090端口,即可打开一个web界面。切换到status---> Targets ,如果mysqld_exporter能正常工作的话,这里应该显示很多的up状态的主机。类似下图这种(图是我从线上截取的):

wKiom1mF1iXC8w5oAABUp19Sndg417.png



安装grafana:

vim  /root/prometheus/grafana-4.3.2/conf/defaults.ini 修改下面的地方:

[dashboards.json]

enabled = true 

path = /var/lib/grafana/dashboards


安装grafana的仪表盘:

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

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


启动grafana-server:

cd /root/prometheus/grafana-4.3.2 

./bin/grafana-cli plugins install percona-percona-app

./bin/grafana-cli plugins install alexanderzobnin-zabbix-app

./bin/grafana-server

默认是监听在3000端口的。浏览器访问即可。默认用户名和密码都是admin。