From 4ce93c0e883b02bfcce660951297b26ad2fb8fa7 Mon Sep 17 00:00:00 2001 From: Matthias Runge Date: Tue, 11 Sep 2018 11:48:21 +0200 Subject: [PATCH] Add address field to memcached plugin As addition to the host field, there is also the address setting, which is helpful sometimes. --- examples/plugins/memcached.pp | 9 +++--- manifests/plugin/memcached.pp | 8 ++++- .../classes/collectd_plugin_memcached_spec.rb | 29 +++++++++++++++---- templates/plugin/memcached.conf.erb | 9 +++++- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/examples/plugins/memcached.pp b/examples/plugins/memcached.pp index c541948ec..1cd5ba7e6 100644 --- a/examples/plugins/memcached.pp +++ b/examples/plugins/memcached.pp @@ -1,10 +1,11 @@ include ::collectd class { '::collectd::plugin::memcached': - instances => { - 'default' => { - 'host' => '192.168.122.1', - 'port' => '11211', + instances => { + 'default' => { + 'host' => 'localhost', + 'address' => '127.0.0.1', + 'port' => '11211', }, }, } diff --git a/manifests/plugin/memcached.pp b/manifests/plugin/memcached.pp index ac8a6db99..2d20c1526 100644 --- a/manifests/plugin/memcached.pp +++ b/manifests/plugin/memcached.pp @@ -1,7 +1,13 @@ # https://collectd.org/wiki/index.php/Plugin:memcached class collectd::plugin::memcached ( $ensure = 'present', - Hash $instances = {'default' => {'host' => '127.0.0.1', 'port' => 11211 } }, + Hash $instances = { + 'default' => { + 'host' => 'localhost', + 'address' => '127.0.0.1', + 'port' => 11211, + }, + }, $interval = undef, ) { diff --git a/spec/classes/collectd_plugin_memcached_spec.rb b/spec/classes/collectd_plugin_memcached_spec.rb index bd978f1ed..6c07df5a4 100644 --- a/spec/classes/collectd_plugin_memcached_spec.rb +++ b/spec/classes/collectd_plugin_memcached_spec.rb @@ -9,12 +9,19 @@ options = os_specific_options(facts) - context ':ensure => present, default host and port' do + context ':ensure => present, default host, address, and port' do it "Will create #{options[:plugin_conf_dir]}/memcached.conf" do + content = < + Host "localhost" + Address "127.0.0.1" + Port 11211 + +EOS is_expected.to contain_file('memcached.load').with( ensure: 'present', path: "#{options[:plugin_conf_dir]}/10-memcached.conf", - content: %r{Host "127.0.0.1"\n.+Port 11211} + content: %r{#{content}} ) end end @@ -24,12 +31,17 @@ { 'instances' => { 'sessions1' => { - 'host' => '127.0.0.1', + 'host' => 'localhost', + 'address' => '127.0.0.1', 'port' => '11211' }, 'cache1' => { - 'host' => '127.0.0.1', + 'host' => 'localhost', 'port' => '11212' + }, + 'cache3' => { + 'address' => '127.0.0.1', + 'port' => '11213' } } } @@ -38,13 +50,18 @@ it "Will create #{options[:plugin_conf_dir]}/memcached.conf" do content = < - Host "127.0.0.1" + Host "localhost" + Address "127.0.0.1" Port 11211 - Host "127.0.0.1" + Host "localhost" Port 11212 + + Address "127.0.0.1" + Port 11213 + EOS is_expected.to contain_file('memcached.load').with( ensure: 'present', diff --git a/templates/plugin/memcached.conf.erb b/templates/plugin/memcached.conf.erb index 576b18d43..851c9e566 100644 --- a/templates/plugin/memcached.conf.erb +++ b/templates/plugin/memcached.conf.erb @@ -1,9 +1,16 @@ <% @instances.each do |name,values| -%> "> -<% if values['host'] and values['port'] -%> +<% if values['host'] or values['address'] or values['port'] -%> +<%- if values['host'] -%> Host "<%= values['host'] %>" +<%- end -%> +<%- if values['address'] -%> + Address "<%= values['address'] %>" +<%- end -%> +<%- if values['port'] -%> Port <%= values['port'] %> +<%- end -%> <% elsif values['socket'] -%> Socket "<%= values['socket'] %>" <% end -%>