0
Fork 0
mirror of https://github.com/netweak/agent.git synced 2025-01-13 22:40:28 -05:00
netweak-agent/heartbeat.sh

47 lines
1.2 KiB
Bash
Raw Normal View History

2024-01-11 11:45:35 +01:00
#!/bin/bash
# Set env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2024-11-03 04:54:25 +01:00
# Get the directory name
2024-11-03 05:12:48 +01:00
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
NETWEAK="$(basename "$SCRIPT_DIR")"
2024-11-03 04:54:25 +01:00
2024-01-11 11:45:35 +01:00
# API Token
2024-11-03 04:54:25 +01:00
if [ -f "/etc/$NETWEAK/token.conf" ]; then
auth=($(cat "/etc/$NETWEAK/token.conf"))
2024-01-11 11:45:35 +01:00
else
2024-11-03 04:54:25 +01:00
echo "Error: File /etc/$NETWEAK/token.conf is missing."
2024-01-11 11:45:35 +01:00
exit 1
fi
2024-11-03 04:54:25 +01:00
# Get endpoint
if [ -f "/etc/$NETWEAK/endpoint.conf" ]; then
ENDPOINT=$(cat "/etc/$NETWEAK/endpoint.conf")
else
ENDPOINT="https://api.netweak.com"
fi
2024-01-11 11:45:35 +01:00
# Build data for post
data_post="token=${auth[0]}"
# API request with automatic termination
2024-11-03 04:54:25 +01:00
if [ -n "$(command -v timeout)" ]; then
timeout -s SIGKILL 30 wget -q -o /dev/null -O "/etc/$NETWEAK/log/agent.log" -T 25 --post-data "$data_post" --no-check-certificate "$ENDPOINT/agent/heartbeat"
2024-01-11 11:45:35 +01:00
else
2024-11-03 04:54:25 +01:00
wget -q -o /dev/null -O "/etc/$NETWEAK/log/agent.log" -T 25 --post-data "$data_post" --no-check-certificate "$ENDPOINT/agent/heartbeat"
2024-01-11 11:45:35 +01:00
wget_pid=$!
wget_counter=0
wget_timeout=30
2024-11-03 04:54:25 +01:00
while kill -0 "$wget_pid" && ((wget_counter < wget_timeout)); do
sleep 1
((wget_counter++))
2024-01-11 11:45:35 +01:00
done
kill -0 "$wget_pid" && kill -s SIGKILL "$wget_pid"
fi
# Finished
exit 1