diff --git a/README.md b/README.md index 31356f647..db6f5dbe2 100644 --- a/README.md +++ b/README.md @@ -1727,6 +1727,29 @@ collectd::plugin::tail::file { 'exim-log': } ``` +#### Class: `collectd::plugin::tail_csv` + +```puppet +class { '::collectd::plugin::tail_csv': + metrics => { + 'snort-dropped' => { + 'type' => 'gauge', + 'values_from' => 1, + 'instance' => "dropped" + }, + }, + files => { + '/var/log/snort/snort.stats' => { + 'collect' => ['snort-dropped'], + 'plugin' => 'snortstats', + 'instance' => 'eth0', + 'interval' => 600, + 'time_from' => 5, + } + } +} +``` + #### Class: `collectd::plugin::thermal` ```puppet diff --git a/manifests/plugin/tail_csv.pp b/manifests/plugin/tail_csv.pp new file mode 100644 index 000000000..58d016b7d --- /dev/null +++ b/manifests/plugin/tail_csv.pp @@ -0,0 +1,15 @@ +#https://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_tail_csv +class collectd::plugin::tail_csv ( + Hash[String, Collectd::Tail_Csv::Metric, 1] $metrics, + Hash[String, Collectd::Tail_Csv::File, 1] $files, + Enum['present', 'absent'] $ensure = 'present', + Integer $order = 10, +) { + include collectd + + collectd::plugin { 'tail_csv': + ensure => $ensure, + content => epp('collectd/plugin/tail_csv.conf.epp'), + order => $order, + } +} diff --git a/spec/classes/collectd_plugin_tail_csv_spec.rb b/spec/classes/collectd_plugin_tail_csv_spec.rb new file mode 100644 index 000000000..19a98db03 --- /dev/null +++ b/spec/classes/collectd_plugin_tail_csv_spec.rb @@ -0,0 +1,123 @@ +require 'spec_helper' + +describe 'collectd::plugin::tail_csv', type: :class do + on_supported_os(baseline_os_hash).each do |os, facts| + context "on #{os} " do + let :facts do + facts + end + + options = os_specific_options(facts) + context 'Minimal attributes' do + let :params do + { + 'metrics' => { + 'snort-dropped' => { + 'type' => 'percent', + 'value_from' => 1 + } + }, + 'files' => { + '/var/log/snort/snort.stats' => { + 'collect' => %w[snort-dropped] + } + } + } + end + + it 'overrides defaults' do + content = < + Globals false + + + + + Type "percent" + ValueFrom 1 + + + Collect "snort-dropped" + + + +EOS + + is_expected.to compile.with_all_deps + is_expected.to contain_class('collectd') + is_expected.to contain_file('tail_csv.load').with( + content: content, + path: "#{options[:plugin_conf_dir]}/10-tail_csv.conf" + ) + end + end + + context 'All attributes' do + let :params do + { + 'metrics' => { + 'snort-dropped' => { + 'type' => 'percent', + 'value_from' => 1, + 'instance' => 'dropped' + }, + 'snort-reject' => { + 'type' => 'percent', + 'value_from' => 2, + 'instance' => 'reject' + } + }, + 'files' => { + '/var/log/snort/snort.stats' => { + 'collect' => %w[snort-dropped snort-reject], + 'plugin' => 'snortstats', + 'instance' => 'eth0', + 'interval' => 600, + 'time_from' => 5 + } + } + } + end + + it 'overrides defaults' do + content = < + Globals false + + + + + Type "percent" + Instance "dropped" + ValueFrom 1 + + + Type "percent" + Instance "reject" + ValueFrom 2 + + + Plugin "snortstats" + Instance "eth0" + Collect "snort-dropped" + Collect "snort-reject" + Interval 600 + TimeFrom 5 + + + +EOS + + is_expected.to compile.with_all_deps + is_expected.to contain_class('collectd') + is_expected.to contain_file('tail_csv.load').with( + content: content, + path: "#{options[:plugin_conf_dir]}/10-tail_csv.conf" + ) + end + end + end + end +end diff --git a/templates/plugin/tail_csv.conf.epp b/templates/plugin/tail_csv.conf.epp new file mode 100644 index 000000000..8c4d5e19c --- /dev/null +++ b/templates/plugin/tail_csv.conf.epp @@ -0,0 +1,34 @@ + +<% $collectd::plugin::tail_csv::metrics.each |$name, $metric| { -%> + "> +<% unless $metric['type'] =~ Undef { -%> + Type "<%= $metric['type'] %>" +<% } -%> +<% unless $metric['instance'] =~ Undef { -%> + Instance "<%= $metric['instance'] %>" +<% } -%> +<% unless $metric['value_from'] =~ Undef { -%> + ValueFrom <%= $metric['value_from'] %> +<% } -%> + +<% } -%> +<% $collectd::plugin::tail_csv::files.each |$path, $file| { -%> + "> +<% unless $file['plugin'] =~ Undef { -%> + Plugin "<%= $file['plugin'] %>" +<% } -%> +<% unless $file['instance'] =~ Undef { -%> + Instance "<%= $file['instance'] %>" +<% } -%> +<% $file['collect'].each |$collect| { -%> + Collect "<%= $collect %>" +<% } -%> +<% unless $file['interval'] =~ Undef { -%> + Interval <%= $file['interval'] %> +<% } -%> +<% unless $file['time_from'] =~ Undef { -%> + TimeFrom <%= $file['time_from'] %> +<% } -%> + +<% } -%> + diff --git a/types/tail_csv/file.pp b/types/tail_csv/file.pp new file mode 100644 index 000000000..fe4ec6f73 --- /dev/null +++ b/types/tail_csv/file.pp @@ -0,0 +1,8 @@ +# https://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_tail_csv +type Collectd::Tail_Csv::File = Struct[{ + 'collect' => Array[String, 1], + 'plugin' => Optional[String[1]], + 'instance' => Optional[String[1]], + 'interval' => Optional[Numeric], + 'time_from' => Optional[Integer[0]], +}] diff --git a/types/tail_csv/metric.pp b/types/tail_csv/metric.pp new file mode 100644 index 000000000..5d411ef64 --- /dev/null +++ b/types/tail_csv/metric.pp @@ -0,0 +1,6 @@ +# https://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_tail_csv +type Collectd::Tail_Csv::Metric = Struct[{ + 'type' => String[1], + 'value_from' => Integer[0], + 'instance' => Optional[String[1]], +}]