Current File : //etc/node_exporter/prometheus_keepalive_pve
#!/bin/bash

if [ "$1" != "nosleep" ]; then
    sleep=$(which sleep)
    $sleep $((RANDOM % 18000));
fi

pgrep=$(which pgrep)

SERVICE=( "node_exporter" "nginx_exporter" "mysqld_exporter" "elasticsearch_exporter" "redis_exporter" )
for i in "${SERVICE[@]}"
do
    if [ -f "/usr/bin/$i" ]; then
            if $pgrep -x "$i" >/dev/null
            then
                echo "$i is running"
                curl -sS "https://prometheus-api.servers.pt/resolver/keepalive/$i/$(hostname --fqdn)" -H "Accept: application/json" -H "Authorization: Bearer ydanu9NWmyRxqfKGRPRgTEEKxgEMDYQu"
                echo ""
            fi
    fi
done


python_exec=$(whereis python | awk '{print $2}')

read_script()
{
  local name="${1//./[.]}"  # escape name for sed regex
  sed -En '/^#---=== '"$name"' ===---$/,$ {/^#---=== '"$name"' ===---$/ n; /^#---===/ q; p; }' "$0"
}


cat /var/log/prometheus_keepalive.log | grep -v "is running" | $python_exec -c "$(read_script format_output.py)" > /etc/node_exporter/text/prometheus_keepalive.prom
echo "prometheus_keepalive_last $(date +%s)" >> /etc/node_exporter/text/prometheus_keepalive.prom

exit 0

#---=== format_output.py ===---
import sys,json
lines = sys.stdin.read().splitlines()
for jsonObj in lines:
        obj=json.loads(jsonObj)
        print("prometheus_keepalive_code{service=\""+obj["data"]["service"]+"\"} "+str(obj["data"]["code"]))
#---===---