feat(ui,snmp): IF-MIB интерфейсы в табл.1 связей, слияние с LLDP, сортировка по локальному порту
Made-with: Cursor
This commit is contained in:
@@ -475,3 +475,60 @@ limit $2`
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *PostgresStore) SaveInterfaceResult(scanID string, result InterfaceResult) error {
|
||||
query := `
|
||||
insert into scan_interfaces (scan_id, ip, if_index, if_descr, if_name, if_oper_status, checked_at)
|
||||
values ($1, $2, $3, $4, $5, $6, $7)`
|
||||
_, err := s.db.ExecContext(
|
||||
context.Background(),
|
||||
query,
|
||||
scanID,
|
||||
result.IP,
|
||||
result.IfIndex,
|
||||
result.IfDescr,
|
||||
result.IfName,
|
||||
result.IfOperStatus,
|
||||
result.CheckedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PostgresStore) ListInterfaceResults(scanID string, filterIP string, limit int) []InterfaceResult {
|
||||
if limit <= 0 {
|
||||
limit = 4096
|
||||
}
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
if filterIP != "" {
|
||||
rows, err = s.db.QueryContext(context.Background(), `
|
||||
select ip::text, if_index, coalesce(if_descr, ''), coalesce(if_name, ''), coalesce(if_oper_status, ''), checked_at
|
||||
from scan_interfaces
|
||||
where scan_id = $1 and ip = $2::inet
|
||||
order by if_index asc
|
||||
limit $3`,
|
||||
scanID, filterIP, limit)
|
||||
} else {
|
||||
rows, err = s.db.QueryContext(context.Background(), `
|
||||
select ip::text, if_index, coalesce(if_descr, ''), coalesce(if_name, ''), coalesce(if_oper_status, ''), checked_at
|
||||
from scan_interfaces
|
||||
where scan_id = $1
|
||||
order by ip asc, if_index asc
|
||||
limit $2`,
|
||||
scanID, limit)
|
||||
}
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]InterfaceResult, 0, 256)
|
||||
for rows.Next() {
|
||||
var r InterfaceResult
|
||||
if err = rows.Scan(&r.IP, &r.IfIndex, &r.IfDescr, &r.IfName, &r.IfOperStatus, &r.CheckedAt); err != nil {
|
||||
return nil
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -101,13 +102,18 @@ func (r *Runner) run(job ScanJob) {
|
||||
}
|
||||
}
|
||||
if isUp && r.cfg.SNMPEnabled {
|
||||
snmpRes, lldpRes := probeSNMPAndLLDP(ip, r.cfg.SNMPCommunity, checkedAt, job.Options.PingTimeoutMS)
|
||||
snmpRes, lldpRes, ifRes := probeSNMPAndLLDP(ip, r.cfg.SNMPCommunity, checkedAt, job.Options.PingTimeoutMS)
|
||||
_ = r.store.SaveSNMPResult(job.ID, snmpRes)
|
||||
for _, lr := range lldpRes {
|
||||
if err := r.store.SaveLLDPResult(job.ID, lr); err != nil {
|
||||
log.Printf("scan %s: save LLDP для %s: %v", job.ID, lr.IP, err)
|
||||
}
|
||||
}
|
||||
for _, iface := range ifRes {
|
||||
if err := r.store.SaveInterfaceResult(job.ID, iface); err != nil {
|
||||
log.Printf("scan %s: save interface %s ifIndex=%d: %v", job.ID, iface.IP, iface.IfIndex, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
@@ -147,7 +153,7 @@ func (r *Runner) run(job ScanJob) {
|
||||
})
|
||||
}
|
||||
|
||||
func probeSNMPAndLLDP(ip, community string, checkedAt time.Time, timeoutMS int) (SNMPResult, []LLDPResult) {
|
||||
func probeSNMPAndLLDP(ip, community string, checkedAt time.Time, timeoutMS int) (SNMPResult, []LLDPResult, []InterfaceResult) {
|
||||
if timeoutMS <= 0 {
|
||||
timeoutMS = 700
|
||||
}
|
||||
@@ -164,7 +170,7 @@ func probeSNMPAndLLDP(ip, community string, checkedAt time.Time, timeoutMS int)
|
||||
Retries: 1,
|
||||
}
|
||||
if err := client.Connect(); err != nil {
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}, nil
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}, nil, nil
|
||||
}
|
||||
defer client.Conn.Close()
|
||||
|
||||
@@ -175,7 +181,7 @@ func probeSNMPAndLLDP(ip, community string, checkedAt time.Time, timeoutMS int)
|
||||
}
|
||||
pkt, err := client.Get(oids)
|
||||
if err != nil {
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}, nil
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}, nil, nil
|
||||
}
|
||||
|
||||
out := SNMPResult{IP: ip, Success: true, CheckedAt: checkedAt}
|
||||
@@ -200,7 +206,84 @@ func probeSNMPAndLLDP(ip, community string, checkedAt time.Time, timeoutMS int)
|
||||
client.MaxRepetitions = 12
|
||||
client.Retries = 2
|
||||
|
||||
return out, probeLLDP(client, ip, checkedAt)
|
||||
lldpRes := probeLLDP(client, ip, checkedAt)
|
||||
var ifList []InterfaceResult
|
||||
if out.Success {
|
||||
ifList = probeIfTable(client, ip, checkedAt)
|
||||
}
|
||||
return out, lldpRes, ifList
|
||||
}
|
||||
|
||||
// probeIfTable собирает ifDescr, ifName (ifXTable), ifOperStatus по IF-MIB для списка портов в UI.
|
||||
func probeIfTable(client *gosnmp.GoSNMP, ip string, checkedAt time.Time) []InterfaceResult {
|
||||
descr := walkAsMap(client, ".1.3.6.1.2.1.2.2.1.2")
|
||||
names := walkAsMap(client, ".1.3.6.1.2.1.31.1.1.1.1")
|
||||
oper := walkAsMap(client, ".1.3.6.1.2.1.2.2.1.8")
|
||||
idxs := mergeIFMIBKeys(descr, names, oper)
|
||||
const maxIF = 2048
|
||||
if len(idxs) > maxIF {
|
||||
idxs = idxs[:maxIF]
|
||||
}
|
||||
out := make([]InterfaceResult, 0, len(idxs))
|
||||
for _, ks := range idxs {
|
||||
idx, err := strconv.Atoi(ks)
|
||||
if err != nil || idx <= 0 {
|
||||
continue
|
||||
}
|
||||
out = append(out, InterfaceResult{
|
||||
IP: ip,
|
||||
IfIndex: idx,
|
||||
IfDescr: descr[ks],
|
||||
IfName: names[ks],
|
||||
IfOperStatus: mapIfOperStatus(oper[ks]),
|
||||
CheckedAt: checkedAt,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mergeIFMIBKeys(maps ...map[string]string) []string {
|
||||
seen := make(map[string]struct{})
|
||||
for _, m := range maps {
|
||||
for k := range m {
|
||||
seen[k] = struct{}{}
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(seen))
|
||||
for k := range seen {
|
||||
out = append(out, k)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
ai, e1 := strconv.Atoi(out[i])
|
||||
bj, e2 := strconv.Atoi(out[j])
|
||||
if e1 != nil || e2 != nil {
|
||||
return out[i] < out[j]
|
||||
}
|
||||
return ai < bj
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
func mapIfOperStatus(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
switch s {
|
||||
case "1":
|
||||
return "up"
|
||||
case "2":
|
||||
return "down"
|
||||
case "3":
|
||||
return "testing"
|
||||
case "4":
|
||||
return "unknown"
|
||||
case "5":
|
||||
return "dormant"
|
||||
case "6":
|
||||
return "notPresent"
|
||||
case "7":
|
||||
return "lowerLayerDown"
|
||||
default:
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
// snmpBytesToDisplay строка для OCTET STRING SNMP: UTF-8 текст, иначе MAC (6 байт) или 0x+hex.
|
||||
|
||||
+65
-11
@@ -67,6 +67,8 @@ type Store interface {
|
||||
ListSNMPResults(scanID string, limit int) []SNMPResult
|
||||
SaveLLDPResult(scanID string, result LLDPResult) error
|
||||
ListLLDPResults(scanID string, limit int) []LLDPResult
|
||||
SaveInterfaceResult(scanID string, result InterfaceResult) error
|
||||
ListInterfaceResults(scanID string, filterIP string, limit int) []InterfaceResult
|
||||
}
|
||||
|
||||
type HostResult struct {
|
||||
@@ -101,22 +103,34 @@ type LLDPResult struct {
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
// InterfaceResult — строка IF-MIB (interfaces/ifTable + ifName) для устройства.
|
||||
type InterfaceResult struct {
|
||||
IP string `json:"ip"`
|
||||
IfIndex int `json:"if_index"`
|
||||
IfDescr string `json:"if_descr"`
|
||||
IfName string `json:"if_name"`
|
||||
IfOperStatus string `json:"if_oper_status"`
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
jobs map[string]ScanJob
|
||||
hosts map[string][]HostResult
|
||||
ports map[string][]OpenPortResult
|
||||
snmp map[string][]SNMPResult
|
||||
lldp map[string][]LLDPResult
|
||||
mu sync.RWMutex
|
||||
jobs map[string]ScanJob
|
||||
hosts map[string][]HostResult
|
||||
ports map[string][]OpenPortResult
|
||||
snmp map[string][]SNMPResult
|
||||
lldp map[string][]LLDPResult
|
||||
ifaces map[string][]InterfaceResult
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
jobs: make(map[string]ScanJob),
|
||||
hosts: make(map[string][]HostResult),
|
||||
ports: make(map[string][]OpenPortResult),
|
||||
snmp: make(map[string][]SNMPResult),
|
||||
lldp: make(map[string][]LLDPResult),
|
||||
jobs: make(map[string]ScanJob),
|
||||
hosts: make(map[string][]HostResult),
|
||||
ports: make(map[string][]OpenPortResult),
|
||||
snmp: make(map[string][]SNMPResult),
|
||||
lldp: make(map[string][]LLDPResult),
|
||||
ifaces: make(map[string][]InterfaceResult),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,6 +319,46 @@ func (s *MemoryStore) ListLLDPResults(scanID string, limit int) []LLDPResult {
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *MemoryStore) SaveInterfaceResult(scanID string, result InterfaceResult) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if _, ok := s.jobs[scanID]; !ok {
|
||||
return errors.New("scan not found")
|
||||
}
|
||||
s.ifaces[scanID] = append(s.ifaces[scanID], result)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ListInterfaceResults(scanID string, filterIP string, limit int) []InterfaceResult {
|
||||
if limit <= 0 {
|
||||
limit = 4096
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
items := s.ifaces[scanID]
|
||||
var filtered []InterfaceResult
|
||||
if filterIP != "" {
|
||||
for _, r := range items {
|
||||
if r.IP == filterIP {
|
||||
filtered = append(filtered, r)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
filtered = append(filtered, items...)
|
||||
}
|
||||
sort.Slice(filtered, func(i, j int) bool { return filtered[i].IfIndex < filtered[j].IfIndex })
|
||||
if len(filtered) <= limit {
|
||||
out := make([]InterfaceResult, len(filtered))
|
||||
copy(out, filtered)
|
||||
return out
|
||||
}
|
||||
out := make([]InterfaceResult, limit)
|
||||
copy(out, filtered[:limit])
|
||||
return out
|
||||
}
|
||||
|
||||
func applyDefaultOptions(opts *ScanOptions) {
|
||||
if opts.PingTimeoutMS <= 0 {
|
||||
opts.PingTimeoutMS = 700
|
||||
|
||||
Reference in New Issue
Block a user