70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
|
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
|
||
|
}
|