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

Added plugin oracle #613

Merged
merged 1 commit into from Jan 30, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions 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 => '<Plugin oracle>',
target => $config_file;
'collectd_plugin_oracle_conf_footer':
order => '99',
content => '</Plugin>',
target => $config_file;
}
}
24 changes: 24 additions & 0 deletions 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,
}
}
20 changes: 20 additions & 0 deletions 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,
}
}
45 changes: 45 additions & 0 deletions 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{<Plugin oracle>}) }
it { is_expected.to contain_concat__fragment('collectd_plugin_oracle_conf_footer').with_content(%r{</Plugin>}) }
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
48 changes: 48 additions & 0 deletions 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{<Database "foo">\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
102 changes: 102 additions & 0 deletions 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{<Query "foo">\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+</Query>}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
8 changes: 8 additions & 0 deletions templates/plugin/oracle/database.conf.erb
@@ -0,0 +1,8 @@
<Database "<%= @database %>">
ConnectID "<%= @connect_id %>"
Username "<%= @username %>"
Password "<%= @password %>"
<% @query.each do |query| -%>
Query "<%= query %>"
<% end -%>
</Database>
15 changes: 15 additions & 0 deletions templates/plugin/oracle/query.conf.erb
@@ -0,0 +1,15 @@
<Query "<%= @query %>">
Statement "<%= @statement %>"
<% @results.each do |result| -%>
<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(' ') %>
</Result>
<% end -%>
</Query>