短信告警這個模塊在很多項目中都用上了,比如之前做過的安防系統(tǒng),溫濕度報警系統(tǒng)等,主要的流程就是收到數(shù)據(jù)判斷屬于某種報警后,組織短信字符串內(nèi)容,發(fā)送到指定的多個手機號碼上面,使用的是短信貓硬件設備,其實就是個短信模塊,沒有使用網(wǎng)絡的發(fā)送短信的api,畢竟大部分的軟件應用場景都不能要求連通外網(wǎng),安全考慮,所以必須采用本地的硬件來實現(xiàn)發(fā)送短信。這個短信貓設備在8年前開始用過,當時用的C#去寫了個純協(xié)議解析短信的收發(fā),后面用Qt重新寫了個。特意寫了個類SendMsgThread封裝了所有的操作,這樣就通用任何開發(fā)環(huán)境了,掌握了原理,從底層原理上著手,好處還是很多的,我看很多初學者熱衷于去調(diào)用第三方廠家封裝好的SDK,一旦換了操作系統(tǒng),完蛋了歇菜,沒有嵌入式linux系統(tǒng)的SDK開發(fā)包咋搞。
通用短信收到組件功能特點:
1. 可設置收發(fā)短信模式
2. 可批量發(fā)送短信以及支持長短信
3. 可讀取指定指定序號短信
4. 可刪除所有短信
5. 可檢測短信貓設備是否正常
6. 支持中文短信發(fā)送
7. 支持撥打電話+掛斷電話+接聽來電+識別用戶按鍵反饋
8. 支持批量發(fā)送給多個號碼
皮膚開源:[https://gitee.com/feiyangqingyun/QWidgetDemo](https://gitee.com/feiyangqingyun/QWidgetDemo) [https://github.com/feiyangqingyun/QWidgetDemo](https://github.com/feiyangqingyun/QWidgetDemo)
文件名稱:styledemo
體驗地址:[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)絡端口,串口支持自由設置串口號+波特率,網(wǎng)絡支持自由設置IP地址+通訊端口,每個端口支持采集周期,默認1秒鐘一個地址,支持設置通訊超時次數(shù),默認3次,支持最大重連時間,用于重新讀取離線的設備。
2. 控制器信息,能夠添加控制器名稱,選擇控制器地址+控制器型號,設置該控制器下面的探測器數(shù)量。
3. 探測器信息,能夠添加位號,可自由選擇探測器型號,氣體種類,氣體符號,高報值,低報值,緩沖值,清零值,是否啟用,報警聲音,背景地圖,存儲周期,數(shù)值換算小數(shù)點位數(shù),報警延時時間,報警的類型(HH,LL,HL)等。
4. 控制器型號+探測器型號+氣體種類+氣體符號,均可自由配置。
5. 地圖支持導入和刪除,所有的探測器對應地圖位置可自由拖動保存。
6. 端口信息+控制器信息+探測器信息,支持導入導出+導出到excel+打印。
7. 運行記錄+報警記錄+用戶記錄,支持多條件組合查詢,比如時間段+控制器+探測器等,所有記錄支持導出到excel+打印。
8. 導出到excel的記錄支持所有excel+wps等表格文件版本,不依賴excel等軟件。
9. 可刪除指定時間范圍內(nèi)的數(shù)據(jù),支持自動清理早期數(shù)據(jù),設置最大保存記錄數(shù)。
10. 支持報警短信轉(zhuǎn)發(fā),支持多個接收手機號碼,可設定發(fā)送間隔,比如即時發(fā)送或者6個小時發(fā)送一次所有的報警信息,短信內(nèi)容過長,自動拆分多條短信。
11. 支持報警郵件轉(zhuǎn)發(fā),支持多個接收郵箱,可設定發(fā)送間隔,比如即時發(fā)送或者6個小時發(fā)送一次所有的報警信息,支持附件發(fā)送。
12. 高報顏色+低報顏色+正常顏色+0值顏色+曲線背景+曲線顏色等,都可以自由選擇。
13. 軟件的中文標題+英文標題+logo路徑+版權(quán)所有都可以自由設置。
14. 提供開關(guān)設置開機運行+報警聲音+自動登錄+記住密碼等。
15. 報警聲音可設置播放次數(shù),界面提供17種皮膚文件選擇。
16. 支持云端數(shù)據(jù)同步,可設置云端數(shù)據(jù)庫的信息,比如數(shù)據(jù)庫名稱,用戶名+密碼等。
17. 支持網(wǎng)絡轉(zhuǎn)發(fā)和網(wǎng)絡接收,網(wǎng)絡接收開啟后,軟件從udp接收數(shù)據(jù)進行解析。網(wǎng)絡轉(zhuǎn)發(fā)支持多個目標IP,這樣就實現(xiàn)了本地采集的軟件,自由將數(shù)據(jù)轉(zhuǎn)到客戶端,隨時查看探測器數(shù)據(jù)。
18. 自動記住用戶最后停留的界面+其他信息,重啟后自動應用。
19. 報警自動切換到對應的地圖,探測器按鈕閃爍。
20. 雙擊探測器圖標,可以進行回控。
21. 支持用戶權(quán)限管理,管理員+操作員兩大類,用戶登錄+用戶退出,可以記住密碼和自動登錄,超過三次報錯提示并關(guān)閉程序。
22. 支持四種監(jiān)控模式,設備面板監(jiān)控+地圖監(jiān)控+表格數(shù)據(jù)監(jiān)控+曲線數(shù)據(jù)監(jiān)控,可自由切換,四種同步應用。
23. 支持報警繼電器聯(lián)動,一個位號可以跨串口聯(lián)動多個模塊和繼電器號,支持多對多。
24. 本地數(shù)據(jù)存儲支持sqlite+mysql,支持遠程數(shù)據(jù)同步到云端數(shù)據(jù)庫。自動重連。
25. 本地設備采集到的數(shù)據(jù)實時上傳到云端,以便手機APP或者web等其他方式提取。
26. 支持兩種數(shù)據(jù)源,一種是串口和網(wǎng)絡通過協(xié)議采集設備數(shù)據(jù),一種是數(shù)據(jù)庫采集。數(shù)據(jù)庫采集模式可以作為通用的系統(tǒng)使用。
27. 自帶設備模擬工具,支持16個設備數(shù)據(jù)模擬,同時還帶數(shù)據(jù)庫數(shù)據(jù)模擬,以便在沒有設備的時候測試數(shù)據(jù)。
28. 默認通信協(xié)議采用modbus協(xié)議,后期增加mqtt等物聯(lián)網(wǎng)協(xié)議的支持,做成通用系統(tǒng)。
29. 支持所有windows操作系統(tǒng)+linux操作系統(tǒng)和其他操作系統(tǒng)。
動互聯(lián)小課堂 (六) 報警服務
視頻鏈接如下:
https://audi-embedded-wap.saic-audi.mobi/share/index.html#/article?flag=0&category=article&id=1232417733879517184
字如面,大家好,我是小斐,上一篇介紹generator.yml文件配置和prometheus.yml文件配置,包含文件服務自動發(fā)現(xiàn)機制,以及根據(jù)generator.yml配置,根據(jù)SNMP Exporter官方提供的生成器,生成snmp.yml配置文件等等,今天主要說明下正式環(huán)境的配置和情況,包括披露部署虛擬機Node Exporter節(jié)點數(shù)據(jù)采集器等問題。
關(guān)于存儲
Prometheus 提供了本地存儲(TSDB)時序型數(shù)據(jù)庫的存儲方式,在2.0版本之后,壓縮數(shù)據(jù)的能力得到了大大的提升(每個采樣數(shù)據(jù)僅僅占用1~2Byte左右空間),單節(jié)點情況下可以滿足大部分用戶的需求,但本地存儲阻礙了Prometheus集群化的實現(xiàn),因此在集群中應當采其他時序性數(shù)據(jù)庫來替代,比如influxdb。
Prometheus分為三個部分,分別是:抓取數(shù)據(jù)、存儲數(shù)據(jù)和查詢數(shù)據(jù)。
抓取數(shù)據(jù),就是各種抓取器,存儲數(shù)據(jù)就是時序數(shù)據(jù)庫,查詢數(shù)據(jù),可以理解為數(shù)據(jù)可視化。
關(guān)于我這里的正式環(huán)境,因指標不是很多,故不采用外部數(shù)據(jù)庫,主要還是基于本地存儲,本地存儲Prometheus 按2小時一個block進行存儲,每個block由一個目錄組成,該目錄里包含:一個或者多個chunk文件(保存時間序列數(shù)據(jù))默認每個chunk大小為512M、一個metadata文件、一個index文件(通過metric name和labels查找時間序列數(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
磁盤大小計算方式:磁盤大小=保留時間 * 每秒獲取樣本數(shù) * 樣本大小
保留時間(retention_time_seconds)和樣本大小(bytes_per_sample)不變的情況下,如果想減少本地磁盤的容量需求,只能通過減少每秒獲取樣本數(shù)(ingested_samples_per_second)的方式。
因此有兩種手段,一是減少時間序列的數(shù)量,二是增加采集樣本的時間間隔。
考慮到Prometheus會對時間序列進行壓縮,因此減少時間序列的數(shù)量效果更明顯。
為什么默認兩個小時存儲一次數(shù)據(jù),寫成一個chuck塊文件
通過時間窗口的形式保存所有的樣本數(shù)據(jù),可以明顯提高Prometheus的查詢效率,當查詢一段時間范圍內(nèi)的所有樣本數(shù)據(jù)時,只需要簡單的從落在該范圍內(nèi)的塊中查詢數(shù)據(jù)即可。
最新寫入的2 小時數(shù)據(jù)保存在內(nèi)存中
最新寫入的數(shù)據(jù)保存在內(nèi)存block中,達到2小時后寫入磁盤,計算2小時寫入數(shù)據(jù)大小,決定出內(nèi)存的大小。
為了防止程序崩潰導致數(shù)據(jù)丟失,采用WAL(write-ahead-log)預寫日志機制,啟動時會以寫入日志(WAL)的方式來實現(xiàn)重播,從而恢復數(shù)據(jù)。
未落盤時數(shù)據(jù)存儲內(nèi)容。有wal文件
./data/01BKGV7JBM69T2G1BGBGM6KB12
./data/01BKGV7JBM69T2G1BGBGM6KB12/meta.json
./data/01BKGV7JBM69T2G1BGBGM6KB12/wal/000002
./data/01BKGV7JBM69T2G1BGBGM6KB12/wal/000001
數(shù)據(jù)存儲方式
落盤后的數(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小時的block會在后臺壓縮成更大的block,數(shù)據(jù)壓縮合并成更高level的block文件后刪除低level的block文件。這個和leveldb、rocksdb等LSM樹的思路一致。
數(shù)據(jù)過期清理時間,默認保存15天
存儲數(shù)據(jù)的目錄,默認為data/,如果要掛外部存儲,可以指定該目錄
刪除數(shù)據(jù)方式
刪除數(shù)據(jù)時,刪除條目會記錄在獨立的tombstone 刪除記錄文件中,而不是立即從chunk文件刪除。
首先是數(shù)據(jù)持久化的問題,默認保存15天,原生的TSDB對于大數(shù)據(jù)量的保存及查詢支持不太友好 ,所以并不適用于保存長期的大量數(shù)據(jù);另外,該數(shù)據(jù)庫的可靠性也較弱,在使用過程中容易出現(xiàn)數(shù)據(jù)損壞等故障,且無法支持集群的架構(gòu)。
關(guān)于遠端存儲,在此就不表,因我的正式環(huán)境,不準備采用遠端存儲存儲數(shù)據(jù),就采用本地存儲就滿足我的需求。
IP | 規(guī)格 | 服務 | 說明 |
172.17.40.51 | 8C 16G 500GB | 單節(jié)點 Prometheus Server + Grafana Server + Alertmanager | 該服務器主要搭建Prometheus服務器、Grafana服務器、Alertmanager服務器 |
172.17.40.54 | 4C 8G 60G | SNMP Exporter | 交換機、防火墻、負載均衡、AC、DELL iDrac、IPMI等設備啟用SNMP協(xié)議采集數(shù)據(jù),該服務器作為SNMP協(xié)議數(shù)據(jù)采集節(jié)點 |
其他服務器 共500臺 | 各種規(guī)格 | Node Exporter | 其他服務器部署Node Exporter采集主機性能、狀態(tài)、網(wǎng)絡、空間容量等各類數(shù)據(jù) |
關(guān)于正式環(huán)境搭建可參考前面文章
Prometheus + Grafana搭建IT監(jiān)控報警最佳實踐(1)
在此補充一下細節(jié)說明和完善的點:
這里部署Prometheus Server、Grafana Server、Alertmanager都是未采用docker安裝部署,而是采用編譯好的二進制文件部署,簡單說明:
# 正式環(huán)境
# 舊版本的 Docker 被稱為docker或docker-engine,如果安裝了這些,請卸載它們以及相關(guān)的依賴項。
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 使用存儲庫安裝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,請查看官網(wǎng)
https://docs.docker.com/engine/install/centos/
# 驗證安裝 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?
# 此時安裝了docker,但并沒有啟動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
# 設置docker開機自啟動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
# 啟動
systemctl enable grafana-server
systemctl start grafana-server
systemctl status grafana-server
netstat -anplut | grep grafana
# 訪問Grafana
瀏覽器本機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
# 運行
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
# 運行
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
# 啟動
# 檢查配置文件格式是否錯誤
./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
# 運行
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
# 啟動
systemctl daemon-reload
systemctl start node_exporter
systemctl status node_exporter
systemctl enable node_exporter
1、主機監(jiān)控:使用文件服務發(fā)現(xiàn)機制,不過我在此沒有使用服務注冊發(fā)現(xiàn)機制consul
# 添加prometheus.yml配置文件
# 采集內(nèi)部VMWare集群中的主機信息
- 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
動態(tài)加載配置文件:
curl -X POST localhost:9090/-/reload
2、交換機監(jiān)控:使用文件服務發(fā)現(xiàn)機制
# 添加交換機類job
- job_name: "SNMP"
file_sd_configs:
- files:
- /data/monitor/prometheus/targets/network-*.yml
refresh_interval: 2m
# scrape_interval: 5s # 針對SNMP采集節(jié)點 覆蓋全局配置15s
metrics_path: /snmp
params:
module:
- if_mib
# community: # 當 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采集目標直接修改為snmp_exporter 服務IP地址
- target_label: __address__
replacement: 172.17.40.54:9116 # snmp_exporter 服務IP地址
- source_labels: ["mib"] # 從自定義的目標標簽獲取MIB模塊名稱
target_label: __param_module
# 新建network-switch.yml文件 如何有多臺就直接在文件后添加多臺交換機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)控目標已添加完成,后續(xù)將講解批量部署采集器和添加目標。
前提:部署alertmanager,部署prometheus-webhook
思路:
# alertmanager.yml 配置模版
global: # 全局配置
resolve_timeout: 5m # 處理告警超時時間 未接收到告警后標記告警狀態(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' # 標記身份
smtp_require_tls: false # TLS關(guān)閉
templates:
- '/root/monitor/prometheus-webhook-dingtalk/template/*.tmpl' # 告警模板路徑
# 每個告警事件如何發(fā)送出去
route:
group_by: ['alertname'] # 采用哪個標簽作為分組的依據(jù)
group_wait: 30s # 分組內(nèi)第一個告警等待時間,10s內(nèi)如有第二個告警會合并一個告警
group_interval: 5m # 發(fā)送新告警間隔時間 上下兩組發(fā)送告警的間隔時間
repeat_interval: 30m # 重復告警間隔發(fā)送時間,如果沒處理過多久再次發(fā)送一次
receiver: 'dingtalk_webhook' # 接收人 定義誰來通知報警
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ā)送恢復消息給接收人
官方示例文件:
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ù)進行掃描加載,用于自定義報警規(guī)則,其報警媒介和route路由由alertmanager插件實現(xiàn)
rule_files:
- "rules/*.yml"
# - "second_rules.yml"
編寫規(guī)則文件
# 這里在網(wǎng)上找了兩個規(guī)則配置文件
groups:
- name: servers_status # 這里名字是填寫在group_by
rules:
- alert: CPU負載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負載告警 "
description: "{{$labels.instance}} 1分鐘CPU負載(當前值: {{ $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%(當前值: {{ $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負載告警 "
description: "{{$labels.instance}} CPU使用率超過90%(當前值: {{ $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)存使用率已達90% (當前值: {{ $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% (當前值: {{ $value }})"
主機存活檢查
groups:
- name: servers_survival
rules:
- alert: 節(jié)點存活--IT環(huán)境--prometheus #告警規(guī)則名稱
expr: up{job="prometheus"}==0
for: 1m #等待評估時間
labels: #自定義標簽,定義一個level標簽,標記這個告警規(guī)則警告級別: critical嚴重,warning警告
level: critical
annotations: #指定附加信息(郵件標題文本)
summary: "機器 {{ $labels.instance }} 掛了"
description: "服務器{{$labels.instance}} 掛了 (當前值: {{ $value }})"
- alert: 節(jié)點存活--IT環(huán)境--其他服務器
expr: up{job="vmware-host"}==0
for: 1m
labels:
level: critical
annotations:
summary: "機器 {{ $labels.instance }} 掛了"
description: "{{$labels.instance}} 宕機(當前值: {{ $value }})"
#- alert: 節(jié)點存活--IT環(huán)境--生產(chǎn)ES服務器
# expr: up{job="hw-nodes-prod-ES"}==0
# for: 1m
# labels:
# level: critical
# annotations:
# summary: "機器 {{ $labels.instance }} 掛了"
# description: "{{$labels.instance}} 宕機(當前值: {{ $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 釘釘機器人加簽
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']
至此就完成告警的基礎配置,下一篇介紹關(guān)于批量部署node exporter和告警規(guī)則文件優(yōu)化和釘釘告警優(yōu)化。
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。