Initial commit — Server_Monitorizare_v2
This commit is contained in:
166
scripts/migrate_automation_models.py
Executable file
166
scripts/migrate_automation_models.py
Executable file
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Migration script to consolidate AnsibleExecution data into PlaybookExecution model
|
||||
and deprecate the old model structure.
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
# Add the project root to Python path
|
||||
sys.path.insert(0, '/home/pi/Desktop/Server_Monitorizare_v2')
|
||||
|
||||
from config.database_config import get_db
|
||||
from app.models import AnsibleExecution, PlaybookExecution, PlaybookHostResult
|
||||
|
||||
|
||||
class AutomationMigration:
|
||||
def __init__(self):
|
||||
from config.database_config import DatabaseConfig
|
||||
db_config = DatabaseConfig()
|
||||
self.engine = db_config.engine
|
||||
self.Session = sessionmaker(bind=self.engine)
|
||||
|
||||
def migrate_executions(self):
|
||||
"""Migrate data from AnsibleExecution to PlaybookExecution"""
|
||||
print("🔄 Starting automation model migration...")
|
||||
|
||||
session = self.Session()
|
||||
try:
|
||||
# Get all existing AnsibleExecutions
|
||||
old_executions = session.query(AnsibleExecution).all()
|
||||
migrated_count = 0
|
||||
|
||||
for old_exec in old_executions:
|
||||
# Check if already migrated
|
||||
existing = session.query(PlaybookExecution).filter_by(
|
||||
playbook_name=old_exec.playbook_name,
|
||||
start_time=old_exec.start_time
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
print(f" ⏭️ Skipping {old_exec.playbook_name} (already migrated)")
|
||||
continue
|
||||
|
||||
# Create new PlaybookExecution from old data
|
||||
new_exec = PlaybookExecution(
|
||||
execution_id=str(uuid.uuid4()),
|
||||
playbook_name=old_exec.playbook_name,
|
||||
target_hosts=old_exec.target_devices,
|
||||
|
||||
# Timing
|
||||
queued_at=old_exec.start_time,
|
||||
started_at=old_exec.start_time,
|
||||
completed_at=old_exec.end_time,
|
||||
|
||||
# Status mapping
|
||||
status=self._map_status(old_exec.status),
|
||||
exit_code=old_exec.exit_code,
|
||||
|
||||
# Logs
|
||||
stdout_log=old_exec.stdout_log,
|
||||
stderr_log=old_exec.stderr_log,
|
||||
ansible_log_file=old_exec.ansible_log_file,
|
||||
|
||||
# Results
|
||||
successful_hosts=old_exec.successful_hosts,
|
||||
failed_hosts=old_exec.failed_hosts,
|
||||
unreachable_hosts=old_exec.unreachable_hosts,
|
||||
total_hosts=old_exec.successful_hosts + old_exec.failed_hosts + old_exec.unreachable_hosts,
|
||||
|
||||
# Metadata
|
||||
execution_user=old_exec.execution_user,
|
||||
command_line=old_exec.command_line
|
||||
)
|
||||
|
||||
session.add(new_exec)
|
||||
migrated_count += 1
|
||||
print(f" ✅ Migrated: {old_exec.playbook_name} ({old_exec.start_time})")
|
||||
|
||||
session.commit()
|
||||
print(f"\n✅ Migration completed: {migrated_count} executions migrated")
|
||||
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
print(f"❌ Migration failed: {e}")
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
def _map_status(self, old_status):
|
||||
"""Map old status values to new enum"""
|
||||
status_mapping = {
|
||||
'running': 'running',
|
||||
'completed': 'completed',
|
||||
'failed': 'failed',
|
||||
'cancelled': 'cancelled'
|
||||
}
|
||||
return status_mapping.get(old_status, 'failed')
|
||||
|
||||
def validate_migration(self):
|
||||
"""Validate that migration was successful"""
|
||||
print("\n🔍 Validating migration...")
|
||||
|
||||
session = self.Session()
|
||||
try:
|
||||
old_count = session.query(AnsibleExecution).count()
|
||||
new_count = session.query(PlaybookExecution).count()
|
||||
|
||||
print(f" 📊 Old executions: {old_count}")
|
||||
print(f" 📊 New executions: {new_count}")
|
||||
|
||||
if new_count >= old_count:
|
||||
print(" ✅ Migration validation passed")
|
||||
return True
|
||||
else:
|
||||
print(" ❌ Migration validation failed: data missing")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ Validation error: {e}")
|
||||
return False
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
def cleanup_old_tables(self, confirm=False):
|
||||
"""Remove old AnsibleExecution table (use with caution)"""
|
||||
if not confirm:
|
||||
print("\n⚠️ Run with confirm=True to actually remove old table")
|
||||
print(" This will permanently delete AnsibleExecution data!")
|
||||
return
|
||||
|
||||
print("\n🗑️ Cleaning up old AnsibleExecution table...")
|
||||
session = self.Session()
|
||||
try:
|
||||
session.query(AnsibleExecution).delete()
|
||||
session.commit()
|
||||
print(" ✅ Old execution data cleaned up")
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
print(f" ❌ Cleanup failed: {e}")
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
migration = AutomationMigration()
|
||||
|
||||
print("🔧 Automation Model Consolidation")
|
||||
print("=" * 50)
|
||||
|
||||
# Step 1: Migrate data
|
||||
migration.migrate_executions()
|
||||
|
||||
# Step 2: Validate
|
||||
if migration.validate_migration():
|
||||
print("\n🎉 Migration successful!")
|
||||
print("\nNext steps:")
|
||||
print("1. Test the new PlaybookExecution model")
|
||||
print("2. Update code to use only PlaybookExecution")
|
||||
print("3. Run cleanup_old_tables() when confident")
|
||||
else:
|
||||
print("\n❌ Migration validation failed!")
|
||||
sys.exit(1)
|
||||
118
scripts/update_database_schema.py
Executable file
118
scripts/update_database_schema.py
Executable file
@@ -0,0 +1,118 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Update database schema for enhanced automation models
|
||||
"""
|
||||
import sys
|
||||
import os
|
||||
from sqlalchemy import create_engine, text
|
||||
|
||||
# Add the project root to Python path
|
||||
sys.path.insert(0, '/home/pi/Desktop/Server_Monitorizare_v2')
|
||||
|
||||
from config.database_config import DatabaseConfig
|
||||
|
||||
class DatabaseSchemaUpdater:
|
||||
def __init__(self):
|
||||
db_config = DatabaseConfig()
|
||||
self.engine = db_config.engine
|
||||
|
||||
def update_playbook_executions_schema(self):
|
||||
"""Add new columns to playbook_executions table"""
|
||||
print("📊 Updating playbook_executions table schema...")
|
||||
|
||||
new_columns = [
|
||||
("playbook_description", "TEXT"),
|
||||
("estimated_duration", "INTEGER"),
|
||||
("priority", "INTEGER DEFAULT 5"),
|
||||
("retry_count", "INTEGER DEFAULT 0"),
|
||||
("max_retries", "INTEGER DEFAULT 0"),
|
||||
("summary_message", "TEXT"),
|
||||
("skipped_hosts", "INTEGER DEFAULT 0"),
|
||||
("changed_hosts", "INTEGER DEFAULT 0")
|
||||
]
|
||||
|
||||
with self.engine.connect() as conn:
|
||||
# Check if table exists
|
||||
result = conn.execute(text("SELECT name FROM sqlite_master WHERE type='table' AND name='playbook_executions'"))
|
||||
if not result.fetchone():
|
||||
print(" ⚠️ playbook_executions table doesn't exist. Creating tables...")
|
||||
self.create_tables()
|
||||
return
|
||||
|
||||
for column_name, column_type in new_columns:
|
||||
try:
|
||||
# Check if column already exists
|
||||
check_sql = f"PRAGMA table_info(playbook_executions)"
|
||||
result = conn.execute(text(check_sql))
|
||||
existing_columns = [row[1] for row in result.fetchall()]
|
||||
|
||||
if column_name not in existing_columns:
|
||||
alter_sql = f"ALTER TABLE playbook_executions ADD COLUMN {column_name} {column_type}"
|
||||
conn.execute(text(alter_sql))
|
||||
conn.commit()
|
||||
print(f" ✅ Added column: {column_name}")
|
||||
else:
|
||||
print(f" ⏭️ Column {column_name} already exists")
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ Error adding {column_name}: {e}")
|
||||
|
||||
def create_tables(self):
|
||||
"""Create all tables using SQLAlchemy metadata"""
|
||||
print("🏗️ Creating all database tables...")
|
||||
|
||||
try:
|
||||
from app.models import Base
|
||||
Base.metadata.create_all(self.engine)
|
||||
print(" ✅ All tables created successfully")
|
||||
except Exception as e:
|
||||
print(f" ❌ Error creating tables: {e}")
|
||||
|
||||
def verify_schema(self):
|
||||
"""Verify the schema updates were successful"""
|
||||
print("\n🔍 Verifying schema updates...")
|
||||
|
||||
with self.engine.connect() as conn:
|
||||
try:
|
||||
# Test with a simple query
|
||||
result = conn.execute(text("SELECT COUNT(*) FROM playbook_executions"))
|
||||
count = result.fetchone()[0]
|
||||
print(f" ✅ playbook_executions table accessible: {count} records")
|
||||
|
||||
# Check for new columns
|
||||
result = conn.execute(text("PRAGMA table_info(playbook_executions)"))
|
||||
columns = [row[1] for row in result.fetchall()]
|
||||
|
||||
expected_columns = [
|
||||
'playbook_description', 'estimated_duration', 'priority',
|
||||
'retry_count', 'max_retries', 'summary_message',
|
||||
'skipped_hosts', 'changed_hosts'
|
||||
]
|
||||
|
||||
missing_columns = [col for col in expected_columns if col not in columns]
|
||||
if missing_columns:
|
||||
print(f" ⚠️ Missing columns: {missing_columns}")
|
||||
else:
|
||||
print(" ✅ All expected columns present")
|
||||
|
||||
return len(missing_columns) == 0
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ Schema verification failed: {e}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
updater = DatabaseSchemaUpdater()
|
||||
|
||||
print("🔧 Database Schema Update for Enhanced Automation")
|
||||
print("=" * 60)
|
||||
|
||||
# Update schema
|
||||
updater.update_playbook_executions_schema()
|
||||
|
||||
# Verify
|
||||
if updater.verify_schema():
|
||||
print("\n🎉 Schema update completed successfully!")
|
||||
else:
|
||||
print("\n❌ Schema update verification failed!")
|
||||
sys.exit(1)
|
||||
130
scripts/vscode_emergency_cleanup.sh
Executable file
130
scripts/vscode_emergency_cleanup.sh
Executable file
@@ -0,0 +1,130 @@
|
||||
#!/bin/bash
|
||||
|
||||
# VS Code Emergency Memory Management
|
||||
# Use this script when VS Code becomes unresponsive during agent iterations
|
||||
|
||||
echo "🔧 VS Code Emergency Memory Management"
|
||||
echo "======================================"
|
||||
|
||||
# Function to show current status
|
||||
show_status() {
|
||||
echo "📊 Current VS Code Status:"
|
||||
echo "Processes: $(ps aux | grep -E "(code|vscode)" | grep -v grep | wc -l)"
|
||||
echo "Memory: $(ps -o pid,ppid,pcpu,pmem,rss,args -C code | awk 'NR>1{sum+=$5} END{print sum/1024 " MB"}')"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Function for gentle cleanup
|
||||
gentle_cleanup() {
|
||||
echo "🧹 Performing gentle cleanup..."
|
||||
|
||||
# Kill high-memory extension processes
|
||||
for pid in $(ps aux | grep 'extensions.*server' | awk '$6 > 300000 {print $2}'); do
|
||||
if [ -n "$pid" ]; then
|
||||
echo "Stopping high-memory extension process: $pid"
|
||||
kill -15 "$pid" 2>/dev/null
|
||||
fi
|
||||
done
|
||||
|
||||
# Clean temporary files
|
||||
rm -rf /tmp/mcp-* 2>/dev/null
|
||||
find ~/.config/Code -name "*.log" -size +50M -delete 2>/dev/null
|
||||
|
||||
sleep 3
|
||||
echo "✅ Gentle cleanup completed"
|
||||
}
|
||||
|
||||
# Function for aggressive cleanup
|
||||
aggressive_cleanup() {
|
||||
echo "⚠️ Performing aggressive cleanup..."
|
||||
|
||||
# Stop all extension hosts
|
||||
pkill -f "extensionHost" 2>/dev/null
|
||||
|
||||
# Stop language servers
|
||||
pkill -f "pylance.*server" 2>/dev/null
|
||||
pkill -f "language.*server" 2>/dev/null
|
||||
|
||||
# Kill utility processes with high memory
|
||||
for pid in $(ps aux | grep 'code.*utility' | awk '$6 > 500000 {print $2}'); do
|
||||
if [ -n "$pid" ]; then
|
||||
echo "Force stopping utility process: $pid"
|
||||
kill -9 "$pid" 2>/dev/null
|
||||
fi
|
||||
done
|
||||
|
||||
sleep 5
|
||||
echo "✅ Aggressive cleanup completed"
|
||||
}
|
||||
|
||||
# Function to restart VS Code safely
|
||||
restart_vscode() {
|
||||
echo "🔄 Restarting VS Code..."
|
||||
|
||||
# Save current workspace
|
||||
WORKSPACE_PATH="$PWD"
|
||||
|
||||
# Gracefully close VS Code
|
||||
pkill -15 code 2>/dev/null
|
||||
sleep 5
|
||||
|
||||
# Force close if still running
|
||||
pkill -9 code 2>/dev/null
|
||||
sleep 2
|
||||
|
||||
# Clean up leftover processes
|
||||
pkill -f "vscode" 2>/dev/null
|
||||
|
||||
echo "VS Code stopped. Starting in 3 seconds..."
|
||||
sleep 3
|
||||
|
||||
# Restart VS Code with the current workspace
|
||||
cd "$WORKSPACE_PATH"
|
||||
nohup code . > /dev/null 2>&1 &
|
||||
|
||||
echo "✅ VS Code restarted"
|
||||
}
|
||||
|
||||
# Interactive menu
|
||||
case "${1:-menu}" in
|
||||
"status")
|
||||
show_status
|
||||
;;
|
||||
"gentle")
|
||||
show_status
|
||||
gentle_cleanup
|
||||
show_status
|
||||
;;
|
||||
"aggressive")
|
||||
show_status
|
||||
aggressive_cleanup
|
||||
show_status
|
||||
;;
|
||||
"restart")
|
||||
restart_vscode
|
||||
;;
|
||||
"full")
|
||||
show_status
|
||||
echo "Performing full cleanup and restart..."
|
||||
gentle_cleanup
|
||||
sleep 2
|
||||
aggressive_cleanup
|
||||
sleep 2
|
||||
restart_vscode
|
||||
;;
|
||||
"menu")
|
||||
show_status
|
||||
echo "Available actions:"
|
||||
echo " status - Show current VS Code status"
|
||||
echo " gentle - Light cleanup (kill high-memory extensions)"
|
||||
echo " aggressive - Heavy cleanup (kill language servers, utilities)"
|
||||
echo " restart - Full VS Code restart"
|
||||
echo " full - Complete cleanup + restart"
|
||||
echo ""
|
||||
echo "Usage: $0 {status|gentle|aggressive|restart|full}"
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $1"
|
||||
$0 menu
|
||||
;;
|
||||
esac
|
||||
112
scripts/vscode_session_monitor.sh
Executable file
112
scripts/vscode_session_monitor.sh
Executable file
@@ -0,0 +1,112 @@
|
||||
#!/bin/bash
|
||||
|
||||
# VS Code Session Monitor and Restart Script
|
||||
# This script monitors VS Code memory usage and session health
|
||||
|
||||
VSCODE_MEMORY_LIMIT=2000000 # 2GB in KB
|
||||
CHECK_INTERVAL=30 # Check every 30 seconds
|
||||
LOG_FILE="/tmp/vscode_monitor.log"
|
||||
|
||||
echo "$(date): Starting VS Code session monitor..." >> "$LOG_FILE"
|
||||
|
||||
monitor_vscode() {
|
||||
while true; do
|
||||
# Get VS Code process info
|
||||
VSCODE_PIDS=$(pgrep -f "code.*--type=(utility|zygote)" | head -5)
|
||||
|
||||
if [ -n "$VSCODE_PIDS" ]; then
|
||||
TOTAL_MEMORY=0
|
||||
PROCESS_COUNT=0
|
||||
|
||||
for pid in $VSCODE_PIDS; do
|
||||
if [ -f "/proc/$pid/status" ]; then
|
||||
MEMORY=$(awk '/VmRSS:/ {print $2}' "/proc/$pid/status" 2>/dev/null || echo 0)
|
||||
TOTAL_MEMORY=$((TOTAL_MEMORY + MEMORY))
|
||||
PROCESS_COUNT=$((PROCESS_COUNT + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
echo "$(date): VS Code processes: $PROCESS_COUNT, Total memory: ${TOTAL_MEMORY}KB" >> "$LOG_FILE"
|
||||
|
||||
# Check if memory usage is too high
|
||||
if [ "$TOTAL_MEMORY" -gt "$VSCODE_MEMORY_LIMIT" ]; then
|
||||
echo "$(date): WARNING - VS Code memory usage too high: ${TOTAL_MEMORY}KB" >> "$LOG_FILE"
|
||||
echo "$(date): Recommend restarting VS Code to prevent crashes" >> "$LOG_FILE"
|
||||
|
||||
# Optional: Force cleanup of specific high-memory processes
|
||||
for pid in $VSCODE_PIDS; do
|
||||
if [ -f "/proc/$pid/status" ]; then
|
||||
MEMORY=$(awk '/VmRSS:/ {print $2}' "/proc/$pid/status" 2>/dev/null || echo 0)
|
||||
if [ "$MEMORY" -gt 500000 ]; then # 500MB
|
||||
echo "$(date): High memory process (PID $pid): ${MEMORY}KB" >> "$LOG_FILE"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
# Check for renderer crashes
|
||||
if grep -q "renderer process gone" ~/.config/Code/logs/*/main.log 2>/dev/null; then
|
||||
echo "$(date): Renderer crash detected - recommend restart" >> "$LOG_FILE"
|
||||
fi
|
||||
fi
|
||||
|
||||
sleep "$CHECK_INTERVAL"
|
||||
done
|
||||
}
|
||||
|
||||
check_session_limits() {
|
||||
echo "=== VS Code Session Limits Analysis ==="
|
||||
echo "Current system limits:"
|
||||
ulimit -a | grep -E "(processes|memory|files)"
|
||||
echo ""
|
||||
|
||||
echo "Current VS Code processes:"
|
||||
ps aux | grep -E "(code|vscode)" | grep -v grep | wc -l
|
||||
echo ""
|
||||
|
||||
echo "VS Code memory usage:"
|
||||
ps -o pid,ppid,pcpu,pmem,rss,args -C code | head -10
|
||||
echo ""
|
||||
|
||||
echo "GitHub Copilot Chat requests (last 50 lines):"
|
||||
find ~/.config/Code/logs -name "*copilot*" -type f -exec tail -50 {} \; 2>/dev/null | grep -c "ccreq:"
|
||||
echo ""
|
||||
|
||||
echo "Recent crashes:"
|
||||
grep "crashed" ~/.config/Code/logs/*/main.log 2>/dev/null | tail -5
|
||||
}
|
||||
|
||||
# Function to clean up VS Code cache and temporary files
|
||||
cleanup_vscode() {
|
||||
echo "$(date): Starting VS Code cleanup..." >> "$LOG_FILE"
|
||||
|
||||
# Clean extension host logs
|
||||
find ~/.config/Code/logs -name "*.log" -size +10M -delete 2>/dev/null
|
||||
|
||||
# Clean old crash dumps
|
||||
find ~/.config/Code/Crashpad -type f -mtime +7 -delete 2>/dev/null
|
||||
|
||||
# Clean temporary MCP sockets
|
||||
find /tmp -name "mcp-*" -type d -mtime +1 -exec rm -rf {} \; 2>/dev/null
|
||||
|
||||
echo "$(date): Cleanup completed" >> "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Main execution
|
||||
case "${1:-check}" in
|
||||
"monitor")
|
||||
monitor_vscode
|
||||
;;
|
||||
"check")
|
||||
check_session_limits
|
||||
;;
|
||||
"cleanup")
|
||||
cleanup_vscode
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {monitor|check|cleanup}"
|
||||
echo " monitor: Start continuous monitoring"
|
||||
echo " check: Show current session limits and status"
|
||||
echo " cleanup: Clean VS Code cache and logs"
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user