Skip to content

Commit 091419e

Browse files
author
Remi Hakim
committed
[windows services] Be more resilient to case
WMI queries are not sensitive to case when fetching services status. So you could have a conf.d with a different case for the service name. But as we were comparing names with the actual case here: https://github.com/DataDog/dd-agent/blob/5.7.4/checks.d/windows_service. py#L71 It would result in a bad logic. This fixes it by having a simpler logic.
1 parent 436d0fb commit 091419e

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

checks.d/windows_service.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,17 @@ def check(self, instance):
6464
self._process_services(wmi_sampler, services, tags)
6565

6666
def _process_services(self, wmi_sampler, services, tags):
67-
expected_services = set(services)
67+
collected_services_by_names = {sc['Name'].lower(): sc for sc in wmi_sampler}
6868

69-
for wmi_obj in wmi_sampler:
70-
service = wmi_obj['Name']
71-
if service not in services:
72-
continue
69+
for service in services:
70+
service_lower = service.lower()
71+
if service_lower in collected_services_by_names:
72+
wmi_obj = collected_services_by_names[service_lower]
73+
sc_name = wmi_obj['Name']
74+
status = self.STATE_TO_VALUE.get(wmi_obj["state"], AgentCheck.UNKNOWN)
75+
self.service_check("windows_service.state", status,
76+
tags=tags + ['service:{0}'.format(sc_name)])
7377

74-
status = self.STATE_TO_VALUE.get(wmi_obj["state"], AgentCheck.UNKNOWN)
75-
self.service_check("windows_service.state", status,
76-
tags=tags + ['service:{0}'.format(service)])
77-
expected_services.remove(service)
78-
79-
for service in expected_services:
80-
self.service_check("windows_service.state", AgentCheck.CRITICAL,
78+
else:
79+
self.service_check("windows_service.state", AgentCheck.CRITICAL,
8180
tags=tags + ['service:{0}'.format(service)])

0 commit comments

Comments
 (0)