Correct the rsyslog template to include timestamps in logs, ensuring compatibility with the Python parser. This change also refactors the log filtering to capture only incoming connections, significantly reducing log volume. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: c2f849f9-105f-452a-bdc3-a956d102c54b Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/6ZTQSoP
206 lines
6.1 KiB
Markdown
206 lines
6.1 KiB
Markdown
# Migrazione a Log Solo Connessioni in Ingresso
|
|
|
|
## Panoramica
|
|
Il filtro MikroTik è stato modificato per catturare **solo connessioni in ingresso**, riducendo drasticamente il volume di log e migliorando le prestazioni del sistema IDS.
|
|
|
|
## Modifiche Implementate
|
|
|
|
### 1. **Configurazione RSyslog Corretta**
|
|
Il file `deployment/rsyslog/99-mikrotik.conf` è stato aggiornato per includere **timestamp completo** nei log salvati:
|
|
|
|
**PRIMA** (❌ problematico):
|
|
```bash
|
|
template(name="MikroTikRawFormat" type="string" string="%msg%\n")
|
|
```
|
|
Salvava solo: `forward: in:<pppoe-user> ...`
|
|
|
|
**ADESSO** (✅ corretto):
|
|
```bash
|
|
template(name="MikroTikRawFormat" type="string" string="%TIMESTAMP% %HOSTNAME% %msg%\n")
|
|
```
|
|
Salva: `Nov 22 08:15:30 FIBRA forward: in:<pppoe-user> ...`
|
|
|
|
### 2. **Parser Python Compatibile**
|
|
Il parser `python_ml/syslog_parser.py` è **100% compatibile** con il nuovo formato:
|
|
- ✅ Gestisce log "forward" e "detected-ddos forward"
|
|
- ✅ Estrae interfacce in/out: `in:<pppoe-xxx> out:sfp-xxx`
|
|
- ✅ Supporta src-mac opzionale
|
|
- ✅ Parse TCP flags: `(SYN)`, `(ACK,PSH)`, etc.
|
|
- ✅ Gestisce NAT info opzionale
|
|
|
|
### 3. **Formato Log Supportato**
|
|
```
|
|
Nov 22 08:00:00 FIBRA forward: in:<pppoe-franco.alfano> out:sfp-sfpplus2_VS_AS, connection-state:new proto TCP (SYN), 10.0.254.77:53783->52.213.60.221:443, len 64
|
|
Nov 22 08:00:01 FIBRA detected-ddos forward: in:sfp-sfpplus2_VS_AS out:VLAN53_PPOE_DATACENTER, connection-state:new src-mac 18:fd:74:7c:aa:85, proto TCP (SYN), 172.217.38.146:35055->185.203.24.95:993, len 60
|
|
Nov 22 08:00:02 DATACENTER forward: in:VLAN53_PPOE_DATACENTER out:sfp-sfpplus2_VS_AS, connection-state:new src-mac 00:50:56:88:61:c7, proto UDP, 185.203.24.22:27417->8.8.8.8:53, len 79
|
|
```
|
|
|
|
## Procedura di Migrazione sul Server
|
|
|
|
### STEP 1: Backup Configurazione Attuale
|
|
```bash
|
|
sudo cp /etc/rsyslog.d/99-mikrotik.conf /etc/rsyslog.d/99-mikrotik.conf.backup
|
|
sudo cp /var/log/mikrotik/raw.log /var/log/mikrotik/raw.log.backup
|
|
```
|
|
|
|
### STEP 2: Push da Replit
|
|
```bash
|
|
./push-gitlab.sh
|
|
```
|
|
**Messaggio commit**: "Fix rsyslog template - Add timestamp to logs"
|
|
|
|
### STEP 3: Aggiornamento sul Server AlmaLinux
|
|
```bash
|
|
cd /opt/ids
|
|
sudo ./deployment/update_from_git.sh
|
|
```
|
|
|
|
Questo eseguirà automaticamente:
|
|
1. Git pull delle modifiche
|
|
2. Riapplicazione configurazione rsyslog corretta (`setup_rsyslog.sh`)
|
|
3. Restart servizio rsyslog
|
|
4. Restart servizio syslog parser
|
|
|
|
### STEP 4: Verifica Funzionamento
|
|
|
|
#### 4.1 Verifica Template RSyslog
|
|
```bash
|
|
grep "template.*MikroTikRawFormat" /etc/rsyslog.d/99-mikrotik.conf
|
|
```
|
|
**Output atteso**:
|
|
```
|
|
template(name="MikroTikRawFormat" type="string" string="%TIMESTAMP% %HOSTNAME% %msg%\n")
|
|
```
|
|
|
|
#### 4.2 Verifica Log con Timestamp
|
|
```bash
|
|
tail -5 /var/log/mikrotik/raw.log
|
|
```
|
|
**Output atteso** (con timestamp!):
|
|
```
|
|
Nov 22 08:15:30 FIBRA forward: in:<pppoe-user> out:sfp-xxx, ...
|
|
Nov 22 08:15:31 FIBRA detected-ddos forward: in:sfp-xxx out:VLAN53, ...
|
|
```
|
|
|
|
Se vedi log **senza timestamp**, il template non è stato applicato correttamente!
|
|
|
|
#### 4.3 Verifica Parser Funzionante
|
|
```bash
|
|
sudo systemctl status ids-syslog-parser
|
|
sudo journalctl -u ids-syslog-parser -n 50 --no-pager
|
|
```
|
|
**Output atteso**:
|
|
```
|
|
[INFO] Processate N righe, salvate M log
|
|
```
|
|
|
|
#### 4.4 Verifica Database Popolato
|
|
```bash
|
|
psql $DATABASE_URL -c "SELECT COUNT(*), MIN(timestamp), MAX(timestamp) FROM network_logs WHERE timestamp > NOW() - INTERVAL '5 minutes';"
|
|
```
|
|
**Output atteso**:
|
|
```
|
|
count | min | max
|
|
-------+-------------------------+-------------------------
|
|
150 | 2025-11-22 08:10:00 | 2025-11-22 08:15:00
|
|
```
|
|
|
|
Se `count = 0`, il parser NON sta processando i log! Verifica il formato.
|
|
|
|
### STEP 5: Test End-to-End
|
|
|
|
#### 5.1 Genera Traffico di Test
|
|
Dal MikroTik, genera alcune connessioni:
|
|
```bash
|
|
# Ping per generare log UDP
|
|
/ping 8.8.8.8 count=5
|
|
```
|
|
|
|
#### 5.2 Verifica Arrivo Log
|
|
```bash
|
|
# Attendi 10 secondi
|
|
sleep 10
|
|
|
|
# Verifica ultimi log
|
|
tail -10 /var/log/mikrotik/raw.log
|
|
|
|
# Verifica database
|
|
psql $DATABASE_URL -c "SELECT COUNT(*) FROM network_logs WHERE timestamp > NOW() - INTERVAL '1 minute';"
|
|
```
|
|
|
|
## Rollback in Caso di Problemi
|
|
|
|
Se qualcosa va storto, ripristina la configurazione precedente:
|
|
|
|
```bash
|
|
# Ripristina rsyslog config
|
|
sudo cp /etc/rsyslog.d/99-mikrotik.conf.backup /etc/rsyslog.d/99-mikrotik.conf
|
|
|
|
# Restart rsyslog
|
|
sudo systemctl restart rsyslog
|
|
|
|
# Restart parser
|
|
sudo systemctl restart ids-syslog-parser
|
|
```
|
|
|
|
## Benefici della Migrazione
|
|
|
|
### Prima (tutte le connessioni):
|
|
- ⚠️ **417 MILIONI di log** accumulati in poche settimane
|
|
- ⚠️ Database pieno ogni 7 giorni
|
|
- ⚠️ Pulizia giornaliera necessaria
|
|
|
|
### Adesso (solo connessioni in ingresso):
|
|
- ✅ **Volume ridotto del 50-70%** (stima)
|
|
- ✅ Retention 7 giorni più che sufficiente
|
|
- ✅ Training ML più veloce (meno dati da processare)
|
|
- ✅ Database stabile e performante
|
|
|
|
## Troubleshooting
|
|
|
|
### Problema: Log senza timestamp in /var/log/mikrotik/raw.log
|
|
**Causa**: Template rsyslog non applicato
|
|
**Soluzione**:
|
|
```bash
|
|
sudo /opt/ids/deployment/setup_rsyslog.sh
|
|
sudo systemctl restart rsyslog
|
|
```
|
|
|
|
### Problema: Parser NON salva dati in database
|
|
**Causa**: Parser non riesce a parsare log senza timestamp
|
|
**Soluzione**:
|
|
```bash
|
|
# Verifica formato log
|
|
head -5 /var/log/mikrotik/raw.log
|
|
|
|
# Se manca timestamp, applica fix rsyslog (vedi sopra)
|
|
|
|
# Restart parser dopo fix
|
|
sudo systemctl restart ids-syslog-parser
|
|
```
|
|
|
|
### Problema: Database non si popola
|
|
**Causa**: Connessione database fallita o credenziali errate
|
|
**Soluzione**:
|
|
```bash
|
|
# Verifica connessione
|
|
psql $DATABASE_URL -c "SELECT 1;"
|
|
|
|
# Verifica errori parser
|
|
sudo journalctl -u ids-syslog-parser -n 100 --no-pager | grep ERROR
|
|
```
|
|
|
|
## Note Importanti
|
|
|
|
1. **NON modificare manualmente** `/var/log/mikrotik/raw.log` - è gestito da rsyslog
|
|
2. **Dopo ogni modifica rsyslog**, riavvia il servizio: `sudo systemctl restart rsyslog`
|
|
3. **Pulizia automatica** dei log vecchi è configurata nel cron (ore 03:00)
|
|
4. **Retention 7 giorni** è sufficiente per training ML
|
|
|
|
## Riferimenti
|
|
|
|
- Configurazione RSyslog: `deployment/rsyslog/99-mikrotik.conf`
|
|
- Script Setup RSyslog: `deployment/setup_rsyslog.sh`
|
|
- Parser Python: `python_ml/syslog_parser.py`
|
|
- Cleanup automatico: `deployment/setup_cron_cleanup.sh`
|