From c4f0a54f580451459e20438157f7ccebf8374482 Mon Sep 17 00:00:00 2001 From: Stuart Fox Date: Mon, 4 Apr 2016 15:10:47 -0700 Subject: [PATCH 1/4] [add_input_define] Alter the define to accept a plugin type, allows for multiple inputs of the same type --- README.md | 56 +++++++++++++++++++++++++++----------- manifests/input.pp | 13 +++++++-- spec/defines/input_spec.rb | 12 ++++---- templates/input.conf.erb | 10 ++++--- 4 files changed, 63 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index f0978de..b271961 100644 --- a/README.md +++ b/README.md @@ -83,29 +83,53 @@ Or here's a Hiera-based example (which is the recommended approach): To configure individual inputs, you can use `telegraf::input` - telegraf::input { 'influxdb': - "options" => { - "urls" => ["http://localhost:8086",], - }, +Example 1 + + telegraf::input { 'my_exec': + plugin_type => 'exec' + options => { + 'commands' => ['/usr/local/bin/my_input.py',], + 'name_suffix' => '_my_input', + 'data_format' => 'json', + }, + require => File['/usr/local/bin/my_input.py'], + } + +Will create the file `/etc/telegraf/telegraf.d/my_exec.conf` + + [[inputs.exec]] + commands = ['/usr/local/bin/my_input.py'] + name_suffix = '_my_input' + data_format = 'json' + +Example 2 + + telegraf::input { 'influxdb-dc1': + plugin_type => 'influxdb', + options => { + 'urls' => ['http://remote-dc1:8086',], + }, } -Will create the file `/etc/telegraf/telegraf.d/influxdb.conf` +Will create the file `/etc/telegraf/telegraf.d/influxdb-dc1.conf` [[inputs.influxdb]] urls = ["http://localhost:8086"] - telegraf::input { 'snmp': - options => { - "interval" => "60s", - }, - "sections" => { - "snmp.host" => { - "address" => "snmp_host1:161", - "community" => "read_only", - "version" => "2", - "get_oids" => ["1.3.6.1.2.1.1.5",], - }, +Example 3 + + telegraf::input { 'my_snmp': + plugin_type = 'snmp', + options => { + 'interval' => '60s', + }, + sections => { + 'snmp.host' => { + 'address' => 'snmp_host1:161', + 'community' => 'read_only', + 'get_oids' => ['1.3.6.1.2.1.1.5',], }, + }, } Will create the file `/etc/telegraf/telegraf.d/snmp.conf` diff --git a/manifests/input.pp b/manifests/input.pp index 397a829..c214df5 100644 --- a/manifests/input.pp +++ b/manifests/input.pp @@ -10,11 +10,20 @@ # Some inputs take multiple sections define telegraf::input ( - $options = undef, - $sections = undef, + $plugin_type = undef, + $options = undef, + $sections = undef, ) { include telegraf + if $options { + validate_hash($options) + } + + if $sections { + validate_hash($sections) + } + Class['telegraf::config'] -> file {"/etc/telegraf/telegraf.d/${name}.conf": diff --git a/spec/defines/input_spec.rb b/spec/defines/input_spec.rb index bb348d5..40a4a74 100644 --- a/spec/defines/input_spec.rb +++ b/spec/defines/input_spec.rb @@ -1,8 +1,9 @@ require 'spec_helper' describe 'telegraf::input' do - let(:title) { 'influxdb' } + let(:title) { 'my_influxdb' } let(:params) {{ + :plugin_type => 'influxdb', :options => { "urls" => ["http://localhost:8086",], }, @@ -10,7 +11,7 @@ let(:facts) { { :osfamily => 'RedHat' } } let(:filename) { "/etc/telegraf/telegraf.d/#{title}.conf" } - describe 'configuration file /etc/telegraf/telegraf.d/influxdb.conf input' do + describe "configuration file /etc/telegraf/telegraf.d/my_influxdb.conf input" do it 'is declared with the correct content' do should contain_file(filename).with_content(/\[\[inputs.influxdb\]\]/) should contain_file(filename).with_content(/ urls = \["http:\/\/localhost:8086"\]/) @@ -27,8 +28,9 @@ end describe 'telegraf::input' do - let(:title) { 'snmp' } + let(:title) { 'my_snmp' } let(:params) {{ + :plugin_type => 'snmp', :options => { "interval" => "60s", }, @@ -36,7 +38,6 @@ "snmp.host" => { "address" => "snmp_host1:161", "community" => "read_only", - "version" => 2, "get_oids" => ["1.3.6.1.2.1.1.5",], }, }, @@ -44,14 +45,13 @@ let(:facts) { { :osfamily => 'RedHat' } } let(:filename) { "/etc/telegraf/telegraf.d/#{title}.conf" } - describe 'configuration file /etc/telegraf/telegraf.d/snmp.conf input with sections' do + describe 'configuration file /etc/telegraf/telegraf.d/my_snmp.conf input with sections' do it 'is declared with the correct content' do should contain_file(filename).with_content(/\[\[inputs.snmp\]\]/) should contain_file(filename).with_content(/ interval = "60s"/) should contain_file(filename).with_content(/\[\[inputs.snmp.host\]\]/) should contain_file(filename).with_content(/ address = "snmp_host1:161"/) should contain_file(filename).with_content(/ community = "read_only"/) - should contain_file(filename).with_content(/ version = 2/) should contain_file(filename).with_content(/ get_oids = \["1.3.6.1.2.1.1.5"\]/) end diff --git a/templates/input.conf.erb b/templates/input.conf.erb index f6c30c7..7cce684 100644 --- a/templates/input.conf.erb +++ b/templates/input.conf.erb @@ -1,4 +1,4 @@ -[[inputs.<%= @name %>]] +[[inputs.<%= @plugin_type %>]] <% if defined?(@options) @options.keys.sort.each do |k| -%> <% if @options[k].kind_of?(Array) -%> @@ -8,15 +8,17 @@ <% end -%> <% end end -%> + <% if defined?(@sections) @sections.keys.each do |k| -%> - [[inputs.<%= "#{k}" %>]] <% @sections[k].keys.sort.each do |v| -%> -<% if @sections[k][v].kind_of?(Array) or @sections[k][v].is_a?(Integer) -%> +<% if @sections[k][v].is_a?(String) -%> +<%= " #{v} = \"#{@sections[k][v]}\"" %> +<% elsif @sections[k][v].kind_of?(Array) -%> <%= " #{v} = #{@sections[k][v]}" %> <% else -%> -<%= " #{v} = \"#{@sections[k][v]}\"" %> +<% v = @sections[k][v] %> <% end -%> <% end end From b73153a1b4e6c30978563ebf9e81031147eb015e Mon Sep 17 00:00:00 2001 From: Stuart Fox Date: Mon, 4 Apr 2016 15:13:16 -0700 Subject: [PATCH 2/4] [add_input_define] Fix a typo in the README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b271961..d90142b 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ Example 2 Will create the file `/etc/telegraf/telegraf.d/influxdb-dc1.conf` [[inputs.influxdb]] - urls = ["http://localhost:8086"] + urls = ["http://remote-dc:8086"] Example 3 From e30abfe4c9232bf76accc79b06b293df64ef1520 Mon Sep 17 00:00:00 2001 From: Stuart Fox Date: Mon, 4 Apr 2016 15:14:15 -0700 Subject: [PATCH 3/4] [add_input_define] Aaaand again, too much caffeine --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d90142b..4309e22 100644 --- a/README.md +++ b/README.md @@ -104,14 +104,14 @@ Will create the file `/etc/telegraf/telegraf.d/my_exec.conf` Example 2 - telegraf::input { 'influxdb-dc1': + telegraf::input { 'influxdb-dc': plugin_type => 'influxdb', options => { - 'urls' => ['http://remote-dc1:8086',], + 'urls' => ['http://remote-dc:8086',], }, } -Will create the file `/etc/telegraf/telegraf.d/influxdb-dc1.conf` +Will create the file `/etc/telegraf/telegraf.d/influxdb-dc.conf` [[inputs.influxdb]] urls = ["http://remote-dc:8086"] From 10188afa02a823cf98a82cd7ca4deba8cf6966ef Mon Sep 17 00:00:00 2001 From: Stuart Fox Date: Mon, 4 Apr 2016 15:15:22 -0700 Subject: [PATCH 4/4] [add_input_define] Typo --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 4309e22..ec80ca8 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,6 @@ Will create the file `/etc/telegraf/telegraf.d/snmp.conf` [[inputs.snmp.host]] address = "snmp_host1:161" community = "read_only" - version = 2 get_oids = ["1.3.6.1.2.1.1.5"] ## Limitations