-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #994 from UGent-DICT/modbus
Add modbus plugin
- Loading branch information
Showing
11 changed files
with
424 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# @summary Install and configure the modbus plugin | ||
# | ||
# @see https://collectd.org/wiki/index.php/Plugin:Modbus | ||
# | ||
# @param ensure Enable/Disable modbus support | ||
# @param manage_package Install collectd-modbus package? Currently supports RedHat and Debian os family. | ||
# @param data modbus data entries | ||
# @param hosts modbus host entries | ||
class collectd::plugin::modbus ( | ||
Enum['present', 'absent'] $ensure = 'present', | ||
Optional[Boolean] $manage_package = undef, | ||
Hash[String[1], Collectd::Modbus::Data] $data = {}, | ||
Hash[String[1], Collectd::Modbus::Host] $hosts = {}, | ||
) { | ||
include collectd | ||
|
||
$_manage_package = pick($manage_package, $collectd::manage_package) | ||
|
||
$_package_name = $facts['os']['family'] ? { | ||
'RedHat' => 'collectd-modbus', | ||
'Debian' => 'libmodbus5', | ||
default => undef, | ||
} | ||
|
||
if $_package_name and $_manage_package { | ||
package { $_package_name: | ||
ensure => $ensure, | ||
} | ||
} | ||
|
||
collectd::plugin { 'modbus': | ||
ensure => $ensure, | ||
content => epp('collectd/plugin/modbus.conf', { | ||
'data' => $data, | ||
'hosts' => $hosts, | ||
}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'collectd::plugin::modbus' do | ||
on_supported_os(baseline_os_hash).each do |os, os_facts| | ||
context "on #{os}" do | ||
let :facts do | ||
os_facts | ||
end | ||
let :pre_condition do | ||
'include collectd' | ||
end | ||
|
||
options = os_specific_options(os_facts) | ||
|
||
context ':ensure => present and dataset for Current Phase A' do | ||
let :params do | ||
{ | ||
data: { | ||
'current_phase_a' => { | ||
'type' => 'gauge', | ||
'instance' => 'Current Phase A', | ||
'register_base' => 1234, | ||
'register_type' => 'Float', | ||
} | ||
}, | ||
hosts: { | ||
'power123' => { | ||
'address' => '127.0.0.1', | ||
'port' => 502, | ||
'interval' => 10, | ||
'slaves' => { | ||
255 => { | ||
'instance' => 'power meter 255', | ||
'collect' => ['current_phase_a'], | ||
} | ||
} | ||
} | ||
} | ||
} | ||
end | ||
|
||
it "Will create #{options[:plugin_conf_dir]}/10-modbus.conf" do | ||
is_expected.to contain_file('modbus.load').with( | ||
ensure: 'present', | ||
path: "#{options[:plugin_conf_dir]}/10-modbus.conf", | ||
content: %r{Data "current_phase_a".+Instance "Current Phase A".+Host "power123".+Slave 255}m | ||
) | ||
end | ||
end | ||
|
||
context ':ensure => absent' do | ||
let :params do | ||
{ | ||
ensure: 'absent', | ||
data: { | ||
'current_phase_a' => { | ||
'type' => 'gauge', | ||
'instance' => 'Current Phase A', | ||
'register_base' => 1234, | ||
'register_type' => 'Float', | ||
} | ||
}, | ||
hosts: { | ||
'power123' => { | ||
'address' => '127.0.0.1', | ||
'port' => 502, | ||
'interval' => 10, | ||
'slaves' => { | ||
255 => { | ||
'instance' => 'power meter 255', | ||
'collect' => ['current_phase_a'], | ||
} | ||
} | ||
} | ||
} | ||
} | ||
end | ||
|
||
it "Will not create #{options[:plugin_conf_dir]}/10-modbus.conf" do | ||
is_expected.to contain_file('modbus.load').with( | ||
ensure: 'absent', | ||
path: "#{options[:plugin_conf_dir]}/10-modbus.conf" | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'Collectd::Modbus::Data' do | ||
it do | ||
is_expected.to allow_values({ | ||
'type' => 'foo', | ||
'register_base' => 123, | ||
'register_type' => 'Int32', | ||
}) | ||
end | ||
|
||
it do | ||
is_expected.to allow_values({ | ||
'type' => 'foo', | ||
'register_base' => 123, | ||
'register_type' => 'Int32', | ||
'register_cmd' => 'ReadInput', | ||
}) | ||
end | ||
|
||
it do | ||
is_expected.to allow_values({ | ||
'instance' => 'foobar', | ||
'type' => 'foo', | ||
'register_base' => 123, | ||
'register_type' => 'Int32', | ||
}) | ||
end | ||
|
||
it { is_expected.not_to allow_values(nil) } | ||
it { is_expected.not_to allow_values({ 'type' => 'foo', 'register_base' => 123 }) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'Collectd::Modbus::Host' do | ||
let(:slaves) do | ||
{ | ||
1 => { 'instance' => 'foo1', 'collect' => 'bar1' }, | ||
2 => { 'instance' => 'foo2', 'collect' => %w[bar1 bar2] }, | ||
} | ||
end | ||
|
||
it { is_expected.to allow_values({ 'address' => '127.0.0.1', 'port' => 1234, 'slaves' => slaves }) } | ||
it { is_expected.to allow_values({ 'address' => '127.0.0.1', 'port' => 1234, 'slaves' => slaves, 'interval' => 120 }) } | ||
|
||
it { is_expected.not_to allow_values(nil) } | ||
it { is_expected.not_to allow_values({ 'address' => '127.0.0.1', 'port' => '1234', 'slaves' => slaves, 'interval' => 120 }) } | ||
it { is_expected.not_to allow_values({ 'port' => 1234, 'slaves' => slaves, 'interval' => 120 }) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe 'Collectd::Modbus::Slave' do | ||
it { is_expected.to allow_values({ 'instance' => 'foo1', 'collect' => 'bar1' }) } | ||
it { is_expected.to allow_values({ 'instance' => 'foo1', 'collect' => %w[bar1 bar2] }) } | ||
|
||
it { is_expected.not_to allow_values(nil) } | ||
it { is_expected.not_to allow_values({ 'collect' => ['bar1'] }) } | ||
it { is_expected.not_to allow_values({ 'instance' => 'foo1' }) } | ||
it { is_expected.not_to allow_values({ 'instance' => 'foo1', 'collect' => [] }) } | ||
end |
Oops, something went wrong.