-
-
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.
This commit implements a modbus plugin. It installs required packages and generates configuration for hosts and datasets.
- Loading branch information
Showing
7 changed files
with
234 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# https://collectd.org/wiki/index.php/Plugin:Modbus | ||
# @summary Creates configuration for a collectd modbus plugin | ||
# | ||
# @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,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 -%> |
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]}] |
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]]}] |