#!/bin/bash
# GEP MCP Tool: System Health Check
#
# Description: Reads system health metrics (CPU, memory, disk, uptime)
# GEP Profile: Low entropy (0.15), read-only, predictable output
# Risk Level: 1 (minimal)
#
# Input: JSON on stdin {"verbose": true|false}
# Output: JSON with system metrics
set -euo pipefail
# Read JSON from stdin
INPUT=$(cat)
VERBOSE=$(echo "$INPUT" | jq -r '.verbose // "false"')
# Get uptime
UPTIME=$(uptime -p 2>/dev/null || echo "unknown")
# Get CPU usage
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//' 2>/dev/null || echo "0.0")
# Get memory usage
MEM_TOTAL=$(free -m | grep Mem | awk '{print $2}')
MEM_USED=$(free -m | grep Mem | awk '{print $3}')
MEM_PERCENT=$(awk "BEGIN {printf \"%.1f\", ($MEM_USED/$MEM_TOTAL)*100}")
# Get disk usage
DISK_USAGE=$(df -h / | tail -1 | awk '{print $5}' | sed 's/%//')
# Get load average
LOAD_AVG=$(uptime | awk -F'load average:' '{print $2}' | xargs)
# Build JSON response
if [ "$VERBOSE" = "true" ]; then
cat <<EOF
{
"status": "healthy",
"timestamp": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
"uptime": "$UPTIME",
"cpu": {
"usage_percent": $CPU_USAGE
},
"memory": {
"total_mb": $MEM_TOTAL,
"used_mb": $MEM_USED,
"usage_percent": $MEM_PERCENT
},
"disk": {
"root_usage_percent": $DISK_USAGE
},
"load_average": "$LOAD_AVG"
}
EOF
else
cat <<EOF
{
"status": "healthy",
"cpu_percent": $CPU_USAGE,
"mem_percent": $MEM_PERCENT,
"disk_percent": $DISK_USAGE
}
EOF
fi