Skip to content

Commit 28ba2a8

Browse files
committed
[ntservice] improved checking of Windows services
- Added flag to check service names for exact match - Fixed UNKNOWN alert if service does not exist - Fixed that the checked service name is displayed in the message when the check result is OK
1 parent 49f7384 commit 28ba2a8

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

check-ntservice/lib/check_ntservice.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var opts struct {
1313
ServiceName string `long:"service-name" short:"s" description:"service name"`
1414
ExcludeService string `long:"exclude-service" short:"x" description:"service name to exclude from matching. This option takes precedence over --service-name"`
1515
ListService bool `long:"list-service" short:"l" description:"list service"`
16+
Exact bool `long:"exact" description:"more exact checking of the service. This option applies only to --service-name."`
1617
}
1718

1819
// Win32Service is struct for Win32_Service.
@@ -58,19 +59,26 @@ func run(args []string) *checkers.Checker {
5859
return checkers.Critical(err.Error())
5960
}
6061

61-
checkSt := checkers.OK
62-
msg := ""
62+
checkSt := checkers.UNKNOWN
63+
msg := fmt.Sprintf("%s: service does not exist.", opts.ServiceName)
6364
for _, s := range ss {
6465
if opts.ExcludeService != "" && strings.Contains(s.Name, opts.ExcludeService) {
6566
continue
6667
}
67-
if !strings.Contains(s.Name, opts.ServiceName) {
68-
continue
68+
if opts.Exact {
69+
if s.Name != opts.ServiceName {
70+
continue
71+
}
72+
} else {
73+
if !strings.Contains(s.Name, opts.ServiceName) {
74+
continue
75+
}
6976
}
7077
if s.State == "Running" {
71-
continue
78+
checkSt = checkers.OK
79+
} else {
80+
checkSt = checkers.CRITICAL
7281
}
73-
checkSt = checkers.CRITICAL
7482
msg = fmt.Sprintf("%s: %s - %s", s.Name, s.Caption, s.State)
7583
break
7684
}

check-ntservice/lib/check_ntservice_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestRun(t *testing.T) {
8989
casename: "check about running service",
9090
cmdline: []string{"-s", "running-service"},
9191
expectStatus: checkers.OK,
92-
expectMessage: "",
92+
expectMessage: "running-service-name: running-service-caption - Running",
9393
},
9494
{
9595
casename: "check about stopped service",
@@ -101,7 +101,25 @@ func TestRun(t *testing.T) {
101101
casename: "check about running service with exclude option",
102102
cmdline: []string{"-s", "service", "-x", "stopped"},
103103
expectStatus: checkers.OK,
104-
expectMessage: "",
104+
expectMessage: "running-service-name: running-service-caption - Running",
105+
},
106+
{
107+
casename: "check about unknown service",
108+
cmdline: []string{"-s", "unknown-service-name"},
109+
expectStatus: checkers.UNKNOWN,
110+
expectMessage: "unknown-service-name: service does not exist.",
111+
},
112+
{
113+
casename: "check about running service with --exact option",
114+
cmdline: []string{"-s", "running-service-name", "--exact"},
115+
expectStatus: checkers.OK,
116+
expectMessage: "running-service-name: running-service-caption - Running",
117+
},
118+
{
119+
casename: "check about unmatched service with --exact option",
120+
cmdline: []string{"-s", "service", "--exact"},
121+
expectStatus: checkers.UNKNOWN,
122+
expectMessage: "service: service does not exist.",
105123
},
106124
}
107125

0 commit comments

Comments
 (0)