shell - 获取consul kv并写入至k8s alertmanager

2022-12-21
#!/bin/bash
basepath=$(cd $(dirname $0); pwd)
source ${basepath}/config.sh
source /etc/profile.d/consul.sh
source /etc/profile
function DownloadConsul() {
    consul_files=$(/usr/local/bin/consul kv get --recurse | grep 'alertManager.d' |awk -F ':' '{print $1}')
    for consul_file in ${consul_files}
    do
        echo $consul_file
        local_file=$(echo ${consul_file} | awk -F '/' '{print $NF}')
        /usr/local/bin/consul kv get ${consul_file} > ${temp_path}/${local_file}
    done
}

function UpdateK8s() {
    cat ${basepath}/alertmanager-demo-head ${temp_path}/alarm_*  ${basepath}/alertmanager-demo-foot > /tmp/alertmanager.yaml
    /usr/local/bin/kubectl --kubeconfig /root/.kube/all-config/pro -n monitoring create secret generic alertmanager-main --from-file=/tmp/alertmanager.yaml --dry-run -o yaml > /tmp/alertmanager-secret.yaml
    /usr/local/bin/kubectl --kubeconfig /root/.kube/all-config/pro -n monitoring apply -f /tmp/alertmanager-secret.yaml
}

temp_path="/tmp/alertmanagerrules.d"
mkdir -p ${temp_path}
cd ${temp_path}
rm -rf ${temp_path}/*

echo `date`
DownloadConsul
UpdateK8s

alertmanager-demo-head

global:
  # 在没有告警的情况下声明为已解决的时间
  resolve_timeout: 5m

route:
  # 告警聚合维度
  group_by: ['id', 'type', 'queue', 'instance', 'InstanceId', 'clusterId']
  # 收到告警,以分组维度等待15秒聚合,然后一起触发告警信息
  group_wait: 15s
  # 分组内容有变更,则发送告警的时间间隔
  group_interval: 1m
  # 分组内发送相同告警的时间间隔
  repeat_interval: 30m
  # 默认接收器
  receiver: 'default'
  routes:
    - receiver: 'ignore'
      group_wait: 15s
      match_re:
        severity: "2"

alertmanager-demo-foot

receivers:
- name: 'default'
  webhook_configs:
#  - url: 'http://127.0.0.1'
  - url: 'http://12000.pro.xx.corp:12000/alert/webHook/0'
    send_resolved: true

- name: 'ignore'
  webhook_configs:
  - url: 'http://127.0.0.1'

#抑制规则
inhibit_rules:
  - source_match:
      severity: '3'
    target_match:
      severity: '2'
    equal: ['id', 'type', 'queue', 'instance', 'InstanceId', 'clusterId']

consul kv demo

- receiver: 'default'
      repeat_interval: 5m
      match:
        id: 12692

标题:shell - 获取consul kv并写入至k8s alertmanager
地址:https://blog.njqhome.com:8443/articles/2021/07/05/1625474389326.html