-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from DataDog/updated_wmi_check
Update the WMI check to checks.d with further configuration
- Loading branch information
Showing
4 changed files
with
152 additions
and
70 deletions.
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,79 @@ | ||
''' | ||
Windows Only. | ||
Generic WMI check. This check allows you to specify particular metrics that you | ||
want from WMI in your configuration. Check wmi.yaml.example in your conf.d | ||
directory for more details on configuration. | ||
''' | ||
from checks import AgentCheck | ||
|
||
UP_METRIC = 'Up' | ||
|
||
class WMICheck(AgentCheck): | ||
def check(self, instance): | ||
wmi_class = instance.get('class') | ||
metrics = instance.get('metrics') | ||
filters = instance.get('filters') | ||
tag_by = instance.get('tag_by') | ||
|
||
if not wmi_class: | ||
raise Exception('WMI instance is missing a value for `class` in wmi.yaml') | ||
|
||
import wmi | ||
w = wmi.WMI() | ||
|
||
# If there are filters, we need one query per filter. | ||
if filters: | ||
for f in filters: | ||
results = getattr(w, wmi_class)(**f) | ||
self._extract_metrics(results, metrics, tag_by) | ||
else: | ||
results = getattr(w, wmi_class)() | ||
self._extract_metrics(results, metrics, tag_by) | ||
|
||
def _extract_metrics(self, results, metrics, tag_by): | ||
if len(results) > 1 and tag_by is None: | ||
raise Exception('WMI query returned multiple rows but no `tag_by` value was given. metrics=%s' % metrics) | ||
|
||
for wmi_property, name, mtype in metrics: | ||
for res in results: | ||
if wmi_property == UP_METRIC: | ||
# Special-case metric will just submit 1 for every value | ||
# returned in the result. | ||
val = 1 | ||
else: | ||
val = float(getattr(res, wmi_property)) | ||
|
||
# Grab the tag from the result if there's a `tag_by` value (e.g.: "name:jenkins") | ||
if tag_by: | ||
tags = ['%s:%s' % (tag_by.lower(), getattr(res, tag_by))] | ||
else: | ||
tags = None | ||
|
||
try: | ||
func = getattr(self, mtype) | ||
except AttributeError: | ||
raise Exception('Invalid metric type: {0}'.format(mtype)) | ||
|
||
# submit the metric to datadog | ||
func(name, val, tags=tags) | ||
|
||
@staticmethod | ||
def parse_agent_config(agentConfig): | ||
if not agentConfig.get('WMI'): | ||
return False | ||
|
||
config = [] | ||
metrics = agentConfig['WMI'] | ||
for metric_name, wmi_conf in metrics.items(): | ||
try: | ||
wmi_class, wmi_prop = wmi_conf.split(':') | ||
except ValueError: | ||
self.logger.error('Invalid WMI line format: %s' % wmi_conf) | ||
|
||
config.append({ | ||
'class': wmi_class, | ||
'metrics': [[wmi_prop, metric_name, 'gauge']] | ||
}) | ||
|
||
return config |
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
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
init_config: | ||
|
||
instances: | ||
# Each WMI query has 2 required options, `class` and `metrics` and two | ||
# optional options, `filters` and `tag_by`. | ||
# | ||
# `class` is the name of the WMI class, for example Win32_OperatingSystem | ||
# or Win32_PerfFormattedData_PerfProc_Process. You can find many of the | ||
# standard class names on the MSDN docs at | ||
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa394084.aspx. | ||
# The Win32_FormattedData_* classes provide many useful performance counters | ||
# by default. | ||
# | ||
# | ||
# `metrics` is a list of metrics you want to capture, with each item in the | ||
# list being a set of [WMI property name, metric name, metric type]. | ||
# | ||
# - The property name is something like `NumberOfUsers` or `ThreadCount`. | ||
# The standard properties are also available on the MSDN docs for each | ||
# class. | ||
# | ||
# - The metric name is the name you want to show up in Datadog. | ||
# | ||
# - The metric type is from the standard choices for all agent checks, such | ||
# as gauge, rate, histogram or counter. | ||
# | ||
# | ||
# `filters` is a list of filters on the WMI query you may want. For example, | ||
# for a process-based WMI class you may want metrics for only certain | ||
# processes running on your machine, so you could add a filter for each | ||
# process name. See below for an example of this case. | ||
# | ||
# | ||
# `tag_by` optionally lets you tag each metric with a property from the | ||
# WMI class you're using. This is only useful when you will have multiple | ||
# values for your WMI query. The examples below show how you can tag your | ||
# process metrics with the process name (giving a tag of "name:app_name"). | ||
|
||
|
||
# Fetch the number of processes and users | ||
- class: Win32_OperatingSystem | ||
metrics: | ||
- [NumberOfProcesses, system.proc.count, gauge] | ||
- [NumberOfUsers, system.users.count, gauge] | ||
|
||
# Fetch metrics for a single running application, called myapp | ||
- class: Win32_PerfFormattedData_PerfProc_Process | ||
metrics: | ||
- [ThreadCount, my_app.threads.count, gauge] | ||
- [VirtualBytes, my_app.mem.virtual, gauge] | ||
filters: | ||
- Name: myapp | ||
|
||
# Fetch process metrics for a set of processes, tagging by app name. | ||
- class: Win32_PerfFormattedData_PerfProc_Process | ||
metrics: | ||
- [ThreadCount, proc.threads.count, gauge] | ||
- [VirtualBytes, proc.mem.virtual, gauge] | ||
- [PercentProcessorTime, proc.cpu_pct, gauge] | ||
filters: | ||
- Name: app1 | ||
- Name: app2 | ||
- Name: app3 | ||
tag_by: Name | ||
|
||
# Fetch process metrics for every available process, tagging by app name. | ||
- class: Win32_PerfFormattedData_PerfProc_Process | ||
metrics: | ||
- [IOReadBytesPerSec, proc.io.bytes_read, gauge] | ||
- [IOWriteBytesPerSec, proc.io.bytes_written, gauge] | ||
tag_by: Name | ||
|
||
|