63 أسطر
1.7 KiB
Bash
63 أسطر
1.7 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
LOG_FILE="Logs/api.log"
|
|
GHAYMAH_ENDPOINT_URL="http://hosted.ghaymah.systems"
|
|
|
|
# Function to get server metrics
|
|
get_server_metrics() {
|
|
# Get IP address
|
|
IP=$(hostname -I | awk '{print $1}')
|
|
|
|
# Get CPU usage
|
|
CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}')
|
|
|
|
# Get RAM available
|
|
RAM_AVAILABLE=$(free -h | grep Mem | awk '{print $7}')
|
|
|
|
# Get disk space
|
|
DISK_SPACE=$(df -h / | awk 'NR==2 {print $4}')
|
|
|
|
# Return JSON formatted metrics
|
|
echo "{
|
|
\"ip\": \"$IP\",
|
|
\"cpu_usage\": \"${CPU_USAGE}%\",
|
|
\"ram_available\": \"$RAM_AVAILABLE\",
|
|
\"disk_space\": \"$DISK_SPACE\"
|
|
}"
|
|
}
|
|
|
|
# Function to send alert
|
|
send_alert() {
|
|
local error_code="$1"
|
|
local error_message="$2"
|
|
local timestamp="$3"
|
|
local server_metrics=$(get_server_metrics)
|
|
|
|
curl -X POST "$GHAYMAH_ENDPOINT_URL" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{
|
|
\"error\": \"$error_code\",
|
|
\"timestamp\": \"$timestamp\",
|
|
\"message\": \"$error_message\",
|
|
\"server_metrics\": $server_metrics
|
|
}"
|
|
}
|
|
|
|
# Main monitoring loop
|
|
tail -f "$LOG_FILE" | while read -r line; do
|
|
level=$(echo "$line" | jq -r '.Level')
|
|
timestamp=$(echo "$line" | jq -r '.Timestamp')
|
|
status_code=$(echo "$line" | jq -r '.Properties.StatusCode')
|
|
message=$(echo "$line" | jq -r '.MessageTemplate')
|
|
|
|
if [ "$level" = "Error" ] || [ "$level" = "Warning" ]; then
|
|
send_alert "$level" "$message" "$timestamp"
|
|
continue
|
|
fi
|
|
|
|
# Check for HTTP error codes (4XX, 5XX)
|
|
if [[ "$status_code" =~ ^[45][0-9]{2}$ ]]; then
|
|
send_alert "$status_code" "$message" "$timestamp"
|
|
fi
|
|
done
|