-
-
Notifications
You must be signed in to change notification settings - Fork 269
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
Add modbus plugin #994
Open
ruleant
wants to merge
2
commits into
voxpupuli:master
Choose a base branch
from
UGent-DICT:modbus
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add modbus plugin #994
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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'), | ||
} | ||
} |
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,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 |
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,42 @@ | ||
<% 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 -%> | ||
<% Array(slave_val['collect']).sort_by {|x| x}.each do |data_name| -%> | ||
Collect "<%= data_name %>" | ||
<% end -%> | ||
</Slave> | ||
<% end -%> | ||
<% end -%> | ||
</Host> | ||
<% end -%> | ||
</Plugin> | ||
<% 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,2 @@ | ||
# | ||
type Collectd::Modbus::Data = Struct[{Optional['instance'] => String, NotUndef['type'] => String[1], NotUndef['register_base'] => Numeric, NotUndef['register_type'] => String[1]}] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add some tests for this and the other types. e.g. like https://github.com/voxpupuli/puppet-systemd/tree/master/spec/type_aliases |
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,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]}] |
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,2 @@ | ||
# | ||
type Collectd::Modbus::Slave = Struct[{NotUndef['instance'] => String[1], NotUndef['collect'] => Variant[String[1], Array[String[1], 1]]}] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If adding a new template could it be written in EPP?