File size: 7,445 Bytes
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash

# =========================================
# πŸ“Š CIDADΓƒO.AI - Deploy Monitoring Stack
# =========================================
# Deploy Grafana + Prometheus monitoring
# for local and production environments
# =========================================

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

# Script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
DEPLOYMENT_DIR="$PROJECT_ROOT/deployment"

echo -e "${BLUE}================================================${NC}"
echo -e "${BLUE}πŸ“Š CIDADΓƒO.AI - Monitoring Stack Deployment${NC}"
echo -e "${BLUE}================================================${NC}"

# Function to check if docker is running
check_docker() {
    if ! docker info > /dev/null 2>&1; then
        echo -e "${RED}❌ Docker is not running! Please start Docker first.${NC}"
        exit 1
    fi
    echo -e "${GREEN}βœ… Docker is running${NC}"
}

# Function to check if required files exist
check_files() {
    echo -e "\n${YELLOW}πŸ“ Checking required files...${NC}"
    
    local required_files=(
        "docker-compose.monitoring.yml"
        "prometheus/prometheus.yml"
        "grafana/provisioning/datasources/prometheus.yml"
        "grafana/provisioning/dashboards/dashboards.yml"
        "alertmanager/alertmanager.yml"
    )
    
    for file in "${required_files[@]}"; do
        if [ -f "$DEPLOYMENT_DIR/$file" ]; then
            echo -e "${GREEN}βœ… Found: $file${NC}"
        else
            echo -e "${RED}❌ Missing: $file${NC}"
            exit 1
        fi
    done
}

# Function to create missing directories
create_directories() {
    echo -e "\n${YELLOW}πŸ“ Creating required directories...${NC}"
    
    local dirs=(
        "$DEPLOYMENT_DIR/prometheus/alerts"
        "$DEPLOYMENT_DIR/grafana/plugins"
        "$DEPLOYMENT_DIR/loki"
        "$DEPLOYMENT_DIR/promtail"
    )
    
    for dir in "${dirs[@]}"; do
        if [ ! -d "$dir" ]; then
            mkdir -p "$dir"
            echo -e "${GREEN}βœ… Created: $dir${NC}"
        fi
    done
}

# Function to set environment variables
set_environment() {
    echo -e "\n${YELLOW}πŸ”§ Setting environment variables...${NC}"
    
    # Default values if not set
    export GRAFANA_USER=${GRAFANA_USER:-admin}
    export GRAFANA_PASSWORD=${GRAFANA_PASSWORD:-cidadao2025}
    
    echo -e "${GREEN}βœ… Grafana User: $GRAFANA_USER${NC}"
    echo -e "${GREEN}βœ… Grafana Password: (hidden)${NC}"
}

# Function to start monitoring stack
start_monitoring() {
    echo -e "\n${YELLOW}πŸš€ Starting monitoring stack...${NC}"
    
    cd "$DEPLOYMENT_DIR"
    
    # Pull latest images
    echo -e "${BLUE}πŸ“₯ Pulling latest images...${NC}"
    docker-compose -f docker-compose.monitoring.yml pull
    
    # Start services
    echo -e "${BLUE}🎯 Starting services...${NC}"
    docker-compose -f docker-compose.monitoring.yml up -d
    
    # Wait for services to be ready
    echo -e "\n${YELLOW}⏳ Waiting for services to start...${NC}"
    sleep 10
}

# Function to check service health
check_health() {
    echo -e "\n${YELLOW}πŸ₯ Checking service health...${NC}"
    
    local services=(
        "prometheus:9090/-/healthy"
        "grafana:3000/api/health"
        "alertmanager:9093/-/healthy"
        "loki:3100/ready"
    )
    
    for service in "${services[@]}"; do
        IFS=':' read -r name endpoint <<< "$service"
        
        if curl -s -o /dev/null -w "%{http_code}" "http://localhost:$endpoint" | grep -q "200"; then
            echo -e "${GREEN}βœ… $name is healthy${NC}"
        else
            echo -e "${YELLOW}⚠️  $name is starting up...${NC}"
        fi
    done
}

# Function to display access information
show_access_info() {
    echo -e "\n${GREEN}================================================${NC}"
    echo -e "${GREEN}πŸŽ‰ Monitoring Stack Deployed Successfully!${NC}"
    echo -e "${GREEN}================================================${NC}"
    
    echo -e "\n${BLUE}πŸ“Š Access URLs:${NC}"
    echo -e "  β€’ Grafana:       ${GREEN}http://localhost:3000${NC}"
    echo -e "  β€’ Prometheus:    ${GREEN}http://localhost:9090${NC}"
    echo -e "  β€’ AlertManager:  ${GREEN}http://localhost:9093${NC}"
    echo -e "  β€’ Node Exporter: ${GREEN}http://localhost:9100${NC}"
    echo -e "  β€’ cAdvisor:      ${GREEN}http://localhost:8080${NC}"
    
    echo -e "\n${BLUE}πŸ” Grafana Login:${NC}"
    echo -e "  β€’ Username: ${GREEN}$GRAFANA_USER${NC}"
    echo -e "  β€’ Password: ${GREEN}$GRAFANA_PASSWORD${NC}"
    
    echo -e "\n${BLUE}πŸ“ˆ Default Dashboards:${NC}"
    echo -e "  β€’ CidadΓ£o.AI Overview"
    echo -e "  β€’ Agent Performance"
    echo -e "  β€’ Zumbi Agent Metrics"
    
    echo -e "\n${YELLOW}πŸ’‘ Tips:${NC}"
    echo -e "  β€’ Check logs: ${GREEN}docker-compose -f deployment/docker-compose.monitoring.yml logs -f${NC}"
    echo -e "  β€’ Stop stack: ${GREEN}docker-compose -f deployment/docker-compose.monitoring.yml down${NC}"
    echo -e "  β€’ Update stack: ${GREEN}docker-compose -f deployment/docker-compose.monitoring.yml pull && docker-compose -f deployment/docker-compose.monitoring.yml up -d${NC}"
}

# Function to configure production deployment
configure_production() {
    echo -e "\n${YELLOW}🌐 Configuring for production deployment...${NC}"
    
    # Create production config overlay
    cat > "$DEPLOYMENT_DIR/docker-compose.monitoring.prod.yml" << 'EOF'
version: '3.9'

# Production overlay for monitoring stack
services:
  prometheus:
    restart: always
    deploy:
      resources:
        limits:
          memory: 2G
          cpus: '1.0'
        reservations:
          memory: 1G
          cpus: '0.5'
    
  grafana:
    restart: always
    environment:
      - GF_SERVER_ROOT_URL=https://monitoring.cidadao.ai
      - GF_SECURITY_ADMIN_USER=${GRAFANA_USER}
      - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}
      - GF_AUTH_ANONYMOUS_ENABLED=false
      - GF_USERS_ALLOW_SIGN_UP=false
      - GF_INSTALL_PLUGINS=grafana-piechart-panel,grafana-worldmap-panel
    deploy:
      resources:
        limits:
          memory: 1G
          cpus: '0.5'
        reservations:
          memory: 512M
          cpus: '0.25'
    
  alertmanager:
    restart: always
    deploy:
      resources:
        limits:
          memory: 512M
          cpus: '0.25'
        reservations:
          memory: 256M
          cpus: '0.1'
    
  loki:
    restart: always
    deploy:
      resources:
        limits:
          memory: 2G
          cpus: '1.0'
        reservations:
          memory: 1G
          cpus: '0.5'
EOF
    
    echo -e "${GREEN}βœ… Production configuration created${NC}"
}

# Main execution
main() {
    check_docker
    check_files
    create_directories
    set_environment
    
    # Ask for deployment type
    echo -e "\n${YELLOW}🎯 Select deployment type:${NC}"
    echo -e "  1) Local Development"
    echo -e "  2) Production (with resource limits)"
    read -p "Choice (1-2): " choice
    
    case $choice in
        2)
            configure_production
            echo -e "${YELLOW}πŸ“ Using production configuration...${NC}"
            ;;
        *)
            echo -e "${YELLOW}πŸ“ Using local development configuration...${NC}"
            ;;
    esac
    
    start_monitoring
    check_health
    show_access_info
}

# Run main function
main "$@"