#!/bin/bash

API_URL="${API_URL:-https://cascirocco.bio/api/metrics/push}"
NODE_NAME="raspberry_pi"
NODE_TOKEN="${NODE_METRICS_TOKEN:-your-secure-node-token-here}"

if [ -f /sys/class/thermal/thermal_zone0/temp ]; then
    TEMP_RAW=$(cat /sys/class/thermal/thermal_zone0/temp)
    CPU_TEMP=$(awk "BEGIN {print $TEMP_RAW / 1000}")
else
    CPU_TEMP=40.0
fi

RAM_TOTAL=$(free -b | awk '/Mem:/ {print $2}')
RAM_USED=$(free -b | awk '/Mem:/ {print $3}')
RAM_FREE=$(free -b | awk '/Mem:/ {print $4}')

DISK_TOTAL=$(df -B1 / | awk 'NR==2 {print $2}')
DISK_USED=$(df -B1 / | awk 'NR==2 {print $3}')
DISK_FREE=$(df -B1 / | awk 'NR==2 {print $4}')

CPU_USAGE='["12.5", "14.2", "10.8", "15.1"]'

curl -s -X POST "$API_URL" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "X-Node-Token: $NODE_TOKEN" \
  -d "{
    \"node_name\": \"$NODE_NAME\",
    \"cpu_temp\": $CPU_TEMP,
    \"cpu_usage\": $CPU_USAGE,
    \"ram_total\": $RAM_TOTAL,
    \"ram_used\": $RAM_USED,
    \"ram_free\": $RAM_FREE,
    \"disk_total\": $DISK_TOTAL,
    \"disk_used\": $DISK_USED,
    \"disk_free\": $DISK_FREE
  }" > /dev/null 2>&1
