Prometheus + alermanager + webhook-dingtalk 告警

?添加釘釘機器人

1. 部署 alermanager

1.1 下載軟件包

wget https://github.com/prometheus/alertmanager/releases/download/v0.26.0/alertmanager-0.26.0.linux-amd64.tar.gz

網址 :Releases · prometheus/alertmanager (github.com)

1.2?解壓軟件包

mkdir -pv /app/tools/
tar xf alertmanager-0.26.0.linux-amd64.tar.gz  -C /app/tools/

1.3 創建符號鏈接

cd /app/tools/ && ln -svf alertmanager-0.26.0.linux-amd64 alertmanager

1.4?修改 alermanager 的配置文件

vim alertmanager.ymlglobal:resolve_timeout: 5mroute:group_by: ['alertname']group_wait: 5sgroup_interval: 5srepeat_interval: 2mreceiver: 'web.hook'
receivers:- name: 'web.hook'webhook_configs:# prometheus-webhook-dingtalk的地址#這里只需要修改IP地址及端口號即可#也可以第一次啟動webhook-dingtalk的時候胡hi有提示這個地址(需要直接使用webhook命令執行)- url: 'http://localhost:8060/dingtalk/webhook1/send'send_resolved: true
inhibit_rules:- source_match:severity: 'critical'target_match:severity: 'warning'equal: ['alertname', 'dev', 'instance']

1.5?啟動alermanager

vim /usr/lib/systemd/system/alertmanager.service[Unit]
Description=The prometheus webhook dingtalk
After=network.target[Service]
WorkingDirectory=/app/tools/alertmanager-0.26.0.linux-amd64/
ExecStart=/app/tools/alertmanager-0.26.0.linux-amd64/alertmanager \
--config.file=/app/tools/alertmanager-0.26.0.linux-amd64/alertmanager.yml [Install]
WantedBy=multi-user.targetsystemctl daemon-reload
systemctl enable --now alertmanager.service

1.6 登錄WebUI

http://192.168.99.181:9093/

有頁面則部署完成

2. 部署 prometheus-webhook-dingtalk

2.1 下載 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

2.2 解壓軟件包

tar xf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz -C /app/tools

2.3 編寫配置文件

cat config.example.yml ## Targets, previously was known as "profiles"
#指定告警模板配置文件
templates:# - /root/prometheus-webhook-dingtalk-2.1.0.linux-amd64/templates/default.tmpl- /app/tools/prometheus-webhook-dingtalk-2.1.0.linux-amd64/templates/test.tmpl
targets:webhook1:#機器人的地址url: https://oapi.dingtalk.com/robot/send?access_token=1859e97456bdcb436f87f8e27147cfe07557901bdc4691c1836e30640f33c60b# secret for signature#機器人的認證標簽secret: SEC13a35ec382cbce1b46f4275ce640aedce28610b8026ceecd50ef59eb74002f8dmessage:## 指定了消息的標題,使用模板 `ding.link.title` 來生成。title: '{{ template "ding.link.title" . }}'# 指定了消息的正文內容,使用模板 `ding.link.content` 來生成。text: '{{ template "ding.link.content" . }}'

2.4 編寫告警模板

cat templates/test.tmpl {{ define "__subject" }}
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}]
{{ end }}{{ define "__alert_list" }}{{ range . }}
---{{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }}
---**告警名稱**: {{ .Labels.alertname }}**告警主機**: {{ .Labels.instance }} **告警級別**: {{ .Labels.severity }} #description 這里引用的是ruels文件中的字段**告警描述**: {{ index .Annotations "description" }} #時間不要動**告警時間**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}#可填寫查看詳情的網址**查看詳情**: http://192.168.99.181:9093
{{ end }}{{ end }}{{ define "__resolved_list" }}{{ range . }}
---**告警名稱**: {{ .Labels.alertname }}**告警主機**: {{ .Labels.instance }}**告警級別**: {{ .Labels.severity }}**告警描述**: {{ index .Annotations "description" }}**告警時間**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}**恢復時間**: {{ dateInZone "2006.01.02 15:04:05" (.EndsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}{{ define "default.title" }}
{{ template "__subject" . }}
{{ end }}{{ define "default.content" }}
{{ if gt (len .Alerts.Firing) 0 }}
**<font color="#FF0000">======偵測到{{ .Alerts.Firing | len  }}個告警======</font>**{{ template "__alert_list" .Alerts.Firing }}
---{{ end }}{{ if gt (len .Alerts.Resolved) 0 }}
**<font color="green">======恢復{{ .Alerts.Resolved | len  }}個故障======</font>**
{{ template "__resolved_list" .Alerts.Resolved }}
{{ end }}
{{ end }}{{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }}
{{ template "default.title" . }}
{{ template "default.content" . }}

2.5?啟動webhook-dingtalk

cat /usr/lib/systemd/system/webhook-dingtalk.service[Unit]
Description=The prometheus webhook dingtalk
After=network.target[Service]
WorkingDirectory=/app/tools/prometheus-webhook-dingtalk-2.1.0.linux-amd64/
ExecStart=/app/tools/prometheus-webhook-dingtalk-2.1.0.linux-amd64/prometheus-webhook-dingtalk \
--config.file=config.example.yml \
--web.enable-lifecycle \
--web.enable-ui[Install]
WantedBy=multi-user.targetsystemctl daemon-reload
systemctl enabled --now webhook-dingtalk.service

3. Promtheus

3.1?配置rules規則文件

[root@prometheus-server31 /app/tools/prometheus]# cat rules/test.yaml 
# 相關的規則設置定義在一個group下。在每一個group中我們可以定義多個告警規則(rule)
groups:# 組名,報警規則組名稱
- name: 內存告警rules:- alert: 內存使用率超過80%告警# expr:基于PromQL表達式告警觸發條件,用于計算是否有時間序列滿足該條件。#expr: floor(100 * (1 - (node_memory_MemFree_bytes + node_memory_Cached_bytes + node_memory_Buffers_bytes) / node_memory_MemTotal_bytes)) > 80expr: floor(100 * (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))) > 80# for:評估等待時間,可選參數。用于表示只有當觸發條件持續一段時間后才發送告警。在等待期間新產生告警的狀態為pending。# for語句會使 Prometheus 服務等待指定的時間, 然后執行查詢表達式。(for 表示告警持續的時長,若持續時長小于該時間就不發給alertmanager了,大于該時間再發。for的值不要小于prometheus中的scrape_interval,例如scrape_interval為30s,for為15s,如果觸發告警規則,則再經過for時長后也一定會告警,這是因為最新的度量指標還沒有拉取,在15s時仍會用原來值進行計算。另外,要注意的是只有在第一次觸發告警時才會等待(for)時長。)for: 1m# labels:自定義標簽,允許用戶指定要附加到告警上的一組附加標簽。labels:# severity: 指定告警級別,有三種等級,分別為:警告、嚴重、緊急,嚴重等級依次遞增。severity: '<font color="#0000FF">警告</font>'# annotations: 附加信息,比如用于描述告警詳細信息的文字等,annotations的內容在告警產生時會一同作為參數發送到Alertmanager。annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**內存使用率持續1分鐘超過**80%**, 請及時處理! 當前值**{{ $value }}%**。"- alert: 內存使用率超過90%告警expr: floor(100 * (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))) > 90for: 1mlabels:severity: '<font color="#FFA500">嚴重</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**內存使用率持續1分鐘超過**90%**, 請馬上處理! 當前值**{{ $value }}%**。"- name: CPU告警rules:- alert: CPU使用率超過80%告警expr: floor(100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[1m])) * 100)) > 80for: 1mlabels:severity: '<font color="#0000FF">警告</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**CPU使用率持續1分鐘超過**80%**, 請及時處理! 當前值**{{ $value }}%**。"- alert: CPU使用率超過90%告警expr: floor(100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[1m])) * 100)) > 90for: 1mlabels:severity: '<font color="#FFA500">嚴重</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**CPU使用率持續1分鐘超過**90%**, 請馬上處理! 當前值**{{ $value }}%**。"- name: 磁盤告警rules:- alert: 磁盤 **/** 目錄使用率超過90%告警expr: floor(100 * ((node_filesystem_size_bytes{mountpoint="/"} - node_filesystem_avail_bytes{mountpoint="/"}) / node_filesystem_size_bytes{mountpoint="/"})) >90for: 30mlabels:severity: '<font color="#0000FF">警告</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**磁盤 **/** 目錄使用率已經超過閾值**90%**, 請及時處理! 當前值**{{ $value }}%**。"- alert: 磁盤 **/home** 目錄使用率超過90%告警expr: floor(100 * ((node_filesystem_size_bytes{mountpoint="/home"} - node_filesystem_avail_bytes{mountpoint="/home"}) / node_filesystem_size_bytes{mountpoint="/home"})) >90for: 30mlabels:severity: '<font color="#0000FF">警告</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**磁盤 **/home** 目錄使用率已經超過閾值**90%**, 請及時處理! 當前值**{{ $value }}%**。"- alert: 磁盤 **/data** 目錄使用率超過90%告警expr: floor(100 * ((node_filesystem_size_bytes{mountpoint="/data"} - node_filesystem_avail_bytes{mountpoint="/data"}) / node_filesystem_size_bytes{mountpoint="/data"})) >90for: 30mlabels:severity: '<font color="#0000FF">警告</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**磁盤 **/data** 目錄使用率已經超過閾值**90%**, 請及時處理! 當前值**{{ $value }}%**。"- alert: 磁盤 **/data1** 目錄使用率超過90%告警expr: floor(100 * ((node_filesystem_size_bytes{mountpoint="/data1"} - node_filesystem_avail_bytes{mountpoint="/data1"}) / node_filesystem_size_bytes{mountpoint="/data1"})) >90for: 30mlabels:severity: '<font color="#0000FF">警告</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**磁盤 **/data1** 目錄使用率已經超過閾值**90%**, 請及時處理! 當前值**{{ $value }}%**。"- alert: 磁盤 **/postgres** 目錄使用率超過90%告警expr: floor(100 * ((node_filesystem_size_bytes{mountpoint="/postgres"} - node_filesystem_avail_bytes{mountpoint="/postgres"}) / node_filesystem_size_bytes{mountpoint="/postgres"})) >90for: 30mlabels:severity: '<font color="#0000FF">警告</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**磁盤 **/postgres** 目錄使用率已經超過閾值**90%**, 請及時處理! 當前值**{{ $value }}%**。"- name: 離線告警rules:- alert: 服務器離線告警expr: up{instance =~ ".*:(9100|9200)"} == 0for: 30slabels:severity: '<font color="#FF0000">緊急</font>'annotations:servername: "{{ $labels.server_name }}"server_ip: "{{ $labels.server_ip }}"value: "{{ $value }}"description: "**{{ $labels.server_name }}**離線了,可能出現宕機情況, 請立即檢查處理! "

3.2 配置 prometheus.yml 文件

vim /app/tools/prometheus/prometheus.yml
....
#指定alermanager
alerting:alertmanagers:- static_configs:- targets:- 192.168.99.181:9093
.....
rule_files:#指定 rules 配置文件- "/app/tools/prometheus/rules/test.yaml"
.....

3.3 重啟prometheus

systemctl reload prometheus.server

4. 測試

根據自己定義的規則關閉一個監控的節點,查看釘釘是否有收到消息

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/44645.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/44645.shtml
英文地址,請注明出處:http://en.pswp.cn/web/44645.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

醫日健集團技術力量體現測試的背后

醫日健集團覆蓋式更新 科技日新月異的時代&#xff0c;醫日健集團始終走在行業的前列。近日&#xff0c;醫日健集團外勤技術人員全面對市場點位投放的數智藥房進行了新系統升級和機器測試&#xff0c;這是醫日健對于科技創新的最新嘗試。 以客戶體驗為核心優化新體驗 醫日健集團…

NCNN源碼學習(1):Mat詳解

前言:最原始的發行版本代碼比較簡潔,我們從2017年ncnn第一次開源的版本閱讀mat的源碼。閱讀源碼味如嚼蠟,下面就開始吧! 目錄 構造函數 內存分配 數據成員 申請和釋放內存 引用計數 輔助函數 填充函數fill 參考 構造函數 ncnn提供了8種構造函數的方式。 // emptyM…

Js 前置,后置補零的原生方法與補字符串 padStart及padEnd

在工作中&#xff0c;遇到了需要將不滿八位的一個字符串進行后補0的操作&#xff0c;所以就在網上學習了關于js原生補充字符串的方法&#xff0c;然后用這篇博客記錄下來。 目錄 前置補充字符串 String.prototype.padStart() 后置補充字符串String.prototype.padEnd() 前置補…

將獨熱碼應用到神經網絡中

引言 接上回&#xff0c;本文繼續說如何用TensorFlow將獨熱編碼應用到一個簡單的神經網絡中&#xff0c;以實現從一段隨機文本到另一段隨機文本的轉換。 步驟一&#xff1a;導入庫 import tensorflow as tf import numpy as np import random import string步驟二&#xff1…

【超音速 專利 CN117710683A】基于分類模型的輕量級工業圖像關鍵點檢測方法

申請號CN202311601629.7公開號&#xff08;公開&#xff09;CN117710683A申請日2023.11.27申請人&#xff08;公開&#xff09;超音速人工智能科技股份有限公司發明人&#xff08;公開&#xff09;張俊峰(總); 楊培文(總); 沈俊羽; 張小村 技術領域 本發明涉及圖像關鍵點檢測…

數據庫MySQL下載安裝

MySQL下載安裝地址如下&#xff1a; MySQL :: Download MySQL Community Server 1、下載界面 2、點擊下載 3、解壓記住目錄 4、配置my.ini文件 未完..

C語言課程回顧:九、C語言之預處理命令

9 預處理命令 9 預處理命令9.1 概述9.2 宏定義9.2.1 無參宏定義9.2.2 帶參宏定義 9.3 文件包含9.4 條件編譯9.5 本章小結9.6 擴展 10種軟件濾波方法的示例程序1、限副濾波2、中位值濾波法3、算術平均濾波法4、遞推平均濾波法&#xff08;又稱滑動平均濾波法&#xff09;5、中位…

Vue.js學習筆記(五)抽獎組件封裝——轉盤抽獎

基于VUE2轉盤組件的開發 文章目錄 基于VUE2轉盤組件的開發前言一、開發步驟1.組件布局2.布局樣式3.數據準備 二、最后效果總結 前言 因為之前的轉盤功能是圖片做的&#xff0c;每次活動更新都要重做UI和前端&#xff0c;為了解決這一問題進行動態配置轉盤組件開發&#xff0c;…

【jvm】字符串常量池問題

目錄 一、基本概念1.1 說明1.2 特點 二、存放位置2.1 JDK1.6及以前2.2 JDK1.72.3 JDK1.8及以后 三、工作原理3.1 創建字符串常量3.2 使用new關鍵字創建字符串 四、intern()方法4.1 作用 五、優點六、字節碼分析6.1 示例16.1.1 代碼示例6.1.2 字節碼6.1.3 解析 6.2 示例26.2.1 代…

STM32智能倉儲管理系統教程

目錄 引言環境準備晶智能倉儲管理系統基礎代碼實現&#xff1a;實現智能倉儲管理系統 4.1 數據采集模塊 4.2 數據處理與決策模塊 4.3 通信與網絡系統實現 4.4 用戶界面與數據可視化應用場景&#xff1a;倉儲管理與優化問題解決方案與優化收尾與總結 1. 引言 智能倉儲管理系統…

7 月12日學習打卡--棧和隊列的相互轉換

hello大家好呀&#xff0c;本博客目的在于記錄暑假學習打卡&#xff0c;后續會整理成一個專欄&#xff0c;主要打算在暑假學習完數據結構&#xff0c;因此會發一些相關的數據結構實現的博客和一些刷的題&#xff0c;個人學習使用&#xff0c;也希望大家多多支持&#xff0c;有不…

什么是STM32?嵌入式和STM32簡單介紹

1、嵌入式和STM32 1.1.什么是嵌入式 除了桌面PC之外&#xff0c;所有的控制類設備都是嵌入式 嵌入式系統的定義&#xff1a;“用于控制、監視或者輔助操作機器和設備的裝置”。 嵌入式系統是一個控制程序存儲在ROM中的嵌入式處理器控制板&#xff0c;是一種專用的計算機系統。…

初階數據結構速成

本篇文章算是對初階數據結構的總結&#xff0c;內容較多&#xff0c;請耐心觀看 基礎概念部分 順序表 線性表&#xff08; linear list &#xff09;是n個具有相同特性的數據元素的有限序列。 線性表是?種在實際中?泛使 ?的數據結構&#xff0c;常?的線性表&#xff1a;…

C語言 錯題本

C語言 錯題本 文章目錄 C語言 錯題本77月11號整數求逆--掌握 7月12號求符合給定條件的整數集水仙花數打印九九口訣表--掌握統計素數并求和--掌握 7月13號湊硬幣前n項和(一加一減)最大公約數 7月14號正序整數分解 7月17號簡單計算器 217月26號求符合給定條件的整數集水仙花數 旨…

【安全設備】上網行為管理

一、什么是上網行為管理 上網行為管理是對企業內部員工使用互聯網行為的監視和管理&#xff0c;旨在規范網絡使用者的上網行為&#xff0c;提高網絡安全性&#xff0c;保護企業信息安全&#xff0c;同時提高員工的工作效率。上網行為管理通過對員工的上網行為進行監控、記錄和…

機器學習——關于極大似然估計法的一些個人思考(通俗易懂極簡版)

最近在回顧機器學習的一些相關理論知識&#xff0c;回顧到極大似然法時&#xff0c;對于極大似然法中的一些公式有些迷糊了&#xff0c;所以本文主要想記錄并分享一下個人關于極大似然估計法的一些思考&#xff0c;如果有誤&#xff0c;請見諒&#xff0c;歡迎一起前來探討。當…

單元測試實施最佳方案(背景、實施、覆蓋率統計)

1. 什么是單元測試&#xff1f; 對于很多開發人員來說&#xff0c;單元測試一定不陌生 單元測試是白盒測試的一種形式&#xff0c;它的目標是測試軟件的最小單元——函數、方法或類。單元測試的主要目的是驗證代碼的正確性&#xff0c;以確保每個單元按照預期執行。單元測試通…

合肥高校大學智能制造實驗室數字孿生可視化系統平臺建設項目驗收

合肥高校大學智能制造實驗室近日迎來了一項重要時刻&#xff0c;數字孿生可視化系統平臺建設項目順利通過了驗收。這一項目的成功實施&#xff0c;不僅標志著合肥高校在智能制造領域取得新的突破&#xff0c;為我國智能制造技術的發展注入新活力。 合肥高校智能制造實驗室作為…

T972 切換至pdm 聲音輸入的方法

1.在hardware/amlogic/audio/audio_hal/audio_hw.c下&#xff0c;直接切換 在 static unsigned int select_port_by_device(struct aml_audio_device *adev) 中先強制切換為pdm 2.在device mk 配置文件中 #add fof fix the mic bug by jason 20230621 PRODUCT_PROPERTY_OVE…

MySQL 數據庫基礎概念

一、什么是數據庫&#xff1f; 數據庫&#xff08;Database&#xff09;是按照數據結構來組織、存儲和管理數據的倉庫。 每個數據庫都有一個或多個不同的 API 用于創建&#xff0c;訪問&#xff0c;管理&#xff0c;搜索和復制所保存的數據。 我們也可以將數據存儲在文件中&…