Skip to content

Commit

Permalink
fix using condition alias on nested filters
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Aug 2, 2024
1 parent 1630dec commit 02247e2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ next:
- fix evaluating nested filter conditions
- fix check_drivesize missing performance data when using free_bytes threshold (#136)
- fix check_http/check_tcp/check_dns help (#135)
- fix using condition alias on nested filters
- update windows exporter to 0.26.1

0.26 Wed Jul 17 15:23:37 CEST 2024
Expand Down
5 changes: 5 additions & 0 deletions pkg/snclient/check_service_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ There is a specific [check_service for windows](../check_service_windows) as wel
result: &CheckResult{
State: CheckExitOK,
},
conditionAlias: map[string]map[string]string{
"state": {
"started": "running",
},
},
args: map[string]CheckArgument{
"service": {
value: &l.services,
Expand Down
7 changes: 4 additions & 3 deletions pkg/snclient/checkdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ func (cd *CheckData) setFallbacks(applyDefaultFilter bool, defaultWarning, defau
}

// apply condition aliases to all filter/warn/crit/ok conditions.
// this is useful for example in service checks, when people match for state running / started
func (cd *CheckData) applyConditionAlias() {
if len(cd.conditionAlias) == 0 {
return
Expand All @@ -862,12 +863,12 @@ func (cd *CheckData) applyConditionAlias() {
}

// apply condition aliases to given conditions.
func (cd *CheckData) applyConditionAliasList(cond ConditionList) {
for _, cond := range cond {
func (cd *CheckData) applyConditionAliasList(condList ConditionList) {
for _, cond := range condList {
if len(cond.group) > 0 {
cd.applyConditionAliasList(cond.group)

return
continue
}

for replaceKeyword, aliasMap := range cd.conditionAlias {
Expand Down
19 changes: 19 additions & 0 deletions pkg/snclient/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,22 @@ func TestConditionPreCheck(t *testing.T) {
assert.Equalf(t, check.expectPre, ok, fmt.Sprintf("final check returned: %v", ok))
}
}

func TestConditionAlias(t *testing.T) {
filterStr := `( name = 'xinetd' or name like 'other' ) and state = 'started'`
cond, err := NewCondition(filterStr)
require.NoError(t, err)

check := &CheckData{
filter: ConditionList{cond},
conditionAlias: map[string]map[string]string{
"state": {
"started": "running",
},
},
}
check.applyConditionAlias()

check.filter[0].original = "" // avoid shortcut String builder
assert.Containsf(t, check.filter[0].String(), `state = running`, "filter condition replaced")
}

0 comments on commit 02247e2

Please sign in to comment.