ids.alfacom.it/extracted_idf/GUIDA_PULIZIA_IP_FIXED.md
marco370 0bfe3258b5 Saved progress at the end of the loop
Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 1c71ce6e-1a3e-4f53-bb5d-77cdd22b8ea3
2025-11-11 09:15:10 +00:00

150 lines
3.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 🛠️ GUIDA RAPIDA - PULIZIA IP DATABASE (VERSIONE CORRETTA)
## ✅ PROBLEMA RISOLTO
**Errore precedente**: `Illegal mix of collations for operation 'concat'`
**Soluzione**: Conversione esplicita di INT in stringhe con `CAST(valore AS CHAR)`
---
## 🚀 INSTALLAZIONE PROCEDURE CORRETTE
### 1. Esegui il file SQL corretto:
```bash
mysql -h SERVER_IP -u USERNAME -p DATABASE_NAME < cleanup_ddos_ips_hours_fixed.sql
```
### 2. Verifica installazione:
```sql
SHOW PROCEDURE STATUS WHERE Db = 'LOG_MIKROTIK' AND Name LIKE 'cleanup%fixed';
```
---
## 🧪 TEST PROCEDURE CORRETTE
### 1. Test DRY RUN (simulazione):
```sql
-- Simulazione pulizia 1 ora
CALL cleanup_ddos_ips_hours_fixed(1, 'ddos_detect_v03', TRUE);
-- Simulazione pulizia 12 ore
CALL cleanup_ddos_ips_hours_fixed(12, 'ddos_detect_v03', TRUE);
-- Simulazione pulizia 24 ore
CALL cleanup_ddos_ips_hours_fixed(24, 'ddos_detect_v03', TRUE);
```
### 2. Test PULIZIA REALE:
```sql
-- Pulizia effettiva 1 ora
CALL cleanup_ddos_ips_hours_fixed(1, 'ddos_detect_v03', FALSE);
-- Pulizia effettiva 12 ore
CALL cleanup_ddos_ips_hours_fixed(12, 'ddos_detect_v03', FALSE);
```
### 3. Test Procedure Wrapper:
```sql
-- Dry run con valori default (24h)
CALL cleanup_ddos_ips_hours_dry_default_fixed();
-- Pulizia con valori default (24h)
CALL cleanup_ddos_ips_hours_default_fixed();
```
---
## 📊 VERIFICA RISULTATI
### 1. Controlla log operazioni:
```sql
SELECT * FROM ip_cleanup_log
WHERE operation_time >= DATE_SUB(NOW(), INTERVAL 1 HOUR)
ORDER BY operation_time DESC
LIMIT 10;
```
### 2. Verifica IP rimossi:
```sql
-- Conta IP per lista
SELECT list_name, COUNT(*) as total_ips
FROM ip_list
GROUP BY list_name;
-- IP più vecchi di 1 ora
SELECT COUNT(*) as old_ips_1h
FROM ip_list
WHERE list_name = 'ddos_detect_v03'
AND retrieved_at < DATE_SUB(NOW(), INTERVAL 1 HOUR);
```
### 3. Statistiche dettagliate:
```sql
SELECT get_ip_stats('ddos_detect_v03') as stats;
```
---
## 🔧 DIFFERENZE VERSIONE CORRETTA
### ❌ PRIMA (con errore):
```sql
CONCAT('Ritenzione: ', retention_hours, ' ore')
-- ERRORE: mescola INT con stringa
```
### ✅ DOPO (corretta):
```sql
CONCAT('Ritenzione: ', CAST(retention_hours AS CHAR), ' ore')
-- OK: converte INT in stringa esplicitamente
```
---
## 🎯 COMANDI RAPIDI
### Pulizie Immediate:
```sql
CALL cleanup_ddos_ips_hours_fixed(1, 'ddos_detect_v03', FALSE); -- 1h
CALL cleanup_ddos_ips_hours_fixed(12, 'ddos_detect_v03', FALSE); -- 12h
CALL cleanup_ddos_ips_hours_fixed(24, 'ddos_detect_v03', FALSE); -- 24h
```
### Simulazioni:
```sql
CALL cleanup_ddos_ips_hours_fixed(1, 'ddos_detect_v03', TRUE); -- 1h-dry
CALL cleanup_ddos_ips_hours_fixed(12, 'ddos_detect_v03', TRUE); -- 12h-dry
CALL cleanup_ddos_ips_hours_fixed(24, 'ddos_detect_v03', TRUE); -- 24h-dry
```
---
## 📈 OUTPUT ATTESO
### Simulazione Successo:
```
🔍 DRY RUN ORARIO: Verrebbero rimossi 150 IP dalla lista ddos_detect_v03 (>12h)
```
### Pulizia Successo:
```
✅ PULIZIA ORARIA COMPLETATA: Rimossi 150 IP dalla lista ddos_detect_v03 (>12h)
```
### Nessuna Pulizia Necessaria:
```
NESSUNA PULIZIA ORARIA NECESSARIA: Tutti gli IP in ddos_detect_v03 sono più recenti di 1 ore
```
---
## 🛡️ SICUREZZA
- **Transazioni**: Tutte le operazioni sono in transazione con ROLLBACK automatico su errore
- **Logging**: Ogni operazione registrata in `ip_cleanup_log`
- **Dry Run**: Sempre testare prima con `dry_run = TRUE`
- **Backup**: Fare backup database prima di pulizie massive
---
💡 **NOTA**: Ora le procedure dovrebbero funzionare correttamente senza errori di collation!