#!/bin/bash # Configuration LOG_FILE="api.log" GHAYMAH_URL="YOUR_GHAYMAH_ENDPOINT_HERE" # Will be provided later echo "Starting log monitor..." echo "Watching file: $LOG_FILE" # Check if log file exists if [ ! -f "$LOG_FILE" ]; then echo "Warning: $LOG_FILE not found. Creating it..." touch "$LOG_FILE" fi # Function to get server info get_server_info() { # 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 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 if command -v free &> /dev/null; then RAM=$(free -h | grep Mem | awk '{print $7}') else RAM="N/A" fi # Get available disk space 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" } # Function to send alert send_alert() { ERROR_MSG=$1 TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") # Get server metrics SERVER_INFO=$(get_server_info) IP=$(echo $SERVER_INFO | cut -d'|' -f1) CPU=$(echo $SERVER_INFO | cut -d'|' -f2) RAM=$(echo $SERVER_INFO | cut -d'|' -f3) DISK=$(echo $SERVER_INFO | cut -d'|' -f4) # Create JSON payload JSON_PAYLOAD=$(cat <> 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 # 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