func()

in plugins/inputs/snmp_legacy/snmp_legacy.go [726:812]


func (h *Host) HandleResponse(
	oids map[string]Data,
	result *gosnmp.SnmpPacket,
	acc telegraf.Accumulator,
	initNode Node,
) (string, error) {
	var lastOid string
	for _, variable := range result.Variables {
		lastOid = variable.Name
	nextresult:
		// Get only oid wanted
		for oid_key, oid := range oids {
			// Skip oids already processed
			for _, processedOid := range h.processedOids {
				if variable.Name == processedOid {
					break nextresult
				}
			}
			// If variable.Name is the same as oid_key
			// OR
			// the result is SNMP table which "." comes right after oid_key.
			// ex: oid_key: .1.3.6.1.2.1.2.2.1.16, variable.Name: .1.3.6.1.2.1.2.2.1.16.1
			if variable.Name == oid_key || strings.HasPrefix(variable.Name, oid_key+".") {
				switch variable.Type {
				// handle Metrics
				case gosnmp.Boolean, gosnmp.Integer, gosnmp.Counter32, gosnmp.Gauge32,
					gosnmp.TimeTicks, gosnmp.Counter64, gosnmp.Uinteger32, gosnmp.OctetString:
					// Prepare tags
					tags := make(map[string]string)
					if oid.Unit != "" {
						tags["unit"] = oid.Unit
					}
					// Get name and instance
					var oid_name string
					var instance string
					// Get oidname and instance from translate file
					oid_name, instance = findnodename(initNode,
						strings.Split(string(variable.Name[1:]), "."))
					// Set instance tag
					// From mapping table
					mapping, inMappingNoSubTable := h.OidInstanceMapping[oid_key]
					if inMappingNoSubTable {
						// filter if the instance in not in
						// OidInstanceMapping mapping map
						if instance_name, exists := mapping[instance]; exists {
							tags["instance"] = instance_name
						} else {
							continue
						}
					} else if oid.Instance != "" {
						// From config files
						tags["instance"] = oid.Instance
					} else if instance != "" {
						// Using last id of the current oid, ie:
						// with .1.3.6.1.2.1.31.1.1.1.10.3
						// instance is 3
						tags["instance"] = instance
					}

					// Set name
					var field_name string
					if oid_name != "" {
						// Set fieldname as oid name from translate file
						field_name = oid_name
					} else {
						// Set fieldname as oid name from inputs.snmp.get section
						// Because the result oid is equal to inputs.snmp.get section
						field_name = oid.Name
					}
					tags["snmp_host"], _, _ = net.SplitHostPort(h.Address)
					fields := make(map[string]interface{})
					fields[string(field_name)] = variable.Value

					h.processedOids = append(h.processedOids, variable.Name)
					acc.AddFields(field_name, fields, tags)
				case gosnmp.NoSuchObject, gosnmp.NoSuchInstance:
					// Oid not found
					log.Printf("E! [snmp input] Oid not found: %s", oid_key)
				default:
					// delete other data
				}
				break
			}
		}
	}
	return lastOid, nil
}