Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ntservice #765

Merged
merged 3 commits into from
Sep 8, 2023
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
5 changes: 3 additions & 2 deletions check-ntservice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```


Expand Down
24 changes: 16 additions & 8 deletions check-ntservice/lib/check_ntservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 20 additions & 2 deletions check-ntservice/lib/check_ntservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.",
},
}

Expand Down