Skip to content

Instantly share code, notes, and snippets.

Created October 1, 2018 13:56
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
this is a sample script that clean old metrics from pushgateway, not very clean but working anyway, used as sample in a issue
trap 'echo "got sigterm" ; exit 0' SIGTERM
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="",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="",job="bk_jenkins"} 1.5383802210997093e+09' 'instance'
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)
metricInstance=$(extract_pushgateway_variable "$_line" 'instance')
metricJob=$(extract_pushgateway_variable "$_line" 'job')
echo "[INFO] job should be deleted $metricJob - $metricInstance age: $METRIC_AGE_SECONDS "
curl -s -X DELETE "$PGW_URL/metrics/job/${metricJob}/instance/${metricInstance}"
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"
sleep $((EXPIRATION_SECONDS / 3 ))
while : ; do
Copy link

ghostiam commented Feb 21, 2020

The function "convert_to_standardnotation" did not work on one of my servers, I fixed it like this:

function convert_to_standardnotation() {
    # convert number from scientific notation to standard( ie  '1.5383780136826127e+09' )
    echo $1 | awk '{printf("%.0f", $1)}'

Copy link

Thanks . It is worked as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment