Add LLDP neighbor collection and endpoint.
Collect LLDP-MIB neighbor fields during SNMP scans, persist records, and expose LLDP results via REST. Made-with: Cursor
This commit is contained in:
@@ -413,3 +413,56 @@ limit $2`
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *PostgresStore) SaveLLDPResult(scanID string, result LLDPResult) error {
|
||||
query := `
|
||||
insert into scan_lldp (scan_id, ip, local_port_num, remote_chassis_id, remote_port_id, remote_sys_name, checked_at)
|
||||
values ($1, $2, $3, $4, $5, $6, $7)`
|
||||
_, err := s.db.ExecContext(
|
||||
context.Background(),
|
||||
query,
|
||||
scanID,
|
||||
result.IP,
|
||||
result.LocalPortNum,
|
||||
result.RemoteChassisID,
|
||||
result.RemotePortID,
|
||||
result.RemoteSysName,
|
||||
result.CheckedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PostgresStore) ListLLDPResults(scanID string, limit int) []LLDPResult {
|
||||
if limit <= 0 {
|
||||
limit = 2000
|
||||
}
|
||||
query := `
|
||||
select ip::text, coalesce(local_port_num, ''), coalesce(remote_chassis_id, ''),
|
||||
coalesce(remote_port_id, ''), coalesce(remote_sys_name, ''), checked_at
|
||||
from scan_lldp
|
||||
where scan_id = $1
|
||||
order by checked_at desc
|
||||
limit $2`
|
||||
rows, err := s.db.QueryContext(context.Background(), query, scanID, limit)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]LLDPResult, 0, limit)
|
||||
for rows.Next() {
|
||||
var lr LLDPResult
|
||||
if err = rows.Scan(
|
||||
&lr.IP,
|
||||
&lr.LocalPortNum,
|
||||
&lr.RemoteChassisID,
|
||||
&lr.RemotePortID,
|
||||
&lr.RemoteSysName,
|
||||
&lr.CheckedAt,
|
||||
); err != nil {
|
||||
return nil
|
||||
}
|
||||
out = append(out, lr)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/netip"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -97,7 +98,11 @@ func (r *Runner) run(job ScanJob) {
|
||||
}
|
||||
}
|
||||
if isUp && r.cfg.SNMPEnabled {
|
||||
_ = r.store.SaveSNMPResult(job.ID, probeSNMP(ip, r.cfg.SNMPCommunity, checkedAt, job.Options.PingTimeoutMS))
|
||||
snmpRes, lldpRes := probeSNMPAndLLDP(ip, r.cfg.SNMPCommunity, checkedAt, job.Options.PingTimeoutMS)
|
||||
_ = r.store.SaveSNMPResult(job.ID, snmpRes)
|
||||
for _, lr := range lldpRes {
|
||||
_ = r.store.SaveLLDPResult(job.ID, lr)
|
||||
}
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
@@ -137,7 +142,7 @@ func (r *Runner) run(job ScanJob) {
|
||||
})
|
||||
}
|
||||
|
||||
func probeSNMP(ip, community string, checkedAt time.Time, timeoutMS int) SNMPResult {
|
||||
func probeSNMPAndLLDP(ip, community string, checkedAt time.Time, timeoutMS int) (SNMPResult, []LLDPResult) {
|
||||
if timeoutMS <= 0 {
|
||||
timeoutMS = 700
|
||||
}
|
||||
@@ -154,7 +159,7 @@ func probeSNMP(ip, community string, checkedAt time.Time, timeoutMS int) SNMPRes
|
||||
Retries: 1,
|
||||
}
|
||||
if err := client.Connect(); err != nil {
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}, nil
|
||||
}
|
||||
defer client.Conn.Close()
|
||||
|
||||
@@ -165,7 +170,7 @@ func probeSNMP(ip, community string, checkedAt time.Time, timeoutMS int) SNMPRes
|
||||
}
|
||||
pkt, err := client.Get(oids)
|
||||
if err != nil {
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}
|
||||
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}, nil
|
||||
}
|
||||
|
||||
out := SNMPResult{IP: ip, Success: true, CheckedAt: checkedAt}
|
||||
@@ -179,7 +184,7 @@ func probeSNMP(ip, community string, checkedAt time.Time, timeoutMS int) SNMPRes
|
||||
out.SysObjectID = snmpValueToString(vb.Value)
|
||||
}
|
||||
}
|
||||
return out
|
||||
return out, probeLLDP(client, ip, checkedAt)
|
||||
}
|
||||
|
||||
func snmpValueToString(v any) string {
|
||||
@@ -189,6 +194,65 @@ func snmpValueToString(v any) string {
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
|
||||
func probeLLDP(client *gosnmp.GoSNMP, ip string, checkedAt time.Time) []LLDPResult {
|
||||
const (
|
||||
lldpRemLocalPortNum = ".1.0.8802.1.1.2.1.4.1.1.2"
|
||||
lldpRemChassisID = ".1.0.8802.1.1.2.1.4.1.1.5"
|
||||
lldpRemPortID = ".1.0.8802.1.1.2.1.4.1.1.7"
|
||||
lldpRemSysName = ".1.0.8802.1.1.2.1.4.1.1.9"
|
||||
)
|
||||
|
||||
ports := walkAsMap(client, lldpRemLocalPortNum)
|
||||
chassis := walkAsMap(client, lldpRemChassisID)
|
||||
remotePorts := walkAsMap(client, lldpRemPortID)
|
||||
sysNames := walkAsMap(client, lldpRemSysName)
|
||||
|
||||
keys := make(map[string]struct{})
|
||||
for k := range ports {
|
||||
keys[k] = struct{}{}
|
||||
}
|
||||
for k := range chassis {
|
||||
keys[k] = struct{}{}
|
||||
}
|
||||
for k := range remotePorts {
|
||||
keys[k] = struct{}{}
|
||||
}
|
||||
for k := range sysNames {
|
||||
keys[k] = struct{}{}
|
||||
}
|
||||
|
||||
out := make([]LLDPResult, 0, len(keys))
|
||||
for k := range keys {
|
||||
item := LLDPResult{
|
||||
IP: ip,
|
||||
LocalPortNum: ports[k],
|
||||
RemoteChassisID: chassis[k],
|
||||
RemotePortID: remotePorts[k],
|
||||
RemoteSysName: sysNames[k],
|
||||
CheckedAt: checkedAt,
|
||||
}
|
||||
if item.LocalPortNum == "" && item.RemoteChassisID == "" && item.RemotePortID == "" && item.RemoteSysName == "" {
|
||||
continue
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func walkAsMap(client *gosnmp.GoSNMP, oid string) map[string]string {
|
||||
out := map[string]string{}
|
||||
pdus, err := client.BulkWalkAll(oid)
|
||||
if err != nil {
|
||||
return out
|
||||
}
|
||||
prefix := oid + "."
|
||||
for _, p := range pdus {
|
||||
key := strings.TrimPrefix(p.Name, prefix)
|
||||
out[key] = snmpValueToString(p.Value)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func expandTargets(cidrs []string, excludeIPs []string) ([]string, error) {
|
||||
excluded := make(map[string]struct{}, len(excludeIPs))
|
||||
for _, ip := range excludeIPs {
|
||||
|
||||
@@ -65,6 +65,8 @@ type Store interface {
|
||||
ListOpenPortResults(scanID string, limit int) []OpenPortResult
|
||||
SaveSNMPResult(scanID string, result SNMPResult) error
|
||||
ListSNMPResults(scanID string, limit int) []SNMPResult
|
||||
SaveLLDPResult(scanID string, result LLDPResult) error
|
||||
ListLLDPResults(scanID string, limit int) []LLDPResult
|
||||
}
|
||||
|
||||
type HostResult struct {
|
||||
@@ -90,12 +92,22 @@ type SNMPResult struct {
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
type LLDPResult struct {
|
||||
IP string `json:"ip"`
|
||||
LocalPortNum string `json:"local_port_num"`
|
||||
RemoteChassisID string `json:"remote_chassis_id"`
|
||||
RemotePortID string `json:"remote_port_id"`
|
||||
RemoteSysName string `json:"remote_sys_name"`
|
||||
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
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
@@ -104,6 +116,7 @@ func NewMemoryStore() *MemoryStore {
|
||||
hosts: make(map[string][]HostResult),
|
||||
ports: make(map[string][]OpenPortResult),
|
||||
snmp: make(map[string][]SNMPResult),
|
||||
lldp: make(map[string][]LLDPResult),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,6 +296,35 @@ func (s *MemoryStore) ListSNMPResults(scanID string, limit int) []SNMPResult {
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *MemoryStore) SaveLLDPResult(scanID string, result LLDPResult) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if _, ok := s.jobs[scanID]; !ok {
|
||||
return errors.New("scan not found")
|
||||
}
|
||||
s.lldp[scanID] = append(s.lldp[scanID], result)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ListLLDPResults(scanID string, limit int) []LLDPResult {
|
||||
if limit <= 0 {
|
||||
limit = 2000
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
items := s.lldp[scanID]
|
||||
if len(items) <= limit {
|
||||
out := make([]LLDPResult, len(items))
|
||||
copy(out, items)
|
||||
return out
|
||||
}
|
||||
out := make([]LLDPResult, limit)
|
||||
copy(out, items[:limit])
|
||||
return out
|
||||
}
|
||||
|
||||
func applyDefaultOptions(opts *ScanOptions) {
|
||||
if opts.PingTimeoutMS <= 0 {
|
||||
opts.PingTimeoutMS = 700
|
||||
|
||||
Reference in New Issue
Block a user