pushgateway group清理
trap 'echo "got sigterm" ; exit 0' SIGTERM
EXPIRATION_SECONDS=${EXPIRATION_SECONDS:-600}
PGW_URL=${PGW_URL:-http://10.176.78.78:9091}
function convert_to_standardnotation(){
# convert number from scientific notation to standar d( ie '1.5383780136826127e+09' )
printf '%.0f' $1
}
function extract_pushgateway_variable(){
local -r _METRIC=$1
local -r _VARNAME=$2
#echo 'push_time_seconds{instance="10.32.32.7",job="bk_jenkins"} 1.5383802210997093e+09' | sed -r 's/.*instance="([^"]*).*/\1/g'
echo $_METRIC | sed -r "s/.*${_VARNAME}=\"([^\"]*).*/\\1/g"
# sample usage :
# extract_pushgateway_variable 'push_time_seconds{instance="10.32.32.7",job="bk_jenkins"} 1.5383802210997093e+09' 'instance'
}
function extract_pushgateway_uri(){
local -r _METRIC=$1
#echo 'push_time_seconds{instance="10.32.32.7",job="bk_jenkins"} 1.5383802210997093e+09' | awk -F{ '{print $2}' | awk -F} '{print $1}' | sed -e "s/[=,]/\//g" -e "s/\"//g"
metricJob=$(extract_pushgateway_variable "$_METRIC" 'job')
echo $_METRIC | awk -F{ '{print $2}' | awk -F} '{print $1}' | sed -e "s/[=,]/\//g" -e "s/\"//g" -e "s/job\///g" -e "s/${metricJob}\///g" -e "s/instance\/\///g"
# sample usage :
# extract_pushgateway_uri 'push_time_seconds{instance="10.32.32.7",job="bk_jenkins"} 1.5383802210997093e+09'
}
function check_metric_line(){
local -r _line=$1
METRIC_TIME=$(echo $_line | awk '{print $2}' )
#echo "mtime = $_line -> $METRIC_TIME "
METRIC_TIME=$(convert_to_standardnotation $METRIC_TIME)
#echo "$CURRENT_TIME - $METRIC_TIME "
METRIC_AGE_SECONDS=$((CURRENT_TIME-METRIC_TIME))
if [ "$METRIC_AGE_SECONDS" -gt "$EXPIRATION_SECONDS" ]; then
metricJob=$(extract_pushgateway_variable "$_line" 'job')
metricUri=$(extract_pushgateway_uri "$_line")
echo "[INFO] job should be deleted $metricUri age: $METRIC_AGE_SECONDS "
curl -s -X DELETE "$PGW_URL/metrics/job/${metricJob}/${metricUri}"
fi
}
function check_expired_metric_loop(){
export CURRENT_TIME=$(date +%s)
METRICS_LIST=$(curl -s $PGW_URL/metrics | egrep "^push_time_seconds")
echo "$METRICS_LIST" | while read -r line || [[ -n "$line" ]]; do
check_metric_line "$line"
done
sleep $((EXPIRATION_SECONDS / 3 ))
}
while : ; do
check_expired_metric_loop
done