Class: Nagios::Base
- Includes:
- Enumerable
- Defined in:
- 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
-
.att ⇒ Object
Returns the value of attribute att.
-
.derivatives ⇒ Object
Returns the value of attribute derivatives.
-
.ldapbase ⇒ Object
Returns the value of attribute ldapbase.
-
.name ⇒ Object
Returns the value of attribute name.
-
.namevar ⇒ Object
Return the namevar for the canonical name.
-
.ocs ⇒ Object
Returns the value of attribute ocs.
-
.parameters ⇒ Object
Returns the value of attribute parameters.
-
.superior ⇒ Object
readonly
Returns the value of attribute superior.
Class Method Summary collapse
-
.attach(hash) ⇒ Object
Attach one class to another.
-
.camelcase(param) ⇒ Object
Convert a parameter to camelcase.
-
.create(name, args = {}) ⇒ Object
Create a new instance of a given class.
-
.decamelcase(param) ⇒ Object
Uncamelcase a parameter.
-
.eachtype ⇒ Object
Yield each type in turn.
-
.map(hash) ⇒ Object
Create a mapping.
-
.mapping(name) ⇒ Object
Return a mapping (or nil) for a param.
-
.newtype(name, &block) ⇒ Object
Create a new type.
-
.paramattr(name) ⇒ Object
Define both the normal case and camelcase method for a parameter.
-
.parameter?(name) ⇒ Boolean
Is the specified name a valid parameter?.
-
.setnamevar(name) ⇒ Object
Manually set the namevar.
-
.setparameters(*array) ⇒ Object
Set the valid parameters for this class.
-
.setsuperior(name) ⇒ Object
Set the superior ldap object class.
-
.suppress(name) ⇒ Object
Parameters to suppress in output.
-
.suppress?(name) ⇒ Boolean
Whether a given parameter is suppressed.
-
.to_s ⇒ Object
Return our name as the string.
-
.type(name) ⇒ Object
Return a type by name.
Instance Method Summary collapse
-
#[](param) ⇒ Object
Convenience methods.
-
#[]=(param, value) ⇒ Object
Convenience methods.
-
#each ⇒ Object
Iterate across all ofour set parameters.
-
#initialize(args = {}) ⇒ Base
constructor
Initialize our object, optionally with a list of parameters.
-
#method_missing(mname, *args) ⇒ Object
Handle parameters like attributes.
-
#name ⇒ Object
Retrieve our name, through a bit of redirection.
-
#name=(value) ⇒ Object
This is probably a bad idea.
- #namevar ⇒ Object
- #parammap(param) ⇒ Object
- #parent ⇒ Object
-
#to_ldif ⇒ Object
okay, this sucks how do i get my list of ocs?.
- #to_s ⇒ Object
-
#type ⇒ Object
The type of object we are.
Methods included from Enumerable
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/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/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.first else return @parameters[mname] end else super end end |
Class Attribute Details
.att ⇒ Object
Returns the value of attribute att.
11 12 13 |
# File 'lib/puppet/external/nagios/base.rb', line 11 def att @att end |
.derivatives ⇒ Object
Returns the value of attribute derivatives.
11 12 13 |
# File 'lib/puppet/external/nagios/base.rb', line 11 def derivatives @derivatives end |
.ldapbase ⇒ Object
Returns the value of attribute ldapbase.
12 13 14 |
# File 'lib/puppet/external/nagios/base.rb', line 12 def ldapbase @ldapbase end |
.name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/puppet/external/nagios/base.rb', line 11 def name @name end |
.namevar ⇒ Object
Return the namevar for the canonical name.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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 |
.ocs ⇒ Object
Returns the value of attribute ocs.
11 12 13 |
# File 'lib/puppet/external/nagios/base.rb', line 11 def ocs @ocs end |
.parameters ⇒ Object
Returns the value of attribute parameters.
11 12 13 |
# File 'lib/puppet/external/nagios/base.rb', line 11 def parameters @parameters end |
.superior ⇒ Object (readonly)
Returns the value of attribute superior.
16 17 18 |
# File 'lib/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/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/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/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/puppet/external/nagios/base.rb', line 33 def self.decamelcase(param) param.gsub(/[A-Z]/) do |match| "_#{match.downcase}" end end |
.eachtype ⇒ Object
Yield each type in turn.
51 52 53 54 55 |
# File 'lib/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/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/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/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/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?
130 131 132 133 |
# 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) ⇒ Object
Manually set the namevar
136 137 138 139 |
# 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) ⇒ Object
Set the valid parameters for this class
142 143 144 |
# File 'lib/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/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/puppet/external/nagios/base.rb', line 153 def self.suppress(name) @suppress ||= [] @suppress << name end |
.suppress?(name) ⇒ Boolean
Whether a given parameter is suppressed.
159 160 161 |
# File 'lib/puppet/external/nagios/base.rb', line 159 def self.suppress?(name) defined?(@suppress) and @suppress.include?(name) end |
.to_s ⇒ Object
Return our name as the string.
164 165 166 |
# File 'lib/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/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/puppet/external/nagios/base.rb', line 176 def [](param) send(param) end |
#[]=(param, value) ⇒ Object
Convenience methods.
181 182 183 |
# File 'lib/puppet/external/nagios/base.rb', line 181 def []=(param,value) send(param.to_s + "=", value) end |
#each ⇒ Object
Iterate across all ofour set parameters.
186 187 188 189 190 |
# File 'lib/puppet/external/nagios/base.rb', line 186 def each @parameters.each { |param,value| yield(param,value) } end |
#name ⇒ Object
Retrieve our name, through a bit of redirection.
228 229 230 |
# File 'lib/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/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 ⇒ Object
239 240 241 |
# File 'lib/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/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 ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 |
# 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 ⇒ Object
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 |
# File 'lib/puppet/external/nagios/base.rb', line 273 def to_ldif 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 { |objclass| str += "objectclass: #{objclass}\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 ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/puppet/external/nagios/base.rb', line 297 def to_s str = "define #{self.type} {\n" @parameters.keys.sort.each { |param| value = @parameters[param] str += %{\t%-30s %s\n} % [ param, if value.is_a? Array value.join(",").sub(';', '\;') else value.to_s.sub(';', '\;') end ] } str += "}\n" str end |
#type ⇒ Object
The type of object we are.
317 318 319 |
# File 'lib/puppet/external/nagios/base.rb', line 317 def type self.class.name end |