Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
短信告警這個(gè)模塊在很多項(xiàng)目中都用上了,比如之前做過的安防系統(tǒng),溫濕度報(bào)警系統(tǒng)等,主要的流程就是收到數(shù)據(jù)判斷屬于某種報(bào)警后,組織短信字符串內(nèi)容,發(fā)送到指定的多個(gè)手機(jī)號(hào)碼上面,使用的是短信貓硬件設(shè)備,其實(shí)就是個(gè)短信模塊,沒有使用網(wǎng)絡(luò)的發(fā)送短信的api,畢竟大部分的軟件應(yīng)用場景都不能要求連通外網(wǎng),安全考慮,所以必須采用本地的硬件來實(shí)現(xiàn)發(fā)送短信。這個(gè)短信貓?jiān)O(shè)備在8年前開始用過,當(dāng)時(shí)用的C#去寫了個(gè)純協(xié)議解析短信的收發(fā),后面用Qt重新寫了個(gè)。特意寫了個(gè)類SendMsgThread封裝了所有的操作,這樣就通用任何開發(fā)環(huán)境了,掌握了原理,從底層原理上著手,好處還是很多的,我看很多初學(xué)者熱衷于去調(diào)用第三方廠家封裝好的SDK,一旦換了操作系統(tǒng),完蛋了歇菜,沒有嵌入式linux系統(tǒng)的SDK開發(fā)包咋搞。
通用短信收到組件功能特點(diǎn):
1. 可設(shè)置收發(fā)短信模式
2. 可批量發(fā)送短信以及支持長短信
3. 可讀取指定指定序號(hào)短信
4. 可刪除所有短信
5. 可檢測短信貓?jiān)O(shè)備是否正常
6. 支持中文短信發(fā)送
7. 支持撥打電話+掛斷電話+接聽來電+識(shí)別用戶按鍵反饋
8. 支持批量發(fā)送給多個(gè)號(hào)碼
皮膚開源:[https://gitee.com/feiyangqingyun/QWidgetDemo](https://gitee.com/feiyangqingyun/QWidgetDemo) [https://github.com/feiyangqingyun/QWidgetDemo](https://github.com/feiyangqingyun/QWidgetDemo)
文件名稱:styledemo
體驗(yàn)地址:[https://gitee.com/feiyangqingyun/QWidgetExe](https://gitee.com/feiyangqingyun/QWidgetExe) [https://github.com/feiyangqingyun/QWidgetExe](https://github.com/feiyangqingyun/QWidgetExe)
文件名稱:bin_sams.zip
1. 采集數(shù)據(jù)端口,支持串口端口+網(wǎng)絡(luò)端口,串口支持自由設(shè)置串口號(hào)+波特率,網(wǎng)絡(luò)支持自由設(shè)置IP地址+通訊端口,每個(gè)端口支持采集周期,默認(rèn)1秒鐘一個(gè)地址,支持設(shè)置通訊超時(shí)次數(shù),默認(rèn)3次,支持最大重連時(shí)間,用于重新讀取離線的設(shè)備。
2. 控制器信息,能夠添加控制器名稱,選擇控制器地址+控制器型號(hào),設(shè)置該控制器下面的探測器數(shù)量。
3. 探測器信息,能夠添加位號(hào),可自由選擇探測器型號(hào),氣體種類,氣體符號(hào),高報(bào)值,低報(bào)值,緩沖值,清零值,是否啟用,報(bào)警聲音,背景地圖,存儲(chǔ)周期,數(shù)值換算小數(shù)點(diǎn)位數(shù),報(bào)警延時(shí)時(shí)間,報(bào)警的類型(HH,LL,HL)等。
4. 控制器型號(hào)+探測器型號(hào)+氣體種類+氣體符號(hào),均可自由配置。
5. 地圖支持導(dǎo)入和刪除,所有的探測器對(duì)應(yīng)地圖位置可自由拖動(dòng)保存。
6. 端口信息+控制器信息+探測器信息,支持導(dǎo)入導(dǎo)出+導(dǎo)出到excel+打印。
7. 運(yùn)行記錄+報(bào)警記錄+用戶記錄,支持多條件組合查詢,比如時(shí)間段+控制器+探測器等,所有記錄支持導(dǎo)出到excel+打印。
8. 導(dǎo)出到excel的記錄支持所有excel+wps等表格文件版本,不依賴excel等軟件。
9. 可刪除指定時(shí)間范圍內(nèi)的數(shù)據(jù),支持自動(dòng)清理早期數(shù)據(jù),設(shè)置最大保存記錄數(shù)。
10. 支持報(bào)警短信轉(zhuǎn)發(fā),支持多個(gè)接收手機(jī)號(hào)碼,可設(shè)定發(fā)送間隔,比如即時(shí)發(fā)送或者6個(gè)小時(shí)發(fā)送一次所有的報(bào)警信息,短信內(nèi)容過長,自動(dòng)拆分多條短信。
11. 支持報(bào)警郵件轉(zhuǎn)發(fā),支持多個(gè)接收郵箱,可設(shè)定發(fā)送間隔,比如即時(shí)發(fā)送或者6個(gè)小時(shí)發(fā)送一次所有的報(bào)警信息,支持附件發(fā)送。
12. 高報(bào)顏色+低報(bào)顏色+正常顏色+0值顏色+曲線背景+曲線顏色等,都可以自由選擇。
13. 軟件的中文標(biāo)題+英文標(biāo)題+logo路徑+版權(quán)所有都可以自由設(shè)置。
14. 提供開關(guān)設(shè)置開機(jī)運(yùn)行+報(bào)警聲音+自動(dòng)登錄+記住密碼等。
15. 報(bào)警聲音可設(shè)置播放次數(shù),界面提供17種皮膚文件選擇。
16. 支持云端數(shù)據(jù)同步,可設(shè)置云端數(shù)據(jù)庫的信息,比如數(shù)據(jù)庫名稱,用戶名+密碼等。
17. 支持網(wǎng)絡(luò)轉(zhuǎn)發(fā)和網(wǎng)絡(luò)接收,網(wǎng)絡(luò)接收開啟后,軟件從udp接收數(shù)據(jù)進(jìn)行解析。網(wǎng)絡(luò)轉(zhuǎn)發(fā)支持多個(gè)目標(biāo)IP,這樣就實(shí)現(xiàn)了本地采集的軟件,自由將數(shù)據(jù)轉(zhuǎn)到客戶端,隨時(shí)查看探測器數(shù)據(jù)。
18. 自動(dòng)記住用戶最后停留的界面+其他信息,重啟后自動(dòng)應(yīng)用。
19. 報(bào)警自動(dòng)切換到對(duì)應(yīng)的地圖,探測器按鈕閃爍。
20. 雙擊探測器圖標(biāo),可以進(jìn)行回控。
21. 支持用戶權(quán)限管理,管理員+操作員兩大類,用戶登錄+用戶退出,可以記住密碼和自動(dòng)登錄,超過三次報(bào)錯(cuò)提示并關(guān)閉程序。
22. 支持四種監(jiān)控模式,設(shè)備面板監(jiān)控+地圖監(jiān)控+表格數(shù)據(jù)監(jiān)控+曲線數(shù)據(jù)監(jiān)控,可自由切換,四種同步應(yīng)用。
23. 支持報(bào)警繼電器聯(lián)動(dòng),一個(gè)位號(hào)可以跨串口聯(lián)動(dòng)多個(gè)模塊和繼電器號(hào),支持多對(duì)多。
24. 本地?cái)?shù)據(jù)存儲(chǔ)支持sqlite+mysql,支持遠(yuǎn)程數(shù)據(jù)同步到云端數(shù)據(jù)庫。自動(dòng)重連。
25. 本地設(shè)備采集到的數(shù)據(jù)實(shí)時(shí)上傳到云端,以便手機(jī)APP或者web等其他方式提取。
26. 支持兩種數(shù)據(jù)源,一種是串口和網(wǎng)絡(luò)通過協(xié)議采集設(shè)備數(shù)據(jù),一種是數(shù)據(jù)庫采集。數(shù)據(jù)庫采集模式可以作為通用的系統(tǒng)使用。
27. 自帶設(shè)備模擬工具,支持16個(gè)設(shè)備數(shù)據(jù)模擬,同時(shí)還帶數(shù)據(jù)庫數(shù)據(jù)模擬,以便在沒有設(shè)備的時(shí)候測試數(shù)據(jù)。
28. 默認(rèn)通信協(xié)議采用modbus協(xié)議,后期增加mqtt等物聯(lián)網(wǎng)協(xié)議的支持,做成通用系統(tǒng)。
29. 支持所有windows操作系統(tǒng)+linux操作系統(tǒng)和其他操作系統(tǒng)。
動(dòng)互聯(lián)小課堂 (六) 報(bào)警服務(wù)
視頻鏈接如下:
https://audi-embedded-wap.saic-audi.mobi/share/index.html#/article?flag=0&category=article&id=1232417733879517184
字如面,大家好,我是小斐,上一篇介紹generator.yml文件配置和prometheus.yml文件配置,包含文件服務(wù)自動(dòng)發(fā)現(xiàn)機(jī)制,以及根據(jù)generator.yml配置,根據(jù)SNMP Exporter官方提供的生成器,生成snmp.yml配置文件等等,今天主要說明下正式環(huán)境的配置和情況,包括披露部署虛擬機(jī)Node Exporter節(jié)點(diǎn)數(shù)據(jù)采集器等問題。
關(guān)于存儲(chǔ)
Prometheus 提供了本地存儲(chǔ)(TSDB)時(shí)序型數(shù)據(jù)庫的存儲(chǔ)方式,在2.0版本之后,壓縮數(shù)據(jù)的能力得到了大大的提升(每個(gè)采樣數(shù)據(jù)僅僅占用1~2Byte左右空間),單節(jié)點(diǎn)情況下可以滿足大部分用戶的需求,但本地存儲(chǔ)阻礙了Prometheus集群化的實(shí)現(xiàn),因此在集群中應(yīng)當(dāng)采其他時(shí)序性數(shù)據(jù)庫來替代,比如influxdb。
Prometheus分為三個(gè)部分,分別是:抓取數(shù)據(jù)、存儲(chǔ)數(shù)據(jù)和查詢數(shù)據(jù)。
抓取數(shù)據(jù),就是各種抓取器,存儲(chǔ)數(shù)據(jù)就是時(shí)序數(shù)據(jù)庫,查詢數(shù)據(jù),可以理解為數(shù)據(jù)可視化。
關(guān)于我這里的正式環(huán)境,因指標(biāo)不是很多,故不采用外部數(shù)據(jù)庫,主要還是基于本地存儲(chǔ),本地存儲(chǔ)Prometheus 按2小時(shí)一個(gè)block進(jìn)行存儲(chǔ),每個(gè)block由一個(gè)目錄組成,該目錄里包含:一個(gè)或者多個(gè)chunk文件(保存時(shí)間序列數(shù)據(jù))默認(rèn)每個(gè)chunk大小為512M、一個(gè)metadata文件、一個(gè)index文件(通過metric name和labels查找時(shí)間序列數(shù)據(jù)在chunk 塊文件的位置。如下圖所示:
./data
├── 01BKGV7JBM69T2G1BGBGM6KB12
│ └── meta.json
├── 01BKGTZQ1SYQJTR4PB43C8PD98
│ ├── chunks
│ │ └── 000001
│ ├── tombstones
│ ├── index
│ └── meta.json
├── 01BKGTZQ1HHWHV8FBJXW1Y3W0K
│ └── meta.json
├── 01BKGV7JC0RY8A6MACW02A2PJD
│ ├── chunks
│ │ └── 000001
│ ├── tombstones
│ ├── index
│ └── meta.json
├── chunks_head
│ └── 000001
└── wal
├── 000000002
└── checkpoint.00000001
└── 00000000
needed_disk_space=retention_time_seconds * ingested_samples_per_second * bytes_per_sample
磁盤大小計(jì)算方式:磁盤大小=保留時(shí)間 * 每秒獲取樣本數(shù) * 樣本大小
保留時(shí)間(retention_time_seconds)和樣本大小(bytes_per_sample)不變的情況下,如果想減少本地磁盤的容量需求,只能通過減少每秒獲取樣本數(shù)(ingested_samples_per_second)的方式。
因此有兩種手段,一是減少時(shí)間序列的數(shù)量,二是增加采集樣本的時(shí)間間隔。
考慮到Prometheus會(huì)對(duì)時(shí)間序列進(jìn)行壓縮,因此減少時(shí)間序列的數(shù)量效果更明顯。
為什么默認(rèn)兩個(gè)小時(shí)存儲(chǔ)一次數(shù)據(jù),寫成一個(gè)chuck塊文件
通過時(shí)間窗口的形式保存所有的樣本數(shù)據(jù),可以明顯提高Prometheus的查詢效率,當(dāng)查詢一段時(shí)間范圍內(nèi)的所有樣本數(shù)據(jù)時(shí),只需要簡單的從落在該范圍內(nèi)的塊中查詢數(shù)據(jù)即可。
最新寫入的2 小時(shí)數(shù)據(jù)保存在內(nèi)存中
最新寫入的數(shù)據(jù)保存在內(nèi)存block中,達(dá)到2小時(shí)后寫入磁盤,計(jì)算2小時(shí)寫入數(shù)據(jù)大小,決定出內(nèi)存的大小。
為了防止程序崩潰導(dǎo)致數(shù)據(jù)丟失,采用WAL(write-ahead-log)預(yù)寫日志機(jī)制,啟動(dòng)時(shí)會(huì)以寫入日志(WAL)的方式來實(shí)現(xiàn)重播,從而恢復(fù)數(shù)據(jù)。
未落盤時(shí)數(shù)據(jù)存儲(chǔ)內(nèi)容。有wal文件
./data/01BKGV7JBM69T2G1BGBGM6KB12
./data/01BKGV7JBM69T2G1BGBGM6KB12/meta.json
./data/01BKGV7JBM69T2G1BGBGM6KB12/wal/000002
./data/01BKGV7JBM69T2G1BGBGM6KB12/wal/000001
數(shù)據(jù)存儲(chǔ)方式
落盤后的數(shù)據(jù)內(nèi)容,wal文件刪除,生成index, tombstones(刪除數(shù)據(jù)的記錄),數(shù)據(jù)文件00001
./data/01BKGV7JC0RY8A6MACW02A2PJD
./data/01BKGV7JC0RY8A6MACW02A2PJD/meta.json
./data/01BKGV7JC0RY8A6MACW02A2PJD/index
./data/01BKGV7JC0RY8A6MACW02A2PJD/chunks
./data/01BKGV7JC0RY8A6MACW02A2PJD/chunks/000001
./data/01BKGV7JC0RY8A6MACW02A2PJD/tombstones
這些2小時(shí)的block會(huì)在后臺(tái)壓縮成更大的block,數(shù)據(jù)壓縮合并成更高level的block文件后刪除低level的block文件。這個(gè)和leveldb、rocksdb等LSM樹的思路一致。
數(shù)據(jù)過期清理時(shí)間,默認(rèn)保存15天
存儲(chǔ)數(shù)據(jù)的目錄,默認(rèn)為data/,如果要掛外部存儲(chǔ),可以指定該目錄
刪除數(shù)據(jù)方式
刪除數(shù)據(jù)時(shí),刪除條目會(huì)記錄在獨(dú)立的tombstone 刪除記錄文件中,而不是立即從chunk文件刪除。
首先是數(shù)據(jù)持久化的問題,默認(rèn)保存15天,原生的TSDB對(duì)于大數(shù)據(jù)量的保存及查詢支持不太友好 ,所以并不適用于保存長期的大量數(shù)據(jù);另外,該數(shù)據(jù)庫的可靠性也較弱,在使用過程中容易出現(xiàn)數(shù)據(jù)損壞等故障,且無法支持集群的架構(gòu)。
關(guān)于遠(yuǎn)端存儲(chǔ),在此就不表,因我的正式環(huán)境,不準(zhǔn)備采用遠(yuǎn)端存儲(chǔ)存儲(chǔ)數(shù)據(jù),就采用本地存儲(chǔ)就滿足我的需求。
IP | 規(guī)格 | 服務(wù) | 說明 |
172.17.40.51 | 8C 16G 500GB | 單節(jié)點(diǎn) Prometheus Server + Grafana Server + Alertmanager | 該服務(wù)器主要搭建Prometheus服務(wù)器、Grafana服務(wù)器、Alertmanager服務(wù)器 |
172.17.40.54 | 4C 8G 60G | SNMP Exporter | 交換機(jī)、防火墻、負(fù)載均衡、AC、DELL iDrac、IPMI等設(shè)備啟用SNMP協(xié)議采集數(shù)據(jù),該服務(wù)器作為SNMP協(xié)議數(shù)據(jù)采集節(jié)點(diǎn) |
其他服務(wù)器 共500臺(tái) | 各種規(guī)格 | Node Exporter | 其他服務(wù)器部署Node Exporter采集主機(jī)性能、狀態(tài)、網(wǎng)絡(luò)、空間容量等各類數(shù)據(jù) |
關(guān)于正式環(huán)境搭建可參考前面文章
Prometheus + Grafana搭建IT監(jiān)控報(bào)警最佳實(shí)踐(1)
在此補(bǔ)充一下細(xì)節(jié)說明和完善的點(diǎn):
這里部署Prometheus Server、Grafana Server、Alertmanager都是未采用docker安裝部署,而是采用編譯好的二進(jìn)制文件部署,簡單說明:
# 正式環(huán)境
# 舊版本的 Docker 被稱為docker或docker-engine,如果安裝了這些,請(qǐng)卸載它們以及相關(guān)的依賴項(xiàng)。
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 使用存儲(chǔ)庫安裝docker
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 安裝最新版本的 Docker Engine、containerd 和 Docker Compose
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 安裝特定版本的docker,請(qǐng)查看官網(wǎng)
https://docs.docker.com/engine/install/centos/
# 驗(yàn)證安裝 docker version
[root@it-prometheus ~]# docker version
Client: Docker Engine - Community
Version: 20.10.20
API version: 1.41
Go version: go1.18.7
Git commit: 9fdeb9c
Built: Tue Oct 18 18:22:47 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
# 此時(shí)安裝了docker,但并沒有啟動(dòng)docker
sudo systemctl start docker
[root@it-prometheus ~]# systemctl start docker
[root@it-prometheus ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2022-10-26 00:15:11 CST; 9s ago
Docs: https://docs.docker.com
Main PID: 19146 (dockerd)
Tasks: 13
Memory: 36.2M
CGroup: /system.slice/docker.service
└─19146 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
# 設(shè)置docker開機(jī)自啟動(dòng)sudo systemctl enable docker
[root@it-prometheus ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
# 部署Grafana
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.2.2-1.x86_64.rpm
sudo yum install grafana-enterprise-9.2.2-1.x86_64.rpm
# 啟動(dòng)
systemctl enable grafana-server
systemctl start grafana-server
systemctl status grafana-server
netstat -anplut | grep grafana
# 訪問Grafana
瀏覽器本機(jī)IP訪問http://172.17.40.51:3000/login,用戶名和密碼均為admin/admin
# 部署Prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.34.0/prometheus-2.34.0.linux-amd64.tar.gz
tar -zxvf prometheus-2.39.1.linux-amd64.tar.gz
# 運(yùn)行
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Wants=network-online.target
After=network.target
[Service]
Type=simple
User=root
ExecStart=/root/monitor/prometheus/current/prometheus --config.file=/root/monitor/prometheus/conf/prometheus.yml --web.listen-address=:9090 --storage.tsdb.path=/root/monitor/prometheus/data/ --storage.tsdb.retention=90d --web.enable-lifecycle --web.enable-admin-api
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start prometheus
systemctl status prometheus
systemctl enable prometheus
# 訪問Prometheus
http://172.17.40.51:9090/
# 部署Alertmanager
wget https://github.com/prometheus/alertmanager/releases/download/v0.24.0/alertmanager-0.24.0.linux-amd64.tar.gz
tar -zxvf alertmanager-0.24.0.linux-amd64.tar.gz
# 運(yùn)行
vim /usr/lib/systemd/system/alertmanager.service
[Unit]
Description=alertmanager
[Service]
ExecStart=/root/monitor/alertmanager/alertmanager --config.file=/root/monitor/alertmanager/conf/alertmanager.yml
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start alertmanager.service
systemctl status alertmanager.service
systemctl enable alertmanager.service
# 啟動(dòng)
# 檢查配置文件格式是否錯(cuò)誤
./amtool check-config /root/monitor/alertmanager/conf/alertmanager.yml
# 訪問
http://172.17.40.51:9093/
# 安裝prometheus-webhook-dingtalk
wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v2.1.0/prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
tar -zxvf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
# 運(yùn)行
vim /usr/lib/systemd/system/prometheus-webhook.service
[Unit]
Description=Prometheus Dingding Webhook
[Service]
ExecStart=/root/monitor/prometheus-webhook-dingtalk/current/prometheus-webhook-dingtalk --config.file=/root/monitor/prometheus-webhook-dingtalk/conf/config.yml -web.enable-ui --web.enable-lifecycle
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start prometheus-webhook.service
systemctl status prometheus-webhook.service
systemctl enable prometheus-webhook.service
# 訪問
http://172.17.40.51:8060/
到此就完成搭建Prometheus Server、Grafana Server、Alertmanager和釘釘告警插件。
在此搭建Node Exporter
curl -LO https://github.com/prometheus/node_exporter/releases/download/v1.4.0/node_exporter-1.4.0.linux-amd64.tar.gz
tar -zxvf node_exporter-1.4.0.linux-amd64.tar.gz
vim /usr/lib/systemd/system/node_exporter.service
[unit]
Description=The node_exporter Server
Wants=network-online.target
After=network.target
[Service]
ExecStart=/opt/node_exporter/node_exporter
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=15s
SyslogIdentifier=node_exporter
[Install]
WantedBy=multi-user.target
# 啟動(dòng)
systemctl daemon-reload
systemctl start node_exporter
systemctl status node_exporter
systemctl enable node_exporter
1、主機(jī)監(jiān)控:使用文件服務(wù)發(fā)現(xiàn)機(jī)制,不過我在此沒有使用服務(wù)注冊(cè)發(fā)現(xiàn)機(jī)制consul
# 添加prometheus.yml配置文件
# 采集內(nèi)部VMWare集群中的主機(jī)信息
- job_name: "vmware-host"
metrics_path: /metrics
scheme: http
scrape_interval: 5s
file_sd_configs:
- files:
- /data/monitor/prometheus/targets/node-*.yml
refresh_interval: 2m
# 新建node-it.yml文件
- labels:
service: it-monitor
brand: dell
targets:
- 172.17.40.51:9100
- 172.17.40.54:9100
動(dòng)態(tài)加載配置文件:
curl -X POST localhost:9090/-/reload
2、交換機(jī)監(jiān)控:使用文件服務(wù)發(fā)現(xiàn)機(jī)制
# 添加交換機(jī)類job
- job_name: "SNMP"
file_sd_configs:
- files:
- /data/monitor/prometheus/targets/network-*.yml
refresh_interval: 2m
# scrape_interval: 5s # 針對(duì)SNMP采集節(jié)點(diǎn) 覆蓋全局配置15s
metrics_path: /snmp
params:
module:
- if_mib
# community: # 當(dāng) snmp_exporter snmp.yml 配置文件沒有指定 community,此處定義的 community 生效。 缺省值一般是 public
# - xxxx
relabel_configs:
- source_labels: ["__address__"]
target_label: __param_target
- source_labels: ["__param_target"]
target_label: instance
# prometheus采集目標(biāo)直接修改為snmp_exporter 服務(wù)IP地址
- target_label: __address__
replacement: 172.17.40.54:9116 # snmp_exporter 服務(wù)IP地址
- source_labels: ["mib"] # 從自定義的目標(biāo)標(biāo)簽獲取MIB模塊名稱
target_label: __param_module
# 新建network-switch.yml文件 如何有多臺(tái)就直接在文件后添加多臺(tái)交換機(jī)target即可
- labels:
mib: HUAWEI
brand: Huawei
hostname: HZZB-B2L-AG-Master
model: S5720-36C-EI-AC
targets:
- 172.18.48.2
- labels:
mib: HUAWEI
brand: Huawei
hostname: HZZB-B2L-Access-Master
model: S5720S-52P-LI-AC
targets:
- 172.18.48.5
- labels:
mib: HUAWEI
brand: Huawei
hostname: HZZB-B2L-POE-Master
model: S5720S-28P-PWR-LI-AC
targets:
- 172.18.48.6
- labels:
mib: HUAWEI
brand: Huawei
hostname: HZZB-BLJC-POE-Master
model: S5720S-28P-PWR-LI-AC
targets:
- 172.17.14.13
- labels:
mib: HUAWEI
brand: Huawei
hostname: HZZB-BLJC-Access-Master
model: S5720S-52P-LI-AC
targets:
- 172.17.14.14
監(jiān)控目標(biāo)已添加完成,后續(xù)將講解批量部署采集器和添加目標(biāo)。
前提:部署alertmanager,部署prometheus-webhook
思路:
# alertmanager.yml 配置模版
global: # 全局配置
resolve_timeout: 5m # 處理告警超時(shí)時(shí)間 未接收到告警后標(biāo)記告警狀態(tài)為resolved
# 郵箱配置
smtp_smarthost: 'localhost:25' # SMTP郵箱地址
smtp_from: 'alertmanager@example.org' # 郵件發(fā)件人
smtp_auth_username: 'alertmanager' # 發(fā)件人用戶
smtp_auth_password: 'password' # 發(fā)件人密碼
smtp_hello: '@example.org' # 標(biāo)記身份
smtp_require_tls: false # TLS關(guān)閉
templates:
- '/root/monitor/prometheus-webhook-dingtalk/template/*.tmpl' # 告警模板路徑
# 每個(gè)告警事件如何發(fā)送出去
route:
group_by: ['alertname'] # 采用哪個(gè)標(biāo)簽作為分組的依據(jù)
group_wait: 30s # 分組內(nèi)第一個(gè)告警等待時(shí)間,10s內(nèi)如有第二個(gè)告警會(huì)合并一個(gè)告警
group_interval: 5m # 發(fā)送新告警間隔時(shí)間 上下兩組發(fā)送告警的間隔時(shí)間
repeat_interval: 30m # 重復(fù)告警間隔發(fā)送時(shí)間,如果沒處理過多久再次發(fā)送一次
receiver: 'dingtalk_webhook' # 接收人 定義誰來通知報(bào)警
receivers:
- name: 'ops'
email_configs:
- to: 'yanghua@souche.com'
html: '{{ template "email.to.html" .}}'
headers: { Subject: "[WARNING]Prometheus告警郵件" }
send_resolved: true
- name: 'dingtalk_webhook'
webhook_configs:
- url: 'http://172.17.40.51:8060/dingtalk/webhook1/send' # 填寫prometheus-webhook的webhook1 url
send_resolved: true # 在恢復(fù)后是否發(fā)送恢復(fù)消息給接收人
官方示例文件:
alertmanager/simple.yml at main · prometheus/alertmanager · GitHubgithub.com/prometheus/alertmanager/blob/main/doc/examples/simple.yml
prometheus配置alert規(guī)則
# 告警插件配置
alerting:
alertmanagers:
- static_configs:
- targets:
- 172.17.40.51:9093
# 按照設(shè)定參數(shù)進(jìn)行掃描加載,用于自定義報(bào)警規(guī)則,其報(bào)警媒介和route路由由alertmanager插件實(shí)現(xiàn)
rule_files:
- "rules/*.yml"
# - "second_rules.yml"
編寫規(guī)則文件
# 這里在網(wǎng)上找了兩個(gè)規(guī)則配置文件
groups:
- name: servers_status # 這里名字是填寫在group_by
rules:
- alert: CPU負(fù)載1分鐘告警
expr: node_load1{job!~"(prometheus|vmware-host)"} / count (count (node_cpu_seconds_total{job!~"(prometheus|vmware-host)"}) without (mode)) by (instance, job) > 2.5
for: 1m
labels:
level: warning
annotations:
summary: "{{ $labels.instance }} CPU負(fù)載告警 "
description: "{{$labels.instance}} 1分鐘CPU負(fù)載(當(dāng)前值: {{ $value }})"
- alert: CPU使用率告警
expr: 1 - avg(irate(node_cpu_seconds_total{mode="idle",job!~"(prometheus|vmware-host)"}[30m])) by (instance) > 0.85
for: 1m
labels:
level: warning
annotations:
summary: "{{ $labels.instance }} CPU使用率告警 "
description: "{{$labels.instance}} CPU使用率超過85%(當(dāng)前值: {{ $value }} )"
- alert: CPU使用率告警
expr: 1 - avg(irate(node_cpu_seconds_total{mode="idle",job=~"(prometheus|vmware-host)"}[30m])) by (instance) > 0.9
for: 1m
labels:
level: warning
annotations:
summary: "{{ $labels.instance }} CPU負(fù)載告警 "
description: "{{$labels.instance}} CPU使用率超過90%(當(dāng)前值: {{ $value }})"
- alert: 內(nèi)存使用率告警
expr: (1-node_memory_MemAvailable_bytes{job!="prometheus|vmware-host"} / node_memory_MemTotal_bytes{job!="prometheus|vmware-host"}) * 100 > 90
labels:
level: critical
annotations:
summary: "{{ $labels.instance }} 可用內(nèi)存不足告警"
description: "{{$labels.instance}} 內(nèi)存使用率已達(dá)90% (當(dāng)前值: {{ $value }})"
- alert: 磁盤使用率告警
expr: 100 - (node_filesystem_avail_bytes{fstype=~"ext4|xfs", mountpoint !~ "/var/lib/[kubelet|rancher].*" } / node_filesystem_size_bytes{fstype=~"ext4|xfs", mountpoint !~ "/var/lib/[kubelet|rancher].*"}) * 100 > 85
labels:
level: warning
annotations:
summary: "{{ $labels.instance }} 磁盤使用率告警"
description: "{{$labels.instance}} 磁盤使用率已超過85% (當(dāng)前值: {{ $value }})"
主機(jī)存活檢查
groups:
- name: servers_survival
rules:
- alert: 節(jié)點(diǎn)存活--IT環(huán)境--prometheus #告警規(guī)則名稱
expr: up{job="prometheus"}==0
for: 1m #等待評(píng)估時(shí)間
labels: #自定義標(biāo)簽,定義一個(gè)level標(biāo)簽,標(biāo)記這個(gè)告警規(guī)則警告級(jí)別: critical嚴(yán)重,warning警告
level: critical
annotations: #指定附加信息(郵件標(biāo)題文本)
summary: "機(jī)器 {{ $labels.instance }} 掛了"
description: "服務(wù)器{{$labels.instance}} 掛了 (當(dāng)前值: {{ $value }})"
- alert: 節(jié)點(diǎn)存活--IT環(huán)境--其他服務(wù)器
expr: up{job="vmware-host"}==0
for: 1m
labels:
level: critical
annotations:
summary: "機(jī)器 {{ $labels.instance }} 掛了"
description: "{{$labels.instance}} 宕機(jī)(當(dāng)前值: {{ $value }})"
#- alert: 節(jié)點(diǎn)存活--IT環(huán)境--生產(chǎn)ES服務(wù)器
# expr: up{job="hw-nodes-prod-ES"}==0
# for: 1m
# labels:
# level: critical
# annotations:
# summary: "機(jī)器 {{ $labels.instance }} 掛了"
# description: "{{$labels.instance}} 宕機(jī)(當(dāng)前值: {{ $value }})"
prometheus-webhook的配置如下:
## Request timeout
# timeout: 5s
## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true
## Customizable templates path
templates:
#- current/contrib/templates/legacy/template.tmpl
- /root/monitor/prometheus-webhook-dingtalk/template/dingding.tmpl
## You can also override default template using `default_message`
## The following example to use the 'legacy' template from v0.3.0
#default_message:
# title: '{{ template "legacy.title" . }}'
# text: '{{ template "legacy.content" . }}'
## Targets, previously was known as "profiles"
targets:
webhook1:
url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
# secret for signature 釘釘機(jī)器人加簽
secret: SECxxxxxxxxxxxx
#webhook2:
# url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
#webhook_legacy:
# url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
# Customize template content
message:
# Use legacy template
title: '{{ template "legacy.title" . }}'
text: '{{ template "legacy.content" . }}'
#webhook_mention_all:
# url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
# mention:
# all: true
#webhook_mention_users:
# url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
# mention:
# mobiles: ['156xxxx8827', '189xxxx8325']
至此就完成告警的基礎(chǔ)配置,下一篇介紹關(guān)于批量部署node exporter和告警規(guī)則文件優(yōu)化和釘釘告警優(yōu)化。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。