From 348f257e3626848dd6a32b6f9eae17a5f30c21dd Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Mon, 31 Dec 2007 13:51:09 -0600 Subject: Adding the metaprogramming to create the Nagios types and Naginator providers. This is basically all of the code that's necessary to create all of the needed Nagios types. --- lib/puppet/util/nagios_maker.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/puppet/util/nagios_maker.rb (limited to 'lib/puppet/util/nagios_maker.rb') diff --git a/lib/puppet/util/nagios_maker.rb b/lib/puppet/util/nagios_maker.rb new file mode 100644 index 000000000..7c019f55e --- /dev/null +++ b/lib/puppet/util/nagios_maker.rb @@ -0,0 +1,40 @@ +require 'puppet/external/nagios' +require 'puppet/external/nagios/base' +require 'puppet/provider/naginator' + +module Puppet::Util::NagiosMaker + # Create a new nagios type, using all of the parameters + # from the parser. + def self.create_nagios_type(name) + name = name.to_sym + full_name = ("nagios_" + name.to_s).to_sym + + raise(Puppet::DevError, "No nagios type for %s" % name) unless nagtype = Nagios::Base.type(name) + + type = Puppet::Type.newtype(full_name) {} + + type.ensurable + + type.newparam(nagtype.namevar, :namevar => true) do + desc "The name parameter for Nagios type %s" % nagtype.name + end + + nagtype.parameters.each do |param| + next if param == nagtype.namevar + + type.newproperty(param) do + desc "Nagios configuration file parameter." + end + end + + type.newproperty(:target) do + desc 'target' + + defaultto do + resource.class.defaultprovider.default_target + end + end + + type.provide(:naginator, :parent => Puppet::Provider::Naginator, :default_target => "/etc/nagios/#{full_name.to_s}.cfg") {} + end +end -- cgit From 33e319a8be8e35fbe4a9ecb7e3185453b8239a83 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Mon, 31 Dec 2007 14:37:03 -0600 Subject: Added builtin support for all Nagios resource types. I use Naginator to parse and generate the files, with ParsedFile to handle record management and the like. Note that each resource type itself is just a call to a Factory method, since everything is just based on Naginator. Given that, all of the tests are in a single unit/ral/types/nagios.rb file, since I used a factory to generate them, too. This is probably either unnecessary or insufficient, but it's as far as I'm willing to go in testing them, and it did actually catch a few bugs. --- lib/puppet/util/nagios_maker.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/puppet/util/nagios_maker.rb') diff --git a/lib/puppet/util/nagios_maker.rb b/lib/puppet/util/nagios_maker.rb index 7c019f55e..f1f85466f 100644 --- a/lib/puppet/util/nagios_maker.rb +++ b/lib/puppet/util/nagios_maker.rb @@ -19,9 +19,14 @@ module Puppet::Util::NagiosMaker desc "The name parameter for Nagios type %s" % nagtype.name end - nagtype.parameters.each do |param| + # We deduplicate the parameters because it makes sense to allow Naginator to have dupes. + nagtype.parameters.uniq.each do |param| next if param == nagtype.namevar + # We can't turn these parameter names into constants, so at least for now they aren't + # supported. + next if param.to_s =~ /^[0-9]/ + type.newproperty(param) do desc "Nagios configuration file parameter." end @@ -35,6 +40,17 @@ module Puppet::Util::NagiosMaker end end - type.provide(:naginator, :parent => Puppet::Provider::Naginator, :default_target => "/etc/nagios/#{full_name.to_s}.cfg") {} + provider = type.provide(:naginator, :parent => Puppet::Provider::Naginator, :default_target => "/etc/nagios/#{full_name.to_s}.cfg") {} + + type.desc "The Nagios type #{name.to_s}. This resource type is autogenerated using the + model developed in Naginator_, and all of the Nagios types are generated using the + same code and the same library. + + This type generates Nagios configuration statements in Nagios-parseable configuration + files. By default, the statements will be added to ``#{provider.default_target}, but + you can send them to a different file by setting their ``target`` attribute. + + .. _naginator: http://reductivelabs.com/trac/naginator + " end end -- cgit