步驟
- 一、概述
-
- 1.1 主要特性
- 1.2 原理架構圖
- 二、服務部署
-
- 2.1 準備工作
- 2.2 開啟Prometheus伺服器
- 2.3 設定被監控端
- 2.4 被監控端添加mysql服務
- 三、設定Grafana可視化圖形工具
-
- 3.1 服務安裝
- 3.2 浏覽器通路
- 3.3 Grafana 圖形顯示mysql監控資料
- 四、Grafana+onealert 報警
一、概述
1.1 主要特性
- 多元度資料模型
- 靈活的查詢語言
- 不依賴分布式存儲,單個伺服器節點是自主的
- 以HTTP方式,通過pull模型拉去時間序列資料
- 也可以通過中間網關支援push模型
- 通過服務發現或者靜态配置,來發現目标服務對象
- 支援多種多樣的圖表和界面展示
1.2 原理架構圖

二、服務部署
2.1 準備工作
Prometheus伺服器:192.168.253.77
安裝服務:Prometheus、grafana
被監控伺服器:192.168.253.66
安裝服務:node_exporter、mysqld_exporter
關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
2.2 開啟Prometheus伺服器
官網下載下傳壓縮包并上傳
cd /opt
tar zxvf prometheus-2.29.1.linux-amd64.tar.gz -C /usr/local
#檔案名較長,更改一下
mv /usr/local/prometheus-2.29.1.linux-amd64/ /usr/local/prometheus
開啟服務
#檔案目錄下有啟動檔案,直接背景運作即可
cd /usr/local/prometheus/
./prometheus &
netstat -anpt | grep 9090
通路網頁
浏覽器打開http://192.168.253.77:9090 通路主界面
預設監控本機,點選上方 Status 再選擇Targets 檢視
也可以追加字尾 /metrics 檢視監控的資料
http://192.168.253.77:9090/metrics
2.3 設定被監控端
安裝服務
官方下載下傳 node_exporter 元件
tar zxvf node_exporter-1.2.2.linux-amd64.tar.gz -C /usr/local/
#重命名減少字元
cd /usr/local/
mv node_exporter-1.2.2.linux-amd64/ node_exporter
開啟服務
cd node_exporter/
ls
LICENSE node_exporter NOTICE
#直接運作啟動指令檔案就可以 加&背景運作
./node_exporter &
#檢查是否開啟
lsof -i:9100
浏覽器通路 http://192.168.253.66:9100/metrics 被監控端的IP 可以看到被收集的監控資訊
Prometheus伺服器配置
#修改配置檔案添加被監控端資訊
vim /usr/local/prometheus/prometheus.yml
#末行添加以下行
- job_name: 'cc66_node' #自定義取一個名字表示監控的機子
static_configs:
- targets: ['192.168.253.66:9100'] #添加被監控機子的IP和端口
#重新開機服務
pkill prometheus
cd /usr/local/prometheus/
./prometheus &
netstat -anpt | grep 9090
傳回web重新整理,多了一台監控機子
2.4 被監控端添加mysql服務
安裝服務
官網下載下傳 mysqld_exporter 元件
#解壓
tar zxvf mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /usr/local/
#重命名
cd /usr/local/
mv mysqld_exporter-0.13.0.linux-amd64/ mysqld_exporter
cd mysqld_exporter/
ls
LICENSE mysqld_exporter NOTICE
安裝mariadb資料庫
#安裝
yum -y install mariadb\*
systemctl start mariadb
systemctl enable mariadb
#進入資料庫設定
mysql
#由于不是prometheus伺服器來直接找mariadb 擷取資料,而是prometheus伺服器 mysql_exporter,mysql_exporter 再找mariadb。是以253.66是指的mysql_exporter的IP
MariaDB [(none)]> grant select,replication client,process ON *.* to 'mysql_monitor'@'192.168.253.66' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
建立mariadb配置檔案
vim /usr/local/mysqld_exporter/.my.cnf
#内容需要和資料庫内設定的一緻
[client]
user=mysql_monitor #使用者名
password=123 #密碼
開啟服務
#這裡要用nuhup開啟
nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf &
lsof -i:9104
Prometheus伺服器設定
配置檔案添加mysql資訊
vim /usr/local/prometheus/prometheus.yml
#和前面node設定類似
- job_name: 'cc66_mariadb'
static_configs:
- targets: ['192.168.253.66:9104']
重新開機服務
pkill prometheus
./prometheus &
netstat -anpt | grep 9090
web頁面重新整理,可以看到已添加
三、設定Grafana可視化圖形工具
Grafana是一個開源的度量分析和可視化工具,可以通過将采集的資料分析,查詢,然後進行可視化的展示,并能實作報警
3.1 服務安裝
官網下載下傳安裝包
#上傳好直接rpm安裝
rpm -ivh grafana-5.3.4-1.x86_64.rpm
#開啟
systemctl start grafana-server.service
systemctl enable grafana-server.service
lsof -i:3000
3.2 浏覽器通路
http://192.168.253.77:3000
登入
- 使用初始賬戶密碼登入
監控系統—Prometheus部署一、概述二、服務部署三、設定Grafana可視化圖形工具四、Grafana+onealert 報警 - 提示修改,可以不變 把prometheus伺服器收集的資料做為一個資料源添加到grafana,讓grafana可以得 prometheus 的資料
監控系統—Prometheus部署一、概述二、服務部署三、設定Grafana可視化圖形工具四、Grafana+onealert 報警 - 添加資料源
監控系統—Prometheus部署一、概述二、服務部署三、設定Grafana可視化圖形工具四、Grafana+onealert 報警 - 設定資訊
監控系統—Prometheus部署一、概述二、服務部署三、設定Grafana可視化圖形工具四、Grafana+onealert 報警 監控系統—Prometheus部署一、概述二、服務部署三、設定Grafana可視化圖形工具四、Grafana+onealert 報警 監控系統—Prometheus部署一、概述二、服務部署三、設定Grafana可視化圖形工具四、Grafana+onealert 報警
圖形化顯示剛才添加的資料源
比對條件顯示資訊
3.3 Grafana 圖形顯示mysql監控資料
修改grafana配置檔案
vim /etc/grafana/grafana.ini
#末行添加
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
下載下傳安裝dashboard
cd /var/lib/grafana/
git clone git://github.com/percona/grafana-dashboards.git
正克隆到 'grafana-dashboards'...
remote: Enumerating objects: 15928, done.
remote: Counting objects: 100% (711/711), done.
remote: Compressing objects: 100% (476/476), done.
remote: Total 15928 (delta 394), reused 475 (delta 220), pack-reused 15217
接收對象中: 100% (15928/15928), 83.29 MiB | 17.96 MiB/s, done.
處理 delta 中: 100% (11313/11313), done.
#重新開機服務
systemctl restart grafana-server.service
四、Grafana+onealert 報警
onealert添加所需應用
grafana内配置
#步驟
1、在Grafana中建立Notification channel,選擇類型為Webhook;
2、推薦選中Send on all alerts和Include image,Cloud Alert體驗更佳;
3、将第一步中生成的Webhook URL填入Webhook settings Url;
URL格式:
http://api.aiops.com/alert/api/event/grafana/v1/72d99fa328f644218cb7ec898c001176/
4、Http Method選擇POST;
5、Send Test&Save;
grafana設定