From 50b4d1dfb0935ff8ce3a6fff15b53c93597179a4 Mon Sep 17 00:00:00 2001 From: Tola Leng Date: Thu, 31 Jul 2025 16:19:16 +0700 Subject: [PATCH] Refactor: Reduce verbose logging output in Regional Script - Reduce verbose logging in install-regional-agent.sh to provide a more minimal output while retaining all core functionality. --- scripts/install-regional-agent.sh | 229 +++++++++++------------------- 1 file changed, 81 insertions(+), 148 deletions(-) diff --git a/scripts/install-regional-agent.sh b/scripts/install-regional-agent.sh index dbf789d..92dba46 100644 --- a/scripts/install-regional-agent.sh +++ b/scripts/install-regional-agent.sh @@ -17,6 +17,30 @@ BASE_PACKAGE_URL="https://github.com/operacle/Distributed-Regional-Monitoring/re PACKAGE_VERSION="1.0.0" SERVICE_NAME="regional-check-agent" +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Helper functions +log_info() { + echo -e "${BLUE}[INFO]${NC} $1" +} + +log_success() { + echo -e "${GREEN}[SUCCESS]${NC} $1" +} + +log_warning() { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + # Function to show usage show_usage() { echo "Usage: $0 [OPTIONS]" @@ -77,38 +101,36 @@ done # Validate required parameters if [[ -z "$REGION_NAME" || -z "$AGENT_ID" || -z "$AGENT_IP_ADDRESS" || -z "$AGENT_TOKEN" || -z "$POCKETBASE_URL" ]]; then - echo "❌ Error: Missing required parameters" + log_error "Missing required parameters" echo "" show_usage exit 1 fi -echo "🚀 CheckCle Regional Monitoring Agent - Universal Installation" -echo "==============================================================" -echo "" +echo "=============================================" +echo " CheckCle Regional Monitoring Agent" +echo " Universal Installation" +echo "=============================================" # Check if running as root if [[ $EUID -ne 0 ]]; then - echo "❌ This script must be run as root (use sudo)" + log_error "This script must be run as root (use sudo)" echo " Usage: sudo bash $0 [options]" exit 1 fi # Detect operating system -echo "🔍 Detecting system information..." OS_TYPE=$(uname -s | tr '[:upper:]' '[:lower:]') -echo " Operating System: $OS_TYPE" # Check if it's a supported OS if [[ "$OS_TYPE" != "linux" ]]; then - echo "❌ Unsupported operating system: $OS_TYPE" - echo " This installer only supports Linux systems" + log_error "Unsupported operating system: $OS_TYPE" + log_info "This installer only supports Linux systems" exit 1 fi # Detect architecture ARCH=$(uname -m) -echo " Hardware Architecture: $ARCH" # Map architecture to package architecture case $ARCH in @@ -120,33 +142,22 @@ case $ARCH in ;; armv7l|armv6l) PKG_ARCH="arm64" - echo "⚠️ ARM 32-bit detected, using ARM64 package (may require compatibility layer)" + log_warning "ARM 32-bit detected, using ARM64 package (may require compatibility layer)" ;; *) - echo "❌ Unsupported architecture: $ARCH" - echo " Supported architectures: x86_64 (amd64), aarch64 (arm64)" - echo " Please contact support for your architecture: $ARCH" + log_error "Unsupported architecture: $ARCH" + log_info "Supported architectures: x86_64 (amd64), aarch64 (arm64)" exit 1 ;; esac -echo " Package Architecture: $PKG_ARCH" +log_success "System: $OS_TYPE ($ARCH -> $PKG_ARCH)" # Construct package URLs and names PACKAGE_URL="$BASE_PACKAGE_URL/distributed-regional-check-agent_${PACKAGE_VERSION}_${PKG_ARCH}.deb" PACKAGE_NAME="distributed-regional-check-agent_${PACKAGE_VERSION}_${PKG_ARCH}.deb" -echo "" -echo "📋 Installation Configuration:" -echo " Region Name: $REGION_NAME" -echo " Agent ID: $AGENT_ID" -echo " Agent IP: $AGENT_IP_ADDRESS" -echo " Package Architecture: $PKG_ARCH" -echo " Package URL: $PACKAGE_URL" -echo "" - # Check for required tools -echo "🔧 Checking system requirements..." MISSING_TOOLS=() if ! command -v wget >/dev/null 2>&1 && ! command -v curl >/dev/null 2>&1; then @@ -162,129 +173,85 @@ if ! command -v systemctl >/dev/null 2>&1; then fi if [ ${#MISSING_TOOLS[@]} -ne 0 ]; then - echo "❌ Missing required tools: ${MISSING_TOOLS[*]}" - echo " Please install missing tools and try again" - echo " On Debian/Ubuntu: sudo apt-get update && sudo apt-get install wget curl" + log_error "Missing required tools: ${MISSING_TOOLS[*]}" + log_info "On Debian/Ubuntu: sudo apt-get update && sudo apt-get install wget curl" exit 1 fi -echo "✅ System requirements satisfied" - # Create temporary directory TEMP_DIR=$(mktemp -d) -echo "📁 Created temporary directory: $TEMP_DIR" # Download the .deb package -echo "" -echo "📥 Downloading Regional Monitoring Agent package for $PKG_ARCH..." +log_info "Downloading package for $PKG_ARCH..." cd "$TEMP_DIR" -# Test if package exists first - Accept both 200 and 302 (redirect) as success -echo "🔍 Checking package availability..." +# Test if package exists first if command -v curl >/dev/null 2>&1; then HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -I "$PACKAGE_URL") if [ "$HTTP_STATUS" != "200" ] && [ "$HTTP_STATUS" != "302" ]; then - echo "❌ Package not found at $PACKAGE_URL (HTTP $HTTP_STATUS)" - echo " Available packages should be:" - echo " - distributed-regional-check-agent_${PACKAGE_VERSION}_amd64.deb" - echo " - distributed-regional-check-agent_${PACKAGE_VERSION}_arm64.deb" - echo "" - echo " Please check the GitHub releases page:" - echo " https://github.com/operacle/Distributed-Regional-Monitoring/releases" + log_error "Package not found at $PACKAGE_URL (HTTP $HTTP_STATUS)" + log_info "Check: https://github.com/operacle/Distributed-Regional-Monitoring/releases" rm -rf "$TEMP_DIR" exit 1 fi - echo "✅ Package found (HTTP $HTTP_STATUS), proceeding with download..." fi # Try wget first, then curl as fallback DOWNLOAD_SUCCESS=false if command -v wget >/dev/null 2>&1; then - echo "📥 Downloading with wget..." if wget -q --show-progress --timeout=30 --tries=3 "$PACKAGE_URL" -O "$PACKAGE_NAME"; then DOWNLOAD_SUCCESS=true - echo "✅ Package downloaded successfully using wget" fi fi if [ "$DOWNLOAD_SUCCESS" = false ] && command -v curl >/dev/null 2>&1; then - echo "📥 Downloading with curl..." if curl -L --connect-timeout 30 --max-time 300 --retry 3 --retry-delay 2 -o "$PACKAGE_NAME" "$PACKAGE_URL" --progress-bar; then DOWNLOAD_SUCCESS=true - echo "✅ Package downloaded successfully using curl" fi fi if [ "$DOWNLOAD_SUCCESS" = false ]; then - echo "❌ Failed to download package from $PACKAGE_URL" - echo " Please check:" - echo " - Internet connection" - echo " - Package availability for $PKG_ARCH architecture" - echo " - GitHub repository access: https://github.com/operacle/Distributed-Regional-Monitoring/releases" - echo " - Firewall/proxy settings" - echo "" - echo " Available packages should be:" - echo " - distributed-regional-check-agent_${PACKAGE_VERSION}_amd64.deb" - echo " - distributed-regional-check-agent_${PACKAGE_VERSION}_arm64.deb" + log_error "Failed to download package from $PACKAGE_URL" + log_info "Check internet connection and package availability" rm -rf "$TEMP_DIR" exit 1 fi # Verify download was successful if [ ! -f "$PACKAGE_NAME" ] || [ ! -s "$PACKAGE_NAME" ]; then - echo "❌ Downloaded package is empty or missing" - echo " File size: $(ls -lh "$PACKAGE_NAME" 2>/dev/null | awk '{print $5}' || echo 'file not found')" + log_error "Downloaded package is empty or missing" rm -rf "$TEMP_DIR" exit 1 fi # Verify package integrity -echo "" -echo "🔍 Verifying package..." if dpkg-deb --info "$PACKAGE_NAME" > /dev/null 2>&1; then - echo "✅ Package verification successful" - - # Show package info - echo "📦 Package Information:" - dpkg-deb --field "$PACKAGE_NAME" Package Version Architecture Description | head -4 + log_success "Package verified" else - echo "❌ Package verification failed - corrupted download" - echo " File size: $(ls -lh "$PACKAGE_NAME" | awk '{print $5}')" - echo " Try downloading manually from: $PACKAGE_URL" + log_error "Package verification failed - corrupted download" rm -rf "$TEMP_DIR" exit 1 fi # Install the package -echo "" -echo "📦 Installing Regional Monitoring Agent package..." +log_info "Installing package..." if dpkg -i "$PACKAGE_NAME" 2>/dev/null; then - echo "✅ Package installed successfully" + log_success "Package installed" else - echo "⚠️ Package installation had dependency issues, attempting to fix..." + log_warning "Fixing dependencies..." if apt-get update && apt-get install -f -y; then - echo "✅ Dependencies fixed and package installed successfully" + log_success "Package installed with dependencies" else - echo "❌ Failed to install package and fix dependencies" - echo " This might be due to:" - echo " - Missing system dependencies" - echo " - Architecture compatibility issues" - echo " - Package conflicts" - echo " - Insufficient disk space" - echo "" - echo " Manual resolution:" - echo " 1. Run: sudo apt-get update" - echo " 2. Run: sudo apt-get install -f" - echo " 3. Retry installation: sudo dpkg -i $PACKAGE_NAME" + log_error "Failed to install package" + log_info "Try: sudo apt-get update && sudo apt-get install -f" rm -rf "$TEMP_DIR" exit 1 fi fi # Configure the agent -echo "" -echo "⚙️ Configuring Regional Monitoring Agent..." +log_info "Configuring agent..." # Ensure configuration directory exists mkdir -p /etc/regional-check-agent @@ -293,7 +260,6 @@ mkdir -p /etc/regional-check-agent cat > /etc/regional-check-agent/regional-check-agent.env << EOF # Distributed Regional Check Agent Configuration # Auto-generated on $(date) -# Architecture: $PKG_ARCH # Server Configuration PORT=8091 @@ -311,7 +277,7 @@ ENABLE_LOGGING=true POCKETBASE_ENABLED=true POCKETBASE_URL=$POCKETBASE_URL -# Regional Agent Configuration - Auto-configured +# Regional Agent Configuration REGION_NAME=$REGION_NAME AGENT_ID=$AGENT_ID AGENT_IP_ADDRESS=$AGENT_IP_ADDRESS @@ -323,112 +289,79 @@ MAX_RETRIES=3 REQUEST_TIMEOUT=10s EOF -echo "✅ Configuration file created at /etc/regional-check-agent/regional-check-agent.env" - # Set proper permissions if id "regional-check-agent" &>/dev/null; then chown root:regional-check-agent /etc/regional-check-agent/regional-check-agent.env chmod 640 /etc/regional-check-agent/regional-check-agent.env - echo "✅ Configuration file permissions set" else - echo "⚠️ regional-check-agent user not found, using root permissions" chown root:root /etc/regional-check-agent/regional-check-agent.env chmod 600 /etc/regional-check-agent/regional-check-agent.env fi +log_success "Configuration complete" + # Enable and start the service -echo "" -echo "🔧 Starting Regional Monitoring Agent service..." +log_info "Starting service..." # Reload systemd daemon systemctl daemon-reload # Enable the service for auto-start if systemctl enable $SERVICE_NAME; then - echo "✅ Service enabled for auto-start" + log_success "Service enabled" else - echo "❌ Failed to enable service" - echo " Check systemd configuration" + log_error "Failed to enable service" rm -rf "$TEMP_DIR" exit 1 fi # Start the service if systemctl start $SERVICE_NAME; then - echo "✅ Service started successfully" + log_success "Service started" else - echo "❌ Failed to start service" - echo " Common issues:" - echo " - Configuration errors" - echo " - Port 8091 already in use" - echo " - Permission issues" - echo "" - echo " Troubleshooting:" - echo " - Check logs: sudo journalctl -u $SERVICE_NAME -f" - echo " - Check config: sudo nano /etc/regional-check-agent/regional-check-agent.env" - echo " - Manual start: sudo systemctl start $SERVICE_NAME" + log_error "Failed to start service" + log_info "Check logs: sudo journalctl -u $SERVICE_NAME -f" rm -rf "$TEMP_DIR" exit 1 fi # Wait a moment for service to initialize -echo "⏳ Waiting for service to initialize..." sleep 5 -# Check service status -echo "" -echo "📊 Service Status:" -systemctl status $SERVICE_NAME --no-pager -l --lines=5 - # Test health endpoint -echo "" -echo "🩺 Testing agent health endpoint..." HEALTH_CHECK_ATTEMPTS=3 -HEALTH_CHECK_DELAY=2 - for i in $(seq 1 $HEALTH_CHECK_ATTEMPTS); do if curl -s -f --connect-timeout 5 http://localhost:8091/health > /dev/null; then - echo "✅ Agent health endpoint is responding" - HEALTH_OK=true + log_success "Health endpoint responding" break else if [ $i -lt $HEALTH_CHECK_ATTEMPTS ]; then - echo "⏳ Health check attempt $i/$HEALTH_CHECK_ATTEMPTS failed, retrying in ${HEALTH_CHECK_DELAY}s..." - sleep $HEALTH_CHECK_DELAY + sleep 2 else - echo "⚠️ Agent health endpoint not responding after $HEALTH_CHECK_ATTEMPTS attempts" - echo " This may be normal if the service is still starting up" - echo " Check status later with: curl http://localhost:8091/health" + log_warning "Health endpoint not responding (service may still be starting)" fi fi done # Cleanup rm -rf "$TEMP_DIR" + echo "" -echo "🎉 Regional Monitoring Agent Installation Complete!" +echo "=============================================" +echo " Installation Complete!" +echo "=============================================" echo "" -echo "📋 Installation Summary:" -echo " Agent ID: $AGENT_ID" -echo " Region: $REGION_NAME" -echo " Architecture: $PKG_ARCH ($ARCH)" -echo " Status: $(systemctl is-active $SERVICE_NAME 2>/dev/null || echo 'unknown')" -echo " Health URL: http://localhost:8091/health" -echo " Service endpoint: http://localhost:8091/operation" -echo " Config file: /etc/regional-check-agent/regional-check-agent.env" +log_success "CheckCle Regional Monitoring Agent installed successfully" echo "" -echo "📝 Useful commands:" -echo " Check status: sudo systemctl status $SERVICE_NAME" -echo " View logs: sudo journalctl -u $SERVICE_NAME -f" -echo " Restart: sudo systemctl restart $SERVICE_NAME" -echo " Stop: sudo systemctl stop $SERVICE_NAME" -echo " Health check: curl http://localhost:8091/health" +echo "Agent Details:" +echo " Region: $REGION_NAME" +echo " Agent ID: $AGENT_ID" +echo " Status: $(systemctl is-active $SERVICE_NAME 2>/dev/null || echo 'unknown')" +echo " Health: http://localhost:8091/health" echo "" -echo "🔧 Troubleshooting:" -echo " If the service fails to start:" -echo " 1. Check logs: sudo journalctl -u $SERVICE_NAME -n 50" -echo " 2. Verify config: cat /etc/regional-check-agent/regional-check-agent.env" -echo " 3. Test connectivity: ping $(echo $POCKETBASE_URL | sed 's|https\?://||' | sed 's|/.*||')" -echo " 4. Check port availability: sudo netstat -tlnp | grep 8091" +echo "Management Commands:" +echo " Status: sudo systemctl status $SERVICE_NAME" +echo " Logs: sudo journalctl -u $SERVICE_NAME -f" +echo " Restart: sudo systemctl restart $SERVICE_NAME" echo "" -echo "✨ The agent is now monitoring and reporting to your CheckCle dashboard!" \ No newline at end of file +log_success "Agent is now monitoring and reporting to your dashboard!" \ No newline at end of file