Skip to content

Commit

Permalink
Merge pull request #994 from UGent-DICT/modbus
Browse files Browse the repository at this point in the history
Add modbus plugin
  • Loading branch information
traylenator authored Jan 28, 2025
2 parents ec8fcc1 + 2bad2bd commit 3b5b30d
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ documentation for each plugin for configurable attributes.
* `memcached`(see [collectd::plugin::memcached](#class-collectdpluginmemcached)
below )
* `memory`(see [collectd::plugin::memory](#class-collectdpluginmemory) below )
* `modbus` (see [collectd::plugin::modbus](#class-collectdpluginmodbus) below)
* `mongodb`(see [collectd::plugin::mongodb](#class-collectdpluginmongodb) below )
* `mysql` (see [collectd::plugin::mysql](#class-collectdpluginmysql) below)
* `netlink` (see [collectd::plugin::netlink](#class-collectdpluginnetlink) below)
Expand Down Expand Up @@ -1073,6 +1074,35 @@ class { 'collectd::plugin::memory':
}
```

#### Class: `collectd::plugin::modbus`

```puppet
class {'collectd::plugin::modbus':
ensure => 'present',
data => {
current_phase_a => {
'type' => 'gauge',
'instance' => 'current phase A',
'register_base' => 1234,
'register_type' => 'Float',
}
},
hosts => {
meter123 => {
'address' => '127.0.0.1',
'port' => 502,
'interval' => 10,
'slaves' => {
255 => {
'instance' => 'power meter 255',
'collect' => ['current_phase_a'],
}
},
}
},
}
```

#### Class: `collectd::plugin::mysql`

```puppet
Expand Down
111 changes: 111 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* [`collectd::plugin::mcelog`](#collectd--plugin--mcelog): https://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_mcelog
* [`collectd::plugin::memcached`](#collectd--plugin--memcached): https://collectd.org/wiki/index.php/Plugin:memcached
* [`collectd::plugin::memory`](#collectd--plugin--memory): https://collectd.org/wiki/index.php/Plugin:Memory
* [`collectd::plugin::modbus`](#collectd--plugin--modbus): Install and configure the modbus plugin
* [`collectd::plugin::mongodb`](#collectd--plugin--mongodb): Class: collectd::plugin::mongodb
* [`collectd::plugin::mysql`](#collectd--plugin--mysql): MySQL plugin https://collectd.org/wiki/index.php/Plugin:MySQL
* [`collectd::plugin::netlink`](#collectd--plugin--netlink): https://collectd.org/wiki/index.php/Plugin:Netlink
Expand Down Expand Up @@ -188,6 +189,9 @@
* [`Collectd::LOGPARSER::Message`](#Collectd--LOGPARSER--Message): https://wiki.opnfv.org/display/fastpath/Logparser+plugin+HLD
* [`Collectd::MCELOG::Memory`](#Collectd--MCELOG--Memory): https://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_mcelog
* [`Collectd::Manifests::Init`](#Collectd--Manifests--Init)
* [`Collectd::Modbus::Data`](#Collectd--Modbus--Data): represents a modbus data entry
* [`Collectd::Modbus::Host`](#Collectd--Modbus--Host): represents a modbus host entry
* [`Collectd::Modbus::Slave`](#Collectd--Modbus--Slave): Represents a modbus host's slave entry
* [`Collectd::Network::SecurityLevel`](#Collectd--Network--SecurityLevel)
* [`Collectd::Redis::Node`](#Collectd--Redis--Node)
* [`Collectd::SNMP::AuthProtocol`](#Collectd--SNMP--AuthProtocol)
Expand Down Expand Up @@ -3591,6 +3595,54 @@ Data type: `Any`

Default value: `undef`

### <a name="collectd--plugin--modbus"></a>`collectd::plugin::modbus`

Install and configure the modbus plugin

* **See also**
* https://collectd.org/wiki/index.php/Plugin:Modbus

#### Parameters

The following parameters are available in the `collectd::plugin::modbus` class:

* [`ensure`](#-collectd--plugin--modbus--ensure)
* [`manage_package`](#-collectd--plugin--modbus--manage_package)
* [`data`](#-collectd--plugin--modbus--data)
* [`hosts`](#-collectd--plugin--modbus--hosts)

##### <a name="-collectd--plugin--modbus--ensure"></a>`ensure`

Data type: `Enum['present', 'absent']`

Enable/Disable modbus support

Default value: `'present'`

##### <a name="-collectd--plugin--modbus--manage_package"></a>`manage_package`

Data type: `Optional[Boolean]`

Install collectd-modbus package? Currently supports RedHat and Debian os family.

Default value: `undef`

##### <a name="-collectd--plugin--modbus--data"></a>`data`

Data type: `Hash[String[1], Collectd::Modbus::Data]`

modbus data entries

Default value: `{}`

##### <a name="-collectd--plugin--modbus--hosts"></a>`hosts`

Data type: `Hash[String[1], Collectd::Modbus::Host]`

modbus host entries

Default value: `{}`

### <a name="collectd--plugin--mongodb"></a>`collectd::plugin::mongodb`

Class: collectd::plugin::mongodb
Expand Down Expand Up @@ -10312,6 +10364,65 @@ The Collectd::Manifests::Init data type.

Alias of `Pattern[/(^5.4|^5.5|^5.6|^5.7|^5.8|^master)/]`

### <a name="Collectd--Modbus--Data"></a>`Collectd::Modbus::Data`

https://github.com/collectd/collectd/blob/main/src/modbus.c

Alias of

```puppet
Struct[{
Optional['instance'] => String,
NotUndef['type'] => String[1],
NotUndef['register_base'] => Integer[0],
NotUndef['register_type'] => Enum[
'Int16',
'Int32',
'Int32LE',
'Uint16',
'Uint32',
'Uint32LE',
'Float',
'FloatLE',
'Uint64',
'Int64',
'Double',
],
Optional['register_cmd'] => Enum['ReadHolding', 'ReadInput'],
}]
```

### <a name="Collectd--Modbus--Host"></a>`Collectd::Modbus::Host`

represents a modbus host entry

Alias of

```puppet
Struct[{
NotUndef['address'] => String[1],
NotUndef['port'] => Stdlib::Port,
NotUndef['slaves'] => Hash[Integer, Collectd::Modbus::Slave],
Optional['interval'] => Integer[0]
}]
```

### <a name="Collectd--Modbus--Slave"></a>`Collectd::Modbus::Slave`

Represents a modbus host's slave entry

Alias of

```puppet
Struct[{
NotUndef['instance'] => String[1],
NotUndef['collect'] => Variant[
String[1],
Array[String[1], 1]
]
}]
```

### <a name="Collectd--Network--SecurityLevel"></a>`Collectd::Network::SecurityLevel`

The Collectd::Network::SecurityLevel data type.
Expand Down
38 changes: 38 additions & 0 deletions manifests/plugin/modbus.pp
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,
}),
}
}
90 changes: 90 additions & 0 deletions spec/classes/collectd_plugin_modbus_spec.rb
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
34 changes: 34 additions & 0 deletions spec/type_aliases/collectd_modbus_data_spec.rb
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
19 changes: 19 additions & 0 deletions spec/type_aliases/collectd_modbus_host_spec.rb
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
13 changes: 13 additions & 0 deletions spec/type_aliases/collectd_modbus_slave_spec.rb
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
Loading

0 comments on commit 3b5b30d

Please sign in to comment.