
俗話說,沒有監(jiān)控的系統(tǒng)就是在裸奔,好的監(jiān)控就是運(yùn)維人員的第三只手,第三只眼。本文將使用prometheus及Grafana搭建一套監(jiān)控系統(tǒng)來監(jiān)控主機(jī)及數(shù)據(jù)庫(MySQL、Redis)。
一、安裝Grafana
Grafana是一個(gè)可視化面板(Dashboard),有著非常漂亮的圖表和布局展示,功能齊全的度量?jī)x表盤和圖形編輯器,支持Graphite、zabbix、InfluxDB、Prometheus等數(shù)據(jù)源。
1、下載并安裝
下載地址:
https://grafana.com/grafana/download。

選擇最新的版本進(jìn)行安裝,按照網(wǎng)站的提示運(yùn)行腳本即可(監(jiān)控服務(wù)器需可訪問外網(wǎng),如無法訪問外網(wǎng)可與我溝通如何離線快速部署)。
運(yùn)行如下腳本
wget https://dl.grafana.com/oss/release/grafana-6.3.3-1.x86_64.rpm
sudo yum localinstall grafana-6.3.3-1.x86_64.rpm
2、啟動(dòng)grafana
安裝完成后,grafana服務(wù)默認(rèn)已安裝,配置文件為/etc/grafana/grafana.ini,如需修改路徑及端口,可在該文件中修改

啟動(dòng)grafana
/etc/init.d/grafana-server  start
3、登錄grafana
訪問頁面http://服務(wù)器IP:3000 ,默認(rèn)賬號(hào)、密碼admin/admin 首次登錄將提示修改密碼,建議修改。

二、安裝Prometheus
1、Prometheus 主程序安裝
Prometheus 主程序,主要是負(fù)責(zé)存儲(chǔ)、抓取、聚合、查詢方面。
可登錄官網(wǎng)進(jìn)行下載,官網(wǎng)下載地址:
https://prometheus.io/download/
根據(jù)操作系統(tǒng)類別選擇文件進(jìn)行下載,本次部署在linux上。

/**  下載*/
wget https://github.com/prometheus/prometheus/releases/download/v2.12.0/prometheus-2.12.0.linux-amd64.tar.gz
/**  解壓*/
tar -zxvf prometheus-2.12.0.linux-amd64.tar.gz
2、啟動(dòng)prometheus主程序
生產(chǎn)環(huán)境可參考如下方式啟動(dòng)。
/** 生產(chǎn)環(huán)境啟動(dòng)*/
nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle --storage.tsdb.retention.time=60d   &
/**   
 --web.enable-lifecycle  加上此參數(shù)可以遠(yuǎn)程熱加載配置文件,無需重啟prometheus,調(diào)用指令是curl -X POST http://ip:9090/-/reload
-- storage.tsdb.retention.time 數(shù)據(jù)默認(rèn)保存時(shí)間為15天,啟動(dòng)時(shí)加上此參數(shù)可以控制數(shù)據(jù)保存時(shí)間
*/
其他的參數(shù)及配置可以在prometheus.yml中調(diào)整及配置。
三、在需監(jiān)控的機(jī)器上部署exporter
1、監(jiān)控linux主機(jī)
下載監(jiān)控linux主機(jī)的node_exporter,依舊從官網(wǎng)下載。

/**  下載  */
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
/**  解壓  */
tar  -zxvf node_exporter-0.18.1.linux-amd64.tar.gz
可以按照默認(rèn)方式啟動(dòng)
/** 啟動(dòng) node_exporter*/
cd  node_exporter-0.18.1.linux-amd64
nohup ./node_exporter  &
/**
默認(rèn)端口9100
*/
2、監(jiān)控MySQL
(1)下載
下載監(jiān)控MySQL的mysqld_exporter,依舊從官網(wǎng)下載

/**  下載  */
wget  https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
/**  解壓  */
tar -zxvf  mysqld_exporter-0.12.1.linux-amd64.tar.gz
(2)監(jiān)控賬號(hào)及修改文件配置
在MySQL里配置MySQL監(jiān)控賬號(hào)。
/**  創(chuàng)建賬號(hào)  */
mysql> CREATE USER 'mysql_monitor'@'localhost' identified by 'mysql_monitor'; 
/** 授權(quán) */
mysql> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysql_monitor'@'localhost'; 
mysql> GRANT SELECT ON performance_schema.* TO 'mysql_monitor'@'localhost';
/** 
注意,不用版本對(duì)權(quán)限要求不一致,啟動(dòng)時(shí)注意查看日志,如權(quán)限不足則繼續(xù)授權(quán)或創(chuàng)建對(duì)應(yīng)的賬號(hào) 
*/
配置文件修改。
cd mysqld_exporter-0.12.0.linux-amd64
vim .my.cnf 
/**  添加如下配置 */
[client]
port=3306
user=mysql_monitor
password=mysql_monitor
(3)啟動(dòng)監(jiān)控腳本
nohup   ./mysqld_exporter --config.my-cnf=.my.cnf  &
3、監(jiān)控redis
(1)下載redis_exporter
官網(wǎng)上沒有redis_exporter, 可以從github上獲取,另外redis插件無需放在redis機(jī)器上也可以
/**  下載  */
wget https://github.com/oliver006/redis_exporter/releases/download/v0.30.0/redis_exporter-v0.30.0.linux-amd64.tar.gz
/**  解壓  */
tar -zxvf  redis_exporter-v0.30.0.linux-amd64.tar.gz
(2)啟動(dòng)redis_exporter
/**  redis無密碼 */
nohup  ./redis_exporter -redis.addr=192.168.56.118:6379 -web.listen-address 0.0.0.0:9121  &
/**  redis有密碼  */
nohup  ./redis_exporter -redis.addr=192.168.56.118:6479 -redis.password 123456   -web.listen-address 0.0.0.0:9122 &
/**   
 -web.listen-address  可以自定義監(jiān)控端口
*/
四、配置prometheus配置文件
1、添加各監(jiān)控項(xiàng)
配置文件可以有多種配置方式,可以根據(jù)不同的分類和習(xí)慣配置??蓞⒖既缦路绞脚渲?/p>
# 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).
# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
# 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: 'OS'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ['192.168.56.114:9100']
      labels:
          instance: '192.168.56.114'
          
    - targets: ['192.168.56.116:9100']
      labels:
          instance: '192.168.56.116'
    - targets: ['192.168.56.117:9100']
      labels:
          instance: '192.168.56.117'
##  上述job單獨(dú)做主機(jī)監(jiān)控,每臺(tái)主機(jī)的instance不同
  - job_name: 'mysql'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ['192.168.56.116:9104']
      labels:
          instance: '192.168.56.116'
    
    - targets: ['192.168.56.117:9104']
      labels:
          instance: '192.168.56.117'
  ## 以上是監(jiān)控mysql的,instance和主機(jī)的instance的相同
  - job_name: 'redis'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ['192.168.56.118:9121','192.168.56.118:9122']
      labels:
          instance: '192.168.56.118'
    - targets: ['192.168.56.118:9100']
      labels:
          instance: '192.168.56.118'
#   可以類似上述這種,redis的主機(jī)及各redis監(jiān)控項(xiàng)組合在一起,instance使用相同的
2、啟動(dòng)或熱加載prometheus
/**  啟動(dòng)  */
nohup ./prometheus --config.file=prometheus.yml --web.enable-lifecycle --storage.tsdb.retention.time=60d   &
/**
-- storage.tsdb.retention.time 數(shù)據(jù)默認(rèn)保存時(shí)間為15天,啟動(dòng)時(shí)加上此參數(shù)可以控制數(shù)據(jù)保存時(shí)間
*/
/**  熱加載  */
curl -X POST http://ip:9090/-/reload
/**
熱加載的前提是啟動(dòng)時(shí)加了--web.enable-lifecycle
*/
五、配置各監(jiān)控儀表盤
1、下載各監(jiān)控儀表盤
以上模板grafana官方網(wǎng)站均有,可以根據(jù)自己的需要下載對(duì)應(yīng)的模板,對(duì)應(yīng)地址為。
https://grafana.com/grafana/dashboards
找到對(duì)應(yīng)的儀表盤模板后進(jìn)入下載。

2、配置數(shù)據(jù)源
本次使用的均為prometheus數(shù)據(jù)源,因此配置一個(gè)prometheus的數(shù)據(jù)源。
如果之前在grafana上沒有配置過數(shù)據(jù)源 登錄后會(huì)提示創(chuàng)建。

選擇prometheus。

配置prometheus地址。

最終save & Test即可。
3、導(dǎo)入儀表盤
將5.1中下載的模板導(dǎo)入。

導(dǎo)入:

修改名稱及數(shù)據(jù)源。

import即可。
4、配置完成后即可查看各監(jiān)控情況
現(xiàn)在可以看一下炫酷的結(jié)果了。
主機(jī)監(jiān)控如下:

MySQL:

Redis:

其他如果需要其他監(jiān)控項(xiàng)也可以自定義添加。
