{{ define "default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts.Firing -}}
{{- if eq $index 0 -}}
Detail: {{ $alert.Labels.alertname }}
# Here I'm going to zero $alert
{{- end }}
{{- end }}
{{- end }}
{{- end }}你好,我想对.Alerts.Firing进行零化,这也是迭代之后的$alert。我刚来金刚,请帮帮忙,谢谢。
补充:实际上它是Prometheus警报的模板。原始日期由alertmanager创建并发送给receiver(微信)。这是微信的模板
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts.Firing -}}
{{- if eq $index 0 -}}
Detail: {{ $alert.Labels.alertname }}
{{- end }}
---- Problem started ------
Message: {{ $alert.Annotations.description }}
Time: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
{{ if gt (len $alert.Labels.instance) 0 }}ip: {{ $alert.Labels.instance }};{{- end }}
{{- if gt (len $alert.Labels.namespace) 0 }}namespace: {{ $alert.Labels.namespace }};{{- end }}
{{- if gt (len $alert.Labels.node) 0 }}Node: {{ $alert.Labels.node }};{{- end }}
{{- if gt (len $alert.Labels.pod_name) 0 }}Pod: {{ $alert.Labels.pod_name }}{{- end }}
-----------------
{{ $alert :="" }}
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts.Resolved -}}
{{- if eq $index 0 }}
!!!! Problem resolved !!!!!
Detail: {{ $alert.Labels.alertname }}
{{- end }}
Started_at: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
Resolved_at: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
-------
{{ if gt (len $alert.Labels.instance) 0 -}}ip: {{ $alert.Labels.instance }};{{- end }}
{{- if gt (len $alert.Labels.namespace) 0 -}}namespace: {{ $alert.Labels.namespace }};{{- end }}
{{- if gt (len $alert.Labels.node) 0 -}}Node: {{ $alert.Labels.node }};{{- end }}
{{- if gt (len $alert.Labels.pod_name) 0 -}}Pod: {{ $alert.Labels.pod_name }}{{- end }}
{{ $alert :="" }}
-----------------
{{- end }}
{{- end }}
{{- end }}因此,很难看到alertmanager发送的原始数据。我只能看到的是这个模板所呈现的结果。请看下面的内容。
################# The first alert #########
##### As we can see 232.20 is in the state of 'resolved' ###
Detail: Node is down
---- Problem started ------
Message: Node is down
VALUE = 0
LABELS = 172.16.232.18:9100
Time: 2022-06-02 14:47:51
ip: 172.16.232.18:9100;
-----------------
!!!! Problem resolved !!!!!
Detail: Node is down
Started_at: 2022-06-02 14:47:21
Resolved_at: 2022-06-02 14:47:21
-------
ip: 172.16.232.20:9100;
-----------------
##################### The second alert #######
####### 232.20 is still in the state of resolved, it seems it's duplicated, that's why I'm going to zero it ####
!!!! Problem resolved !!!!!
Detail: Node is down
Started_at: 2022-06-02 14:47:51
Resolved_at: 2022-06-02 14:47:51
-------
ip: 172.16.232.18:9100;
-----------------
Started_at: 2022-06-02 14:47:21
Resolved_at: 2022-06-02 14:47:21
-------
ip: 172.16.232.20:9100;
-----------------补充:
[FIRING:1] Node is down (powerdns prod node_exporter critical)
instance is down.
Alerts Firing:
Labels:
- alertname = Node is down
- app = powerdns
- env = prod
- instance = 172.16.232.18:9100
- job = node_exporter
- severity = critical
Annotations:
- description = Node is down
VALUE = 0
LABELS = 172.16.232.18:9100
- summary = instance is down.
Source: http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
Alerts Resolved:
Labels:
- alertname = Node is down
- app = powerdns
- env = prod
- instance = 172.16.232.20:9100
- job = node_exporter
- severity = critical
Annotations:
- description = Node is down
VALUE = 0
LABELS = 172.16.232.20:9100
- summary = instance is down.
Source: http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
AlertmanagerUrl:
http://alertmgr.pacific-textiles.com/#/alerts?receiver=wechat和
[RESOLVED] Node is down (powerdns prod node_exporter critical)
instance is down.
Alerts Resolved:
Labels:
- alertname = Node is down
- app = powerdns
- env = prod
- instance = 172.16.232.18:9100
- job = node_exporter
- severity = critical
Annotations:
- description = Node is down
VALUE = 0
LABELS = 172.16.232.18:9100
- summary = instance is down.
Source: http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
Labels:
- alertname = Node is down
- app = powerdns
- env = prod
- instance = 172.16.232.20:9100
- job = node_exporter
- severity = critical
Annotations:
- description = Node is down
VALUE = 0
LABELS = 172.16.232.20:9100
- summary = instance is down.
Source: http://pt-prometheus-server-85944ccff6-qvtc4:9090/graph?g0.expr=up%7Bjob%3D%22node_exporter%22%7D+%3D%3D+0&g0.tab=1
AlertmanagerUrl:
http://alertmgr.pacific-textiles.com/#/alerts?receiver=wechat发布于 2022-06-02 09:41:29
只要释放它,就不能用零来解析一个对象。因为:
。
谢谢。
https://stackoverflow.com/questions/72471955
复制相似问题