Make OidInstanceMapping a field of the snmp host

fixes #1171
This commit is contained in:
Cameron Sparr 2016-05-09 14:11:57 -07:00
parent 5deb22a539
commit afdebbc3a2
1 changed files with 21 additions and 13 deletions

View File

@ -26,9 +26,6 @@ type Snmp struct {
nameToOid map[string]string nameToOid map[string]string
initNode Node initNode Node
subTableMap map[string]Subtable subTableMap map[string]Subtable
// TODO change as unexportable
//OidInstanceMapping map[string]map[string]string
} }
type Host struct { type Host struct {
@ -53,6 +50,8 @@ type Host struct {
// array of processed oids // array of processed oids
// to skip oid duplication // to skip oid duplication
processedOids []string processedOids []string
OidInstanceMapping map[string]map[string]string
} }
type Table struct { type Table struct {
@ -116,9 +115,6 @@ type Node struct {
subnodes map[string]Node subnodes map[string]Node
} }
// TODO move this var to snmp struct
var OidInstanceMapping = make(map[string]map[string]string)
var sampleConfig = ` var sampleConfig = `
## Use 'oids.txt' file to translate oids to names ## Use 'oids.txt' file to translate oids to names
## To generate 'oids.txt' you need to run: ## To generate 'oids.txt' you need to run:
@ -396,7 +392,7 @@ func (s *Snmp) Gather(acc telegraf.Accumulator) error {
// TODO save mapping and computed oids // TODO save mapping and computed oids
// to do it only the first time // to do it only the first time
// only if len(s.OidInstanceMapping) == 0 // only if len(s.OidInstanceMapping) == 0
if len(OidInstanceMapping) >= 0 { if len(host.OidInstanceMapping) >= 0 {
if err := host.SNMPMap(acc, s.nameToOid, s.subTableMap); err != nil { if err := host.SNMPMap(acc, s.nameToOid, s.subTableMap); err != nil {
log.Printf("SNMP Mapping error for host '%s': %s", host.Address, err) log.Printf("SNMP Mapping error for host '%s': %s", host.Address, err)
continue continue
@ -413,7 +409,14 @@ func (s *Snmp) Gather(acc telegraf.Accumulator) error {
return nil return nil
} }
func (h *Host) SNMPMap(acc telegraf.Accumulator, nameToOid map[string]string, subTableMap map[string]Subtable) error { func (h *Host) SNMPMap(
acc telegraf.Accumulator,
nameToOid map[string]string,
subTableMap map[string]Subtable,
) error {
if h.OidInstanceMapping == nil {
h.OidInstanceMapping = make(map[string]map[string]string)
}
// Get snmp client // Get snmp client
snmpClient, err := h.GetSNMPClient() snmpClient, err := h.GetSNMPClient()
if err != nil { if err != nil {
@ -523,11 +526,11 @@ func (h *Host) SNMPMap(acc telegraf.Accumulator, nameToOid map[string]string, su
// Building mapping table // Building mapping table
mapping := map[string]string{strings.Trim(key, "."): string(variable.Value.([]byte))} mapping := map[string]string{strings.Trim(key, "."): string(variable.Value.([]byte))}
_, exists := OidInstanceMapping[table.oid] _, exists := h.OidInstanceMapping[table.oid]
if exists { if exists {
OidInstanceMapping[table.oid][strings.Trim(key, ".")] = string(variable.Value.([]byte)) h.OidInstanceMapping[table.oid][strings.Trim(key, ".")] = string(variable.Value.([]byte))
} else { } else {
OidInstanceMapping[table.oid] = mapping h.OidInstanceMapping[table.oid] = mapping
} }
// Add table oid in bulk oid list // Add table oid in bulk oid list
@ -720,7 +723,12 @@ func (h *Host) GetSNMPClient() (*gosnmp.GoSNMP, error) {
return snmpClient, nil return snmpClient, nil
} }
func (h *Host) HandleResponse(oids map[string]Data, result *gosnmp.SnmpPacket, acc telegraf.Accumulator, initNode Node) (string, error) { func (h *Host) HandleResponse(
oids map[string]Data,
result *gosnmp.SnmpPacket,
acc telegraf.Accumulator,
initNode Node,
) (string, error) {
var lastOid string var lastOid string
for _, variable := range result.Variables { for _, variable := range result.Variables {
lastOid = variable.Name lastOid = variable.Name
@ -755,7 +763,7 @@ func (h *Host) HandleResponse(oids map[string]Data, result *gosnmp.SnmpPacket, a
strings.Split(string(variable.Name[1:]), ".")) strings.Split(string(variable.Name[1:]), "."))
// Set instance tag // Set instance tag
// From mapping table // From mapping table
mapping, inMappingNoSubTable := OidInstanceMapping[oid_key] mapping, inMappingNoSubTable := h.OidInstanceMapping[oid_key]
if inMappingNoSubTable { if inMappingNoSubTable {
// filter if the instance in not in // filter if the instance in not in
// OidInstanceMapping mapping map // OidInstanceMapping mapping map