Add support for the systemd check
ADD
recipes/systemd.rb
+include_recipe 'datadog::dd-agent'
+
+# Monitor Systemd
+#
+# node.default['datadog']['systemd']['instances'] = [
+# {
+# 'unit_names' => [
+# 'myservice1.service',
+# 'myservice2.service',
+# 'mysocket.socket'
+# ],
+# 'substate_status_mapping' => [
+# 'services' => [
+# 'myservice1' => {
+# 'running' => 'ok',
+# 'exited' => 'critical'
+# },
+# 'myservice2' => {
+# 'plugged' => 'ok',
+# 'mounted' => 'ok',
+# 'running' => 'ok',
+# 'exited' => 'critical',
+# 'stopped' => 'critical'
+# }
+# ],
+# 'sockets' => [
+# 'mysocket' => {
+# 'running' => 'ok',
+# 'exited' => 'critical'
+# }
+# ]
+# ],
+# 'tags' => [
+# 'mykey1:myvalue1',
+# 'mykey2:myvalue2'
+# ]
+# }
+# ]
+
+datadog_monitor 'systemd' do
+ instances node['datadog']['systemd']['instances']
+ logs node['datadog']['systemd']['logs']
+ action :add
+ notifies :restart, 'service[datadog-agent]' if node['datadog']['agent_start']
+end

ADD
spec/integrations/systemd_spec.rb
+describe 'datadog::systemd' do
+ expected_yaml = <<-EOF
+ init_config: ~
+ instances:
+ - substate_status_mapping:
+ myservice1.service:
+ exited: critical
+ running: ok
+ myservice2.service:
+ exited: critical
+ mounted: ok
+ plugged: ok
+ running: ok
+ stopped: critical
+ mysocket.socket:
+ exited: critical
+ running: ok
+ tags:
+ - 'mykey1:myvalue1'
+ - 'mykey2:myvalue2'
+ unit_names:
+ - myservice1.service
+ - myservice2.service
+ - mysocket.socket
+ logs: ~
+ EOF
+
+ cached(:chef_run) do
+ ChefSpec::SoloRunner.new(
+ platform: 'ubuntu',
+ version: '16.04',
+ step_into: ['datadog_monitor']
+ ) do |node|
+ node.automatic['languages'] = { 'python' => { 'version' => '2.7.2' } }
+ node.normal['datadog'] = {
+ api_key: 'someapikey',
+ systemd: {
+ instances: [
+ {
+ unit_names: [
+ 'myservice1.service',
+ 'myservice2.service',
+ 'mysocket.socket'
+ ],
+ substate_status_mapping: [
+ services: [
+ myservice1: {
+ running: 'ok',
+ exited: 'critical'
+ },
+ myservice2: {
+ plugged: 'ok',
+ mounted: 'ok',
+ running: 'ok',
+ exited: 'critical',
+ stopped: 'critical'
+ }
+ ],
+ sockets: [
+ mysocket: {
+ running: 'ok',
+ exited: 'critical'
+ }
+ ]
+ ],
+ tags: [
+ 'mykey1:myvalue1',
+ 'mykey2:myvalue2'
+ ]
+ }
+ ]
+ }
+ }
+ 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('systemd') }
+
+ it 'renders expected YAML config file' do
+ expect(chef_run).to(render_file('/etc/datadog-agent/conf.d/systemd.d/conf.yaml').with_content { |content|
+ expect(YAML.safe_load(content).to_json).to be_json_eql(YAML.safe_load(expected_yaml).to_json)
+ })
+ end
+end

ADD
templates/default/systemd.yaml.erb
+<%= JSON.parse(({'logs' => @logs }).to_json).to_yaml %>
+
+instances:
+ <% @instances.each do |i| -%>
+ - unit_names:
+ <% i['unit_names'].each do |u| -%>
+ - <%= u %>
+ <% end -%>
+ <% if i.key?('substate_status_mapping') -%>
+ substate_status_mapping:
+ <% i['substate_status_mapping'].each do |s| -%>
+ <% if s.key?('services') -%>
+ <% s['services'].each do |k| -%>
+ <% k.keys.each do |j| -%>
+ <%= j %>.service:
+ <% k["#{j}"].each do |l,v| %>
+ <%= l %>: <%= v %>
+ <% end -%>
+ <% end -%>
+ <% end -%>
+ <% end -%>
+ <% if s.key?('sockets') -%>
+ <% s['sockets'].each do |k| -%>
+ <% k.keys.each do |j| -%>
+ <%= j %>.socket:
+ <% k["#{j}"].each do |l,v| %>
+ <%= l %>: <%= v %>
+ <% end -%>
+ <% end -%>
+ <% end -%>
+ <% end -%>
+ <% end -%>
+ <% end -%>
+ <% if i.key?('tags') -%>
+ tags:
+ <% i['tags'].each do |t| -%>
+ - <%= t %>
+ <% end -%>
+ <% end -%>
+ <% end -%>
+
+init_config:
+# No init_config details needed

ADD
test/integration/datadog_systemd/serverspec/systemd_spec.rb
+# Encoding: utf-8
+
+require 'spec_helper'
+
+AGENT_CONFIG = File.join(@agent_config_dir, 'conf.d/systemd.d/conf.yaml')
+
+describe service(@agent_service_name) do
+ it { should be_running }
+end
+
+describe file(AGENT_CONFIG) do
+ it { should be_a_file }
+
+ it 'is valid yaml matching input values' do
+ generated = YAML.load_file(AGENT_CONFIG)
+
+ expected = {
+ 'instances' => [
+ {
+ 'unit_names' => [
+ 'myservice1.service',
+ 'myservice2.service',
+ 'mysocket.socket'
+ ]
+ }
+ ],
+ 'logs' => nil,
+ 'init_config' => nil
+ }
+
+ expect(generated.to_json).to be_json_eql expected.to_json
+ end
+end

Add support for the systemd check (#755)