func()

in plugins/inputs/snmp_legacy/snmp_legacy.go [414:597]


func (h *Host) SNMPMap(
	nameToOid map[string]string,
	subTableMap map[string]Subtable,
) error {
	if h.OidInstanceMapping == nil {
		h.OidInstanceMapping = make(map[string]map[string]string)
	}
	// Get snmp client
	snmpClient, err := h.GetSNMPClient()
	if err != nil {
		return err
	}
	// Deconnection
	defer snmpClient.Conn.Close()
	// Prepare OIDs
	for _, table := range h.tables {
		// We don't have mapping
		if table.mappingTable == "" {
			if len(table.subTables) == 0 {
				// If We don't have mapping table
				// neither subtables list
				// This is just a bulk request
				oid := Data{}
				oid.Oid = table.oid
				if val, ok := nameToOid[oid.Oid]; ok {
					oid.rawOid = "." + val
				} else {
					oid.rawOid = oid.Oid
				}
				h.bulkOids = append(h.bulkOids, oid)
			} else {
				// If We don't have mapping table
				// but we have subtables
				// This is a bunch of bulk requests
				// For each subtable ...
				for _, sb := range table.subTables {
					// ... we create a new Data (oid) object
					oid := Data{}
					// Looking for more information about this subtable
					ssb, exists := subTableMap[sb]
					if exists {
						// We found a subtable section in config files
						oid.Oid = ssb.Oid
						oid.rawOid = ssb.Oid
						oid.Unit = ssb.Unit
					} else {
						// We did NOT find a subtable section in config files
						oid.Oid = sb
						oid.rawOid = sb
					}
					// TODO check oid validity

					// Add the new oid to bulkOids list
					h.bulkOids = append(h.bulkOids, oid)
				}
			}
		} else {
			// We have a mapping table
			// We need to query this table
			// To get mapping between instance id
			// and instance name
			oidAsked := table.mappingTable
			oidNext := oidAsked
			needMoreRequests := true
			// Set max repetition
			maxRepetition := uint32(32)
			// Launch requests
			for needMoreRequests {
				// Launch request
				result, err3 := snmpClient.GetBulk([]string{oidNext}, 0, maxRepetition)
				if err3 != nil {
					return err3
				}

				lastOid := ""
				for _, variable := range result.Variables {
					lastOid = variable.Name
					if strings.HasPrefix(variable.Name, oidAsked) {
						switch variable.Type {
						// handle instance names
						case gosnmp.OctetString:
							// Check if instance is in includes instances
							getInstances := true
							if len(table.IncludeInstances) > 0 {
								getInstances = false
								for _, instance := range table.IncludeInstances {
									if instance == string(variable.Value.([]byte)) {
										getInstances = true
									}
								}
							}
							// Check if instance is in excludes instances
							if len(table.ExcludeInstances) > 0 {
								getInstances = true
								for _, instance := range table.ExcludeInstances {
									if instance == string(variable.Value.([]byte)) {
										getInstances = false
									}
								}
							}
							// We don't want this instance
							if !getInstances {
								continue
							}

							// remove oid table from the complete oid
							// in order to get the current instance id
							key := strings.Replace(variable.Name, oidAsked, "", 1)

							if len(table.subTables) == 0 {
								// We have a mapping table
								// but no subtables
								// This is just a bulk request

								// Building mapping table
								mapping := map[string]string{strings.Trim(key, "."): string(variable.Value.([]byte))}
								_, exists := h.OidInstanceMapping[table.oid]
								if exists {
									h.OidInstanceMapping[table.oid][strings.Trim(key, ".")] = string(variable.Value.([]byte))
								} else {
									h.OidInstanceMapping[table.oid] = mapping
								}

								// Add table oid in bulk oid list
								oid := Data{}
								oid.Oid = table.oid
								if val, ok := nameToOid[oid.Oid]; ok {
									oid.rawOid = "." + val
								} else {
									oid.rawOid = oid.Oid
								}
								h.bulkOids = append(h.bulkOids, oid)
							} else {
								// We have a mapping table
								// and some subtables
								// This is a bunch of get requests
								// This is the best case :)

								// For each subtable ...
								for _, sb := range table.subTables {
									// ... we create a new Data (oid) object
									oid := Data{}
									// Looking for more information about this subtable
									ssb, exists := subTableMap[sb]
									if exists {
										// We found a subtable section in config files
										oid.Oid = ssb.Oid + key
										oid.rawOid = ssb.Oid + key
										oid.Unit = ssb.Unit
										oid.Instance = string(variable.Value.([]byte))
									} else {
										// We did NOT find a subtable section in config files
										oid.Oid = sb + key
										oid.rawOid = sb + key
										oid.Instance = string(variable.Value.([]byte))
									}
									// TODO check oid validity

									// Add the new oid to internalGetOids list
									h.internalGetOids = append(h.internalGetOids, oid)
								}
							}
						default:
						}
					} else {
						break
					}
				}
				// Determine if we need more requests
				if strings.HasPrefix(lastOid, oidAsked) {
					needMoreRequests = true
					oidNext = lastOid
				} else {
					needMoreRequests = false
				}
			}
		}
	}
	// Mapping finished

	// Create newoids based on mapping

	return nil
}