From 457c98f4c2489fec139df59dcef58e068b104b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laurent=20Sesqu=C3=A8s?= Date: Tue, 11 Jan 2022 23:17:05 +0100 Subject: [PATCH] fix: snmp input plugin errors if mibs folder doesn't exist (#10346) (#10354) --- internal/snmp/translate.go | 12 +++++++++--- plugins/inputs/snmp/snmp_test.go | 7 +++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/snmp/translate.go b/internal/snmp/translate.go index 4f8943c88..c6d779d8a 100644 --- a/internal/snmp/translate.go +++ b/internal/snmp/translate.go @@ -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) // 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 { @@ -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 { diff --git a/plugins/inputs/snmp/snmp_test.go b/plugins/inputs/snmp/snmp_test.go index 450f2ea7b..2e489cefd 100644 --- a/plugins/inputs/snmp/snmp_test.go +++ b/plugins/inputs/snmp/snmp_test.go @@ -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) +}