shell - 获取consul kv并写入k8s prometheusRule(全局)

2021-07-05
#!/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 '\---' |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} > ${PROMETHEUS_RULE_PATH}/${local_file} consul_temp_file=$(echo ${consul_file} | awk -F '/' '{print $NF}') ls ${PROMETHEUS_RULE_PATH} | grep ${consul_temp_file} done } function RsyncNas() { rsync -av ${PROMETHEUS_RULE_PATH}/* ${PROMETHEUS_RULE_PATH}/ files=$(diff /tmp/new /tmp/old | grep '>' | awk '{print $NF}') for file in ${files} do rm ${PROMETHEUS_RULE_PATH}/${file} done } 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 echo `date` /usr/local/bin/kubectl --kubeconfig /root/.kube/all-config/pro apply -f ${temp_k8s_rule_path}/rules-${sid}.yml } function DeleteRule() { /usr/local/bin/kubectl --kubeconfig /root/.kube/all-config/pro -n monitoring delete prometheusrules.monitoring.coreos.com rules-${sid} } function UpdateK8s() { ls -l ${PROMETHEUS_RULE_PATH} | grep yml | awk '{print $NF}' | sort > /tmp/new /usr/local/bin/kubectl --kubeconfig /root/.kube/all-config/pro -n monitoring get prometheusrule | grep rules | awk '{print $1}' |awk -F 'rules-' '{print $2".yml"}' | sort > /tmp/old files=$(diff /tmp/new /tmp/old | grep '<' | awk '{print $NF}') for file in ${files} do sid=$(echo ${file} | awk -F '.yml' '{print $1}') if [[ ${sid} == "341" || ${sid} == "1131" || ${sid} == "347" ]];then echo "ignore" else CreateRule fi done files=$(diff /tmp/new /tmp/old | grep '>' | awk '{print $NF}') for file in ${files} do sid=$(echo ${file} | awk -F '.yml' '{print $1}') # DeleteRule done } PROMETHEUS_RULE_PATH="/opt/prometheus-rules.d" temp_k8s_rule_path="/tmp/k8s-rules.d" mkdir -p ${PROMETHEUS_RULE_PATH} ${temp_k8s_rule_path} cd ${PROMETHEUS_RULE_PATH} rm -rf ${PROMETHEUS_RULE_PATH}/* echo `date` DownloadConsul RsyncNas cd ${basepath} UpdateK8s

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