diff --git a/check-ntservice/README.md b/check-ntservice/README.md index 2420a7fb..7e0c8b89 100644 --- a/check-ntservice/README.md +++ b/check-ntservice/README.md @@ -42,9 +42,10 @@ command = ["check-ntservice", "--service-name", "SERVICE_NAME"] ### Options ``` - -s, --service-name= service name - -E, --exclude-service= service name to exclude from matching. This option takes precedence over --service-name + -s, --service-name= matches if contained in service name + -E, --exclude-service= exclude if contained in service name. This option takes precedence over --service-name -l, --list-service list service + --exact more exact checking of the service. This option applies only to --service-name. ``` diff --git a/check-ntservice/lib/check_ntservice.go b/check-ntservice/lib/check_ntservice.go index ad16166b..6e7c6573 100644 --- a/check-ntservice/lib/check_ntservice.go +++ b/check-ntservice/lib/check_ntservice.go @@ -10,9 +10,10 @@ import ( ) var opts struct { - ServiceName string `long:"service-name" short:"s" description:"service name"` - ExcludeService string `long:"exclude-service" short:"x" description:"service name to exclude from matching. This option takes precedence over --service-name"` + ServiceName string `long:"service-name" short:"s" description:"matches if contained in service name."` + ExcludeService string `long:"exclude-service" short:"x" description:"exclude if contained in service name. This option takes precedence over --service-name."` ListService bool `long:"list-service" short:"l" description:"list service"` + Exact bool `long:"exact" description:"more exact checking of the service. This option applies only to --service-name."` } // Win32Service is struct for Win32_Service. @@ -58,19 +59,26 @@ func run(args []string) *checkers.Checker { return checkers.Critical(err.Error()) } - checkSt := checkers.OK - msg := "" + checkSt := checkers.UNKNOWN + msg := fmt.Sprintf("%s: service does not exist.", opts.ServiceName) for _, s := range ss { if opts.ExcludeService != "" && strings.Contains(s.Name, opts.ExcludeService) { continue } - if !strings.Contains(s.Name, opts.ServiceName) { - continue + if opts.Exact { + if s.Name != opts.ServiceName { + continue + } + } else { + if !strings.Contains(s.Name, opts.ServiceName) { + continue + } } if s.State == "Running" { - continue + checkSt = checkers.OK + } else { + checkSt = checkers.CRITICAL } - checkSt = checkers.CRITICAL msg = fmt.Sprintf("%s: %s - %s", s.Name, s.Caption, s.State) break } diff --git a/check-ntservice/lib/check_ntservice_test.go b/check-ntservice/lib/check_ntservice_test.go index 6f4d7aa5..18d15859 100644 --- a/check-ntservice/lib/check_ntservice_test.go +++ b/check-ntservice/lib/check_ntservice_test.go @@ -89,7 +89,7 @@ func TestRun(t *testing.T) { casename: "check about running service", cmdline: []string{"-s", "running-service"}, expectStatus: checkers.OK, - expectMessage: "", + expectMessage: "running-service-name: running-service-caption - Running", }, { casename: "check about stopped service", @@ -101,7 +101,25 @@ func TestRun(t *testing.T) { casename: "check about running service with exclude option", cmdline: []string{"-s", "service", "-x", "stopped"}, expectStatus: checkers.OK, - expectMessage: "", + expectMessage: "running-service-name: running-service-caption - Running", + }, + { + casename: "check about unknown service", + cmdline: []string{"-s", "unknown-service-name"}, + expectStatus: checkers.UNKNOWN, + expectMessage: "unknown-service-name: service does not exist.", + }, + { + casename: "check about running service with --exact option", + cmdline: []string{"-s", "running-service-name", "--exact"}, + expectStatus: checkers.OK, + expectMessage: "running-service-name: running-service-caption - Running", + }, + { + casename: "check about unmatched service with --exact option", + cmdline: []string{"-s", "service", "--exact"}, + expectStatus: checkers.UNKNOWN, + expectMessage: "service: service does not exist.", }, }