From 0600c6e76b2dfeded0dc77d3ba424b86e6810b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kro=CC=88pke?= Date: Thu, 22 Dec 2016 01:04:52 +0100 Subject: [PATCH] Added plugin oracle --- manifests/plugin/oracle.pp | 42 ++++++++ manifests/plugin/oracle/database.pp | 24 +++++ manifests/plugin/oracle/query.pp | 20 ++++ spec/classes/collectd_plugin_oracle_spec.rb | 45 ++++++++ .../collectd_plugin_oracle_database_spec.rb | 48 +++++++++ .../collectd_plugin_oracle_query_spec.rb | 102 ++++++++++++++++++ templates/plugin/oracle/database.conf.erb | 8 ++ templates/plugin/oracle/query.conf.erb | 15 +++ 8 files changed, 304 insertions(+) create mode 100644 manifests/plugin/oracle.pp create mode 100644 manifests/plugin/oracle/database.pp create mode 100644 manifests/plugin/oracle/query.pp create mode 100644 spec/classes/collectd_plugin_oracle_spec.rb create mode 100644 spec/defines/collectd_plugin_oracle_database_spec.rb create mode 100644 spec/defines/collectd_plugin_oracle_query_spec.rb create mode 100644 templates/plugin/oracle/database.conf.erb create mode 100644 templates/plugin/oracle/query.conf.erb diff --git a/manifests/plugin/oracle.pp b/manifests/plugin/oracle.pp new file mode 100644 index 000000000..0c3520535 --- /dev/null +++ b/manifests/plugin/oracle.pp @@ -0,0 +1,42 @@ +# Oracle plugin +# https://collectd.org/wiki/index.php/Plugin:Oracle +class collectd::plugin::oracle ( + $ensure = 'present', + $manage_package = undef, + $interval = undef, +) { + include ::collectd + + $config_file = "${collectd::plugin_conf_dir}/15-oracle.conf" + + if $manage_package { + package { 'collectd-oracle': + ensure => $ensure, + } + } + + collectd::plugin { 'oracle': + ensure => $ensure, + interval => $interval, + } + + concat { $config_file: + ensure => $ensure, + mode => '0640', + owner => 'root', + group => $collectd::root_group, + notify => Service['collectd'], + ensure_newline => true, + } + + concat::fragment { + 'collectd_plugin_oracle_conf_header': + order => '00', + content => '', + target => $config_file; + 'collectd_plugin_oracle_conf_footer': + order => '99', + content => '', + target => $config_file; + } +} diff --git a/manifests/plugin/oracle/database.pp b/manifests/plugin/oracle/database.pp new file mode 100644 index 000000000..260f9594f --- /dev/null +++ b/manifests/plugin/oracle/database.pp @@ -0,0 +1,24 @@ +# https://collectd.org/wiki/index.php/Plugin:Oracle +define collectd::plugin::oracle::database ( + $username, + $password, + $query, + $connect_id = $name, + $database = $name, +) { + + include ::collectd + include ::collectd::plugin::oracle + + validate_string($database) + validate_string($connect_id) + validate_string($username) + validate_string($password) + validate_array($query) + + concat::fragment { "collectd_plugin_oracle_database_${name}": + order => '10', + content => template('collectd/plugin/oracle/database.conf.erb'), + target => $collectd::plugin::oracle::config_file, + } +} diff --git a/manifests/plugin/oracle/query.pp b/manifests/plugin/oracle/query.pp new file mode 100644 index 000000000..6c22a4163 --- /dev/null +++ b/manifests/plugin/oracle/query.pp @@ -0,0 +1,20 @@ +# https://collectd.org/wiki/index.php/Plugin:Oracle +define collectd::plugin::oracle::query ( + $statement, + $results, + $query = $name, +) { + + include ::collectd + include ::collectd::plugin::oracle + + validate_string($query) + validate_string($statement) + validate_array($results) + + concat::fragment { "collectd_plugin_oracle_query_${name}": + order => '10', + content => template('collectd/plugin/oracle/query.conf.erb'), + target => $collectd::plugin::oracle::config_file, + } +} diff --git a/spec/classes/collectd_plugin_oracle_spec.rb b/spec/classes/collectd_plugin_oracle_spec.rb new file mode 100644 index 000000000..5937ba20f --- /dev/null +++ b/spec/classes/collectd_plugin_oracle_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +describe 'collectd::plugin::oracle', type: :class do + let :facts do + { + osfamily: 'RedHat', + collectd_version: '4.8.0', + operatingsystemmajrelease: '7', + python_dir: '/usr/local/lib/python2.7/dist-packages' + } + end + + let(:config_filename) { '/etc/collectd.d/15-oracle.conf' } + + context ':ensure => present, default host and port' do + it 'Will create /etc/collectd.d/10-oracle.conf' do + is_expected.to contain_file('oracle.load').with(ensure: 'present', + path: '/etc/collectd.d/10-oracle.conf', + content: %r{LoadPlugin oracle}) + end + + it 'Will create /etc/collectd.d/15-oracle.conf' do + is_expected.to contain_concat(config_filename).with(ensure: 'present', + path: config_filename) + end + + it { is_expected.to contain_concat__fragment('collectd_plugin_oracle_conf_header').with_content(%r{}) } + it { is_expected.to contain_concat__fragment('collectd_plugin_oracle_conf_footer').with_content(%r{}) } + end + + context ':ensure => absent' do + let :params do + { ensure: 'absent' } + end + it 'Will not create /etc/collectd.d/10-oracle.conf' do + is_expected.to contain_file('oracle.load').with(ensure: 'absent', + path: '/etc/collectd.d/10-oracle.conf') + end + + it 'Will not create /etc/collectd.d/15-oracle.conf' do + is_expected.to contain_concat(config_filename).with(ensure: 'absent', + path: config_filename) + end + end +end diff --git a/spec/defines/collectd_plugin_oracle_database_spec.rb b/spec/defines/collectd_plugin_oracle_database_spec.rb new file mode 100644 index 000000000..2a36e91bc --- /dev/null +++ b/spec/defines/collectd_plugin_oracle_database_spec.rb @@ -0,0 +1,48 @@ +require 'spec_helper' + +describe 'collectd::plugin::oracle::database', type: :define do + let(:facts) do + { + osfamily: 'RedHat', + id: 'root', + concat_basedir: '/dne', + path: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + collectd_version: '4.8.0', + operatingsystemmajrelease: '7', + python_dir: '/usr/local/lib/python2.7/dist-packages' + } + end + + let(:config_filename) { '/etc/collectd.d/15-oracle.conf' } + + let(:default_params) do + { + connect_id: 'connect_id', + username: 'username', + password: 'password', + query: [] + } + end + + let(:title) { 'foo' } + let(:concat_fragment_name) { 'collectd_plugin_oracle_database_foo' } + + # empty values array is technically not valid, but we'll test those cases later + context 'defaults' do + let(:params) { default_params } + it 'provides an oracle database stanza concat fragment' do + is_expected.to contain_concat__fragment(concat_fragment_name).with(target: config_filename, + order: '10') + end + + it { is_expected.to contain_concat__fragment(concat_fragment_name).with_content(%r{\s+ConnectID "connect_id"\s+Username "username"\s+Password "password"}m) } + end + + context 'query array' do + let(:params) do + default_params.merge(query: %w(foo bar baz)) + end + + it { is_expected.to contain_concat__fragment(concat_fragment_name).with_content(%r{Query "foo"\s+Query "bar"\s+Query "baz"}) } + end +end diff --git a/spec/defines/collectd_plugin_oracle_query_spec.rb b/spec/defines/collectd_plugin_oracle_query_spec.rb new file mode 100644 index 000000000..786f6b7fa --- /dev/null +++ b/spec/defines/collectd_plugin_oracle_query_spec.rb @@ -0,0 +1,102 @@ +require 'spec_helper' + +describe 'collectd::plugin::oracle::query', 'type' => :define do + let(:facts) do + { + osfamily: 'RedHat', + id: 'root', + concat_basedir: '/dne', + path: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + collectd_version: '4.8.0', + operatingsystemmajrelease: '7', + python_dir: '/usr/local/lib/python2.7/dist-packages' + } + end + + let(:config_filename) { '/etc/collectd.d/15-oracle.conf' } + + let(:default_params) do + { + statement: "select (select count(*) as count from v$session) ACTUAL_SESSIONS, (select value from v$parameter where name='sessions') MAX_SESSIONS FROM dual", + results: [] + } + end + + let(:title) { 'foo' } + let(:concat_fragment_name) { 'collectd_plugin_oracle_query_foo' } + + # empty values array is technically not valid, but we'll test those cases later + context 'defaults' do + let(:params) { default_params } + it 'provides an oracle query stanza concat fragment' do + is_expected.to contain_concat__fragment(concat_fragment_name).with(target: config_filename, + order: '10') + end + + it { is_expected.to contain_concat__fragment(concat_fragment_name).with_content(%r{\s+Statement "select \(select count\(\*\) as count from v\$session\) ACTUAL_SESSIONS, \(select value from v\$parameter where name='sessions'\) MAX_SESSIONS FROM dual"\s+}m) } + end + + context 'query results with instance_prefix and values_from string' do + let(:params) do + default_params.merge( + results: [ + { + 'type' => 'sessions', + 'instance_prefix' => 'sessions', + 'values_from' => 'ACTUAL_SESSIONS' + } + ] + ) + end + + it { is_expected.to contain_concat__fragment(concat_fragment_name).with_content(%r{Type "sessions"\s+InstancePrefix "sessions"\s+ValuesFrom "ACTUAL_SESSIONS"}) } + end + + context 'query results with instance_prefix and values_from array' do + let(:params) do + default_params.merge( + results: [ + { + 'type' => 'sessions', + 'instance_prefix' => 'sessions', + 'values_from' => %w(ACTUAL_SESSIONS MAX_SESSIONS) + } + ] + ) + end + + it { is_expected.to contain_concat__fragment(concat_fragment_name).with_content(%r{Type "sessions"\s+InstancePrefix "sessions"\s+ValuesFrom "ACTUAL_SESSIONS" "MAX_SESSIONS"}) } + end + + context 'query results with instances_from string and values_from array' do + let(:params) do + default_params.merge( + results: [ + { + 'type' => 'sessions', + 'instances_from' => 'STAT_NAME', + 'values_from' => 'VALUE' + } + ] + ) + end + + it { is_expected.to contain_concat__fragment(concat_fragment_name).with_content(%r{Type "sessions"\s+InstancesFrom "STAT_NAME"\s+ValuesFrom "VALUE"}) } + end + + context 'query results with instances_from array and values_from array' do + let(:params) do + default_params.merge( + results: [ + { + 'type' => 'sessions', + 'instances_from' => %w(STAT_NAME STAT_VAR), + 'values_from' => %w(VALUE VALUE2) + } + ] + ) + end + + it { is_expected.to contain_concat__fragment(concat_fragment_name).with_content(%r{Type "sessions"\s+InstancesFrom "STAT_NAME" "STAT_VAR"\s+ValuesFrom "VALUE" "VALUE2"}) } + end +end diff --git a/templates/plugin/oracle/database.conf.erb b/templates/plugin/oracle/database.conf.erb new file mode 100644 index 000000000..c12bdb4de --- /dev/null +++ b/templates/plugin/oracle/database.conf.erb @@ -0,0 +1,8 @@ + "> + ConnectID "<%= @connect_id %>" + Username "<%= @username %>" + Password "<%= @password %>" +<% @query.each do |query| -%> + Query "<%= query %>" +<% end -%> + \ No newline at end of file diff --git a/templates/plugin/oracle/query.conf.erb b/templates/plugin/oracle/query.conf.erb new file mode 100644 index 000000000..090730129 --- /dev/null +++ b/templates/plugin/oracle/query.conf.erb @@ -0,0 +1,15 @@ + "> + Statement "<%= @statement %>" +<% @results.each do |result| -%> + + Type "<%= result['type'] %>" +<% if result['instance_prefix'] -%> + InstancePrefix "<%= result['instance_prefix'] %>" +<% end -%> +<% if result['instances_from'] -%> + InstancesFrom <%= Array(result['instances_from']).flatten.map {|entry| "\"#{entry}\""}.join(' ') %> +<% end -%> + ValuesFrom <%= Array(result['values_from']).flatten.map {|entry| "\"#{entry}\""}.join(' ') %> + +<% end -%> + \ No newline at end of file