Files
dtic-DIIAA/adn/tools/scripts/dasuten_sql_monitor.rb
T

103 lines
3.2 KiB
Ruby

#!/usr/bin/env ruby
# frozen_string_literal: true
# Monitor de instalación SQL Server en dasu-sql4 vía W-Zombi
# Ejecutar como dron: ./adn/tools/run dron lanzar --nota "Monitor SQL Install dasu-sql4" -- ruby adn/tools/scripts/dasuten_sql_monitor.rb
require 'json'
require 'fileutils'
TELE_LOG = File.expand_path("../w-zombi/data/telemetria.log", __dir__)
WZOMBI = File.expand_path("../w-zombi/bin/w-zombi", __dir__)
def wzombi_cmd(cmd)
system("ruby", WZOMBI, "cmd", cmd)
end
def last_result_for(id_prefix)
lines = File.readlines(TELE_LOG, encoding: "utf-8")
result_line = lines.reverse.find { |l| l.include?("RESULTADO [ID:#{id_prefix}]") }
return nil unless result_line
result_line
end
def wait_for_result(target_id, timeout: 600, interval: 30)
elapsed = 0
loop do
lines = File.readlines(TELE_LOG, encoding: "utf-8")
result = lines.reverse.find { |l| l.include?("[ID:#{target_id}]") && (l.include?("RESULTADO") || l.include?("ERROR")) }
if result
puts "✅ Resultado detectado: #{result.strip}"
return result
end
elapsed += interval
if elapsed >= timeout
puts "⏰ Timeout esperando resultado de ID:#{target_id} (#{timeout}s)"
return nil
end
puts "⏳ Esperando resultado ID:#{target_id}... (#{elapsed}s/#{timeout}s)"
sleep interval
end
end
def get_last_id
lines = File.readlines(TELE_LOG, encoding: "utf-8")
ids = lines.grep(/EJECUTANDO \[ID:(\d+)\]/) { $1.to_i }
ids.max
end
# === FASE 1: Esperar que termine la instalación SQL (ID:221) ===
puts "🔍 Fase 1: Esperando finalización de instalación SQL Server (ID:221)..."
result = wait_for_result(221, timeout: 900, interval: 30)
if result.nil?
puts "❌ Timeout esperando instalación SQL. Abortando."
exit 1
end
if result.include?("ERROR")
puts "❌ Error en instalación SQL: #{result.strip}"
exit 1
end
puts "✅ Instalación SQL completada."
# === FASE 2: Abrir firewall ===
puts "\n🔥 Fase 2: Configurando regla de firewall para puerto 1433..."
wzombi_cmd("New-NetFirewallRule -DisplayName SQL-Server -Direction Inbound -LocalPort 1433 -Protocol TCP -Action Allow -Enabled True")
sleep 20
fw_id = get_last_id
fw_result = wait_for_result(fw_id, timeout: 60)
puts fw_result ? "✅ Firewall configurado." : "⚠️ Sin confirmación de firewall."
# === FASE 3: Verificar servicio SQL ===
puts "\n🔍 Fase 3: Verificando servicio MSSQLSERVER..."
wzombi_cmd("Get-Service MSSQLSERVER | Select-Object Status, StartType | ConvertTo-Json")
sleep 20
svc_id = get_last_id
svc_result = wait_for_result(svc_id, timeout: 60)
if svc_result
puts "✅ Estado del servicio SQL: #{svc_result.strip}"
else
puts "⚠️ No se pudo verificar el servicio."
end
# === FASE 4: Test sqlcmd ===
puts "\n🔍 Fase 4: Verificando login SA con sqlcmd..."
wzombi_cmd('sqlcmd -S localhost -U sa -P UTNlarioja00DASU -Q "SELECT @@VERSION"')
sleep 20
sql_id = get_last_id
sql_result = wait_for_result(sql_id, timeout: 60)
if sql_result
puts "✅ SQL Server respondió: #{sql_result.strip}"
else
puts "⚠️ No se pudo verificar sqlcmd."
end
puts "\n🎯 === MONITOR COMPLETADO ==="
puts "SQL Server 2019 instalado y verificado en dasu-sql4."
puts "Próximo paso: transferir y restaurar sysdasuten_compressed_ADN.bak"