Skip to content

Commit 8a14be2

Browse files
Ahmet OeztuerkAhmet Oeztuerk
authored andcommitted
check_logfile: empty state is unknown, conditionally set to 0 if files exist without matched lines
checkdata.finalizeOutput() requires explicit filters or implicit argument filters to apply emptySyntax on output. apply empty syntax on empty state, by setting the "file" and files" argument is set as a filter.
1 parent e1d777c commit 8a14be2

3 files changed

Lines changed: 10 additions & 9 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 | 0 (OK) |
61+
| empty-state | 3 (UNKNOWN) |
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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ 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: CheckExitOK,
70+
emptyState: CheckExitUnknown,
7171
args: map[string]CheckArgument{
72-
"file": {value: &c.FilePathPatterns, description: "The file that should be checked"},
73-
"files": {value: &c.FilePathPatternsCS, description: "Comma separated list of files"},
72+
"file": {value: &c.FilePathPatterns, description: "The file that should be checked", isFilter: true},
73+
"files": {value: &c.FilePathPatternsCS, description: "Comma separated list of files", isFilter: true},
7474
"offset": {value: &c.Offset, description: "Starting position (in bytes) for scanning the file (0 for beginning). This overrides any saved offset"},
7575
"line-split": {value: &c.LineDelimiter, description: "Character string used to split a file into several lines (default \\n)"},
7676
"column-split": {value: &c.ColumnDelimiter, description: "Tab split default: \\t"},
@@ -167,10 +167,11 @@ func (c *CheckLogFile) Check(_ context.Context, snc *Agent, check *CheckData, _
167167
}
168168

169169
if len(checkedFilesWithMatchedEntries) == 0 {
170-
check.okSyntax = "%(status) - No files found to search lines in"
170+
check.emptySyntax = fmt.Sprintf("%%(status) - No files found to search lines in, search paths: '%s' ", strings.Join(c.FilePathPatterns, ","))
171171
} else if len(check.listData) == 0 {
172-
check.emptySyntax = fmt.Sprintf("%%(status) - No matching lines found in files (%s)", check.details["file_counts"])
172+
check.emptyState = CheckExitOK
173173
check.emptyStateSet = true
174+
check.emptySyntax = fmt.Sprintf("%%(status) - No matching lines found in files (%s)", check.details["file_counts"])
174175
}
175176

176177
return check.Finalize()

pkg/snclient/check_logfile_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestCheckLogFilePathWildCards(t *testing.T) {
5252

5353
res = snc.RunCheck("check_logfile", []string{"files=./t/test*", "warn=line LIKE WARNING"})
5454
assert.Equalf(t, CheckExitOK, res.State, "state OK")
55-
assert.Contains(t, string(res.BuildPluginOutput()), "OK - All 0 / 0")
55+
assert.Contains(t, string(res.BuildPluginOutput()), "OK - No matching lines found in files ")
5656

5757
StopTestAgent(t, snc)
5858
}
@@ -131,8 +131,8 @@ func TestCheckLogFileFileDoesNotExist(t *testing.T) {
131131
snc := StartTestAgent(t, testLogfileConfig)
132132

133133
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")
134+
assert.Equalf(t, CheckExitUnknown, res.State, "state should be UNKNOWN")
135+
assert.Contains(t, string(res.BuildPluginOutput()), "UNKNOWN - No files found to search lines in")
136136

137137
StopTestAgent(t, snc)
138138
}

0 commit comments

Comments
 (0)