#!/bin/bash # Portfolio Monitoring Script # Usage: ./scripts/monitor.sh [action] set -e # Configuration CONTAINER_NAME="portfolio-app" COMPOSE_FILE="docker-compose.prod.yml" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging function log() { echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" >&2 } success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # Check container health check_health() { log "Checking application health..." if curl -f http://localhost/api/health > /dev/null 2>&1; then success "Application is healthy" return 0 else error "Application is unhealthy" return 1 fi } # Show container status show_status() { log "Container status:" docker compose -f $COMPOSE_FILE ps echo "" log "Resource usage:" docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}" echo "" log "Container logs (last 20 lines):" docker compose -f $COMPOSE_FILE logs --tail=20 } # Show detailed metrics show_metrics() { log "Detailed metrics:" # Container info echo "=== Container Information ===" docker inspect $CONTAINER_NAME --format='{{.State.Status}} - {{.State.StartedAt}}' 2>/dev/null || echo "Container not found" # Memory usage echo "" echo "=== Memory Usage ===" docker stats --no-stream --format "{{.MemUsage}}" $CONTAINER_NAME 2>/dev/null || echo "Container not running" # CPU usage echo "" echo "=== CPU Usage ===" docker stats --no-stream --format "{{.CPUPerc}}" $CONTAINER_NAME 2>/dev/null || echo "Container not running" # Network usage echo "" echo "=== Network Usage ===" docker stats --no-stream --format "{{.NetIO}}" $CONTAINER_NAME 2>/dev/null || echo "Container not running" # Disk usage echo "" echo "=== Disk Usage ===" docker system df } # Restart container restart_container() { log "Restarting container..." docker compose -f $COMPOSE_FILE restart # Wait for health check log "Waiting for container to be healthy..." sleep 10 if check_health; then success "Container restarted successfully" else error "Container restart failed" exit 1 fi } # Show logs show_logs() { local lines=${1:-50} log "Showing last $lines lines of logs:" docker compose -f $COMPOSE_FILE logs --tail=$lines -f } # Cleanup cleanup() { log "Cleaning up Docker resources..." # Remove unused containers docker container prune -f # Remove unused images docker image prune -f # Remove unused volumes docker volume prune -f # Remove unused networks docker network prune -f success "Cleanup completed" } # Main script logic case "${1:-status}" in "health") check_health ;; "status") show_status ;; "metrics") show_metrics ;; "restart") restart_container ;; "logs") show_logs $2 ;; "cleanup") cleanup ;; *) echo "Usage: $0 {health|status|metrics|restart|logs|cleanup}" echo "" echo "Commands:" echo " health - Check application health" echo " status - Show container status and resource usage" echo " metrics - Show detailed metrics" echo " restart - Restart the container" echo " logs - Show container logs (optional: number of lines)" echo " cleanup - Clean up unused Docker resources" exit 1 ;; esac