我使用 Alertmanager 进行通知,默认情况下alertmanager
只有一个 Webhook。根据我的要求,还需要一个 webhook。
现有配置
global:
resolve_timeout: 5m
route:
group_by: ['alertname']
group_wait: {{ getsdcv "system" "group_wait" "10s"}}
group_interval: {{ getsdcv "system" "group_interval" "10s"}}
{{- $repeat_interval := getsdcv "system" "repeat_interval" "" }}
{{- if not (eq $repeat_interval "") }}
repeat_interval: {{ $repeat_interval }}
{{- end }}
receiver: 'web.hook'
receivers:
- name: 'web.hook'
webhook_configs:
- url: 'http://{{ getsdcv "system" "ip" "localhost"}}:{{ getsdcv "system" "port" "8005"}}/'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
添加新的 Webhook 后
global:
resolve_timeout: 5m
route:
group_by: ['alertname']
group_wait: {{ getsdcv "system" "group_wait" "10s"}}
group_interval: {{ getsdcv "system" "group_interval" "10s"}}
{{- $repeat_interval := getsdcv "system" "repeat_interval" "" }}
{{- if not (eq $repeat_interval "") }}
repeat_interval: {{ $repeat_interval }}
{{- end }}
receiver: 'web.hook'
routes:
- receiver: "web.hook"
continue: true
- receiver: "web.hook1"
match:
alertname: BusinessKpiDown
continue: true
receivers:
- name: 'web.hook'
webhook_configs:
- url: 'http://{{ getsdcv "system" "ip" "localhost"}}:{{ getsdcv "system" "port" "8005"}}/'
- name: 'web.hook1'
webhook_configs:
- url: 'http://0.0.0.0:8010/hooks/my-webhook'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
我尝试通过 shell 脚本执行此操作,但失败了
awk -v RS='^$' '{$0=gensub(/(receivers:\s+users:)(\s+)/,"\\1\\2- name: 'web.hook2'\\2",1)}1'
但无法附加新的 webhook。
答案1
注意:这个答案适用于一个非常不同的输入文档,它看起来像
global:
resolve_timeout: 5m
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 12h
receiver: 'web.hook'
receivers:
- name: 'web.hook'
webhook_configs:
- url: ''
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
该文档现已在问题中进行了修改,因此它不再是有效的 YAML 文件,因此不幸的是,该答案不能按原样使用。我把它留在这里是为了那些可能觉得它有趣的人的利益。
您似乎想要将以下条目添加到receivers
数组中:
- name: web.hook2
webhook_configs:
- url: ''
...然后还有
- continue: true
到每个receivers
元素的webhook_configs
部分。
我们可以这样做yq
我们可以用from 来https://kislyuk.github.io/yq/:
yq -y '
.receivers += [{"name":"web.hook2","webhook_configs":[{"url":""}]}] |
.receivers |= map(.webhook_configs += [{"continue":true}])' file.yaml
它使用yq
,它是 JSON 解析器的 YAML 感知包装器jq
,将所需部分添加到数组中,然后向数组中的条目receivers
添加额外的键值。webhook_configs
receivers
如果您想在命令行上传递新 Web 挂钩的名称及其 URL,请不要将其作为带有 shell 变量的字符串注入到表达式中yq
,而是正确创建一个yq
变量(允许yq
安全地对值进行编码)如果需要的话):
myname='web.hook2'
myurl=''
yq -y --arg name "$myname" --arg url "$myurl" '
.receivers += [{"name":$name,"webhook_configs":[{"url":$url}]}] |
.receivers |= map(.webhook_configs += [{"continue":true}])' file.yaml
yq
-i
还支持使用其或--in-place
选项进行“就地”编辑。
结果给出了问题中的示例:
global:
resolve_timeout: 5m
route:
group_by:
- alertname
group_wait: 10s
group_interval: 10s
repeat_interval: 12h
receiver: web.hook
receivers:
- name: web.hook
webhook_configs:
- url: ''
- continue: true
- name: web.hook2
webhook_configs:
- url: ''
- continue: true
inhibit_rules:
- source_match:
severity: critical
target_match:
severity: warning
equal:
- alertname
- dev
- instance
如果您使用-Y
in 代替运行-y
,您将停止yq
整理混合标记:
global:
resolve_timeout: 5m
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 12h
receiver: 'web.hook'
receivers:
- name: 'web.hook'
webhook_configs:
- url: ''
- continue: true
- name: web.hook2
webhook_configs:
- url: ''
- continue: true
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
--indentless-lists
您还可以通过提供选项来删除列表上的缩进,就像在原始文档中一样yq
。
由于yq
是 的包装器jq
,因此该yq
表达式处理已转换为 JSON 的文档。然后该文档由 翻译回 YAML yq
。作为参考,以下是原始文档的 JSON 变体,由表达式修改yq
:
{
"global": {
"resolve_timeout": "5m"
},
"route": {
"group_by": [
"alertname"
],
"group_wait": "10s",
"group_interval": "10s",
"repeat_interval": "12h",
"receiver": "web.hook"
},
"receivers": [
{
"name": "web.hook",
"webhook_configs": [
{
"url": ""
}
]
}
],
"inhibit_rules": [
{
"source_match": {
"severity": "critical"
},
"target_match": {
"severity": "warning"
},
"equal": [
"alertname",
"dev",
"instance"
]
}
]
}
答案2
实际上有两个受欢迎的yq
项目,唯一的那个@Kusalananda 精选和麦克法拉的 yq
。以下是如何对第二个(yq 版本 4.11.2)执行相同的操作:
yq e -P '
.receivers += [{"name":"web.hook2", "webhook_configs": [{"url":""}]}] |
.receivers[].webhook_configs += [{"continue":true}]
' infile.yaml
交换机-P
漂亮地打印 yaml。输出:
global:
resolve_timeout: 5m
route:
group_by:
- alertname
group_wait: 10s
group_interval: 10s
repeat_interval: 12h
receiver: web.hook
receivers:
- name: web.hook
webhook_configs:
- url: ""
- continue: true
- name: web.hook2
webhook_configs:
- url: ""
- continue: true
inhibit_rules:
- source_match:
severity: critical
target_match:
severity: warning
equal:
- alertname
- dev
- instance