Class: Nagios::Base

Inherits:
Object show all
Includes:
Enumerable
Defined in:
core_modules/nagios_core/lib/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.



189
190
191
192
193
194
195
196
197
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 189

def initialize(args = {})
  @parameters = {}

  args.each do |param, value|
    self[param] = value
  end

  self['_naginator_name'] = self['name'] if @namevar == :_naginator_name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mname, *args) ⇒ Object

Handle parameters like attributes.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 200

def method_missing(mname, *args) # rubocop:disable Style/MethodMissing
  pname = mname.to_s
  pname.sub!(%r{=}, '')

  if self.class.parameter?(pname)
    if pname =~ %r{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 =~ %r{=$}
      @parameters[pname] = args.first
    else
      @parameters[mname]
    end
  else
    super
  end
end

Class Attribute Details

.attObject

Returns the value of attribute att.



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

def att
  @att
end

.derivativesObject

Returns the value of attribute derivatives.



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

def derivatives
  @derivatives
end

.ldapbaseObject

Returns the value of attribute ldapbase.



13
14
15
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 13

def ldapbase
  @ldapbase
end

.nameObject

Returns the value of attribute name.



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

def name
  @name
end

.namevarObject

Return the namevar for the canonical name.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 73

def self.namevar
  if defined?(@namevar)
    @namevar
  elsif parameter?(:name)
    :name
  elsif (tmp = (name.to_s + '_name').to_sym) && parameter?(tmp)
    @namevar = tmp
    @namevar
  else
    raise "Type #{name} has no name var"
  end
end

.ocsObject

Returns the value of attribute ocs.



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

def ocs
  @ocs
end

.parametersObject

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

.superiorObject (readonly)

Returns the value of attribute superior.



17
18
19
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 17

def superior
  @superior
end

Class Method Details

.attach(hash) ⇒ Object

Attach one class to another.



21
22
23
24
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 21

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

.camelcase(param) ⇒ Object

Convert a parameter to camelcase



27
28
29
30
31
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 27

def self.camelcase(param)
  param.gsub(%r{_.}) do |match|
    match.sub(%r{_}, '').capitalize
  end
end

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

Create a new instance of a given class.

Raises:



41
42
43
44
45
46
47
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 41

def self.create(name, args = {})
  name = name.to_sym if name.is_a? String

  raise UnknownNagiosType, "Unknown type #{name}" unless @types.include?(name)

  @types[name].new(args)
end

.decamelcase(param) ⇒ Object

Uncamelcase a parameter.



34
35
36
37
38
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 34

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

.eachtypeObject

Yield each type in turn.



50
51
52
53
54
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 50

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

.map(hash) ⇒ Object

Create a mapping.



57
58
59
60
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 57

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

.mapping(name) ⇒ Object

Return a mapping (or nil) for a param



63
64
65
66
67
68
69
70
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 63

def self.mapping(name)
  name = name.to_sym if name.is_a? String
  if defined?(@map)
    @map[name]
  else
    nil
  end
end

.newtype(name, &block) ⇒ Object

Create a new type.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 87

def self.newtype(name, &block)
  name = name.to_sym 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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 110

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)


126
127
128
129
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 126

def self.parameter?(name)
  name = name.to_sym if name.is_a? String
  @parameters.include?(name)
end

.setnamevar(name) ⇒ Object

Manually set the namevar



132
133
134
135
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 132

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

.setparameters(*array) ⇒ Object

Set the valid parameters for this class



138
139
140
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 138

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.



144
145
146
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 144

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

.suppress(name) ⇒ Object

Parameters to suppress in output.



149
150
151
152
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 149

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

.suppress?(name) ⇒ Boolean

Whether a given parameter is suppressed.

Returns:

  • (Boolean)


155
156
157
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 155

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

.to_sObject

Return our name as the string.



160
161
162
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 160

def self.to_s
  name.to_s
end

.type(name) ⇒ Object

Return a type by name.



165
166
167
168
169
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 165

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

  @types[name]
end

Instance Method Details

#[](param) ⇒ Object

Convenience methods.



172
173
174
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 172

def [](param)
  send(param)
end

#[]=(param, value) ⇒ Object

Convenience methods.



177
178
179
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 177

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

#eachObject

Iterate across all ofour set parameters.



182
183
184
185
186
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 182

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

#nameObject

Retrieve our name, through a bit of redirection.



223
224
225
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 223

def name
  send(self.class.namevar)
end

#name=(value) ⇒ Object

This is probably a bad idea.



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

def name=(value)
  send(self.class.namevar.to_s + '=', value) unless self.class.namevar.to_s == 'name'
end

#namevarObject



232
233
234
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 232

def namevar
  (type + '_name').to_sym
end

#parammap(param) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 236

def parammap(param)
  unless defined?(@map)
    map = {
      namevar => 'cn',
    }
    map.update(self.class.map) if self.class.map
  end
  if map.include?(param)
    map[param]
  else
    'nagios-' + param.id2name.tr('_', '-')
  end
end

#parentObject



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 250

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?



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 266

def to_ldif
  str = dn + "\n"
  ocs = []
  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!(%r{Nagios}, 'nagios')
  oc.sub!(%r{::}, '')
  ocs.push oc
  ocs.each do |objclass|
    str += "objectclass: #{objclass}\n"
  end
  @parameters.each do |name, value|
    next if self.class.suppress.include?(name)
    ldapname = parammap(name)
    str += ldapname + ": #{value}\n"
  end
  str += "\n"
end

#to_sObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 290

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

  @parameters.keys.sort.each do |param|
    value = @parameters[param]
    str += %(\t%-30s %s\n) % [param,
                              if value.is_a? Array
                                value.join(',').sub(';', '\;')
                              else
                                value.to_s.sub(';', '\;')
                              end]
  end

  str += "}\n"

  str
end

#typeObject

The type of object we are.



309
310
311
# File 'core_modules/nagios_core/lib/puppet/external/nagios/base.rb', line 309

def type
  self.class.name
end