الملفات
Building-API-and-Monitoring…/log_monitor.sh
ahmedgamalyousef f95728213f
نجحت جميع الفحوصات
CI - Fruit API / build-and-test (push) Successful in 41s
Updating Scripts and CI Workflow
2025-10-04 22:36:26 +03:00

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

#!/usr/bin/bash
LOG_FILE="fruit_api.log"
ALERT_URL=" " # Here Any Endpoint
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 Ahmed Gamal's 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