probe/snmp/snmp.go

55 lines
1.1 KiB
Go
Raw Normal View History

2018-08-15 06:18:40 +00:00
package snmp
import (
"fmt"
"github.com/k-sone/snmpgo"
)
// in progress
func getV2(ip string, port int, community string, oidMap map[string]string) (map[string]string, error) {
address := fmt.Sprintf("%s:%d", ip, port)
snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: snmpgo.V2c,
Address: address,
Retries: 1,
Community: community,
})
if err != nil {
return nil, err
}
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
}
pdu, err := snmp.GetRequest(oids)
if err != nil {
return nil, err
}
if pdu.ErrorStatus() != snmpgo.NoError {
return nil, fmt.Errorf("%s", pdu.ErrorStatus().String())
}
if pdu == nil {
return nil, fmt.Errorf("%s", "Empty PDU")
}
res := 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()
}
return res, nil
}