ing
This commit is contained in:
69
discovery/protocol/snmp/snmp.go
Normal file
69
discovery/protocol/snmp/snmp.go
Normal file
@@ -0,0 +1,69 @@
|
||||
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
|
||||
}
|
||||
|
||||
type SNMPResponse struct {
|
||||
Host *omd.Host
|
||||
Value map[string]string
|
||||
Error error
|
||||
}
|
||||
|
||||
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,
|
||||
Retries: 1,
|
||||
Community: community,
|
||||
})
|
||||
if err != nil {
|
||||
ch <- &SNMPResponse{host, nil, err}
|
||||
return
|
||||
}
|
||||
defer snmp.Close()
|
||||
|
||||
oids, err := snmpgo.NewOids(_oids)
|
||||
if err != nil {
|
||||
ch <- &SNMPResponse{host, nil, err}
|
||||
return
|
||||
}
|
||||
|
||||
pdu, err := snmp.GetRequest(oids)
|
||||
if err != nil {
|
||||
ch <- &SNMPResponse{host, nil, err}
|
||||
return
|
||||
}
|
||||
|
||||
if pdu.ErrorStatus() != snmpgo.NoError {
|
||||
ch <- &SNMPResponse{host, nil, fmt.Errorf("%s", pdu.ErrorStatus().String())}
|
||||
return
|
||||
}
|
||||
|
||||
if pdu == nil {
|
||||
ch <- &SNMPResponse{host, nil, fmt.Errorf("%s", "Empty PDU")}
|
||||
return
|
||||
}
|
||||
|
||||
resMap := make(map[string]string)
|
||||
for _, val := range pdu.VarBinds() {
|
||||
resMap[val.Oid.String()] = val.Variable.String()
|
||||
}
|
||||
ch <- &SNMPResponse{host, resMap, nil}
|
||||
return
|
||||
}
|
||||
23
discovery/protocol/snmp/snmp_test.go
Normal file
23
discovery/protocol/snmp/snmp_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package snmp
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
omd "git.loafle.net/overflow/model/discovery"
|
||||
omm "git.loafle.net/overflow/model/meta"
|
||||
)
|
||||
|
||||
func TestSNMPScan(t *testing.T) {
|
||||
host := &omd.Host{
|
||||
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
|
||||
Address: "192.168.1.229",
|
||||
}
|
||||
|
||||
ch := make(chan *SNMPResponse)
|
||||
defer close(ch)
|
||||
ScanSNMP(host, "test1252serc", ch)
|
||||
|
||||
msg := <-ch
|
||||
t.Log(msg)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user