­

基於prometheus+grafana 搭建監控mysql redis mongodb等

  • 2019 年 10 月 4 日
  • 筆記

寫的比較粗糙。等整個項目上生產環境的時候,再補充完善這篇博文。

官方網站

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=promnpassword=123456nhost=$(/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,類似如下:

然後,使用 ./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。