#!/bin/bash # Sharenet Monitoring Script # This script monitors the application status and system resources set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration APP_NAME="${APP_NAME:-sharenet}" MONITOR_TYPE="${MONITOR_TYPE:-production}" # production or ci-cd # 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" } show_help() { cat << EOF Sharenet Monitoring Script Usage: $0 [OPTIONS] Options: --type TYPE Monitoring type: production or ci-cd (default: production) --app-name NAME Application name (default: sharenet) --help Show this help message Environment Variables: MONITOR_TYPE Set monitoring type (production/ci-cd) APP_NAME Set application name Examples: $0 # Monitor production environment $0 --type ci-cd # Monitor CI/CD environment $0 --app-name myapp # Monitor specific application MONITOR_TYPE=ci-cd $0 # Monitor CI/CD environment EOF } monitor_production() { log_info "=== $APP_NAME Production Environment Status ===" echo "Date: $(date)" echo "Uptime: $(uptime)" echo # Check if we're in the application directory if [ -f "docker-compose.yml" ]; then log_info "Container Status:" if docker-compose ps; then log_success "Docker Compose is running" else log_error "Docker Compose is not running" fi echo log_info "Recent Application Logs:" docker-compose logs --tail=20 echo else log_warning "Not in application directory (docker-compose.yml not found)" fi log_info "System Resources:" echo "CPU Usage:" top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1 echo echo "Memory Usage:" free -h echo echo "Disk Usage:" df -h echo echo "Network Connections:" netstat -tuln | grep -E ':(80|443|3000|3001)' || log_warning "No application ports found" echo # Health check log_info "Health Check:" if curl -s -f http://localhost/health > /dev/null 2>&1; then log_success "Application health check passed" else log_error "Application health check failed" fi } monitor_ci_cd() { log_info "=== CI/CD Server Status ===" echo "Date: $(date)" echo "Uptime: $(uptime)" echo log_info "Docker Status:" docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" echo log_info "Registry Status:" if [ -d "/opt/registry" ]; then cd /opt/registry docker-compose ps cd - > /dev/null else log_warning "Registry directory not found" fi echo log_info "Actions Runner Status:" if systemctl is-active --quiet forgejo-runner.service; then log_success "Forgejo runner is running" systemctl status forgejo-runner.service --no-pager else log_error "Forgejo runner is not running" fi echo log_info "System Resources:" echo "CPU Usage:" top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1 echo echo "Memory Usage:" free -h | grep Mem echo echo "Disk Usage:" df -h / echo # Registry health check log_info "Registry Health Check:" if curl -s -f http://localhost:5000/v2/_catalog > /dev/null 2>&1; then log_success "Registry is accessible" else log_error "Registry is not accessible" fi } # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in --type) MONITOR_TYPE="$2" shift 2 ;; --app-name) APP_NAME="$2" shift 2 ;; --help|-h) show_help exit 0 ;; *) log_error "Unknown option: $1" show_help exit 1 ;; esac done # Main monitoring logic case "$MONITOR_TYPE" in production) monitor_production ;; ci-cd) monitor_ci_cd ;; *) log_error "Invalid monitor type: $MONITOR_TYPE" log_error "Valid types: production, ci-cd" exit 1 ;; esac log_success "Monitoring completed"