日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
【創(chuàng)新互聯(lián)】Prometheus自定義告警規(guī)則

一、概述

通過創(chuàng)建Prometheus監(jiān)控告警規(guī)則,您可以制定針對特定Prometheus實例的告警規(guī)則。當告警規(guī)則設置的條件滿足后,系統(tǒng)會產(chǎn)生對應的告警事件。如果想要收到通知,需要進一步配置對應的通知策略以生成告警并且以短信、郵件、電話、釘群機器人、企業(yè)微信機器人或者Webhook等方式發(fā)送通知。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供云溪企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站制作、網(wǎng)站設計、H5高端網(wǎng)站建設、小程序制作等業(yè)務。10年已為云溪眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。

從Prometheus server端接收到alerts后,會基于PromQL的告警規(guī)則 分析數(shù)據(jù),如果滿足PromQL定義的規(guī)則,則會產(chǎn)生一條告警,并發(fā)送告警信息到Alertmanager,Alertmanager則是根據(jù)配置處理告警信息并發(fā)送。所以Prometheus的告警配置依賴于PromQL與AlertManager,關于這兩個介紹可以參考我以下文章:

  • 【云原生】Prometheus AlertManager講解與實戰(zhàn)操作
  • 【云原生】Prometheus PromQL講解與實戰(zhàn)操作
  • 【云原生】Prometheus Pushgetway講解與實戰(zhàn)操作
  • 官方文檔:https://prometheus.io/docs/alerting/latest/overview/

二、告警實現(xiàn)流程

設置警報和通知的主要步驟是:

  1. 在Prometheus中配置告警規(guī)則。
  2. 配置Prometheus 與 AlertManager 關聯(lián)。
  3. 配置 AlertManager 告警通道。

三、告警規(guī)則

官方文檔:https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/

1)告警規(guī)則配置

在Prometheus 配置(prometheus.yml)中添加報警規(guī)則配置,配置文件中 rule_files 就是用來指定報警規(guī)則文件的,如下配置即指定存放報警規(guī)則的目錄為/etc/prometheus,規(guī)則文件為rules.yml:

rule_files:
- /etc/prometheus/rules.yml

設置報警規(guī)則:

警報規(guī)則允許基于 Prometheus 表達式語言的表達式來定義報警報條件的,并在觸發(fā)警報時發(fā)送通知給外部的接收者(Alertmanager),一條警報規(guī)則主要由以下幾部分組成:

  • alert——告警規(guī)則的名稱。
  • expr——是用于進行報警規(guī)則 PromQL 查詢語句。
  • for——評估告警的等待時間(Pending Duration)。
  • labels——自定義標簽,允許用戶指定額外的標簽列表,把它們附加在告警上。
  • annotations——用于存儲一些額外的信息,用于報警信息的展示之類的。

rules.yml示例如下:

groups:
- name: example
rules:
- alert: high_memory
# 當內(nèi)存占有率超過10%,持續(xù)1min,則觸發(fā)告警
expr: 100 - ((node_memory_MemAvailable_bytes{instance="192.168.182.110:9100",job="node_exporter"} * 100) / node_memory_MemTotal_bytes{instance="192.168.182.110:9100",job="node_exporter"}) > 90
for: 1m
labels:
severity: page
annotations:
summary: spike memeory

2)監(jiān)控服務器是否在線

對于被Prometheus監(jiān)控的服務器,我們都有一個up指標,可以知道該服務是否在線。

up == 0  #服務下線了。
up == 1 #服務在線。

【示例】

groups:
- name: Test-Group-001 # 組的名字,在這個文件中必須要唯一
rules:
- alert: InstanceDown # 告警的名字,在組中需要唯一
expr: up == 0 # 表達式, 執(zhí)行結果為true: 表示需要告警
for: 1m # 超過多少時間才認為需要告警(即up==0需要持續(xù)的時間)
labels:
severity: warning # 定義標簽
annotations:
summary: "服務 {{ $labels.instance }} 下線了"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."

注意:

  • for 指定達到告警閾值之后,一致要持續(xù)多長時間,才發(fā)送告警數(shù)據(jù)。
  • labels 中可以指定自定義的標簽,如果定義的標簽已經(jīng)存在,則會被覆蓋。可以使用模板。
  • annotations 中的數(shù)據(jù),可以使用模板,$labels表示告警數(shù)據(jù)的標簽,{{$value}}表示時間序列的值。

3)告警數(shù)據(jù)的狀態(tài)

  • Inactive——表示沒有達到告警的閾值,即expr表達式不成立。
  • Pending——表示達到了告警的閾值,即expr表達式成立了,但是未滿足告警的持續(xù)時間,即for的值。
  • Firing——已經(jīng)達到閾值,且滿足了告警的持續(xù)時間。

【溫馨提示】經(jīng)測試發(fā)現(xiàn),如果同一個告警數(shù)據(jù)達到了Firing,那么不會再次產(chǎn)生一個告警數(shù)據(jù),除非該告警解決了。

四、實戰(zhàn)操作

1)下載 node_exporter

node-exporter用于采集node的運行指標,包括node的cpu、load、filesystem、meminfo、network等基礎監(jiān)控指標,類似于zabbix監(jiān)控系統(tǒng)的的zabbix-agent。

下載地址:https://github.com/prometheus/node_exporter/releases/

wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
tar -xzf node_exporter-1.5.0.linux-amd64.tar.gz

2)啟動 node_exporter

ln -s /opt/prometheus/exporter/node_exporter/node_exporter-1.5.0.linux-amd64/node_exporter  /usr/local/bin/node_exporter
# 指定端口啟動,默認端口:9100
node_exporter --web.listen-address=":9100"

配置node_exporter.service啟動

# 默認端口9100
cat >/usr/lib/systemd/system/node_exporter.service<[Unit]
Descriptinotallow=node_exporter
After=network.target
#可以創(chuàng)建相應的用戶和組 啟動
#User=prometheus
#Group=prometheus

[Service]
ExecStart=/opt/prometheus/exporter/node_exporter/node_exporter-1.5.0.linux-amd64/node_exporter --web.listen-address=:9100
[Install]
WantedBy=multi-user.target
EOF

啟動服務

systemctl daemon-reload
systemctl start node_exporter
systemctl status node_exporter
systemctl enable node_exporter

檢查

curl http://localhost:9100/metrics

3)配置Prometheus加載node_exporter

添加或修改配置 prometheus.yml

重啟加載配置

systemctl restart prometheus
# 1、 kill方式
#kill -HUP pid
# 2、curl方式(推薦)
#curl -X POST http://IP/-/reload
# 【注意】需要在啟動的命令行增加參數(shù):--web.enable-lifecycle
curl -X POST http://192.168.182.110:9090/-/reload
# 3、重啟(不推薦,重啟會導致所有的連接短暫性中斷)
systemctl restart prometheus

檢查web:http://ip:9090/targets

4)告警規(guī)則配置

在Prometheus配置文件rometheus.yml 中配置如下:

在/etc/prometheus/rule.yml配置如下:

groups:
- name: Test-Group-001 # 組的名字,在這個文件中必須要唯一
rules:
- alert: InstanceDown # 告警的名字,在組中需要唯一
expr: up == 0 # 表達式, 執(zhí)行結果為true: 表示需要告警
for: 1m # 超過多少時間才認為需要告警(即up==0需要持續(xù)的時間)
labels:
severity: warning # 定義標簽
annotations:
summary: "服務 {{ $labels.instance }} 下線了"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."

重新加載

curl -X POST http://localhost:9090/-/reload

在web上就可以看到一個告警規(guī)則。

5)模擬告警

手動關機

sudo shutdown -h now

過了一段時間告警狀態(tài)就變成Pending

再過一段時間告警就變成了Firing

6)配置告警通道

這里以有郵件告警為示例,其它的也差不多。修改配置之前最好先備份一下之前的配置

cp alertmanager.yml alertmanager.bak

【1】配置 alertmanager.yml

global:
resolve_timeout: 5m
## 這里為qq郵箱 SMTP 服務地址,官方地址為 smtp.qq.com 端口為 465 或 587,同時要設置開啟 POP3/SMTP 服務。
smtp_smarthost: 'smtp.qq.com:465'
smtp_from: 'xxxxxxxx@qq.com'
smtp_auth_username: 'xxxxxxxx@qq.com'
#授權碼,不是密碼,在 QQ 郵箱服務端設置開啟 POP3/SMTP 服務時會提示
smtp_auth_password: 'xxxxxxxx'
smtp_require_tls: false

#1、模板
templates:
- '/opt/prometheus/alertmanager/alertmanager-0.24.0.linux-amd64/templates/email.tmpl'

#2、路由
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
#郵箱
receiver: 'email'

receivers:
- name: 'email'
email_configs:
## 接收警報的email(這里是引用模板文件中定義的變量)
- to: '{{ template "email.to"}}'
## 發(fā)送郵件的內(nèi)容(調(diào)用模板文件中的)
html: '{{ template "email.to.html" .}}'
send_resolved: true

# 抑制器配置
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
#確保這個配置下的標簽內(nèi)容相同才會抑制,也就是說警報中必須有這三個標簽值才會被抑制。
equal: ['alertname', 'dev', 'instance']

【2】模板 alert.tmpl

模板文件配置了email.from、email.to、email.to.html 三種模板變量,可以在 alertmanager.yml 文件中直接配置引用。這里 email.to.html 就是要發(fā)送的郵件內(nèi)容,支持 Html 和 Text 格式,這里為了顯示好看,采用 Html 格式簡單顯示信息。下邊 {{ range .Alerts }} 是個循環(huán)語法,用于循環(huán)獲取匹配的 Alerts 的信息。

{{ define "email.from" }}xxxxxxxx@qq.com{{ end }}
{{ define "email.to" }}xxxxxxxx@163.com{{ end }}
{{ define "email.to.html" }}
{{ range .Alerts }}
=========start==========

告警程序: prometheus_alert

告警級別: {{ .Labels.severity }} 級

告警類型: {{ .Labels.alertname }}

故障主機: {{ .Labels.instance }}

告警主題: {{ .Annotations.summary }}

告警詳情: {{ .Annotations.description }}

觸發(fā)時間: {{ .StartsAt.Format "2019-08-04 16:58:15" }}

=========end==========

{{ end }}
{{ end }}

【溫馨提示】這里記得換成自己的郵箱地址?。?!

重啟alertmanager

systemctl restart alertmanager

在web上就可以看到對應的告警信息了。

接下來就靜待告警了。


網(wǎng)站題目:【創(chuàng)新互聯(lián)】Prometheus自定義告警規(guī)則
當前路徑:http://www.dlmjj.cn/article/djieseh.html