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号