fix(snmp,db): save FDB to PG (empty inet null), bridge_port + MAC normalize
Made-with: Cursor
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -541,16 +542,23 @@ func (s *PostgresStore) PurgeAllScanData() error {
|
||||
|
||||
func (s *PostgresStore) SavePortDeviceResult(scanID string, result PortDeviceResult) error {
|
||||
query := `
|
||||
insert into scan_port_devices (scan_id, ip, if_index, mac, learned_ip, checked_at)
|
||||
values ($1, $2, $3, $4, $5, $6)`
|
||||
insert into scan_port_devices (scan_id, ip, if_index, bridge_port, mac, learned_ip, checked_at)
|
||||
values ($1, $2, $3, $4, $5, $6, $7)`
|
||||
var learnedIP any
|
||||
if v := strings.TrimSpace(result.LearnedIP); v != "" {
|
||||
learnedIP = v
|
||||
} else {
|
||||
learnedIP = nil
|
||||
}
|
||||
_, err := s.db.ExecContext(
|
||||
context.Background(),
|
||||
query,
|
||||
scanID,
|
||||
result.IP,
|
||||
result.IfIndex,
|
||||
result.BridgePort,
|
||||
result.MAC,
|
||||
result.LearnedIP,
|
||||
learnedIP,
|
||||
result.CheckedAt,
|
||||
)
|
||||
return err
|
||||
@@ -561,11 +569,11 @@ func (s *PostgresStore) ListPortDeviceResults(scanID string, filterIP string, fi
|
||||
limit = 20000
|
||||
}
|
||||
query := `
|
||||
select ip::text, if_index, coalesce(mac, ''), coalesce(learned_ip::text, ''), checked_at
|
||||
select ip::text, if_index, bridge_port, coalesce(mac, ''), coalesce(learned_ip::text, ''), checked_at
|
||||
from scan_port_devices
|
||||
where scan_id = $1
|
||||
and ($2 = '' or ip = $2::inet)
|
||||
and ($3 = 0 or if_index = $3)
|
||||
and ($3 = 0 or if_index = $3 or bridge_port = $3)
|
||||
order by if_index asc, mac asc, learned_ip asc
|
||||
limit $4`
|
||||
rows, err := s.db.QueryContext(context.Background(), query, scanID, filterIP, filterIfIndex, limit)
|
||||
@@ -577,7 +585,7 @@ limit $4`
|
||||
out := make([]PortDeviceResult, 0, 256)
|
||||
for rows.Next() {
|
||||
var r PortDeviceResult
|
||||
if err = rows.Scan(&r.IP, &r.IfIndex, &r.MAC, &r.LearnedIP, &r.CheckedAt); err != nil {
|
||||
if err = rows.Scan(&r.IP, &r.IfIndex, &r.BridgePort, &r.MAC, &r.LearnedIP, &r.CheckedAt); err != nil {
|
||||
return nil
|
||||
}
|
||||
out = append(out, r)
|
||||
|
||||
Reference in New Issue
Block a user