الملفات
Building-API-and-Monitoring…/log_monitor.sh
ahmedgamalyousef efa5d907c9 Updating Files
2025-10-01 00:05:36 +03:00

33 أسطر
850 B
Bash
ملف تنفيذي

#!/usr/bin/bash
LOG_FILE="fruit_api.log"
# ALERT_URL="https://gahymah.systems.com"
tail -Fn0 "$LOG_FILE" | while read line; do
if echo "$line" | grep -qiE "STATUS:(4[0-9]{2}|5[0-9]{2})|failed|timeout|error|exception"; then
echo "🚨 Error detected: $line"
IP=$(hostname -I | awk '{print $1}')
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2"%"}')
RAM=$(free -m | awk '/Mem:/ {print $4"MB"}')
DISK=$(df -h / | awk 'NR==2 {print $4}')
payload=$(cat <<EOF
{
"error": "$line",
"timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
"message": "Error detected in Fruit API logs",
"server_metrics": {
"ip": "$IP",
"cpu_usage": "$CPU",
"ram_available": "$RAM",
"disk_space": "$DISK"
}
}
EOF
)
echo "$payload"
curl -s -X POST -H "Content-Type: application/json" -d "$payload" "$ALERT_URL"
fi
done