Skip to content

Commit

Permalink
Support multiple carbon backends
Browse files Browse the repository at this point in the history
  • Loading branch information
deric committed Jul 4, 2015
1 parent 27adfd3 commit 15125a9
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 55 deletions.
48 changes: 33 additions & 15 deletions manifests/plugin/write_graphite.pp
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
# https://collectd.org/wiki/index.php/Graphite
class collectd::plugin::write_graphite (
$ensure = present,
$graphitehost = 'localhost',
$graphiteport = 2003,
$storerates = true,
$graphiteprefix = 'collectd.',
$graphitepostfix = undef,
$carbons = {},
$interval = undef,
$escapecharacter = '_',
$alwaysappendds = false,
$protocol = 'tcp',
$separateinstances = false,
$logsenderrors = true,
$ensure = present,
$globals = false,
) {
validate_bool($storerates)
validate_bool($separateinstances)
validate_bool($logsenderrors)
include collectd::params

validate_hash($carbons)

collectd::plugin {'write_graphite':
ensure => $ensure,
content => template('collectd/plugin/write_graphite.conf.erb'),
globals => $globals,
interval => $interval,
}

# should be loaded after global plugin configuration
$graphite_conf = "${collectd::params::plugin_conf_dir}/write_graphite-config.conf"

concat{ $graphite_conf:
ensure => $ensure,
mode => '0640',
owner => 'root',
group => $collectd::params::root_group,
notify => Service['collectd'],
ensure_newline => true,
}

concat::fragment{'collectd_plugin_write_graphite_conf_header':
order => '00',
content => '<Plugin write_graphite>',
target => $graphite_conf,
}

concat::fragment{'collectd_plugin_write_graphite_conf_footer':
order => '99',
content => '</Plugin>',
target => $graphite_conf,
}

create_resources(collectd::plugin::write_graphite::carbon, $carbons)
}
30 changes: 30 additions & 0 deletions manifests/plugin/write_graphite/carbon.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# a single graphite backend
define collectd::plugin::write_graphite::carbon (
$ensure = present,
$graphitehost = 'localhost',
$graphiteport = 2003,
$storerates = true,
$graphiteprefix = 'collectd.',
$graphitepostfix = undef,
$interval = undef,
$escapecharacter = '_',
$alwaysappendds = false,
$protocol = 'tcp',
$separateinstances = false,
$logsenderrors = true,
){
include collectd::params
include collectd::plugin::write_graphite

validate_bool($storerates)
validate_bool($alwaysappendds)
validate_bool($separateinstances)
validate_bool($logsenderrors)

concat::fragment{"collectd_plugin_write_graphite_conf_${title}":
ensure => $ensure,
order => '50', # somewhere between header and footer
target => $collectd::plugin::write_graphite::graphite_conf,
content => template('collectd/plugin/write_graphite/carbon.conf.erb'),
}
}
64 changes: 42 additions & 22 deletions spec/classes/collectd_plugin_write_graphite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,57 @@

describe 'collectd::plugin::write_graphite', :type => :class do

context 'protocol should not be include with version < 5.4' do
let :facts do
{ :osfamily => 'RedHat',
:collectd_version => '5.3',
}
end
let :facts do
{
:osfamily => 'Debian',
:concat_basedir => tmpfilename('collectd-write_graphite'),
:id => 'root',
:kernel => 'Linux',
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
:collectd_version => '5.0'
}
end


context 'single carbon writer' do

let :params do
{ :protocol => 'udp',
{
:carbons => { 'graphite_tcp' => {} },
}
end

it 'Should not include protocol in /etc/collectd.d/write_graphite.conf for collectd < 5.4' do
should_not contain_file('write_graphite.conf').with_content(/.*Protocol \"udp\".*/)
it 'Will create /etc/collectd.d/conf.d/write_graphite-config.conf' do
should contain_concat__fragment('collectd_plugin_write_graphite_conf_header').with({
:content => /<Plugin write_graphite>/,
:target => '/etc/collectd/conf.d/write_graphite-config.conf',
:order => '00'
})
end
end

context 'protocol should be include with version >= 5.4' do
let :facts do
{ :osfamily => 'RedHat',
:collectd_version => '5.4',
}
end
let :params do
{ :protocol => 'udp',
}
it 'Will create /etc/collectd.d/conf.d/write_graphite-config' do
should contain_concat__fragment('collectd_plugin_write_graphite_conf_footer').with({
:content => /<\/Plugin>/,
:target => '/etc/collectd/conf.d/write_graphite-config.conf',
:order => '99'
})
end

it 'Should include protocol in /etc/collectd.d/write_graphite.conf for collectd >= 5.4' do
should contain_file('write_graphite.load') \
.with_content(/.*Protocol \"udp\".*/)
it 'includes carbon configuration' do
should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_tcp').with({
:content => /<Carbon>/,
:target => '/etc/collectd/conf.d/write_graphite-config.conf',
})

should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_tcp').with({
:content => /Host "localhost"/,
})

should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_tcp').with({
:content => /Port "2003"/,
})
end

end

end
70 changes: 70 additions & 0 deletions spec/defines/collectd_plugin_write_graphite_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'spec_helper'

describe 'collectd::plugin::write_graphite::carbon', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:id => 'root',
:concat_basedir => tmpfilename('collectd-graphite'),
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
}
end

context 'protocol should not be include with version < 5.4' do
let(:title) { 'graphite_udp' }
let :facts do
{ :osfamily => 'RedHat',
:collectd_version => '5.3',
}
end
let :params do
{ :protocol => 'udp',
}
end

it 'Should not include protocol in /etc/collectd.d/write_graphite.conf for collectd < 5.4' do
should_not contain_concat__fragment(
'collectd_plugin_write_graphite_conf_localhost_2003'
).with_content(/.*Protocol \"udp\".*/)
end
end

context 'protocol should be include with version >= 5.4' do
let(:title) { 'graphite_udp' }
let :facts do
{ :osfamily => 'RedHat',
:collectd_version => '5.4',
}
end
let :params do
{ :protocol => 'udp',
}
end

it 'Should include protocol in /etc/collectd.d/write_graphite.conf for collectd >= 5.4' do
should contain_concat__fragment(
'collectd_plugin_write_graphite_conf_localhost_2003'
).with_content(/.*Protocol \"udp\".*/)
end
end

context 'default configuration' do

it 'includes carbon configuration' do
should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_tcp').with({
:content => /<Carbon>/,
:target => '/etc/collectd/conf.d/write_graphite-config.conf',
})

should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_tcp').with({
:content => /Host "localhost"/,
})

should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_tcp').with({
:content => /Port "2003"/,
})
end

end

end
18 changes: 0 additions & 18 deletions templates/plugin/write_graphite.conf.erb

This file was deleted.

16 changes: 16 additions & 0 deletions templates/plugin/write_graphite/carbon.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Carbon>
Host "<%= @graphitehost %>"
Port "<%= @graphiteport %>"
Prefix "<%= @graphiteprefix %>"
<% if @graphitepostfix -%>
Postfix "<%= @graphitepostfix %>"
<% end -%>
EscapeCharacter "<%= @escapecharacter %>"
StoreRates <%= @storerates %>
AlwaysAppendDS <%= @alwaysappendds %>
SeparateInstances <%= @separateinstances %>
<% if @collectd_version and (scope.function_versioncmp([@collectd_version, '5.4']) >= 0) -%>
LogSendErrors <%= @logsenderrors %>
Protocol "<%= @protocol %>"
<% end -%>
</Carbon>

0 comments on commit 15125a9

Please sign in to comment.