Skip to content

Commit ad5c627

Browse files
Ahmet OeztuerkAhmet Oeztuerk
authored andcommitted
change the default empty state to ok
differentiate between empty states 1) where no files are found, due to given file paths 2) where files are found but do not contain matched lines. write different outputs for both of them, and add tests
1 parent 6adccab commit ad5c627

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

docs/checks/commands/check_logfile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Naemon Config
5858

5959
| Argument | Default Value |
6060
| ------------- | ---------------------------------------------------------------------- |
61-
| empty-state | 3 (UNKNOWN) |
61+
| empty-state | 0 (OK) |
6262
| empty-syntax | %(status) - No files found |
6363
| top-syntax | %(status) - %(problem_count)/%(count) lines (%(count)) %(problem_list) |
6464
| ok-syntax | %(status) - All %(count) / %(total) Lines OK |

pkg/snclient/check_logfile.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (c *CheckLogFile) Build() *CheckData {
6767
okSyntax: "%(status) - All %(count) / %(total) Lines OK",
6868
topSyntax: "%(status) - %(problem_count)/%(count) lines (%(count)) %(problem_list)",
6969
emptySyntax: "%(status) - No files found",
70-
emptyState: CheckExitUnknown,
70+
emptyState: CheckExitOK,
7171
args: map[string]CheckArgument{
7272
"file": {value: &c.FilePathPatterns, description: "The file that should be checked"},
7373
"files": {value: &c.FilePathPatternsCS, description: "Comma separated list of files"},
@@ -166,8 +166,11 @@ func (c *CheckLogFile) Check(_ context.Context, snc *Agent, check *CheckData, _
166166
"file_counts": c.buildFileCountsDetailString(checkedFilesWithMatchedEntries),
167167
}
168168

169-
if len(check.listData) == 0 {
169+
if len(checkedFilesWithMatchedEntries) == 0 {
170+
check.okSyntax = "%(status) - No files found to search lines in."
171+
} else if len(check.listData) == 0 {
170172
check.emptySyntax = fmt.Sprintf("%%(status) - No matching lines found in files (%s)", check.details["file_counts"])
173+
check.emptyStateSet = true
171174
}
172175

173176
return check.Finalize()

pkg/snclient/check_logfile_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,18 @@ func TestCheckLogFileFileExistsButHasNoLines(t *testing.T) {
121121
snc := StartTestAgent(t, testLogfileConfig)
122122

123123
res := snc.RunCheck("check_logfile", []string{"files=./t/test*", "filter=line LIKE this-pattern-does-not-exist-in-the-test-files", "show-all"})
124-
assert.Equalf(t, CheckExitUnknown, res.State, "state UNKNOWN")
125-
assert.Contains(t, string(res.BuildPluginOutput()), "UNKNOWN - No matching lines found in files ")
124+
assert.Equalf(t, CheckExitOK, res.State, "state should be OK")
125+
assert.Contains(t, string(res.BuildPluginOutput()), "OK - No matching lines found in files")
126+
127+
StopTestAgent(t, snc)
128+
}
129+
130+
func TestCheckLogFileFileDoesNotExist(t *testing.T) {
131+
snc := StartTestAgent(t, testLogfileConfig)
132+
133+
res := snc.RunCheck("check_logfile", []string{"files=./t/testfiledoesnotexist*"})
134+
assert.Equalf(t, CheckExitOK, res.State, "state should be OK")
135+
assert.Contains(t, string(res.BuildPluginOutput()), "OK - No files found to search lines in.")
126136

127137
StopTestAgent(t, snc)
128138
}

0 commit comments

Comments
 (0)