Skip to content

Commit 1af7353

Browse files
committed
check_logfile: improve detection of required macros
1 parent 296c1fa commit 1af7353

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ next:
44
- fix plugin output ending in chinese characters (#261)
55
- chore: update dependencies
66
- improve logging of invalid http requests
7+
- check_logfile: improve detection of required macros
78
- add list-combine option
89

910
0.37 Sun Sep 7 11:41:00 CEST 2025

pkg/snclient/check_logfile.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010
"regexp"
11+
"slices"
1112
"strings"
1213

1314
"github.com/consol-monitoring/snclient/pkg/convert"
@@ -278,22 +279,22 @@ func (c *CheckLogFile) getCustomSplitFunction() bufio.SplitFunc {
278279
}
279280

280281
func (c *CheckLogFile) getRequiredColumnNumbers(check *CheckData) []int {
281-
// get all thresholds with prefix column
282-
allThresh := append(check.warnThreshold, check.critThreshold...)
283-
284282
// extract all required threshold numbers
285283
columnNumbers := []int{}
286-
for _, thresh := range allThresh {
287-
if !strings.HasPrefix(thresh.keyword, "column") {
284+
for _, macro := range check.AllRequiredMacros() {
285+
if !strings.HasPrefix(macro, "column") {
288286
continue
289287
}
290-
match := numReg.FindString(thresh.keyword)
288+
match := numReg.FindString(macro)
291289
if match == "" {
292290
continue
293291
}
294292
index := convert.Int(match)
295293
columnNumbers = append(columnNumbers, index)
296294
}
297295

296+
slices.Sort(columnNumbers)
297+
columnNumbers = slices.Compact(columnNumbers)
298+
298299
return columnNumbers
299300
}

pkg/snclient/checkdata.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,26 @@ func (cd *CheckData) HasMacro(name string) bool {
944944
return false
945945
}
946946

947+
// AllRequiredMacros returns a list of all required macros in the syntax attributes and thresholds.
948+
func (cd *CheckData) AllRequiredMacros() []string {
949+
var allMacros []string
950+
951+
// extract macros from syntax templates
952+
for _, syntax := range []string{cd.detailSyntax, cd.topSyntax, cd.okSyntax, cd.emptySyntax, cd.perfSyntax} {
953+
macros := MacroNames(syntax)
954+
allMacros = append(allMacros, macros...)
955+
}
956+
957+
// append macros from all conditions
958+
allMacros = append(allMacros, cd.GetAllThresholdKeywords()...)
959+
960+
// make list unique
961+
slices.Sort(allMacros)
962+
allMacros = slices.Compact(allMacros)
963+
964+
return allMacros
965+
}
966+
947967
// apply condition aliases to all filter/warn/crit/ok conditions.
948968
// this is useful for example in service checks, when people match for state running / started
949969
func (cd *CheckData) applyConditionAlias() {
@@ -984,10 +1004,28 @@ func (cd *CheckData) HasThreshold(name string) bool {
9841004
if cd.hasThresholdCond(cd.critThreshold, name) {
9851005
return true
9861006
}
1007+
if cd.hasThresholdCond(cd.okThreshold, name) {
1008+
return true
1009+
}
9871010

9881011
return false
9891012
}
9901013

1014+
// GetAllThresholdKeywords returns a list of all keywords used in warn/crit/ok thresholds.
1015+
func (cd *CheckData) GetAllThresholdKeywords() []string {
1016+
keywords := []string{}
1017+
1018+
keywords = append(keywords, cd.getAllThresholdKeywords(cd.warnThreshold)...)
1019+
keywords = append(keywords, cd.getAllThresholdKeywords(cd.critThreshold)...)
1020+
keywords = append(keywords, cd.getAllThresholdKeywords(cd.okThreshold)...)
1021+
1022+
// make list unique
1023+
slices.Sort(keywords)
1024+
keywords = slices.Compact(keywords)
1025+
1026+
return keywords
1027+
}
1028+
9911029
// hasThresholdCond returns true is the given list of conditions uses the given name at least once.
9921030
func (cd *CheckData) hasThresholdCond(condList ConditionList, name string) bool {
9931031
for _, cond := range condList {
@@ -1003,6 +1041,21 @@ func (cd *CheckData) hasThresholdCond(condList ConditionList, name string) bool
10031041
return false
10041042
}
10051043

1044+
// hasThresholdCond returns true is the given list of conditions uses the given name at least once.
1045+
func (cd *CheckData) getAllThresholdKeywords(condList ConditionList) []string {
1046+
keywords := []string{}
1047+
1048+
for _, cond := range condList {
1049+
if len(cond.group) > 0 {
1050+
keywords = append(keywords, cd.getAllThresholdKeywords(cond.group)...)
1051+
}
1052+
1053+
keywords = append(keywords, cond.keyword)
1054+
}
1055+
1056+
return keywords
1057+
}
1058+
10061059
// SetDefaultThresholdUnit sets default unit for all threshold conditions matching
10071060
// the name and not having a unit already
10081061
func (cd *CheckData) SetDefaultThresholdUnit(defaultUnit string, names []string) {

0 commit comments

Comments
 (0)