Skip to content

Commit

Permalink
Merge pull request #359 from DataDog/updated_wmi_check
Browse files Browse the repository at this point in the history
Update the WMI check to checks.d with further configuration
  • Loading branch information
conorbranagan committed Feb 5, 2013
2 parents 3c5dacd + aa0c5c8 commit b8c0e7f
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 70 deletions.
79 changes: 79 additions & 0 deletions checks.d/wmi_check.py
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
2 changes: 0 additions & 2 deletions checks/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from checks.ganglia import Ganglia
from checks.cassandra import Cassandra
from checks.datadog import Dogstreams, DdForwarder
from checks.wmi_check import WMICheck
from checks.ec2 import EC2
from checks.check_status import CheckStatus, CollectorStatus, EmitterStatus
from resources.processes import Processes as ResProcesses
Expand Down Expand Up @@ -94,7 +93,6 @@ def __init__(self, agentConfig, emitters, systemStats):

# Metric Checks
self._metrics_checks = [
WMICheck(log),
Memcache(log),
]

Expand Down
68 changes: 0 additions & 68 deletions checks/wmi_check.py

This file was deleted.

73 changes: 73 additions & 0 deletions conf.d/wmi_check.yaml.example
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


0 comments on commit b8c0e7f

Please sign in to comment.