edited monitor script
هذا الالتزام موجود في:
44
monitor.sh
44
monitor.sh
@@ -2,7 +2,7 @@
|
||||
|
||||
# Configuration
|
||||
LOG_FILE="api.log"
|
||||
GHAYMAH_URL="GHAYMAH_ENDPOINT_HERE" # don't forget to edit it
|
||||
GHAYMAH_URL="YOUR_GHAYMAH_ENDPOINT_HERE" # Will be provided later
|
||||
|
||||
echo "Starting log monitor..."
|
||||
echo "Watching file: $LOG_FILE"
|
||||
@@ -15,17 +15,35 @@ fi
|
||||
|
||||
# Function to get server info
|
||||
get_server_info() {
|
||||
# Get IP address
|
||||
IP=$(hostname -I | awk '{print $1}')
|
||||
# Get IP address - try different methods
|
||||
if command -v hostname &> /dev/null; then
|
||||
IP=$(hostname -I | awk '{print $1}')
|
||||
elif command -v ip &> /dev/null; then
|
||||
IP=$(ip addr show | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | cut -d'/' -f1 | head -n1)
|
||||
else
|
||||
IP="127.0.0.1"
|
||||
fi
|
||||
|
||||
# Get CPU usage
|
||||
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
|
||||
if command -v top &> /dev/null; then
|
||||
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
|
||||
else
|
||||
CPU="N/A"
|
||||
fi
|
||||
|
||||
# Get available RAM
|
||||
RAM=$(free -h | grep Mem | awk '{print $7}')
|
||||
if command -v free &> /dev/null; then
|
||||
RAM=$(free -h | grep Mem | awk '{print $7}')
|
||||
else
|
||||
RAM="N/A"
|
||||
fi
|
||||
|
||||
# Get available disk space
|
||||
DISK=$(df -h / | tail -1 | awk '{print $4}')
|
||||
if command -v df &> /dev/null; then
|
||||
DISK=$(df -h / | tail -1 | awk '{print $4}')
|
||||
else
|
||||
DISK="N/A"
|
||||
fi
|
||||
|
||||
echo "$IP|$CPU|$RAM|$DISK"
|
||||
}
|
||||
@@ -58,11 +76,14 @@ send_alert() {
|
||||
EOF
|
||||
)
|
||||
|
||||
echo "Alert detected: $ERROR_MSG"
|
||||
echo "=========================================="
|
||||
echo "⚠️ ALERT DETECTED!"
|
||||
echo "Error: $ERROR_MSG"
|
||||
echo "Server IP: $IP | CPU: ${CPU}% | RAM: $RAM | Disk: $DISK"
|
||||
echo "=========================================="
|
||||
|
||||
# Send to Ghaymah endpoint
|
||||
# do not Uncomment without the endpoint
|
||||
# Uncomment when endpoint URL is provided
|
||||
# curl -X POST "$GHAYMAH_URL" \
|
||||
# -H "Content-Type: application/json" \
|
||||
# -d "$JSON_PAYLOAD"
|
||||
@@ -70,14 +91,15 @@ EOF
|
||||
# For now, just save to alert file
|
||||
echo "$JSON_PAYLOAD" >> alerts.log
|
||||
echo "Alert saved to alerts.log"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Monitor log file continuously
|
||||
# Only trigger on actual ERROR lines with 4XX or 5XX codes
|
||||
tail -f "$LOG_FILE" | while read line
|
||||
do
|
||||
# Check for error patterns
|
||||
if echo "$line" | grep -E "(ERROR|400|404|500|failed|timeout)" > /dev/null; then
|
||||
echo "Error found: $line"
|
||||
# Only check lines that have ERROR level AND contain error codes
|
||||
if echo "$line" | grep "ERROR" | grep -E "(400|404|500|503|failed|timeout)" > /dev/null; then
|
||||
send_alert "$line"
|
||||
fi
|
||||
done
|
||||
|
المرجع في مشكلة جديدة
حظر مستخدم