寫的比較粗糙。等整個項目上生産環境的時候,再補充完善這篇博文。
官方網站
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,類似如下:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuYjN4QjU0BDWwlXYBFUQxMGUHF1THFjRtFDTvl2S39CX3M0LcRUOvwFMw00LcJDMzZWe39CXvwVbvNmLvR3YxUjL0M3Lc9CX6MHc0RHaiojIsJye.png)
然後,使用 ./prometheus 來啟動prometheus。
然後浏覽器通路prometheus的伺服器的9090端口,即可打開一個web界面。切換到status---> Targets ,如果mysqld_exporter能正常工作的話,這裡應該顯示很多的up狀态的主機。類似下圖這種(圖是我從線上截取的):
安裝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。