Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: snmp input plugin errors if mibs folder doesn't exist (#10346) #10354

Merged
merged 4 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions internal/snmp/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ func LoadMibsFromPath(paths []string, log telegraf.Logger) error {
}

appendPath(mibPath)
folders = append(folders, mibPath)
err := filepath.Walk(mibPath, func(path string, info os.FileInfo, err error) error {
if info == nil {
return fmt.Errorf("no mibs found")
log.Warnf("No mibs found")
if os.IsNotExist(err) {
log.Warnf("MIB path doesn't exist: %q", mibPath)
} else if err != nil {
return err
}
return nil
}
folders = append(folders, mibPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this got moved?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in case we don't find any mibs, we don't want to append mibPath to folders.

// symlinks are files so we need to double check if any of them are folders
// Will check file vs directory later on
if info.Mode()&os.ModeSymlink != 0 {
Expand All @@ -71,7 +77,7 @@ func LoadMibsFromPath(paths []string, log telegraf.Logger) error {
return nil
})
if err != nil {
return fmt.Errorf("Filepath could not be walked: %v", err)
return fmt.Errorf("Filepath %q could not be walked: %v", mibPath, err)
}

for _, folder := range folders {
Expand Down
7 changes: 7 additions & 0 deletions plugins/inputs/snmp/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,3 +1328,10 @@ func TestCanNotParse(t *testing.T) {
err := s.Init()
require.Error(t, err)
}

func TestMissingMibPath(t *testing.T) {
log := testutil.Logger{}
path := []string{"non-existing-directory"}
err := snmp.LoadMibsFromPath(path, log)
require.NoError(t, err)
}