# class Nagios::Base

The base class for all of our Nagios object types. Everything else is mostly just data.

### Attributes

att[RW]
derivatives[RW]
ldapbase[RW]
name[RW]
namevar[W]
ocs[RW]
parameters[RW]
superior[R]

### Public Class Methods

attach(hash) click to toggle source

Attach one class to another.

    # File lib/puppet/external/nagios/base.rb, line 20
    def self.attach(hash)
      @attach ||= {}
      hash.each do |n, v| @attach[n] = v end
    end

camelcase(param) click to toggle source

Convert a parameter to camelcase

    # File lib/puppet/external/nagios/base.rb, line 26
    def self.camelcase(param)
      param.gsub(/_./) do |match|
        match.sub(/_/,'').capitalize
      end
    end

create(name, args = {}) click to toggle source

Create a new instance of a given class.

    # File lib/puppet/external/nagios/base.rb, line 40
    def self.create(name, args = {})
      name = name.intern if name.is_a? String

      if @types.include?(name)
        @types[name].new(args)
      else
        raise UnknownNagiosType, "Unknown type #{name}"
      end
    end

decamelcase(param) click to toggle source

Uncamelcase a parameter.

    # File lib/puppet/external/nagios/base.rb, line 33
    def self.decamelcase(param)
      param.gsub(/[A-Z]/) do |match|
        "_#{match.downcase}"
      end
    end

eachtype() { |name, type| ... } click to toggle source

Yield each type in turn.

    # File lib/puppet/external/nagios/base.rb, line 51
    def self.eachtype
      @types.each do |name, type|
        yield [name, type]
      end
    end

map(hash) click to toggle source

Create a mapping.

    # File lib/puppet/external/nagios/base.rb, line 58
    def self.map(hash)
      @map ||= {}
      hash.each do |n, v| @map[n] = v end
    end

mapping(name) click to toggle source

Return a mapping (or nil) for a param

    # File lib/puppet/external/nagios/base.rb, line 64
    def self.mapping(name)
      name = name.intern if name.is_a? String
      if defined?(@map)
        @map[name]
      else
        nil
      end
    end

namevar() click to toggle source

Return the namevar for the canonical name.

    # File lib/puppet/external/nagios/base.rb, line 74
    def self.namevar
      if defined?(@namevar)
        return @namevar
      else
        if parameter?(:name)
          return :name
        elsif tmp = (self.name.to_s + "_name").intern and parameter?(tmp)
          @namevar = tmp
          return @namevar
        else
          raise "Type #{self.name} has no name var"
        end
      end
    end

new(args = {}) click to toggle source

Initialize our object, optionally with a list of parameters.

    # File lib/puppet/external/nagios/base.rb, line 193
    def initialize(args = {})
      @parameters = {}

      args.each { |param,value|
        self[param] = value
      }
      if @namevar == :_naginator_name
        self['_naginator_name'] = self['name']
      end
    end

newtype(name, &block) click to toggle source

Create a new type.

    # File lib/puppet/external/nagios/base.rb, line 90
    def self.newtype(name, &block)
      name = name.intern if name.is_a? String

      @types ||= {}

      # Create the class, with the correct name.
      t = Class.new(self)
      t.name = name

      # Everyone gets this.  There should probably be a better way, and I
      # should probably hack the attribute system to look things up based on
      # this "use" setting, but, eh.
      t.parameters = [:use]

      const_set(name.to_s.capitalize,t)

      # Evaluate the passed block.  This should usually define all of the work.
      t.class_eval(&block)

      @types[name] = t
    end

paramattr(name) click to toggle source

Define both the normal case and camelcase method for a parameter

    # File lib/puppet/external/nagios/base.rb, line 113
    def self.paramattr(name)
      camel = camelcase(name)
      param = name

      [name, camel].each do |method|
        define_method(method) do
          @parameters[param]
        end

        define_method(method.to_s + "=") do |value|
          @parameters[param] = value
        end
      end

    end

parameter?(name) click to toggle source

Is the specified name a valid parameter?

    # File lib/puppet/external/nagios/base.rb, line 130
    def self.parameter?(name)
      name = name.intern if name.is_a? String
      @parameters.include?(name)
    end

setnamevar(name) click to toggle source

Manually set the namevar

    # File lib/puppet/external/nagios/base.rb, line 136
    def self.setnamevar(name)
      name = name.intern if name.is_a? String
      @namevar = name
    end

setparameters(*array) click to toggle source

Set the valid parameters for this class

    # File lib/puppet/external/nagios/base.rb, line 142
    def self.setparameters(*array)
      @parameters += array
    end

setsuperior(name) click to toggle source

Set the superior ldap object class. Seems silly to include this in this class, but, eh.

    # File lib/puppet/external/nagios/base.rb, line 148
    def self.setsuperior(name)
      @superior = name
    end

suppress(name) click to toggle source

Parameters to suppress in output.

    # File lib/puppet/external/nagios/base.rb, line 153
    def self.suppress(name)
      @suppress ||= []
      @suppress << name
    end

suppress?(name) click to toggle source

Whether a given parameter is suppressed.

    # File lib/puppet/external/nagios/base.rb, line 159
    def self.suppress?(name)
      defined?(@suppress) and @suppress.include?(name)
    end

to_s() click to toggle source

Return our name as the string.

    # File lib/puppet/external/nagios/base.rb, line 164
    def self.to_s
      self.name.to_s
    end

type(name) click to toggle source

Return a type by name.

    # File lib/puppet/external/nagios/base.rb, line 169
    def self.type(name)
      name = name.intern if name.is_a? String

      @types[name]
    end

### Public Instance Methods

[](param) click to toggle source

Convenience methods.

    # File lib/puppet/external/nagios/base.rb, line 176
    def [](param)
      send(param)
    end

[]=(param,value) click to toggle source

Convenience methods.

    # File lib/puppet/external/nagios/base.rb, line 181
    def []=(param,value)
      send(param.to_s + "=", value)
    end

each() { |param,value| ... } click to toggle source

Iterate across all ofour set parameters.

    # File lib/puppet/external/nagios/base.rb, line 186
    def each
      @parameters.each { |param,value|
        yield(param,value)
      }
    end

method_missing(mname, *args) click to toggle source

Handle parameters like attributes.

Calls superclass method

    # File lib/puppet/external/nagios/base.rb, line 205
    def method_missing(mname, *args)
      pname = mname.to_s
      pname.sub!(/=/, '')

      if self.class.parameter?(pname)
        if pname =~ /A-Z/
          pname = self.class.decamelcase(pname)
        end
        self.class.paramattr(pname)

        # Now access the parameters directly, to make it at least less
        # likely we'll end up in an infinite recursion.
        if mname.to_s =~ /=$/
          @parameters[pname] = *args
        else
          return @parameters[mname]
        end
      else
        super
      end
    end

name() click to toggle source

Retrieve our name, through a bit of redirection.

    # File lib/puppet/external/nagios/base.rb, line 228
    def name
      send(self.class.namevar)
    end

name=(value) click to toggle source

This is probably a bad idea.

    # File lib/puppet/external/nagios/base.rb, line 233
    def name=(value)
      unless self.class.namevar.to_s == "name"
        send(self.class.namevar.to_s + "=", value)
      end
    end

namevar() click to toggle source

    # File lib/puppet/external/nagios/base.rb, line 239
    def namevar
      (self.type + "_name").intern
    end

parammap(param) click to toggle source

    # File lib/puppet/external/nagios/base.rb, line 243
    def parammap(param)
      unless defined?(@map)
        map = {
          self.namevar => "cn"
        }
        map.update(self.class.map) if self.class.map
      end
      if map.include?(param)
        return map[param]
      else
        return "nagios-" + param.id2name.gsub(/_/,'-')
      end
    end

parent() click to toggle source

    # File lib/puppet/external/nagios/base.rb, line 257
    def parent
      unless defined?(self.class.attached)
        puts "Duh, you called parent on an unattached class"
        return
      end

      klass,param = self.class.attached
      unless @parameters.include?(param)
        puts "Huh, no attachment param"
        return
      end
      klass[@parameters[param]]
    end

to_ldif() click to toggle source

okay, this sucks how do i get my list of ocs?

    # File lib/puppet/external/nagios/base.rb, line 273
    def to_ldif
      base = self.class.ldapbase
      str = self.dn + "\n"
      ocs = Array.new
      if self.class.ocs
        # i'm storing an array, so i have to flatten it and stuff
        kocs = self.class.ocs
        ocs.push(*kocs)
      end
      ocs.push "top"
      oc = self.class.to_s
      oc.sub!(/Nagios/,'nagios')
      oc.sub!(/::/,'')
      ocs.push oc
      ocs.each { |oc|
        str += "objectclass: #{oc}\n"
      }
      @parameters.each { |name,value|
        next if self.class.suppress.include?(name)
        ldapname = self.parammap(name)
        str += ldapname + ": #{value}\n"
      }
      str += "\n"
    end

to_s() click to toggle source

    # File lib/puppet/external/nagios/base.rb, line 298
    def to_s
      str = "define #{self.type} {\n"

      self.each { |param,value|
        str += %Q{\t%-30s %s\n} % [ param,
          if value.is_a? Array
            value.join(",")
          else
            value
          end
          ]
      }

      str += "}\n"

      str
    end

type() click to toggle source

The type of object we are.

    # File lib/puppet/external/nagios/base.rb, line 317
    def type
      self.class.name
    end