-
Notifications
You must be signed in to change notification settings - Fork 812
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
Conor/windows services #1225
Merged
Merged
Conor/windows services #1225
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5fa62ac
Add an Agent Check for getting the state of Windows services.
conorbranagan 2f0a7e1
Use Service check values and use service checks
whatarthurcodes aee073e
Change Var name and remove sq brackets
whatarthurcodes 0b66f18
Remove tags add host name
whatarthurcodes 8d7aeda
Add hostname to tags
whatarthurcodes 8421954
Missing Else Statement
whatarthurcodes 22a30de
Remove hash for password
whatarthurcodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" Collect status information for Windows services | ||
""" | ||
# project | ||
from checks import AgentCheck | ||
|
||
# 3rd party | ||
import wmi | ||
|
||
class WindowsService(AgentCheck): | ||
STATE_TO_VALUE = { | ||
'Stopped': AgentCheck.CRITICAL, | ||
'Start Pending': AgentCheck.WARNING, | ||
'Stop Pending': AgentCheck.WARNING, | ||
'Running': AgentCheck.OK, | ||
'Continue Pending': AgentCheck.WARNING, | ||
'Pause Pending': AgentCheck.WARNING, | ||
'Paused': AgentCheck.WARNING, | ||
'Unknown': AgentCheck.UNKNOWN | ||
} | ||
|
||
def __init__(self, name, init_config, agentConfig): | ||
AgentCheck.__init__(self, name, init_config, agentConfig) | ||
self.wmi_conns = {} | ||
|
||
def _get_wmi_conn(self, host, user, password): | ||
key = "%s:%s" % (host, user) | ||
if key not in self.wmi_conns: | ||
self.wmi_conns[key] = wmi.WMI(host, user=user, password=password) | ||
return self.wmi_conns[key] | ||
|
||
def check(self, instance): | ||
# Connect to the WMI provider | ||
host = instance.get('host', None) | ||
user = instance.get('username', None) | ||
password = instance.get('password', None) | ||
tags = instance.get('tags') or [] | ||
services = instance.get('services') or [] | ||
w = self._get_wmi_conn(host, user, password) | ||
|
||
if len(services) == 0: | ||
raise Exception('No services defined in windows_service.yaml') | ||
|
||
for service in services: | ||
results = w.Win32_Service(name=service) | ||
if len(results) == 0: | ||
self.warning(u"No services found matching %s" % service) | ||
continue | ||
elif len(results) > 1: | ||
self.warning(u"Multiple services found matching %s" % service) | ||
continue | ||
|
||
wmi_service = results[0] | ||
self._create_service_check(wmi_service, host) | ||
|
||
def _create_service_check(self, wmi_service, host): | ||
""" Given an instance of a wmi_object from Win32_Service, write any | ||
performance counters to be gathered and flushed by the collector. | ||
""" | ||
if host == ".": | ||
host_name = self.hostname | ||
else: | ||
host_name = host | ||
|
||
tags = [u'service:%s' % wmi_service.Name, u'host:%s' % host_name] | ||
state_value = self.STATE_TO_VALUE.get(wmi_service.State, AgentCheck.UNKNOWN) | ||
self.service_check('windows_service.state', state_value, tags=tags) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
init_config: | ||
|
||
instances: | ||
# For each instance you define what host to connect to (defaulting to the | ||
# current host) as well as a list of services you care about. The service | ||
# names should match the Service name in the properties and NOT the display | ||
# name in the services.msc list. | ||
# | ||
# If you want to check services on a remote host, you have to specify a | ||
# hostname and (optional) credentials | ||
# | ||
#- host: MYREMOTESERVER | ||
# username: MYREMOTESERVER\fred | ||
# password: mysecretpassword | ||
# tags: | ||
# - fredserver | ||
# | ||
# The sample configuration will monitor the WMI Performance Adapter service, | ||
# named "wmiApSrv" in the service properties. | ||
# | ||
- host: . # "." means the current host | ||
services: | ||
- wmiApSrv |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you forgot the:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup i did thanks