Skip to content

Commit

Permalink
Merge pull request #765 from mackerelio/improve-ntservice
Browse files Browse the repository at this point in the history
Improve ntservice
  • Loading branch information
yseto authored Sep 8, 2023
2 parents 49f7384 + bba0550 commit 0f93286
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
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

0 comments on commit 0f93286

Please sign in to comment.