Skip to content
Merged
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
8 changes: 4 additions & 4 deletions docs/checks/commands/check_service_linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ Naemon Config

## Check Specific Arguments

| Argument | Description |
| -------- | ----------------------------------------------------------------------------------------------------- |
| exclude | List of services to exclude from the check (mainly used when service is set to \*) (case insensitive) |
| service | List of services to check (set to \* to check all services). (case insensitive) Default: \* |
| Argument | Description |
| -------- | ---------------------------------------------------------------------------------------------------------- |
| exclude | List of case-insensitive services to exclude from the check (mainly used when service is set to \*). Supports wildcards. |
| service | List of services to check (set to \* to check all services). (case insensitive) Default: \* |

## Attributes

Expand Down
8 changes: 4 additions & 4 deletions docs/checks/commands/check_service_windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ Naemon Config

## Check Specific Arguments

| Argument | Description |
| -------- | ----------------------------------------------------------------------------------------------------- |
| exclude | List of services to exclude from the check (mainly used when service is set to \*) (case insensitive) |
| service | List of services to check (set to \* to check all services). (case insensitive) Default: \* |
| Argument | Description |
| -------- | ---------------------------------------------------------------------------------------------------------- |
| exclude | List of case-insensitive services to exclude from the check (mainly used when service is set to \*). Supports wildcards. |
| service | List of services to check (set to \* to check all services). (case insensitive) Default: \* |

## Attributes

Expand Down
17 changes: 16 additions & 1 deletion pkg/snclient/check_service_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package snclient
import (
"context"
"fmt"
"path"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -163,10 +164,24 @@ func (l *CheckService) addServiceMetrics(service string, serviceState float64, c
}
}

// matchesServiceExclude reports whether a value matches a case-insensitive exclusion pattern, including wildcards.
func matchesServiceExclude(excludes []string, value string) bool {
value = strings.ToLower(value)
if slices.Contains(excludes, value) {
return true
}

return slices.ContainsFunc(excludes, func(pattern string) bool {
match, err := path.Match(strings.ToLower(pattern), value)

return err == nil && match
})
}

func (l *CheckService) isRequired(check *CheckData, entry map[string]string, services, excludes []string) bool {
name := strings.ToLower(entry["name"])
desc := strings.ToLower(entry["desc"])
if slices.Contains(excludes, name) || slices.Contains(excludes, desc) {
if matchesServiceExclude(excludes, name) || matchesServiceExclude(excludes, desc) {
log.Tracef("service %s excluded by exclude list", name)

return false
Expand Down
4 changes: 2 additions & 2 deletions pkg/snclient/check_service_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ There is a specific [check_service for windows](../check_service_windows) as wel
description: "List of services to check (set to * to check all services). (case insensitive) Default: *",
defaultCritical: stateCondition,
},
"exclude": {value: &l.excludes, description: "List of services to exclude from the check (mainly used when service is set to *) (case insensitive)"},
"exclude": {value: &l.excludes, description: "List of case-insensitive services to exclude from the check (mainly used when service is set to *). Supports wildcards."},
},
defaultFilter: "active != inactive",
defaultCritical: stateCondition + " && preset != 'disabled'",
Expand Down Expand Up @@ -331,7 +331,7 @@ func (l *CheckService) parseAllServices(ctx context.Context, check *CheckData, o
}
service := serviceMatches[1]

if slices.Contains(l.excludes, strings.ToLower(service)) {
if matchesServiceExclude(l.excludes, service) {
log.Tracef("service %s excluded by 'exclude' argument", service)

continue
Expand Down
14 changes: 14 additions & 0 deletions pkg/snclient/check_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build windows || linux

package snclient

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCheckServiceExcludeWildcard(t *testing.T) {
assert.True(t, matchesServiceExclude([]string{"Wildcard*"}, "Wildcard"))
assert.True(t, matchesServiceExclude([]string{"Wildcard*"}, "WildcardTest"))
}
4 changes: 2 additions & 2 deletions pkg/snclient/check_service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ There is a specific [check_service for linux](../check_service_linux) as well.`,
defaultWarning: "state != 'running'",
defaultCritical: "state != 'running'",
},
"exclude": {value: &l.excludes, description: "List of services to exclude from the check (mainly used when service is set to *) (case insensitive)"},
"exclude": {value: &l.excludes, description: "List of case-insensitive services to exclude from the check (mainly used when service is set to *). Supports wildcards."},
},
defaultFilter: "none",
defaultCritical: "state != 'running' && start_type = 'auto'",
Expand Down Expand Up @@ -150,7 +150,7 @@ func (l *CheckService) Check(ctx context.Context, _ *Agent, check *CheckData, _
}

for _, service := range serviceList {
if slices.Contains(l.excludes, strings.ToLower(strings.TrimSpace(service))) {
if matchesServiceExclude(l.excludes, strings.TrimSpace(service)) {
log.Tracef("service %s excluded by 'exclude' argument", service)

continue
Expand Down