pushgateway定時刪除過期數據
1.pushgateway:1.9的數據是永久保存的,不會自動刪除,除非調用接口或者在頁面手動刪除
2.使用腳本刪除
#####根據pushgateway頁面返回的數據中在last_push_time來判斷數據是否過期,如果過期則按照job和instance進行刪除##### #時間差(單位:秒),超過這個時間差則認為是過期數據 time_interval=120 #pushgateway地址 url=197.166.254.146:9091 current_time=$(date "+%Y-%m-%d %H:%M:%S") echo "----------------------$current_time------------------------" content=`curl -s $url | grep -A 30 "push_time_seconds" | grep "pushed\|instance\|job"` IFS=$'\n' for line in $content do if [[ ${line} == *pushed* ]] then last_push_time=${line#*: } last_push_time=${last_push_time%%&#*} last_push_time=${last_push_time/T/ } echo "last_push_time: $last_push_time" fi if [[ ${line} == *\>instance* ]] then instance=${line#*instance=\"} instance=${instance%%\"*} echo "instance: $instance" fi if [[ ${line} == *job* ]] then job=${line#*job=\"} job=${job%%\"*} echo "job: $job" if [ "$instance" != "" ] then current_time=$(date "+%Y-%m-%d %H:%M:%S") #echo $current_time sec1=$(( $(date -d "$current_time" +%s%N)/1000000000 )) sec2=$(( $(date -d "$last_push_time" +%s%N)/1000000000 )) diff_time=$(( sec1 - sec2 )) echo "時間差為:$diff_time 秒" if (( $diff_time > $time_interval )) then echo "時間間隔大于$time_interval秒!" echo "開始刪除指標..." curl -X DELETE $url/metrics/job/$job/instance/$instance fi echo "------------------------------------------" fi fi done
3.k8s定時任務
apiVersion: batch/v1
kind: CronJob
metadata:
name: pushgateway-cj
namespace: pushgateway
spec:
schedule: "*/1 * * * *"
successfulJobsHistoryLimit: 1
failedJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
containers:
- name: pushgateway-cj
image: image_pushgateway
imagePullPolicy: Always
command:
- /bin/sh
- -c
- sh /apps/pushgateway-delete-data.sh
volumeMounts:
- mountPath: /apps/
name: pushgateway-delete-data-config
volumes:
- configMap:
defaultMode: 420
name: pushgateway-delete-data-config
name: pushgateway-delete-data-config
restartPolicy: OnFailure
---
apiVersion: v1
data:
pushgateway-delete-data.sh: |-
#####根據pushgateway頁面返回的數據中在last_push_time來判斷數據是否過期,如果過期則按照job和instance進行刪除#####
#時間差(單位:秒),超過這個時間差則認為是過期數據
time_interval=120
#pushgateway地址
url=197.166.254.146:9091
current_time=$(date "+%Y-%m-%d %H:%M:%S")
echo "----------------------$current_time------------------------"
content=`curl -s $url | grep -A 30 "push_time_seconds" | grep "pushed\|instance\|job"`
IFS=$'\n'
for line in $content
do
if [[ ${line} == *pushed* ]]
then
last_push_time=${line#*: }
last_push_time=${last_push_time%%&#*}
last_push_time=${last_push_time/T/ }
echo "last_push_time: $last_push_time"
fi
if [[ ${line} == *\>instance* ]]
then
instance=${line#*instance=\"}
instance=${instance%%\"*}
echo "instance: $instance"
fi
if [[ ${line} == *job* ]]
then
job=${line#*job=\"}
job=${job%%\"*}
echo "job: $job"
if [ "$instance" != "" ]
then
current_time=$(date "+%Y-%m-%d %H:%M:%S")
#echo $current_time
sec1=$(( $(date -d "$current_time" +%s%N)/1000000000 ))
sec2=$(( $(date -d "$last_push_time" +%s%N)/1000000000 ))
diff_time=$(( sec1 - sec2 ))
echo "時間差為:$diff_time 秒"
if (( $diff_time > $time_interval ))
then
echo "時間間隔大于$time_interval秒!"
echo "開始刪除指標..."
curl -X DELETE $url/metrics/job/$job/instance/$instance
fi
echo "------------------------------------------"
fi
fi
done
kind: ConfigMap
metadata:
name: pushgateway-delete-data-config
namespace: pushgateway

浙公網安備 33010602011771號