func()

in plugins/inputs/snmp_legacy/snmp_legacy.go [280:412]


func (s *Snmp) Gather(acc telegraf.Accumulator) error {
	// TODO put this in cache on first run
	// Create subtables mapping
	if len(s.subTableMap) == 0 {
		s.subTableMap = make(map[string]Subtable)
		for _, sb := range s.Subtable {
			s.subTableMap[sb.Name] = sb
		}
	}
	// TODO put this in cache on first run
	// Create oid tree
	if s.SnmptranslateFile != "" && len(s.initNode.subnodes) == 0 {
		s.nameToOid = make(map[string]string)
		s.initNode = Node{
			id:       "1",
			name:     "",
			subnodes: make(map[string]Node),
		}

		data, err := os.ReadFile(s.SnmptranslateFile)
		if err != nil {
			s.Log.Errorf("Reading SNMPtranslate file error: %s", err.Error())
			return err
		}

		for _, line := range strings.Split(string(data), "\n") {
			oids := strings.Fields(line)
			if len(oids) == 2 && oids[1] != "" {
				oidName := oids[0]
				oid := oids[1]
				fillnode(s.initNode, oidName, strings.Split(oid, "."))
				s.nameToOid[oidName] = oid
			}
		}
	}
	// Fetching data
	for _, host := range s.Host {
		// Set default args
		if len(host.Address) == 0 {
			host.Address = "127.0.0.1:161"
		}
		if host.Community == "" {
			host.Community = "public"
		}
		if host.Timeout <= 0 {
			host.Timeout = 2.0
		}
		if host.Retries <= 0 {
			host.Retries = 2
		}
		// Prepare host
		// Get Easy GET oids
		for _, oidstring := range host.GetOids {
			oid := Data{}
			if val, ok := s.nameToOid[oidstring]; ok {
				// TODO should we add the 0 instance ?
				oid.Name = oidstring
				oid.Oid = val
				oid.rawOid = "." + val + ".0"
			} else {
				oid.Name = oidstring
				oid.Oid = oidstring
				if oidstring[:1] != "." {
					oid.rawOid = "." + oidstring
				} else {
					oid.rawOid = oidstring
				}
			}
			host.internalGetOids = append(host.internalGetOids, oid)
		}

		for _, oidName := range host.Collect {
			// Get GET oids
			for _, oid := range s.Get {
				if oid.Name == oidName {
					if val, ok := s.nameToOid[oid.Oid]; ok {
						// TODO should we add the 0 instance ?
						if oid.Instance != "" {
							oid.rawOid = "." + val + "." + oid.Instance
						} else {
							oid.rawOid = "." + val + ".0"
						}
					} else {
						oid.rawOid = oid.Oid
					}
					host.internalGetOids = append(host.internalGetOids, oid)
				}
			}
			// Get GETBULK oids
			for _, oid := range s.Bulk {
				if oid.Name == oidName {
					if val, ok := s.nameToOid[oid.Oid]; ok {
						oid.rawOid = "." + val
					} else {
						oid.rawOid = oid.Oid
					}
					host.bulkOids = append(host.bulkOids, oid)
				}
			}
		}
		// Table
		for _, hostTable := range host.Table {
			for _, snmpTable := range s.Table {
				if hostTable.Name == snmpTable.Name {
					table := hostTable
					table.oid = snmpTable.Oid
					table.mappingTable = snmpTable.MappingTable
					table.subTables = snmpTable.SubTables
					host.tables = append(host.tables, table)
				}
			}
		}
		// Launch Mapping
		// TODO put this in cache on first run
		// TODO save mapping and computed oids
		// to do it only the first time
		// only if len(s.OidInstanceMapping) == 0
		if len(host.OidInstanceMapping) >= 0 {
			if err := host.SNMPMap(s.nameToOid, s.subTableMap); err != nil {
				s.Log.Errorf("Mapping error for host %q: %s", host.Address, err.Error())
				continue
			}
		}
		// Launch Get requests
		if err := host.SNMPGet(acc, s.initNode); err != nil {
			s.Log.Errorf("Error for host %q: %s", host.Address, err.Error())
		}
		if err := host.SNMPBulk(acc, s.initNode); err != nil {
			s.Log.Errorf("Error for host %q: %s", host.Address, err.Error())
		}
	}
	return nil
}