test
This commit is contained in:
parent
69f9095a71
commit
05ee390bc2
|
@ -33,7 +33,7 @@ func ScanHost(zone *omd.Zone, dh *omd.DiscoverHost, resultChan chan interface{},
|
|||
return
|
||||
}
|
||||
|
||||
hostRanges, err := getTargetHostRange(dh, cr)
|
||||
hostRanges, err := GetTargetHostRange(dh, cr)
|
||||
if nil != err {
|
||||
errChan <- err
|
||||
return
|
||||
|
@ -162,7 +162,7 @@ func handlePacketARP(zone *omd.Zone, hostRanges []net.IP, hosts map[string]*omd.
|
|||
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
|
||||
if "" != dh.FirstScanRange {
|
||||
firstIP = net.ParseIP(dh.FirstScanRange)
|
||||
|
|
|
@ -1,11 +1,17 @@
|
|||
package service
|
||||
|
||||
/*
|
||||
Start()
|
||||
Stop()
|
||||
StartProcess()
|
||||
StopProcess()
|
||||
|
||||
Interfaces()
|
||||
Discover()
|
||||
StartDiscover()
|
||||
StopDiscover()
|
||||
|
||||
PingHost()
|
||||
PingService()
|
||||
DiscoverSNMP()
|
||||
PingAll() // hosts, services, snmp
|
||||
|
||||
ScanSNMP() // by a host
|
||||
PingSNMP()
|
||||
*/
|
||||
|
|
49
snmp/snmp.go
49
snmp/snmp.go
|
@ -3,14 +3,29 @@ package snmp
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
"github.com/k-sone/snmpgo"
|
||||
)
|
||||
|
||||
var defaultPort = 161
|
||||
var defaultOIDs = []string{
|
||||
"1.3.6.1.2.1.1.5.0", //sysName
|
||||
}
|
||||
|
||||
// in progress
|
||||
func getV2(ip string, port int, community string, oidMap map[string]string) (map[string]string, error) {
|
||||
type SNMPResponse struct {
|
||||
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{
|
||||
Version: snmpgo.V2c,
|
||||
Address: address,
|
||||
|
@ -18,37 +33,37 @@ func getV2(ip string, port int, community string, oidMap map[string]string) (map
|
|||
Community: community,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
ch <- &SNMPResponse{host, nil, err}
|
||||
return
|
||||
}
|
||||
defer snmp.Close()
|
||||
|
||||
_oids := make([]string, 0, len(oidMap))
|
||||
for k := range oidMap {
|
||||
_oids = append(_oids, k)
|
||||
}
|
||||
|
||||
oids, err := snmpgo.NewOids(_oids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
ch <- &SNMPResponse{host, nil, err}
|
||||
return
|
||||
}
|
||||
|
||||
pdu, err := snmp.GetRequest(oids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
ch <- &SNMPResponse{host, nil, err}
|
||||
return
|
||||
}
|
||||
|
||||
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 {
|
||||
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() {
|
||||
// fmt.Printf("[%s] %s = %s: %s\n", ip, oidMap[val.Oid.String()], val.Variable.Type(), val.Variable.String())
|
||||
res[oidMap[val.Oid.String()]] = val.Variable.String()
|
||||
resMap[val.Oid.String()] = val.Variable.String()
|
||||
}
|
||||
return res, nil
|
||||
ch <- &SNMPResponse{host, resMap, nil}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,59 +1,23 @@
|
|||
package snmp
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
)
|
||||
|
||||
type Agent struct {
|
||||
ip string
|
||||
port int
|
||||
community string
|
||||
}
|
||||
|
||||
func TestV2c(t *testing.T) {
|
||||
|
||||
oids := map[string]string{
|
||||
"1.3.6.1.2.1.1.5.0": "sysName",
|
||||
func TestSNMPScan(t *testing.T) {
|
||||
host := &omd.Host{
|
||||
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
||||
Address: "192.168.1.229",
|
||||
}
|
||||
|
||||
agents := []Agent{
|
||||
Agent{
|
||||
ip: "192.168.1.229",
|
||||
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",
|
||||
},
|
||||
}
|
||||
ch := make(chan *SNMPResponse)
|
||||
defer close(ch)
|
||||
ScanSNMP(host, "test1252serc", ch)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(agents))
|
||||
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()
|
||||
msg := <-ch
|
||||
t.Log(msg)
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user