Skip to content

Commit

Permalink
Merge pull request sensu#785 from jeffmccune/783_sensu_check_write_json
Browse files Browse the repository at this point in the history
(sensu#783) Add sensu::check content parameter, use sensu::write_json
  • Loading branch information
ghoneycutt authored Aug 18, 2017
2 parents 438fbaa + e79df12 commit cafa78d
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 105 deletions.
76 changes: 62 additions & 14 deletions manifests/check.pp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
# Note: The validity of the other checks is not enforced by puppet
# Set this to 'absent' to remove it completely.
#
# @param content Mapping of arbitrary attributes from the top-level of the target
# configuration JSON map. This parameter is intended to configure plugins and
# extensions which look up values outside of the check configuration scope.
# Example: { "mailer" => { "mail_from" => "[email protected]", "mail_to" => "[email protected]" } }
#
# @param custom List of custom attributes to include in the check.
# You can use it to pass any attribute that is not listed here explicitly.
# Example: { 'remediation' => { 'low_remediation' => { 'occurrences' => [1,2], 'severities' => [1], 'command' => "/bin/command", 'publish' => false, } } }
Expand Down Expand Up @@ -107,6 +112,7 @@
Variant[Undef,Enum['absent'],Boolean] $publish = undef,
Variant[Undef,String,Array] $dependencies = undef,
Optional[Hash] $custom = undef,
Hash $content = {},
Variant[Undef,Enum['absent'],Integer] $ttl = undef,
Variant[Undef,Enum['absent'],Hash] $subdue = undef,
Variant[Undef,Enum['absent'],Hash] $proxy_requests = undef,
Expand Down Expand Up @@ -161,17 +167,9 @@
Anchor['plugins_before_checks']
~> Sensu::Check[$name]

file { "${conf_dir}/checks/${check_name}.json":
ensure => $ensure,
owner => $user,
group => $group,
mode => $file_mode,
before => Sensu_check[$check_name],
}

sensu_check { $check_name:
ensure => $ensure,
base_path => "${conf_dir}/checks",
# This Hash map will ultimately exist at `{"checks" => {"$check_name" =>
# $check_config}}`
$check_config_start = {
type => $type,
standalone => $standalone,
command => $command,
Expand All @@ -191,11 +189,61 @@
handle => $handle,
publish => $publish,
dependencies => $dependencies,
custom => $custom,
subdue => $subdue,
proxy_requests => $proxy_requests,
require => File["${conf_dir}/checks"],
notify => $::sensu::check_notify,
ttl => $ttl,
}

# Remove key/value pares where the value is `undef` or `"absent"`.
$check_config_pruned = $check_config_start.reduce({}) |$memo, $kv| {
$kv[1] ? {
undef => $memo,
'absent' => $memo,
default => $memo + Hash.new($kv),
}
}

# Merge the specified properties on top of the custom hash.
if $custom == undef {
$check_config = $check_config_pruned
} else {
$check_config = $custom + $check_config_pruned
}

# Merge together the "checks" scope with any arbitrary config specified via
# `content`.
$checks_scope_start = { $check_name => $check_config }
if $content['checks'] == undef {
$checks_scope = { 'checks' => $checks_scope_start }
} else {
$checks_scope = { 'checks' => $content['checks'] + $checks_scope_start }
}

# The final structure from the top level. Check configuration scope is merged
# on top of any arbitrary plugin and extension configuration in $content.
$content_real = $content + $checks_scope

# Compute the services to notify
$notification_map = {
'Class[Sensu::Client::Service]' => $::sensu::client,
'Class[Sensu::Server::Service]' => $::sensu::server,
'Class[Sensu::Api::Service]' => $::sensu::api,
}

$notification_ary = $notification_map.reduce([]) |$memo, $kv| {
if $kv[1] {
$memo + [$kv[0]]
} else {
$memo
}
}

sensu::write_json { "${conf_dir}/checks/${check_name}.json":
ensure => $ensure,
content => $content_real,
owner => $user,
group => $group,
mode => $file_mode,
notify_list => $notification_ary,
}
}
6 changes: 6 additions & 0 deletions manifests/write_json.pp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@
# @param content The hash content that will be converted to json
# and written into the target config file.
#
# [*notify_list*]
# Array. A listing of resources to notify upon changes to the target JSON
# file.
# Default: []
define sensu::write_json (
Enum['present', 'absent'] $ensure = 'present',
String $owner = 'sensu',
String $group = 'sensu',
String $mode = '0755',
Boolean $pretty = true,
Hash $content = {},
Array $notify_list = [],
) {

# ensure we have a properly formatted file path for our target OS
Expand All @@ -52,5 +57,6 @@
group => $group,
mode => $mode,
content => sensu_sorted_json($content, $pretty),
notify => $notify_list,
}
}
4 changes: 2 additions & 2 deletions spec/classes/sensu_init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,15 +526,15 @@
}
} }

it { should contain_sensu_check('some-check').with(
it { should contain_sensu__check('some-check').with(
:type => 'pipe',
:command => '/usr/local/bin/some-check',
:occurrences => '1',
:handlers => ['email']
) }
it { should contain_file('/etc/sensu/conf.d/checks/some-check.json') }

it { should contain_sensu_check('check-cpu').with(
it { should contain_sensu__check('check-cpu').with(
:type => 'pipe',
:command => '/usr/local/bin/check-cpu.rb',
:occurrences => '5',
Expand Down
Loading

0 comments on commit cafa78d

Please sign in to comment.