Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add input define #3

Merged
merged 4 commits into from
Apr 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 42 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/influxdb.conf`
Will create the file `/etc/telegraf/telegraf.d/my_exec.conf`

[[inputs.influxdb]]
urls = ["http://localhost:8086"]
[[inputs.exec]]
commands = ['/usr/local/bin/my_input.py']
name_suffix = '_my_input'
data_format = 'json'

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 2

telegraf::input { 'influxdb-dc':
plugin_type => 'influxdb',
options => {
'urls' => ['http://remote-dc:8086',],
},
}

Will create the file `/etc/telegraf/telegraf.d/influxdb-dc.conf`

[[inputs.influxdb]]
urls = ["http://remote-dc:8086"]

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`
Expand All @@ -116,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
Expand Down
13 changes: 11 additions & 2 deletions manifests/input.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
12 changes: 6 additions & 6 deletions spec/defines/input_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
require 'spec_helper'

describe 'telegraf::input' do
let(:title) { 'influxdb' }
let(:title) { 'my_influxdb' }
let(:params) {{
:plugin_type => 'influxdb',
:options => {
"urls" => ["http://localhost:8086",],
},
}}
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"\]/)
Expand All @@ -27,31 +28,30 @@
end

describe 'telegraf::input' do
let(:title) { 'snmp' }
let(:title) { 'my_snmp' }
let(:params) {{
:plugin_type => '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",],
},
},
}}
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

Expand Down
10 changes: 6 additions & 4 deletions templates/input.conf.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[[inputs.<%= @name %>]]
[[inputs.<%= @plugin_type %>]]
<% if defined?(@options)
@options.keys.sort.each do |k| -%>
<% if @options[k].kind_of?(Array) -%>
Expand All @@ -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
Expand Down