#!/bin/sh

INTERVAL=60 # How often to push metrics (in seconds)
TMP_FILE="/tmp/metrics.txt"
HOSTNAME=$(uci get system.@system[0].hostname)
PUSHHOST=$(uci get prometheus-push.server.host)
PUSHPORT=$(uci get prometheus-push.server.port)
PUSHPATH=$(uci get prometheus-push.server.path)
URL="http://${PUSHHOST}:${PUSHPORT}${PUSHPATH}${HOSTNAME}"

while true; do
    # execute the ucode node exporter and save to /tmp
    prometheus-node-exporter-ucode | tail -n +4 > "$TMP_FILE"

    # upload to pushgateway
    wget -q -O - \
        --header="Content-Type: application/x-www-form-urlencoded" \
        --post-file="$TMP_FILE" \
        "$URL"

    # start background process to gather ping stats
    /usr/bin/pingbot &

    sleep "$INTERVAL"
done
