Skip to content
Open
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
28 changes: 23 additions & 5 deletions pkg/snclient/check_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ func (l *CheckFiles) Check(_ context.Context, _ *Agent, check *CheckData, _ []Ar
}
}

// Cleanup the listData if a filter is used
if l.pattern != "*" {
l.removeDirectoriesWithoutFilesUnder(check)
}
l.removeDirectoriesThatDontMatchPattern(check)

if l.calculateSubdirectorySizes {
l.addSubdirectorySizes(check)
Expand Down Expand Up @@ -558,10 +555,31 @@ func checkSlowFileOperations(check *CheckData, entry map[string]string, path str
return nil
}

func (l *CheckFiles) removeDirectoriesThatDontMatchPattern(check *CheckData) {
if l.pattern == "*" {
return
}

newListdata := make([]map[string]string, 0)

for _, data := range check.listData {
if data["type"] == "dir" {
if match, _ := filepath.Match(l.pattern, data["filename"]); !match {
continue
}
}
newListdata = append(newListdata, data)
}

check.listData = newListdata
}

// The WalkDir normally adds every directory and files under the search path.
// If a pattern is specified, this prevents files that dont match the pattern to be skipped.
// If a pattern/filter is specified, this prevents files that dont match the pattern/filter to be skipped.
// This can lead to some directories being in the listData, while not having any matched files under them.
// This function cleans those directories up.
//
//nolint:unused // this function was called if pattern was specified, in previous versions. still keeping it for possible future use with filters.
func (l *CheckFiles) removeDirectoriesWithoutFilesUnder(check *CheckData) {
fileFilepaths := make([]string, 0)

Expand Down
18 changes: 14 additions & 4 deletions pkg/snclient/check_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func TestCheckFilesSizePerfdata(t *testing.T) {
// The second pass should remove the "a" folder where files with "a" extension is found
res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=*.b", "crit='size > 100MiB'"})
outputString = string(res.BuildPluginOutput())
assert.Containsf(t, outputString, "OK - All 6 files are ok", "output matches")
assert.Containsf(t, outputString, "OK - All 5 files are ok", "output matches")
res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=*.b", "crit='size > 100MiB'", "filter=' type == file'"})
outputString = string(res.BuildPluginOutput())
assert.Containsf(t, outputString, "OK - All 5 files are ok", "output matches")
Expand All @@ -517,9 +517,19 @@ func TestCheckFilesSizePerfdata(t *testing.T) {
// When using a pattern and calculate subdirectory sizes is enabled, it will add the subdirectory sizes as metrics
res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=*_3.*", "crit='size > 0Mib'", "calculate-subdirectory-sizes=true"})
outputString = string(res.BuildPluginOutput())
assert.Containsf(t, outputString, "CRITICAL - 6/6 files (3.50 MiB) critical", "output matches")
assert.Containsf(t, outputString, "'a size'=1048576B", "should calculate the size of the subfolder a")
assert.Containsf(t, outputString, "'b size'=1048576B", "should calculate the size of the subfolder b")
assert.Containsf(t, outputString, "CRITICAL - 4/4 files (3.50 MiB) critical", "output matches")
assert.NotContainsf(t, outputString, "'a size'=1048576B", "should calculate the size of the subfolder a, as it does not match pattern")
assert.NotContainsf(t, outputString, "'b size'=1048576B", "should calculate the size of the subfolder b, as it does not match pattern")

// only matches the directory "a" itself
res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=a"})
outputString = string(res.BuildPluginOutput())
assert.Containsf(t, outputString, "OK - All 1 files are ok: (0 B)", "output matches")

// matches the four files inside directory "a": file_1024kb_1.a , file_1024kb_2.a , file_1024kb_3.a , file_1024kb_4.a
res = snc.RunCheck("check_files", []string{"path=" + geneartionDirectory, "pattern=*.a"})
outputString = string(res.BuildPluginOutput())
assert.Containsf(t, outputString, "OK - All 4 files are ok: (4.00 MiB)", "output matches")

StopTestAgent(t, snc)
}
Expand Down