Skip to content

Commit

Permalink
Merge pull request voxpupuli#262 from deric/graphite
Browse files Browse the repository at this point in the history
Support multiple carbon backends
  • Loading branch information
blkperl committed Jul 21, 2015
2 parents 0e138ce + b861433 commit 975e1d5
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 53 deletions.
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,3 @@ DEPENDENCIES
rspec-puppet
serverspec
simplecov

BUNDLED WITH
1.10.3
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1013,9 +1013,29 @@ class { 'collectd::plugin::vmem':

####Class: `collectd::plugin::write_graphite`

The `write_graphite` plugin writes data to Graphite, an open-source metrics storage and graphing project.
```puppet
class { 'collectd::plugin::write_graphite':
graphitehost => 'graphite.example.org',
collectd::plugin::write_graphite::carbon {'my_graphite':
graphitehost => 'graphite.example.org',
graphiteport => 2003,
graphiteprefix => '',
protocol => 'tcp'
}
```

You can define multiple Graphite backends where will be metrics send. Each backend should have unique title:

```puppet
collectd::plugin::write_graphite::carbon {'secondary_graphite':
graphitehost => 'graphite.example.org',
graphiteport => 2004,
graphiteprefix => '',
protocol => 'udp',
escapecharacter => '_',
alwaysappendds => true,
storerates => true,
separateinstances => false,
logsenderrors => true
}
```

Expand Down
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}_${protocol}_${graphiteport}":
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'),
}
}
113 changes: 98 additions & 15 deletions spec/classes/collectd_plugin_write_graphite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,120 @@

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',
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
{
:carbons => { 'graphite' => {} },
}
end

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

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 'includes carbon configuration' do
should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_tcp_2003').with({
:content => /<Carbon>/,
:target => '/etc/collectd/conf.d/write_graphite-config.conf',
})

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

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

context 'multiple carbon writers, collectd <= 5.2' do
let :params do
{ :protocol => 'udp',
{
:carbons => {
'graphite_one' => {'graphitehost' => '192.168.1.1', 'graphiteport' => 2004},
'graphite_two' => {'graphitehost' => '192.168.1.2', 'graphiteport' => 2005},
},
}
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 'includes graphite_one configuration' do
should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_one_tcp_2004').with({
:content => /<Carbon>/,
:target => '/etc/collectd/conf.d/write_graphite-config.conf',
})

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

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

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

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

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

context 'protocol should be include with version >= 5.4' do
context 'collectd >= 5.3' do
let :facts do
{ :osfamily => 'RedHat',
:collectd_version => '5.4',
{
: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.3'
}
end
let :params do
{ :protocol => 'udp',
{
:carbons => { 'graphite' => {} },
}
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 <Node "name"> syntax' do
should contain_concat__fragment('collectd_plugin_write_graphite_conf_graphite_tcp_2003').with({
:content => /<Node "graphite">/,
:target => '/etc/collectd/conf.d/write_graphite-config.conf',
})
end
end

end
end
80 changes: 80 additions & 0 deletions spec/defines/collectd_plugin_write_graphite_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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',
:concat_basedir => tmpfilename('collectd-graphite'),
}
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) { 'wg' }
let :facts do
{ :osfamily => 'RedHat',
:collectd_version => '5.4',
:concat_basedir => tmpfilename('collectd-graphite'),
}
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_wg_udp_2003'
).with_content(/.*Protocol \"udp\".*/)
end

it 'uses Node definition' do
should contain_concat__fragment('collectd_plugin_write_graphite_conf_wg_udp_2003').with({
:content => /<Node "wg">/,
:target => '/etc/collectd.d/write_graphite-config.conf',
})
end
end

context 'default configuration (undefined collectd version)' do
let(:title) { 'graphite_default' }

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

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

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

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

This file was deleted.

24 changes: 24 additions & 0 deletions templates/plugin/write_graphite/carbon.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<%- if @collectd_version and (scope.function_versioncmp([@collectd_version, '5.3']) >= 0) -%>
<Node "<%= @title %>">
<%- else -%>
<Carbon>
<%- end -%>
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 -%>
<%- if @collectd_version and (scope.function_versioncmp([@collectd_version, '5.3']) >= 0) -%>
</Node>
<%- else -%>
</Carbon>
<%- end -%>

0 comments on commit 975e1d5

Please sign in to comment.