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.
This commit is contained in:
Tola Leng
2025-07-31 16:19:16 +07:00
parent 8bb78e68ea
commit 50b4d1dfb0
+79 -146
View File
@@ -17,6 +17,30 @@ BASE_PACKAGE_URL="https://github.com/operacle/Distributed-Regional-Monitoring/re
PACKAGE_VERSION="1.0.0" PACKAGE_VERSION="1.0.0"
SERVICE_NAME="regional-check-agent" 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 # Function to show usage
show_usage() { show_usage() {
echo "Usage: $0 [OPTIONS]" echo "Usage: $0 [OPTIONS]"
@@ -77,38 +101,36 @@ done
# Validate required parameters # Validate required parameters
if [[ -z "$REGION_NAME" || -z "$AGENT_ID" || -z "$AGENT_IP_ADDRESS" || -z "$AGENT_TOKEN" || -z "$POCKETBASE_URL" ]]; then 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 "" echo ""
show_usage show_usage
exit 1 exit 1
fi fi
echo "🚀 CheckCle Regional Monitoring Agent - Universal Installation" echo "============================================="
echo "==============================================================" echo " CheckCle Regional Monitoring Agent"
echo "" echo " Universal Installation"
echo "============================================="
# Check if running as root # Check if running as root
if [[ $EUID -ne 0 ]]; then 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]" echo " Usage: sudo bash $0 [options]"
exit 1 exit 1
fi fi
# Detect operating system # Detect operating system
echo "🔍 Detecting system information..."
OS_TYPE=$(uname -s | tr '[:upper:]' '[:lower:]') OS_TYPE=$(uname -s | tr '[:upper:]' '[:lower:]')
echo " Operating System: $OS_TYPE"
# Check if it's a supported OS # Check if it's a supported OS
if [[ "$OS_TYPE" != "linux" ]]; then if [[ "$OS_TYPE" != "linux" ]]; then
echo "Unsupported operating system: $OS_TYPE" log_error "Unsupported operating system: $OS_TYPE"
echo " This installer only supports Linux systems" log_info "This installer only supports Linux systems"
exit 1 exit 1
fi fi
# Detect architecture # Detect architecture
ARCH=$(uname -m) ARCH=$(uname -m)
echo " Hardware Architecture: $ARCH"
# Map architecture to package architecture # Map architecture to package architecture
case $ARCH in case $ARCH in
@@ -120,33 +142,22 @@ case $ARCH in
;; ;;
armv7l|armv6l) armv7l|armv6l)
PKG_ARCH="arm64" 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" log_error "Unsupported architecture: $ARCH"
echo " Supported architectures: x86_64 (amd64), aarch64 (arm64)" log_info "Supported architectures: x86_64 (amd64), aarch64 (arm64)"
echo " Please contact support for your architecture: $ARCH"
exit 1 exit 1
;; ;;
esac esac
echo " Package Architecture: $PKG_ARCH" log_success "System: $OS_TYPE ($ARCH -> $PKG_ARCH)"
# Construct package URLs and names # Construct package URLs and names
PACKAGE_URL="$BASE_PACKAGE_URL/distributed-regional-check-agent_${PACKAGE_VERSION}_${PKG_ARCH}.deb" 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" 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 # Check for required tools
echo "🔧 Checking system requirements..."
MISSING_TOOLS=() MISSING_TOOLS=()
if ! command -v wget >/dev/null 2>&1 && ! command -v curl >/dev/null 2>&1; then 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 fi
if [ ${#MISSING_TOOLS[@]} -ne 0 ]; then if [ ${#MISSING_TOOLS[@]} -ne 0 ]; then
echo "Missing required tools: ${MISSING_TOOLS[*]}" log_error "Missing required tools: ${MISSING_TOOLS[*]}"
echo " Please install missing tools and try again" log_info "On Debian/Ubuntu: sudo apt-get update && sudo apt-get install wget curl"
echo " On Debian/Ubuntu: sudo apt-get update && sudo apt-get install wget curl"
exit 1 exit 1
fi fi
echo "✅ System requirements satisfied"
# Create temporary directory # Create temporary directory
TEMP_DIR=$(mktemp -d) TEMP_DIR=$(mktemp -d)
echo "📁 Created temporary directory: $TEMP_DIR"
# Download the .deb package # Download the .deb package
echo "" log_info "Downloading package for $PKG_ARCH..."
echo "📥 Downloading Regional Monitoring Agent package for $PKG_ARCH..."
cd "$TEMP_DIR" cd "$TEMP_DIR"
# Test if package exists first - Accept both 200 and 302 (redirect) as success # Test if package exists first
echo "🔍 Checking package availability..."
if command -v curl >/dev/null 2>&1; then if command -v curl >/dev/null 2>&1; then
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -I "$PACKAGE_URL") HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -I "$PACKAGE_URL")
if [ "$HTTP_STATUS" != "200" ] && [ "$HTTP_STATUS" != "302" ]; then if [ "$HTTP_STATUS" != "200" ] && [ "$HTTP_STATUS" != "302" ]; then
echo "Package not found at $PACKAGE_URL (HTTP $HTTP_STATUS)" log_error "Package not found at $PACKAGE_URL (HTTP $HTTP_STATUS)"
echo " Available packages should be:" log_info "Check: https://github.com/operacle/Distributed-Regional-Monitoring/releases"
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"
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
exit 1 exit 1
fi fi
echo "✅ Package found (HTTP $HTTP_STATUS), proceeding with download..."
fi fi
# Try wget first, then curl as fallback # Try wget first, then curl as fallback
DOWNLOAD_SUCCESS=false DOWNLOAD_SUCCESS=false
if command -v wget >/dev/null 2>&1; then 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 if wget -q --show-progress --timeout=30 --tries=3 "$PACKAGE_URL" -O "$PACKAGE_NAME"; then
DOWNLOAD_SUCCESS=true DOWNLOAD_SUCCESS=true
echo "✅ Package downloaded successfully using wget"
fi fi
fi fi
if [ "$DOWNLOAD_SUCCESS" = false ] && command -v curl >/dev/null 2>&1; then 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 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 DOWNLOAD_SUCCESS=true
echo "✅ Package downloaded successfully using curl"
fi fi
fi fi
if [ "$DOWNLOAD_SUCCESS" = false ]; then if [ "$DOWNLOAD_SUCCESS" = false ]; then
echo "Failed to download package from $PACKAGE_URL" log_error "Failed to download package from $PACKAGE_URL"
echo " Please check:" log_info "Check internet connection and package availability"
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"
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
exit 1 exit 1
fi fi
# Verify download was successful # Verify download was successful
if [ ! -f "$PACKAGE_NAME" ] || [ ! -s "$PACKAGE_NAME" ]; then if [ ! -f "$PACKAGE_NAME" ] || [ ! -s "$PACKAGE_NAME" ]; then
echo "Downloaded package is empty or missing" log_error "Downloaded package is empty or missing"
echo " File size: $(ls -lh "$PACKAGE_NAME" 2>/dev/null | awk '{print $5}' || echo 'file not found')"
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
exit 1 exit 1
fi fi
# Verify package integrity # Verify package integrity
echo ""
echo "🔍 Verifying package..."
if dpkg-deb --info "$PACKAGE_NAME" > /dev/null 2>&1; then if dpkg-deb --info "$PACKAGE_NAME" > /dev/null 2>&1; then
echo "Package verification successful" log_success "Package verified"
# Show package info
echo "📦 Package Information:"
dpkg-deb --field "$PACKAGE_NAME" Package Version Architecture Description | head -4
else else
echo "Package verification failed - corrupted download" log_error "Package verification failed - corrupted download"
echo " File size: $(ls -lh "$PACKAGE_NAME" | awk '{print $5}')"
echo " Try downloading manually from: $PACKAGE_URL"
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
exit 1 exit 1
fi fi
# Install the package # Install the package
echo "" log_info "Installing package..."
echo "📦 Installing Regional Monitoring Agent package..."
if dpkg -i "$PACKAGE_NAME" 2>/dev/null; then if dpkg -i "$PACKAGE_NAME" 2>/dev/null; then
echo "Package installed successfully" log_success "Package installed"
else else
echo "⚠️ Package installation had dependency issues, attempting to fix..." log_warning "Fixing dependencies..."
if apt-get update && apt-get install -f -y; then if apt-get update && apt-get install -f -y; then
echo "✅ Dependencies fixed and package installed successfully" log_success "Package installed with dependencies"
else else
echo "Failed to install package and fix dependencies" log_error "Failed to install package"
echo " This might be due to:" log_info "Try: sudo apt-get update && sudo apt-get install -f"
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"
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
exit 1 exit 1
fi fi
fi fi
# Configure the agent # Configure the agent
echo "" log_info "Configuring agent..."
echo "⚙️ Configuring Regional Monitoring Agent..."
# Ensure configuration directory exists # Ensure configuration directory exists
mkdir -p /etc/regional-check-agent 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 cat > /etc/regional-check-agent/regional-check-agent.env << EOF
# Distributed Regional Check Agent Configuration # Distributed Regional Check Agent Configuration
# Auto-generated on $(date) # Auto-generated on $(date)
# Architecture: $PKG_ARCH
# Server Configuration # Server Configuration
PORT=8091 PORT=8091
@@ -311,7 +277,7 @@ ENABLE_LOGGING=true
POCKETBASE_ENABLED=true POCKETBASE_ENABLED=true
POCKETBASE_URL=$POCKETBASE_URL POCKETBASE_URL=$POCKETBASE_URL
# Regional Agent Configuration - Auto-configured # Regional Agent Configuration
REGION_NAME=$REGION_NAME REGION_NAME=$REGION_NAME
AGENT_ID=$AGENT_ID AGENT_ID=$AGENT_ID
AGENT_IP_ADDRESS=$AGENT_IP_ADDRESS AGENT_IP_ADDRESS=$AGENT_IP_ADDRESS
@@ -323,112 +289,79 @@ MAX_RETRIES=3
REQUEST_TIMEOUT=10s REQUEST_TIMEOUT=10s
EOF EOF
echo "✅ Configuration file created at /etc/regional-check-agent/regional-check-agent.env"
# Set proper permissions # Set proper permissions
if id "regional-check-agent" &>/dev/null; then if id "regional-check-agent" &>/dev/null; then
chown root:regional-check-agent /etc/regional-check-agent/regional-check-agent.env chown root:regional-check-agent /etc/regional-check-agent/regional-check-agent.env
chmod 640 /etc/regional-check-agent/regional-check-agent.env chmod 640 /etc/regional-check-agent/regional-check-agent.env
echo "✅ Configuration file permissions set"
else else
echo "⚠️ regional-check-agent user not found, using root permissions"
chown root:root /etc/regional-check-agent/regional-check-agent.env chown root:root /etc/regional-check-agent/regional-check-agent.env
chmod 600 /etc/regional-check-agent/regional-check-agent.env chmod 600 /etc/regional-check-agent/regional-check-agent.env
fi fi
log_success "Configuration complete"
# Enable and start the service # Enable and start the service
echo "" log_info "Starting service..."
echo "🔧 Starting Regional Monitoring Agent service..."
# Reload systemd daemon # Reload systemd daemon
systemctl daemon-reload systemctl daemon-reload
# Enable the service for auto-start # Enable the service for auto-start
if systemctl enable $SERVICE_NAME; then if systemctl enable $SERVICE_NAME; then
echo "Service enabled for auto-start" log_success "Service enabled"
else else
echo "Failed to enable service" log_error "Failed to enable service"
echo " Check systemd configuration"
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
exit 1 exit 1
fi fi
# Start the service # Start the service
if systemctl start $SERVICE_NAME; then if systemctl start $SERVICE_NAME; then
echo "Service started successfully" log_success "Service started"
else else
echo "Failed to start service" log_error "Failed to start service"
echo " Common issues:" log_info "Check logs: sudo journalctl -u $SERVICE_NAME -f"
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"
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
exit 1 exit 1
fi fi
# Wait a moment for service to initialize # Wait a moment for service to initialize
echo "⏳ Waiting for service to initialize..."
sleep 5 sleep 5
# Check service status
echo ""
echo "📊 Service Status:"
systemctl status $SERVICE_NAME --no-pager -l --lines=5
# Test health endpoint # Test health endpoint
echo ""
echo "🩺 Testing agent health endpoint..."
HEALTH_CHECK_ATTEMPTS=3 HEALTH_CHECK_ATTEMPTS=3
HEALTH_CHECK_DELAY=2
for i in $(seq 1 $HEALTH_CHECK_ATTEMPTS); do for i in $(seq 1 $HEALTH_CHECK_ATTEMPTS); do
if curl -s -f --connect-timeout 5 http://localhost:8091/health > /dev/null; then if curl -s -f --connect-timeout 5 http://localhost:8091/health > /dev/null; then
echo "✅ Agent health endpoint is responding" log_success "Health endpoint responding"
HEALTH_OK=true
break break
else else
if [ $i -lt $HEALTH_CHECK_ATTEMPTS ]; then if [ $i -lt $HEALTH_CHECK_ATTEMPTS ]; then
echo "⏳ Health check attempt $i/$HEALTH_CHECK_ATTEMPTS failed, retrying in ${HEALTH_CHECK_DELAY}s..." sleep 2
sleep $HEALTH_CHECK_DELAY
else else
echo "⚠️ Agent health endpoint not responding after $HEALTH_CHECK_ATTEMPTS attempts" log_warning "Health endpoint not responding (service may still be starting)"
echo " This may be normal if the service is still starting up"
echo " Check status later with: curl http://localhost:8091/health"
fi fi
fi fi
done done
# Cleanup # Cleanup
rm -rf "$TEMP_DIR" rm -rf "$TEMP_DIR"
echo "" echo ""
echo "🎉 Regional Monitoring Agent Installation Complete!" echo "============================================="
echo " Installation Complete!"
echo "============================================="
echo "" echo ""
echo "📋 Installation Summary:" log_success "CheckCle Regional Monitoring Agent installed successfully"
echo " Agent ID: $AGENT_ID" echo ""
echo "Agent Details:"
echo " Region: $REGION_NAME" echo " Region: $REGION_NAME"
echo " Architecture: $PKG_ARCH ($ARCH)" echo " Agent ID: $AGENT_ID"
echo " Status: $(systemctl is-active $SERVICE_NAME 2>/dev/null || echo 'unknown')" echo " Status: $(systemctl is-active $SERVICE_NAME 2>/dev/null || echo 'unknown')"
echo " Health URL: http://localhost:8091/health" echo " Health: http://localhost:8091/health"
echo " Service endpoint: http://localhost:8091/operation"
echo " Config file: /etc/regional-check-agent/regional-check-agent.env"
echo "" echo ""
echo "📝 Useful commands:" echo "Management Commands:"
echo " Check status: sudo systemctl status $SERVICE_NAME" echo " Status: sudo systemctl status $SERVICE_NAME"
echo " View logs: sudo journalctl -u $SERVICE_NAME -f" echo " Logs: sudo journalctl -u $SERVICE_NAME -f"
echo " Restart: sudo systemctl restart $SERVICE_NAME" echo " Restart: sudo systemctl restart $SERVICE_NAME"
echo " Stop: sudo systemctl stop $SERVICE_NAME"
echo " Health check: curl http://localhost:8091/health"
echo "" echo ""
echo "🔧 Troubleshooting:" log_success "Agent is now monitoring and reporting to your dashboard!"
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 ""
echo "✨ The agent is now monitoring and reporting to your CheckCle dashboard!"