This commit is contained in:
insanity 2018-08-16 18:05:21 +09:00
parent 69f9095a71
commit 05ee390bc2
4 changed files with 56 additions and 71 deletions

View File

@ -33,7 +33,7 @@ func ScanHost(zone *omd.Zone, dh *omd.DiscoverHost, resultChan chan interface{},
return return
} }
hostRanges, err := getTargetHostRange(dh, cr) hostRanges, err := GetTargetHostRange(dh, cr)
if nil != err { if nil != err {
errChan <- err errChan <- err
return return
@ -162,7 +162,7 @@ func handlePacketARP(zone *omd.Zone, hostRanges []net.IP, hosts map[string]*omd.
return h return h
} }
func getTargetHostRange(dh *omd.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) { func GetTargetHostRange(dh *omd.DiscoverHost, cr cidr.CIDRRanger) ([]net.IP, error) {
var firstIP net.IP var firstIP net.IP
if "" != dh.FirstScanRange { if "" != dh.FirstScanRange {
firstIP = net.ParseIP(dh.FirstScanRange) firstIP = net.ParseIP(dh.FirstScanRange)

View File

@ -1,11 +1,17 @@
package service package service
/* /*
Start() StartProcess()
Stop() StopProcess()
Interfaces() Interfaces()
Discover() StartDiscover()
StopDiscover()
PingHost() PingHost()
PingService() PingService()
DiscoverSNMP() PingAll() // hosts, services, snmp
ScanSNMP() // by a host
PingSNMP()
*/ */

View File

@ -3,14 +3,29 @@ package snmp
import ( import (
"fmt" "fmt"
omd "git.loafle.net/overflow/model/discovery"
"github.com/k-sone/snmpgo" "github.com/k-sone/snmpgo"
) )
var defaultPort = 161
var defaultOIDs = []string{
"1.3.6.1.2.1.1.5.0", //sysName
}
// in progress type SNMPResponse struct {
func getV2(ip string, port int, community string, oidMap map[string]string) (map[string]string, error) { Host *omd.Host
Value map[string]string
Error error
}
address := fmt.Sprintf("%s:%d", ip, port) func ScanSNMP(host *omd.Host, community string, ch chan *SNMPResponse) {
go func() {
getV2(host, defaultPort, community, defaultOIDs, ch)
}()
}
func getV2(host *omd.Host, port int, community string, _oids []string, ch chan *SNMPResponse) {
address := fmt.Sprintf("%s:%d", host.Address, port)
snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{ snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: snmpgo.V2c, Version: snmpgo.V2c,
Address: address, Address: address,
@ -18,37 +33,37 @@ func getV2(ip string, port int, community string, oidMap map[string]string) (map
Community: community, Community: community,
}) })
if err != nil { if err != nil {
return nil, err ch <- &SNMPResponse{host, nil, err}
return
} }
defer snmp.Close() defer snmp.Close()
_oids := make([]string, 0, len(oidMap))
for k := range oidMap {
_oids = append(_oids, k)
}
oids, err := snmpgo.NewOids(_oids) oids, err := snmpgo.NewOids(_oids)
if err != nil { if err != nil {
return nil, err ch <- &SNMPResponse{host, nil, err}
return
} }
pdu, err := snmp.GetRequest(oids) pdu, err := snmp.GetRequest(oids)
if err != nil { if err != nil {
return nil, err ch <- &SNMPResponse{host, nil, err}
return
} }
if pdu.ErrorStatus() != snmpgo.NoError { if pdu.ErrorStatus() != snmpgo.NoError {
return nil, fmt.Errorf("%s", pdu.ErrorStatus().String()) ch <- &SNMPResponse{host, nil, fmt.Errorf("%s", pdu.ErrorStatus().String())}
return
} }
if pdu == nil { if pdu == nil {
return nil, fmt.Errorf("%s", "Empty PDU") ch <- &SNMPResponse{host, nil, fmt.Errorf("%s", "Empty PDU")}
return
} }
res := make(map[string]string) resMap := make(map[string]string)
for _, val := range pdu.VarBinds() { for _, val := range pdu.VarBinds() {
// fmt.Printf("[%s] %s = %s: %s\n", ip, oidMap[val.Oid.String()], val.Variable.Type(), val.Variable.String()) resMap[val.Oid.String()] = val.Variable.String()
res[oidMap[val.Oid.String()]] = val.Variable.String()
} }
return res, nil ch <- &SNMPResponse{host, resMap, nil}
return
} }

View File

@ -1,59 +1,23 @@
package snmp package snmp
import ( import (
"sync"
"testing" "testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
) )
type Agent struct { func TestSNMPScan(t *testing.T) {
ip string host := &omd.Host{
port int MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
community string Address: "192.168.1.229",
}
func TestV2c(t *testing.T) {
oids := map[string]string{
"1.3.6.1.2.1.1.5.0": "sysName",
} }
agents := []Agent{ ch := make(chan *SNMPResponse)
Agent{ defer close(ch)
ip: "192.168.1.229", ScanSNMP(host, "test1252serc", ch)
port: 161,
community: "test1252serc",
},
Agent{
ip: "192.168.1.254",
port: 161,
community: "loafle",
},
Agent{
ip: "192.168.1.99",
port: 161,
community: "public",
},
Agent{
ip: "192.168.1.1",
port: 161,
community: "public",
},
Agent{
ip: "192.168.1.2",
port: 161,
community: "public",
},
}
var wg sync.WaitGroup msg := <-ch
wg.Add(len(agents)) t.Log(msg)
for _, agent := range agents {
go func(a Agent) {
res, _ := getV2(a.ip, a.port, a.community, oids)
t.Log(res)
defer wg.Done()
}(agent)
}
wg.Wait()
} }