Skip to content

Commit

Permalink
Add single_section to input plugins (#41) (#60)
Browse files Browse the repository at this point in the history
* fix fatal linter error

* Add single_section to input plugins (#41)

Single-bracket directives are required for certain sorts of input plugin
options, e.g. setting tags on that input plugin. This adds a
single_section template and directive (via @kkzinger 's solution, ty!),
as well as tests and updated docs with an example.
  • Loading branch information
mxjessie authored and yankcrime committed Jun 10, 2017
1 parent 5767524 commit da7c66f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,23 @@ Example 3:

```puppet
telegraf::input { 'my_snmp':
plugin_type => 'snmp',
options => {
plugin_type => 'snmp',
options => {
'interval' => '60s',
},
sections => {
sections => {
'snmp.host' => {
'address' => 'snmp_host1:161',
'community' => 'read_only',
'version' => 2,
'get_oids' => ['1.3.6.1.2.1.1.5',],
},
},
single_section => {
'snmp.tags' => {
'environment' => 'development',
},
},
}
```

Expand All @@ -168,6 +173,9 @@ Will create the file `/etc/telegraf/telegraf.d/snmp.conf`:
version = 2
get_oids = ["1.3.6.1.2.1.1.5"]

[inputs.snmp.tags]
environment = "development"

Example 4:

```puppet
Expand Down
6 changes: 3 additions & 3 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
contain ::telegraf::config
contain ::telegraf::service

Class['::telegraf::install'] ->
Class['::telegraf::config'] ->
Class['::telegraf::service']
Class['::telegraf::install']
-> Class['::telegraf::config']
-> Class['::telegraf::service']
}
24 changes: 15 additions & 9 deletions manifests/input.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,35 @@
# [*options*]
# Hash. Plugin options for use the the input template.
#
# [*sections*]
# Hash. Some inputs take multiple sections.
# [*single_section*]
# Hash. Some inputs take a single unique section in [single brackets].
#
# [*sections*]
# Hash. Some inputs take multiple sections in [[double brackets]].

define telegraf::input (
$plugin_type = $name,
$options = undef,
$sections = undef,
$plugin_type = $name,
$options = undef,
$single_section = undef,
$sections = undef,
) {
include telegraf

if $options {
validate_hash($options)
}

if $single_section {
validate_hash($single_section)
}

if $sections {
validate_hash($sections)
}

Class['::telegraf::config']
->
file {"${telegraf::config_folder}/${name}.conf":
-> file {"${telegraf::config_folder}/${name}.conf":
content => template('telegraf/input.conf.erb')
}
~>
Class['::telegraf::service']
~> Class['::telegraf::service']
}
6 changes: 3 additions & 3 deletions spec/classes/telegraf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
context 'Supported operating systems' do
['RedHat', 'CentOS', 'OracleLinux'].each do |operatingsystem|
[6,7].each do |releasenum|
context "#{osfamily} #{releasenum} release specifics" do
context "#{operatingsystem} #{releasenum} release specifics" do
let(:facts) {{
:operatingsystem => operatingsystem,
:operatingsystemrelease => releasenum,
Expand Down Expand Up @@ -80,15 +80,15 @@
it { should contain_service('telegraf') }
it { should contain_yumrepo('influxdata')
.with(
:baseurl => "https://repos.influxdata.com/#{operatingsystem.downcase}/\$releasever/\$basearch/stable",
:baseurl => "https://repos.influxdata.com/rhel/\$releasever/\$basearch/stable",
)
}

describe 'allow custom repo_type' do
let(:params) { {:repo_type => 'unstable' } }
it { should contain_yumrepo('influxdata')
.with(
:baseurl => "https://repos.influxdata.com/#{operatingsystem.downcase}/\$releasever/\$basearch/unstable",
:baseurl => "https://repos.influxdata.com/rhel/\$releasever/\$basearch/unstable",
)
}
end
Expand Down
7 changes: 7 additions & 0 deletions spec/defines/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
:options => {
"interval" => "60s",
},
:single_section => {
"snmp.tags" => {
"environment" => "development",
},
},
:sections => {
"snmp.host" => {
"address" => "snmp_host1:161",
Expand All @@ -48,6 +53,8 @@
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.tags\]/)
should contain_file(filename).with_content(/ environment = "development"/)
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"/)
Expand Down
10 changes: 10 additions & 0 deletions templates/input.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@
<% end -%>
<% end -%>
<% end -%>
<% if @single_section -%>
<% @single_section.sort.each do |section, option| -%>
[inputs.<%= section %>]
<% unless option == nil -%>
<% option.sort.each do | suboption, value | -%>
<%= suboption -%> = <% if value.is_a?(String) %>"<%= value %>"<% elsif value.is_a?(Array) %><%= value.inspect %><% else %><%= value %><% end %>
<% end -%>
<% end -%>
<% end -%>
<% end -%>

0 comments on commit da7c66f

Please sign in to comment.