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