#!/bin/bash
# AITBC Basic Monitoring Setup
# Creates simple monitoring without Grafana/Prometheus
set -e
echo "=== ๐ AITBC BASIC MONITORING SETUP ==="
echo "Timestamp: $(date)"
echo ""
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo "1. ๐ SIMPLE MONITORING SETUP"
echo "============================"
# Create basic monitoring directory
mkdir -p /opt/aitbc/monitoring
mkdir -p /var/log/aitbc/monitoring
# Create simple health check script
cat > /opt/aitbc/monitoring/health_monitor.sh << 'EOF'
#!/bin/bash
# Basic health monitoring script
LOG_FILE="/var/log/aitbc/monitoring/health.log"
mkdir -p $(dirname "$LOG_FILE")
# Function to log
log_health() {
echo "[$(date)] $1" >> "$LOG_FILE"
}
# Check blockchain health
check_blockchain() {
local height=$(curl -s http://localhost:8006/rpc/head | jq -r .height 2>/dev/null || echo "0")
local status=$(curl -s http://localhost:8006/rpc/info >/dev/null 2>&1 && echo "healthy" || echo "unhealthy")
echo "$height,$status"
}
# Check system resources
check_system() {
local cpu=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//')
local mem=$(free | grep Mem | awk '{printf "%.1f", $3/$2 * 100.0}')
local disk=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')
echo "$cpu,$mem,$disk"
}
# Main monitoring
log_health "Starting health check"
# Blockchain status
blockchain_status=$(check_blockchain)
height=$(echo "$blockchain_status" | cut -d',' -f1)
health=$(echo "$blockchain_status" | cut -d',' -f2)
log_health "Blockchain height: $height, status: $health"
# System status
system_status=$(check_system)
cpu=$(echo "$system_status" | cut -d',' -f1)
mem=$(echo "$system_status" | cut -d',' -f2)
disk=$(echo "$system_status" | cut -d',' -f3)
log_health "System: CPU=${cpu}%, MEM=${mem}%, DISK=${disk}%"
log_health "Health check completed"
EOF
chmod +x /opt/aitbc/monitoring/health_monitor.sh
echo -e " ${GREEN}โ
${NC} Basic monitoring script created"
echo ""
echo "2. ๐ MONITORING DASHBOARD"
echo "========================"
# Create simple web dashboard
cat > /opt/aitbc/monitoring/dashboard.html << 'EOF'
AITBC Monitoring Dashboard
AITBC Monitoring Dashboard
EOF
echo -e " ${GREEN}โ
${NC} Simple dashboard created"
echo ""
echo "3. ๏ฟฝ MONITORING AUTOMATION"
echo "=========================="
# Create metrics API endpoint
cat > /opt/aitbc/monitoring/metrics_api.py << 'EOF'
#!/usr/bin/env python3
import json
import subprocess
import requests
from flask import Flask, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/metrics')
def get_metrics():
try:
# Get blockchain height
height = subprocess.getoutput("curl -s http://localhost:8006/rpc/head | jq -r .height 2>/dev/null || echo 0")
# Check blockchain health
health = "healthy" if subprocess.getoutput("curl -s http://localhost:8006/rpc/info >/dev/null 2>&1 && echo healthy || echo unhealthy").strip() == "healthy"
# Get system metrics
cpu = subprocess.getoutput("top -bn1 | grep 'Cpu(s)' | awk '{print $2}' | sed 's/%us,//'")
memory = subprocess.getoutput("free | grep Mem | awk '{printf \"%.1f\", $3/$2 * 100.0}'")
disk = subprocess.getoutput("df / | awk 'NR==2 {print $5}' | sed 's/%//'")
return jsonify({
'height': int(height),
'health': health,
'cpu': cpu,
'memory': memory,
'disk': int(disk)
})
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
EOF
chmod +x /opt/aitbc/monitoring/metrics_api.py
echo -e " ${GREEN}โ
${NC} Metrics API created"
echo ""
echo "4. ๏ฟฝ MONITORING SCHEDULE"
echo "======================"
# Add health monitoring to cron
(crontab -l 2>/dev/null; echo "*/2 * * * * /opt/aitbc/monitoring/health_monitor.sh") | crontab -
echo -e " ${GREEN}โ
${NC} Health monitoring scheduled (every 2 minutes)"
echo ""
echo "5. ๐งช MONITORING VERIFICATION"
echo "=========================="
# Test health monitor
echo "Testing health monitor..."
/opt/aitbc/monitoring/health_monitor.sh
# Check log file
if [ -f "/var/log/aitbc/monitoring/health.log" ]; then
echo -e " ${GREEN}โ
${NC} Health log created"
echo " Recent entries:"
tail -3 /var/log/aitbc/monitoring/health.log
else
echo -e " ${RED}โ${NC} Health log not found"
fi
echo ""
echo "6. ๐ MONITORING ACCESS"
echo "===================="
echo "Basic monitoring components deployed:"
echo "โข Health monitor script: /opt/aitbc/monitoring/health_monitor.sh"
echo "โข Dashboard: /opt/aitbc/monitoring/dashboard.html"
echo "โข Metrics API: /opt/aitbc/monitoring/metrics_api.py"
echo "โข Health logs: /var/log/aitbc/monitoring/health.log"
echo ""
echo "To start metrics API:"
echo " python3 /opt/aitbc/monitoring/metrics_api.py"
echo ""
echo "Then access dashboard at:"
echo " http://$(hostname -I | awk '{print $1}'):8080"
echo ""
echo "=== ๐ BASIC MONITORING SETUP COMPLETE ==="
echo ""
echo "Basic monitoring deployed without Grafana/Prometheus:"
echo "โข Health monitoring script"
echo "โข Simple web dashboard"
echo "โข Metrics API endpoint"
echo "โข Automated health checks"
echo ""
echo -e "${GREEN}โ
Basic monitoring setup completed!${NC}"