shell - 获取consul kv并写入k8s prometheusRule

2021-07-05
#!/bin/bash
basepath=$(cd $(dirname $0); pwd)
source ${basepath}/config.sh

function CreateRule() {
    rule_file=${PROMETHEUS_RULE_PATH}/${sid}.yml
    sed -i '/---/d' ${rule_file}
    sed -i 's/^/  &/g' ${rule_file}
    sed -i 's/severity: 3/severity: "3"/g' ${rule_file}
    sed -i "s/id: ${sid}/id: \"${sid}\"/g" ${rule_file}
    cat k8s-rule-demo ${rule_file} >  ${temp_k8s_rule_path}/rules-${sid}.yml
    sed -i "s/__ID__/${sid}/g" ${temp_k8s_rule_path}/rules-${sid}.yml
    kubectl --kubeconfig /root/.kube/all-config/pro apply -f ${temp_k8s_rule_path}/rules-${sid}.yml
}

function DeleteRule() {
    kubectl --kubeconfig /root/.kube/all-config/pro -n monitoring delete prometheusrules.monitoring.coreos.com  rules-${sid}
}

action=$1
sid=$2

cd ${basepath}
temp_k8s_rule_path="/tmp/rules-k8s"
mkdir -p ${temp_k8s_rule_path}

if [[ ${action} == "CREATE" ]];then
    CreateRule
else
    DeleteRule
fi

consul kv demo

---
groups:
- name: "CPU使用率(192.168.10.151)-10128"
  rules:
  - alert: "CPU使用率(192.168.10.151)-10128-alert"
    expr: "round(100 - (avg without (cpu)(irate(node_cpu_seconds_total{instance=\"\
      192.168.10.151:9100\", mode=\"idle\"}[5m])) * 100)) > 85.00"
    for: "2m"
    labels:
      type: "ecs"
      severity: 3
      desc: "CPU使用率(192.168.10.151)"
      id: 10128
    annotations:
      for: "2m"
      dashboard_url: "https://grafana.yunquna.com/d/9CWBz0bik/ecs-da-pan-linux?orgId=1&from=now-1h&to=now&var-dpt=All&var-appid=9703&var-job=node_exporter&var-env=pro&var-instance={{\
        \ $labels.instance }}&var-device=All&var-temp_instance=All"
      summary: "ECS 9703  192.168.10.151 CPU使用率 > 85.00%"
      description: "当前:{{$value}}%"


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