Centos7 - Prometheus + Grafana 監控平台搭建
Prometheus 是一套開源的系統監控報警框架。Prometheus 所有采集的監控數據均以指標(metric)的形式保存在內置的時間序列數據庫當中(TSDB):屬於同一指標名稱,同一標簽集合的、有時間戳標記的數據流。除了存儲的時間序列,Prometheus 還可以根據查詢請求產生臨時的、衍生的時間序列作為返回結果
Exporter 是Prometheus的一類數據采集組件的總稱。它負責從目標處搜集數據,並將其轉化為Prometheus支持的格式。與傳統的數據采集組件不同的是,它並不向中央服務器發送數據,而是等待中央服務器主動前來抓取
Grafana 是一個跨平台的開源的度量分析和可視化工具,可以通過將采集的數據查詢然后可視化的展示,並及時通知
本次實驗環境: 操作系統 :CentOS Linux release 7.6.1810 MySQL :5.6.43
一.下載Prometheus
下載地址:https://prometheus.io/download/

選擇合適的版本,右鍵點擊鏈接並復制鏈接地址,到linux系統內 用wget 命令下載並解壓,這里我下的是2.8.1版本
wget https://github.com/prometheus/prometheus/releases/download/v2.8.1/prometheus-2.8.1.linux-amd64.tar.gz tar zxvf prometheus-2.8.1.linux-amd64.tar.gz mv prometheus-2.8.1.linux-amd64 /opt/prometheus
二.下載並運行 mysqld_exporter,node_exporter
exporter 是需要安裝在需要被監控的服務器上的,本次演示為了方便,我就把所有軟件都安裝在同一個服務器上了
1.下載解壓 這兩個node_exporter 需要運行在需要被監控的服務器上 在上面 Prometheus 的下載頁面,也提供了很多exporter的下載,其中就包括了mysqld_exporter和node_exporter

#下載解壓mysqld_exporter wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz tar zxvf mysqld_exporter-0.11.0.linux-amd64.tar.gz mv mysqld_exporter-0.11.0.linux-amd64 /opt/mysqld_exporter #下載解壓node_exporter wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz tar zxvf node_exporter-0.17.0.linux-amd64.tar.gz mv node_exporter-0.17.0.linux-amd64 /opt/node_exporter
2.運行
進入mysqld_exporter安裝目錄並運行node_exporter
cd /opt/node_exporter
nohup ./node_exporter &
運行mysqld_exporter需要連接到MySQL,需要授權,在本案例中,被授權的賬號為mysql_monitor,密碼為123123
#先用root 賬號登錄mysql mysql -u root -p #輸入密碼登錄成功執行授權sql語句 grant replication client, process on *.* to mysql_monitor@"localhost" identified by "123123"; grant select on performance_schema.* to mysql_monitor@"localhost";
授權后進入mysqld_exporter安裝目錄創建.my.cnf配置文件,並運行mysqld_exporter
cd /opt/mysqld_exporter
vim .my.cnf
.my.cnf文件中寫入以下內容
[client] user=mysql_monitor password=123123
保存后,運行 mysqld_exporter
nohup ./mysqld_exporter --config.my-cnf=.my.cnf &
mysqld_exporter占用9104端口, node_exporter 占用9100端口
三. 配置prometheus 並運行
進入prometheus安裝路徑並修改配置文件
cd /opt/prometheus
vim prometheus.yml
配置文件修改后內容如下
# my global config global: scrape_interval: 15s # 設置刮擦間隔為每15秒。默認值是每1分鍾。 evaluation_interval: 15s # 每15秒評估一次規則。默認值是每1分鍾。 # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 #加載規則一次,然后根據全局'evaluation_interval'定期對它們求值 rule_files: # - "first_rules.yml" # - "second_rules.yml" # 一種刮板配置,包含一個要刮的端點: # Here it's Prometheus itself. scrape_configs: #將作業名稱作為標簽' job=<job_name> '添加到從該配置中提取的任何timeseries中。- job_name: 'prometheus' #metrics_path默認為'/metrics' # scheme默認為“http” static_configs: - targets: ['localhost:9090'] - job_name: 'mysql' static_configs: - targets: ['localhost:9104'] labels: instance: 'db1' - job_name: 'node' static_configs: - targets: ['localhost:9100'] labels: instance: 'nd1'
每個job_name標簽標示一個監控的job targets 標簽標示受監控的應用的 ip和端口號
注意:這個配置文件要特別注意格式縮進,嚴格按照他原來的格式來修改,不然會導致prometheus運行不了。
運行prometheus
nohup ./prometheus --config.file=./prometheus.yml &
運行后可以通過 cat nohup.out 查看運行日志
瀏覽器訪問 服務器的9090端口可以訪問prometheus的頁面

然后我們進入status 目錄下的Targets頁面

我們可以看到,我們在配置文件配置的三個監控的job狀態都是up的

如果狀態不是up,則證明該job的配置有問題或着監控的應用沒有運行起來,可以返回去檢查一下。
prometheus 對於數據的展現並不直觀和美觀,所以,我們需要grafana
四.下載安裝並運行Grafana
下載地址:https://grafana.com/grafana/download

可以按照官網的指導下載安裝合適的版本,這里我下載的是6.1.3版本
wget https://dl.grafana.com/oss/release/grafana-6.1.3-1.x86_64.rpm sudo yum localinstall grafana-6.1.3-1.x86_64.rpm
然后運行grafana
systemctl start grafana-server
運行后我們從瀏覽器訪問服務的3000端口,可以訪問grafana的頁面

初始賬號和密碼 都是 admin 登錄成功並修改了密碼之后,我們添加一個data source ,並且在選擇data source type 時選擇 Prometheus

在配置好data source 后點擊 save&test 按鈕,如果提示data source is working 則為成功

配置好 data sources 后,我們需要去下載dashboard 的json文件並導入, 當然,你也可以自己去創建dashboard.
本次我們下載 “mysql overview” 和 “1 Node Exporter 0.16 0.17 for Prometheus 監控展示看板” ,下面演示 “mysql overview” dashboard 的json文件下載過程
dashboard 的json文件下載地址:https://grafana.com/dashboards

你可以搜索你需要的json文件

我選擇了一個下載次數最多的,選擇后,我們可以預覽這個dashbord 展示的內容,點擊 dowload json 下載 json 文件,這里還需要注意一下 Dependencies里的版本,因為有些版本不支持的問題可能會導致導入的dashboard 不顯示圖標或者圖表都是空的。如果下載的dashboard用不了,可以換一個試試。

“1 Node Exporter 0.16 0.17 for Prometheus 監控展示看板” 的下載方法和上面差不多。
下載了 “”mysql overview“” 和 “1 Node Exporter 0.16 0.17 for Prometheus 監控展示看板” 的json文件之后,我們需要導入到grafana, 我演示下 ‘1 Node Exporter 0.16 0.17 for Prometheus 監控展示看板’ dashboard的json文件導入。

點擊 upload json file,並選擇下載好的json文件

修改好 name 和 prometheus node 后 點擊import

然后就備件款的node的信息就很直觀且美觀的展現出來了。但是這個dashboard的磁盤總空間 那一塊 有警告 說找不到 grafana-piechart-panel 插件,接下來我們就進行插件的安裝

grafana-piechart-panel插件 是一個餅狀圖插件,grafana的插件安裝很簡單
在grafana 安裝的服務器環境 執行以下命令進行插件安裝
grafana-cli plugins install grafana-piechart-panel
#插件安裝后重啟grafana
systemctl restart grafana-server
然后我們刷新下 grafana的dashboar頁面 就可以看到餅狀圖顯示出來了

報警需要郵件功能,郵件功能配置在 /etc/grafana/grafana.ini 文件
修改grafana的配置文件
grafana的配置文件默認是在 /etc/grafana/grafana.ini
#################################### SMTP / Emailing ########################## [smtp] enabled = true #是否允許開啟 host = smtp.exmail.qq.com:465 #發送服務器地址,可以再郵箱的配置教程中找到: user = 你的郵箱 # If the password contains # or ; you have to wrap it with trippel quotes. Ex """#password;""" # 這個密碼是你開啟smtp服務生成的密碼 password = 你的密碼 ;cert_file = ;key_file = ;skip_verify = false from_address = 你的郵箱 from_name = Grafana # EHLO identity in SMTP dialog (defaults to instance_name) ehlo_identity = dashboard.example.com [emails] ;welcome_email_on_sign_up = true #################################### Logging ##########################
重新啟動grafana服務,讓配置文件生效
service grafana-server restart