#!/bin/bash
# Status Panel Agent
# 用法: ./agent.sh <服务器地址> <密钥>

SERVER="${1:-http://your-server:3800}"
SECRET="$2"

[ -z "$SECRET" ] && echo "Usage: $0 <server> <secret>" && exit 1

while true; do
  # CPU
  cpu=$(awk '/cpu /{printf "%.1f", 100-$8}' /proc/stat 2>/dev/null || echo "0")
  
  # Memory
  read mem_used mem_total <<< $(awk '/MemAvailable/{avail=$2} /MemTotal/{total=$2} END{printf "%d %d", total-avail, total}' /proc/meminfo)
  
  # Disk
  read disk_used disk_total <<< $(df -B1 / | awk 'NR==2{print $3" "$2}')
  
  # Network
  read net_in net_out <<< $(awk '/eth0|ens|enp/{print $2" "$10}' /proc/net/dev | head -1)
  
  # Load
  read load1 load5 load15 <<< $(awk '{print $1" "$2" "$3}' /proc/loadavg)
  
  # Uptime
  uptime=$(awk '{print int($1)}' /proc/uptime)
  
  curl -s -X POST "$SERVER/report" \
    -H "Content-Type: application/json" \
    -d "{
      \"secret\": \"$SECRET\",
      \"cpu\": $cpu,
      \"mem_used\": $mem_used,
      \"mem_total\": $mem_total,
      \"disk_used\": $disk_used,
      \"disk_total\": $disk_total,
      \"net_in\": $net_in,
      \"net_out\": $net_out,
      \"uptime\": $uptime,
      \"load1\": $load1,
      \"load5\": $load5,
      \"load15\": $load15
    }" > /dev/null 2>&1
  
  sleep 30
done
