Prometheus(由go語言(golang)開發(fā))是一套開源的監(jiān)控&報(bào)警&時間序列數(shù)據(jù)庫的組合。適合監(jiān)控docker容器。
基于時間序列的數(shù)值數(shù)據(jù)的容器監(jiān)控解決方案。
按照時間順序記錄系統(tǒng)、設(shè)備狀態(tài)變化的數(shù)據(jù)被稱為時序數(shù)據(jù)
優(yōu)勢
易于管理
輕易獲取服務(wù)內(nèi)部狀態(tài)
高效靈活的查詢語句
支持本地和遠(yuǎn)程存儲
采用http協(xié)議,默認(rèn)pull模式拉取數(shù)據(jù),也可以通過中間網(wǎng)關(guān)push數(shù)據(jù)
支持自動發(fā)現(xiàn)
可擴(kuò)展
易集成
grafana 將Prometheus收集到的數(shù)據(jù)以圖形化展現(xiàn)
通過grafana+onealert實(shí)現(xiàn)報(bào)警
模擬項(xiàng)目---對遠(yuǎn)程機(jī)器進(jìn)行監(jiān)控
實(shí)現(xiàn)7*24小時監(jiān)控
報(bào)警分級
一級 電話通知
二級 微信通知
三級 郵件通知
完成報(bào)警處理機(jī)制
異地集中監(jiān)控
實(shí)驗(yàn)環(huán)境準(zhǔn)備
grafana服務(wù)器一臺,prometheus服務(wù)器一臺,被監(jiān)控服務(wù)器一臺
初始化服務(wù)器
設(shè)置好IP后,修改/etc/hosts
172.16.30.217 prometheus
172.16.30.222 grafana
172.16.30.223 agent
1 #!/bin/bash
2 for li in `cat ip.txt`
3 do
4? ? echo "lianjie $li..."
5? ? sshpass -p saka ssh -o "StrictHostKeyChecking=no" root@$li mv /etc/hosts /etc/hosts.bk
6? ? scp /etc/hosts root@$li:/etc/
7 done
時間同步
由于連接了外網(wǎng),使用了外網(wǎng)的NTP server 在此只做時間檢查
#!/bin/bash
for li in `cat ip.txt`
do
sshpass -p saka ssh -o StrictHostKeyChecking=no root@$li date
done
prometheus安裝
tar -xvf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local
mv prometheus-2.5.0.linux-amd64 prometheus
cd prometheus
./prometheus --config.file="/usr/local/prometheus/prometheus.yml" & //執(zhí)行后臺安裝
ss -natlp | grep 9090 //測試端口占用情況
ip:9090 即可看到prometheus的自帶界面
默認(rèn)監(jiān)控自己
監(jiān)控項(xiàng)有搜索提示
監(jiān)控遠(yuǎn)程主機(jī)
在被監(jiān)控的主機(jī)上安裝組件(所有需要監(jiān)控的機(jī)器都需要安裝)
監(jiān)控不同的服務(wù)需要不同的組件,比如mysqld_exporter
安裝node_exporter(主要監(jiān)控系統(tǒng)信息)
tar -xvf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local
mv node_exporter-0.16.0.linux-amd64 node_exporter
cd node_exporter
nohup ./node_exporter & //永久運(yùn)行node_exporter
查看是否運(yùn)行
lsof -i:9100 //node_exporter 默認(rèn)監(jiān)聽9100端口
通過ip:9100/metrices 來查看監(jiān)控的數(shù)據(jù)
回到prometheus服務(wù)器的配置文件中添加被監(jiān)控機(jī)器的地址段
vim prometheus.yml
- job_name: 'agent1'
? static_configs:
? - targets: ['172.16.30.223:9100'] //嚴(yán)格控制縮進(jìn)
改完后重啟服務(wù)
回到prometheus服務(wù)器的監(jiān)控頁面,已經(jīng)可以看到添加的被監(jiān)控主機(jī)
監(jiān)控mysqld_exporter
解壓安裝
安裝mysql(start enable)
lsof -i:3306 //查看是否運(yùn)行
創(chuàng)建mysql賬號,只允許本機(jī)訪問
grant select,replication client,process ON *.* to 'mysql_monitor'@'localhost' identified by '123';
刷新權(quán)限
flush privileges;
完成之后,需要去添加配置文件來給prometheus登錄Mysql來拉取數(shù)據(jù)
vim /usr/local/mysqld_exporter/.my.cnf
[client]
user=mysql_monitor
password=123
運(yùn)行mysqld_exporter
nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf & //添加參數(shù),指明配置文件路徑
ss -natlp | grep 9104 //確認(rèn)已啟動
到網(wǎng)頁中查看是否已搜集到數(shù)據(jù)
回到prometheus服務(wù)器的配置文件中添加被監(jiān)控機(jī)器的地址段
? - job_name: 'agent1_mariadb'
static_configs:
- targets: ['172.16.30.223:9104']
修改完成后重啟prometheus服務(wù),在web上已經(jīng)能看到監(jiān)控的targets
grafana圖形工具
grafana是開源的度量分析和可視化工具,可以通過將prometheus采集的數(shù)據(jù)分析,查詢,然后進(jìn)行可視化的展示,并實(shí)現(xiàn)報(bào)警。監(jiān)聽3000端口
安裝
rpm -ivh grafana-5.3.4-1.x86_64.rpm
systemctl start grafana-server
systemctl enable grafana-server
通過Ip:3000進(jìn)入 grafana測試頁面,默認(rèn)用戶名密碼均為admin,第一次登陸后需要更改密碼
與prometheus關(guān)聯(lián)
添加數(shù)據(jù)源
點(diǎn)擊頁面中的add data source添加prometheus服務(wù)器的信息
點(diǎn)+給數(shù)據(jù)源做圖形展示
可以通過兩種方式來篩選監(jiān)控?cái)?shù)據(jù)
1.{instance="IP:PORT"}
2.{job="JOB NAME"} //job name在prometheus的配置文件中
通過grafana監(jiān)控mysql數(shù)據(jù)
需要安裝模板并修改配置文件
修改配置文件
vim /etc/grafana/grafana.ini
[dashboards.json]
enabled = true //開啟json
path = /var/lib/grafana/dashboards //模板路徑
cd /var/lib/grafana/dashboards后
git clone https://github.com/percona/grafana-dashboards.git //下載模板
cp -r grafana-dashboards/dashboards/ /var/lib/grafana? //把模板拉出來
systemctl restart grafana-server
在grafana中導(dǎo)入json 文件
頁面中存在上傳頁面
選擇上傳mysql_overview.json
//上傳后沒有數(shù)據(jù),因?yàn)闆]設(shè)置數(shù)據(jù)源
//設(shè)置數(shù)據(jù)源
//默認(rèn)名字為Prometheus,之前的名字叫prometheus_data
//一覺睡醒grafana沒采集到數(shù)據(jù),prometheus報(bào)錯no data points 發(fā)現(xiàn)3臺機(jī)器時間不同步,重新校時 ntpdate cn.ntp.org.cn 所有機(jī)器保證時間同步
更改dashboards界面 https://grafana.com/grafana/dashboards?
編號9276比較酷炫,是收集系統(tǒng)信息的,需要node_exporter
grafana+onealert實(shí)現(xiàn)報(bào)警
prometheus報(bào)警需要alertmanager組件,且報(bào)警規(guī)則需要手動添加,并不友好,所以使用grafana+onealert實(shí)現(xiàn)報(bào)警
onealert需要注冊
https://caweb.aiops.com/#/? //頁面有創(chuàng)建提示
在grafana中配置webhook url
在grafana中創(chuàng)建Notification channel 類型為webhook
鏈接地址不能填錯
將webhook Notification Channel添加到grafana Alert中
在grafana dashboards中添加報(bào)警觸發(fā)條件
cat /dev/urandom | md5sum
//目前未付費(fèi)狀態(tài) 只能微信推送或者釘釘 //綁定微信后已經(jīng)可以從郵箱和微信推送獲取報(bào)警信息
dpkg-reconfigure openssh-server重新生成ssh配置文件