Skip to content

Commit

Permalink
Add modbus plugin
Browse files Browse the repository at this point in the history
This commit implements a modbus plugin.
It installs required packages and generates configuration for hosts and
datasets.
  • Loading branch information
ruleant committed Jan 26, 2022
1 parent 12791f3 commit 78cafc2
Show file tree
Hide file tree
Showing 7 changed files with 236 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
72 changes: 72 additions & 0 deletions manifests/plugin/modbus.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# @summary Creates configuration for a collectd modbus plugin
#
# More information on the modbus plugin configuration options:
# https://collectd.org/wiki/index.php/Plugin:Modbus
#
# @example
#
# 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'],
# }
# },
# }
# },
# }
#
# @param ensure
# Ensures modbus module
# @param manage_package
# If enabled, install required packages
# @param data
# Defines data set configuration
# @param hosts
# Defines hosts configuration

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)

if $facts['os']['family'] == 'RedHat' {
if $_manage_package {
package { 'collectd-modbus':
ensure => $ensure,
}
}
}

if $facts['os']['family'] == 'Debian' {
if $_manage_package {
package { 'libmodbus5':
ensure => $ensure,
}
}
}

collectd::plugin { 'modbus':
ensure => $ensure,
content => template('collectd/plugin/modbus.conf.erb'),
}
}
88 changes: 88 additions & 0 deletions spec/classes/collectd_plugin_modbus_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'spec_helper'

describe 'collectd::plugin::modbus', type: :class do
on_supported_os(baseline_os_hash).each do |os, facts|
context "on #{os} " do
let :facts do
facts
end
let :pre_condition do
'include collectd'
end

options = os_specific_options(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
40 changes: 40 additions & 0 deletions templates/plugin/modbus.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<% if @data or @hosts -%>
<Plugin modbus>
<% @data.sort_by {|k,v| k}.each do |key,val| -%>
<Data "<%= key %>">
Type "<%= val['type'] %>"
<% if val['instance'] -%>
Instance "<%= val['instance'] %>"
<% end -%>
<% if val['register_base'] -%>
RegisterBase <%= val['register_base'] %>
<% end -%>
<% if val['register_type'] -%>
RegisterType <%= val['register_type'] %>
<% end -%>
</Data>
<% end -%>
<% @hosts.sort_by {|k,v| k}.each do |key,val| -%>
<Host "<%= key %>">
Address "<%= val['address'] %>"
<% if val['port'] -%>
Port <%= val['port'] %>
<% end -%>
<% if val['interval'] -%>
Interval <%= val['interval'] %>
<% end -%>
<% if val['slaves'] -%>
<% Array(val['slaves']).sort_by {|k,v| k}.each do |slave_key,slave_val| -%>
<Slave <%= slave_key %>>
<% if slave_val['instance'] -%>
Instance "<%= slave_val['instance'] %>"
<% end -%>
Collect <%= Array(slave_val['collect']).sort.map { |x| %("#{x}") }.join(' ') %>
</Slave>
<% end -%>
<% end -%>
</Host>
<% end -%>
</Plugin>
<% end -%>
2 changes: 2 additions & 0 deletions types/modbus/data.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::Modbus::Data = Struct[{Optional['instance'] => String, NotUndef['type'] => String[1], NotUndef['register_base'] => Numeric, NotUndef['register_type'] => String[1]}]
2 changes: 2 additions & 0 deletions types/modbus/host.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::Modbus::Host = Struct[{NotUndef['address'] => String[1], NotUndef['port'] => String[1], NotUndef['slaves'] => Hash[Integer, Collectd::Modbus::Slave], Optional['interval'] => Integer[0]}]
2 changes: 2 additions & 0 deletions types/modbus/slave.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::Modbus::Slave = Struct[{NotUndef['instance'] => String[1], NotUndef['collect'] => Variant[String[1], Array[String[1], 1]]}]

0 comments on commit 78cafc2

Please sign in to comment.