Skip to content

Commit

Permalink
Optimize locking for SNMP MIBs loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Rebhan committed Dec 2, 2021
1 parent ea7b059 commit 0e707eb
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions internal/snmp/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@ import (
var m sync.Mutex
var once sync.Once

func LoadMibsFromPath(paths []string, log telegraf.Logger) error {
func appendPath(path string) {
m.Lock()
defer m.Unlock()

gosmi.AppendPath(path)
}

func loadModule(path string) error {
m.Lock()
defer m.Unlock()

_, err := gosmi.LoadModule(path)
return err
}

func LoadMibsFromPath(paths []string, log telegraf.Logger) error {
once.Do(gosmi.Init)

var folders []string
for _, mibPath := range paths {
gosmi.AppendPath(mibPath)
appendPath(mibPath)
folders = append(folders, mibPath)
err := filepath.Walk(mibPath, func(path string, info os.FileInfo, err error) error {
// symlinks are files so we need to double check if any of them are folders
Expand All @@ -40,14 +54,14 @@ func LoadMibsFromPath(paths []string, log telegraf.Logger) error {
if err != nil {
return fmt.Errorf("Filepath could not be walked %v", err)
}

for _, folder := range folders {
err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
// checks if file or directory
if info.IsDir() {
gosmi.AppendPath(path)
appendPath(path)
} else if info.Mode()&os.ModeSymlink == 0 {
_, err := gosmi.LoadModule(info.Name())
if err != nil {
if err := loadModule(info.Name()); err != nil {
log.Warnf("Module could not be loaded %v", err)
}
}
Expand Down

0 comments on commit 0e707eb

Please sign in to comment.