From 441eb1ac4e759882fd2c9403350bcc7bdafc51fd Mon Sep 17 00:00:00 2001 From: Mitchell Cooper Date: Fri, 8 Dec 2017 12:36:49 -0500 Subject: [PATCH 1/3] Add recipe for wmi_check --- recipes/wmi_check.rb | 112 +++++++++++++++++++ spec/integrations/wmi_check_spec.rb | 161 +++++++++++++++++++++++++++ templates/default/wmi_check.yaml.erb | 76 +------------ 3 files changed, 275 insertions(+), 74 deletions(-) create mode 100644 recipes/wmi_check.rb create mode 100644 spec/integrations/wmi_check_spec.rb diff --git a/recipes/wmi_check.rb b/recipes/wmi_check.rb new file mode 100644 index 00000000..6694b64d --- /dev/null +++ b/recipes/wmi_check.rb @@ -0,0 +1,112 @@ +include_recipe 'datadog::dd-agent' + +# see example configuration file here: +# https://github.com/DataDog/integrations-core/blob/master/wmi_check/conf.yaml.example + +# node['datadog']['wmi_check'] = +# { +# "init_config": null, +# "instances": [ +# { +# "class": "Win32_OperatingSystem", +# "metrics": [ +# [ +# "NumberOfProcesses", +# "system.proc.count", +# "gauge" +# ], +# [ +# "NumberOfUsers", +# "system.users.count", +# "gauge" +# ] +# ] +# }, +# { +# "class": "Win32_PerfFormattedData_PerfProc_Process", +# "metrics": [ +# [ +# "ThreadCount", +# "my_app.threads.count", +# "gauge" +# ], +# [ +# "VirtualBytes", +# "my_app.mem.virtual", +# "gauge" +# ] +# ], +# "filters": [ +# { +# "Name": "myapp" +# } +# ], +# "constant_tags": [ +# "role:test" +# ] +# }, +# { +# "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" +# }, +# { +# "class": "Win32_PerfFormattedData_PerfProc_Process", +# "metrics": [ +# [ +# "IOReadBytesPerSec", +# "proc.io.bytes_read", +# "gauge" +# ] +# ], +# "filters": [ +# { +# "Name": "app%" +# } +# ], +# "tag_by": "Name", +# "tag_queries": [ +# [ +# "IDProcess", +# "Win32_Process", +# "Handle", +# "CommandLine" +# ] +# ] +# } +# ], +# "logs": null +# } + +datadog_monitor 'wmi_check' do + init_config node['datadog']['wmi_check']['init_config'] + instances node['datadog']['wmi_check']['instances'] + logs node['datadog']['wmi_check']['logs'] +end diff --git a/spec/integrations/wmi_check_spec.rb b/spec/integrations/wmi_check_spec.rb new file mode 100644 index 00000000..a1c0f2ef --- /dev/null +++ b/spec/integrations/wmi_check_spec.rb @@ -0,0 +1,161 @@ +describe 'datadog::wmi_check' do + context 'config wmi_check' do + expected_yaml = <<-EOF + init_config: + instances: + - class: Win32_OperatingSystem + metrics: + - [NumberOfProcesses, system.proc.count, gauge] + - [NumberOfUsers, system.users.count, gauge] + - class: Win32_PerfFormattedData_PerfProc_Process + metrics: + - [ThreadCount, my_app.threads.count, gauge] + - [VirtualBytes, my_app.mem.virtual, gauge] + filters: + - Name: myapp + constant_tags: + - 'role:test' + - 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 + - class: Win32_PerfFormattedData_PerfProc_Process + metrics: + - [IOReadBytesPerSec, proc.io.bytes_read, gauge] + filters: + - Name: 'app%' + tag_by: Name + tag_queries: + - [IDProcess, Win32_Process, Handle, CommandLine] + logs: ~ + EOF + + cached(:chef_run) do + ChefSpec::SoloRunner.new(step_into: ['datadog_monitor']) do |node| + node.automatic['languages'] = { 'python' => { 'version' => '2.7.2' } } + node.set['datadog'] = { + api_key: 'someapikey', + wmi_check: { + init_config: nil, + instances: [ + { + class: 'Win32_OperatingSystem', + metrics: [ + [ + 'NumberOfProcesses', + 'system.proc.count', + 'gauge' + ], + [ + 'NumberOfUsers', + 'system.users.count', + 'gauge' + ] + ] + }, + { + class: 'Win32_PerfFormattedData_PerfProc_Process', + metrics: [ + [ + 'ThreadCount', + 'my_app.threads.count', + 'gauge' + ], + [ + 'VirtualBytes', + 'my_app.mem.virtual', + 'gauge' + ] + ], + filters: [ + { + Name: 'myapp' + } + ], + constant_tags: [ + 'role:test' + ] + }, + { + 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' + }, + { + class: 'Win32_PerfFormattedData_PerfProc_Process', + metrics: [ + [ + 'IOReadBytesPerSec', + 'proc.io.bytes_read', + 'gauge' + ] + ], + filters: [ + { + Name: 'app%' + } + ], + tag_by: 'Name', + tag_queries: [ + [ + 'IDProcess', + 'Win32_Process', + 'Handle', + 'CommandLine' + ] + ] + } + ] + } + } + end.converge(described_recipe) + end + + subject { chef_run } + + it_behaves_like 'datadog-agent' + + it { is_expected.to include_recipe('datadog::dd-agent') } + + it { is_expected.to add_datadog_monitor('wmi_check') } + + it 'renders expected YAML config file' do + expect(chef_run).to(render_file('/etc/dd-agent/conf.d/wmi_check.yaml').with_content { |content| + expect(YAML.safe_load(content).to_json).to be_json_eql(YAML.safe_load(expected_yaml).to_json) + }) + end + end +end diff --git a/templates/default/wmi_check.yaml.erb b/templates/default/wmi_check.yaml.erb index a80c523a..11b348e0 100644 --- a/templates/default/wmi_check.yaml.erb +++ b/templates/default/wmi_check.yaml.erb @@ -1,75 +1,3 @@ -<%= JSON.parse(({'logs' => @logs }).to_json).to_yaml %> - -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 - +# Generated by Chef, local modifications will be overwritten +<%= JSON.parse(({'init_config' => @init_config, 'instances' => @instances, 'logs' => @logs}).to_json).to_yaml %> From 7780bdef51a908cc5d5836e55c2403cc15fdd5a2 Mon Sep 17 00:00:00 2001 From: Mitchell Cooper Date: Fri, 8 Dec 2017 12:41:56 -0500 Subject: [PATCH 2/3] Rubocop fix --- spec/integrations/wmi_check_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/integrations/wmi_check_spec.rb b/spec/integrations/wmi_check_spec.rb index a1c0f2ef..b21f616a 100644 --- a/spec/integrations/wmi_check_spec.rb +++ b/spec/integrations/wmi_check_spec.rb @@ -78,8 +78,8 @@ Name: 'myapp' } ], - constant_tags: [ - 'role:test' + constant_tags: [ + 'role:test' ] }, { From 275da2d2b9ca4a82ef53289c9e24cd079bd8cc47 Mon Sep 17 00:00:00 2001 From: Mitchell Cooper Date: Fri, 8 Dec 2017 15:15:40 -0500 Subject: [PATCH 3/3] Nothing to configure in the init_config section in the YAML template --- recipes/wmi_check.rb | 1 - spec/integrations/wmi_check_spec.rb | 1 - templates/default/wmi_check.yaml.erb | 5 ++++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/recipes/wmi_check.rb b/recipes/wmi_check.rb index 6694b64d..b5c31e3e 100644 --- a/recipes/wmi_check.rb +++ b/recipes/wmi_check.rb @@ -5,7 +5,6 @@ # node['datadog']['wmi_check'] = # { -# "init_config": null, # "instances": [ # { # "class": "Win32_OperatingSystem", diff --git a/spec/integrations/wmi_check_spec.rb b/spec/integrations/wmi_check_spec.rb index b21f616a..e0283300 100644 --- a/spec/integrations/wmi_check_spec.rb +++ b/spec/integrations/wmi_check_spec.rb @@ -42,7 +42,6 @@ node.set['datadog'] = { api_key: 'someapikey', wmi_check: { - init_config: nil, instances: [ { class: 'Win32_OperatingSystem', diff --git a/templates/default/wmi_check.yaml.erb b/templates/default/wmi_check.yaml.erb index 11b348e0..ab6c4d5b 100644 --- a/templates/default/wmi_check.yaml.erb +++ b/templates/default/wmi_check.yaml.erb @@ -1,3 +1,6 @@ # Generated by Chef, local modifications will be overwritten -<%= JSON.parse(({'init_config' => @init_config, 'instances' => @instances, 'logs' => @logs}).to_json).to_yaml %> +<%= JSON.parse(({ 'instances' => @instances, 'logs' => @logs }).to_json).to_yaml %> + +# Nothing to configure here +init_config: