Class: Nagios::Base

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/vendor/puppet/external/nagios/base.rb

Overview

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

Defined Under Namespace

Classes: UnknownNagiosType

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Base

Initialize our object, optionally with a list of parameters.



193
194
195
196
197
198
199
200
201
202
# File 'lib/vendor/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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mname, *args) ⇒ Object

Handle parameters like attributes.



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/vendor/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

Class Attribute Details

.attObject

Returns the value of attribute att.



11
12
13
# File 'lib/vendor/puppet/external/nagios/base.rb', line 11

def att
  @att
end

.derivativesObject

Returns the value of attribute derivatives.



11
12
13
# File 'lib/vendor/puppet/external/nagios/base.rb', line 11

def derivatives
  @derivatives
end

.ldapbaseObject

Returns the value of attribute ldapbase.



12
13
14
# File 'lib/vendor/puppet/external/nagios/base.rb', line 12

def ldapbase
  @ldapbase
end

.nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/vendor/puppet/external/nagios/base.rb', line 11

def name
  @name
end

.namevarObject

Return the namevar for the canonical name.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/vendor/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

.ocsObject

Returns the value of attribute ocs.



11
12
13
# File 'lib/vendor/puppet/external/nagios/base.rb', line 11

def ocs
  @ocs
end

.parametersObject

Returns the value of attribute parameters.



11
12
13
# File 'lib/vendor/puppet/external/nagios/base.rb', line 11

def parameters
  @parameters
end

.superiorObject (readonly)

Returns the value of attribute superior.



16
17
18
# File 'lib/vendor/puppet/external/nagios/base.rb', line 16

def superior
  @superior
end

Class Method Details

.attach(hash) ⇒ Object

Attach one class to another.



20
21
22
23
# File 'lib/vendor/puppet/external/nagios/base.rb', line 20

def self.attach(hash)
  @attach ||= {}
  hash.each do |n, v| @attach[n] = v end
end

.camelcase(param) ⇒ Object

Convert a parameter to camelcase



26
27
28
29
30
# File 'lib/vendor/puppet/external/nagios/base.rb', line 26

def self.camelcase(param)
  param.gsub(/_./) do |match|
    match.sub(/_/,'').capitalize
  end
end

.create(name, args = {}) ⇒ Object

Create a new instance of a given class.



40
41
42
43
44
45
46
47
48
# File 'lib/vendor/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) ⇒ Object

Uncamelcase a parameter.



33
34
35
36
37
# File 'lib/vendor/puppet/external/nagios/base.rb', line 33

def self.decamelcase(param)
  param.gsub(/[A-Z]/) do |match|
    "_#{match.downcase}"
  end
end

.eachtypeObject

Yield each type in turn.



51
52
53
54
55
# File 'lib/vendor/puppet/external/nagios/base.rb', line 51

def self.eachtype
  @types.each do |name, type|
    yield [name, type]
  end
end

.map(hash) ⇒ Object

Create a mapping.



58
59
60
61
# File 'lib/vendor/puppet/external/nagios/base.rb', line 58

def self.map(hash)
  @map ||= {}
  hash.each do |n, v| @map[n] = v end
end

.mapping(name) ⇒ Object

Return a mapping (or nil) for a param



64
65
66
67
68
69
70
71
# File 'lib/vendor/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

.newtype(name, &block) ⇒ Object

Create a new type.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vendor/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) ⇒ Object

Define both the normal case and camelcase method for a parameter



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/vendor/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) ⇒ Boolean

Is the specified name a valid parameter?

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/vendor/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) ⇒ Object

Manually set the namevar



136
137
138
139
# File 'lib/vendor/puppet/external/nagios/base.rb', line 136

def self.setnamevar(name)
  name = name.intern if name.is_a? String
  @namevar = name
end

.setparameters(*array) ⇒ Object

Set the valid parameters for this class



142
143
144
# File 'lib/vendor/puppet/external/nagios/base.rb', line 142

def self.setparameters(*array)
  @parameters += array
end

.setsuperior(name) ⇒ Object

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



148
149
150
# File 'lib/vendor/puppet/external/nagios/base.rb', line 148

def self.setsuperior(name)
  @superior = name
end

.suppress(name) ⇒ Object

Parameters to suppress in output.



153
154
155
156
# File 'lib/vendor/puppet/external/nagios/base.rb', line 153

def self.suppress(name)
  @suppress ||= []
  @suppress << name
end

.suppress?(name) ⇒ Boolean

Whether a given parameter is suppressed.

Returns:

  • (Boolean)


159
160
161
# File 'lib/vendor/puppet/external/nagios/base.rb', line 159

def self.suppress?(name)
  defined?(@suppress) and @suppress.include?(name)
end

.to_sObject

Return our name as the string.



164
165
166
# File 'lib/vendor/puppet/external/nagios/base.rb', line 164

def self.to_s
  self.name.to_s
end

.type(name) ⇒ Object

Return a type by name.



169
170
171
172
173
# File 'lib/vendor/puppet/external/nagios/base.rb', line 169

def self.type(name)
  name = name.intern if name.is_a? String

  @types[name]
end

Instance Method Details

#[](param) ⇒ Object

Convenience methods.



176
177
178
# File 'lib/vendor/puppet/external/nagios/base.rb', line 176

def [](param)
  send(param)
end

#[]=(param, value) ⇒ Object

Convenience methods.



181
182
183
# File 'lib/vendor/puppet/external/nagios/base.rb', line 181

def []=(param,value)
  send(param.to_s + "=", value)
end

#eachObject

Iterate across all ofour set parameters.



186
187
188
189
190
# File 'lib/vendor/puppet/external/nagios/base.rb', line 186

def each
  @parameters.each { |param,value|
    yield(param,value)
  }
end

#nameObject

Retrieve our name, through a bit of redirection.



228
229
230
# File 'lib/vendor/puppet/external/nagios/base.rb', line 228

def name
  send(self.class.namevar)
end

#name=(value) ⇒ Object

This is probably a bad idea.



233
234
235
236
237
# File 'lib/vendor/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

#namevarObject



239
240
241
# File 'lib/vendor/puppet/external/nagios/base.rb', line 239

def namevar
  (self.type + "_name").intern
end

#parammap(param) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/vendor/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

#parentObject



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/vendor/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_ldifObject

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



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/vendor/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_sObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/vendor/puppet/external/nagios/base.rb', line 298

def to_s
  str = "define #{self.type} {\n"

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

  str += "}\n"

  str
end

#typeObject

The type of object we are.



317
318
319
# File 'lib/vendor/puppet/external/nagios/base.rb', line 317

def type
  self.class.name
end