Menu
Public repository / a8ff0f6c293e8e373c1ef695ede698e856d940fe
AkurAI-Monitor / scripts/titan-monitor-agent
Latest change b6107870452cfdfe4af6d140ebac6622434956ff - AKURAI-MONITOR-3 share collector metric contract by Ólafur Búi Ólafsson
#!/usr/bin/env bash
# Collection agent for the Titan workstation host.
#
# Samples the same metric namespace rust-monitor's own in-process collector
# uses (src/collectors.rs) and pushes each point to the central monitor's
# management API, attributed to this host's registered host_id. Run as a
# systemd oneshot on a timer (see titan-monitor-agent.service/.timer);
# each invocation is a single sample-and-push cycle.
#
# Config (env, normally supplied via /etc/titan-monitor-agent.env):
# MONITOR_BASE_URL default https://monitor.olibuijr.com
# MONITOR_API_TOKEN required — management bearer token (PassVault entry 25)
# MONITOR_HOST_ID required — this host's id in the `hosts` table
set -euo pipefail
# shellcheck source=scripts/metric-contract.sh
source "$(dirname "${BASH_SOURCE[0]}")/metric-contract.sh"
BASE_URL="${MONITOR_BASE_URL:-https://monitor.olibuijr.com}"
TOKEN="${MONITOR_API_TOKEN:?MONITOR_API_TOKEN is required}"
HOST_ID="${MONITOR_HOST_ID:?MONITOR_HOST_ID is required}"
push() {
local name="$1" value="$2"
curl -sf -m 5 -X POST "$BASE_URL/api/manage/metrics" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d "{\"host_id\":${HOST_ID},\"name\":\"${name}\",\"value\":${value}}" \
>/dev/null || echo "titan-monitor-agent: failed to push ${name}" >&2
}
# --- cpu.usage: percent busy over a 1s /proc/stat sample window ---
read_cpu_ticks() {
local line idle total field
read -r line < /proc/stat
# line: cpu user nice system idle iowait irq softirq steal guest guest_nice
local -a f=($line)
idle="${f[4]}"
total=0
for field in "${f[@]:1}"; do total=$((total + field)); done
echo "$idle $total"
}
read -r idle1 total1 <<<"$(read_cpu_ticks)"
sleep 1
read -r idle2 total2 <<<"$(read_cpu_ticks)"
idle_delta=$((idle2 - idle1))
total_delta=$((total2 - total1))
if [ "$total_delta" -gt 0 ]; then
cpu_usage=$(awk -v i="$idle_delta" -v t="$total_delta" 'BEGIN { printf "%.1f", (1 - i / t) * 100 }')
push "$METRIC_CPU_USAGE" "$cpu_usage"
fi
# --- memory ---
mem_total_kb=$(awk '/^MemTotal:/ {print $2}' /proc/meminfo)
mem_available_kb=$(awk '/^MemAvailable:/ {print $2}' /proc/meminfo)
mem_used_kb=$((mem_total_kb - mem_available_kb))
mem_used_pct=$(awk -v u="$mem_used_kb" -v t="$mem_total_kb" 'BEGIN { printf "%.1f", (t > 0) ? (u / t * 100) : 0 }')
push "$METRIC_MEM_TOTAL" "$mem_total_kb"
push "$METRIC_MEM_USED" "$mem_used_kb"
push "$METRIC_MEM_AVAILABLE" "$mem_available_kb"
push "$METRIC_MEM_USED_PCT" "$mem_used_pct"
# --- disk (root filesystem) ---
read -r _ disk_total_kb disk_used_kb _ disk_used_pct_raw _ < <(df -kP / | tail -n1)
disk_used_pct="${disk_used_pct_raw%\%}"
disk_total_gb=$(awk -v k="$disk_total_kb" 'BEGIN { printf "%.1f", k / 1048576 }')
disk_used_gb=$(awk -v k="$disk_used_kb" 'BEGIN { printf "%.1f", k / 1048576 }')
push "$METRIC_DISK_ROOT_TOTAL" "$disk_total_gb"
push "$METRIC_DISK_ROOT_USED" "$disk_used_gb"
push "$METRIC_DISK_ROOT_USED_PCT" "$disk_used_pct"
# --- load average ---
read -r load1 load5 load15 _ < /proc/loadavg
push "$METRIC_LOAD_1M" "$load1"
push "$METRIC_LOAD_5M" "$load5"
push "$METRIC_LOAD_15M" "$load15"
# --- uptime ---
read -r uptime_seconds _ < /proc/uptime
push "$METRIC_UPTIME" "${uptime_seconds%.*}"
# --- network, per physical-looking interface (delta over a 1s window) ---
read_net() {
while IFS=: read -r iface stats; do
iface="${iface// /}"
[ "$iface" = "lo" ] && continue
[[ "$iface" == veth* || "$iface" == docker* || "$iface" == br-* ]] && continue
read -r rx _ _ _ _ _ _ _ tx _ <<<"$stats"
printf '%s %s %s\n' "$iface" "$rx" "$tx"
done < <(tail -n +3 /proc/net/dev)
}
declare -A rx1 tx1 rx2 tx2
while read -r iface rx tx; do
rx1["$iface"]="$rx"
tx1["$iface"]="$tx"
done < <(read_net)
sleep 1
while read -r iface rx tx; do
rx2["$iface"]="$rx"
tx2["$iface"]="$tx"
done < <(read_net)
for iface in "${!rx2[@]}"; do
[ -n "${rx1[$iface]:-}" ] || continue
rx_delta=$((rx2[$iface] - rx1[$iface]))
tx_delta=$((tx2[$iface] - tx1[$iface]))
[ "$rx_delta" -lt 0 ] && rx_delta=0
[ "$tx_delta" -lt 0 ] && tx_delta=0
push "net.${iface}.${METRIC_NET_RX_SUFFIX}" "$rx_delta"
push "net.${iface}.${METRIC_NET_TX_SUFFIX}" "$tx_delta"
done