1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
# The base class for all of our Nagios object types. Everything else
# is mostly just data.
class Nagios::Base
class UnknownNagiosType < RuntimeError # When an unknown type is asked for by name.
end
include Enumerable
class << self
attr_accessor :parameters, :derivatives, :ocs, :name, :att
attr_accessor :ldapbase
attr_writer :namevar
attr_reader :superior
end
# Attach one class to another.
def self.attach(hash)
@attach ||= {}
hash.each do |n, v| @attach[n] = v end
end
# Convert a parameter to camelcase
def self.camelcase(param)
param.gsub(/_./) do |match|
match.sub(/_/,'').capitalize
end
end
# Uncamelcase a parameter.
def self.decamelcase(param)
param.gsub(/[A-Z]/) do |match|
"_#{match.downcase}"
end
end
# Create a new instance of a given class.
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
# Yield each type in turn.
def self.eachtype
@types.each do |name, type|
yield [name, type]
end
end
# Create a mapping.
def self.map(hash)
@map ||= {}
hash.each do |n, v| @map[n] = v end
end
# Return a mapping (or nil) for a param
def self.mapping(name)
name = name.intern if name.is_a? String
if defined?(@map)
@map[name]
else
nil
end
end
# Return the namevar for the canonical name.
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
# Create a new type.
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
# Define both the normal case and camelcase method for a parameter
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
# Is the specified name a valid parameter?
def self.parameter?(name)
name = name.intern if name.is_a? String
@parameters.include?(name)
end
# Manually set the namevar
def self.setnamevar(name)
name = name.intern if name.is_a? String
@namevar = name
end
# Set the valid parameters for this class
def self.setparameters(*array)
@parameters += array
end
# Set the superior ldap object class. Seems silly to include this
# in this class, but, eh.
def self.setsuperior(name)
@superior = name
end
# Parameters to suppress in output.
def self.suppress(name)
@suppress ||= []
@suppress << name
end
# Whether a given parameter is suppressed.
def self.suppress?(name)
defined?(@suppress) and @suppress.include?(name)
end
# Return our name as the string.
def self.to_s
self.name.to_s
end
# Return a type by name.
def self.type(name)
name = name.intern if name.is_a? String
@types[name]
end
# Convenience methods.
def [](param)
send(param)
end
# Convenience methods.
def []=(param,value)
send(param.to_s + "=", value)
end
# Iterate across all ofour set parameters.
def each
@parameters.each { |param,value|
yield(param,value)
}
end
# Initialize our object, optionally with a list of parameters.
def initialize(args = {})
@parameters = {}
args.each { |param,value|
self[param] = value
}
if @namevar == :_naginator_name
self['_naginator_name'] = self['name']
end
end
# Handle parameters like attributes.
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
# Retrieve our name, through a bit of redirection.
def name
send(self.class.namevar)
end
# This is probably a bad idea.
def name=(value)
unless self.class.namevar.to_s == "name"
send(self.class.namevar.to_s + "=", value)
end
end
def namevar
(self.type + "_name").intern
end
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
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
# okay, this sucks
# how do i get my list of ocs?
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
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
# The type of object we are.
def type
self.class.name
end
# object types
newtype :host do
setparameters :host_name, :alias, :display_name, :address, :parents,
:hostgroups, :check_command, :initial_state, :max_check_attempts,
:check_interval, :retry_interval, :active_checks_enabled,
:passive_checks_enabled, :check_period, :obsess_over_host,
:check_freshness, :freshness_threshold, :event_handler,
:event_handler_enabled, :low_flap_threshold, :high_flap_threshold,
:flap_detection_enabled, :flap_detection_options,
:failure_prediction_enabled, :process_perf_data,
:retain_status_information, :retain_nonstatus_information, :contacts,
:contact_groups, :notification_interval, :first_notification_delay,
:notification_period, :notification_options, :notifications_enabled,
:stalking_options, :notes, :notes_url, :action_url, :icon_image,
:icon_image_alt, :vrml_image, :statusmap_image, "2d_coords".intern,
"3d_coords".intern,
:register, :use
setsuperior "person"
map :address => "ipHostNumber"
end
newtype :hostgroup do
setparameters :hostgroup_name, :alias, :members, :hostgroup_members, :notes,
:notes_url, :action_url,
:register, :use
end
newtype :service do
attach :host => :host_name
setparameters :host_name, :hostgroup_name, :service_description,
:display_name, :servicegroups, :is_volatile, :check_command,
:initial_state, :max_check_attempts, :check_interval, :retry_interval,
:normal_check_interval, :retry_check_interval, :active_checks_enabled,
:passive_checks_enabled, :parallelize_check, :check_period,
:obsess_over_service, :check_freshness, :freshness_threshold,
:event_handler, :event_handler_enabled, :low_flap_threshold,
:high_flap_threshold, :flap_detection_enabled,:flap_detection_options,
:process_perf_data, :failure_prediction_enabled, :retain_status_information,
:retain_nonstatus_information, :notification_interval,
:first_notification_delay, :notification_period, :notification_options,
:notifications_enabled, :contacts, :contact_groups, :stalking_options,
:notes, :notes_url, :action_url, :icon_image, :icon_image_alt,
:register, :use,
:_naginator_name
suppress :host_name
setnamevar :_naginator_name
end
newtype :servicegroup do
setparameters :servicegroup_name, :alias, :members, :servicegroup_members,
:notes, :notes_url, :action_url,
:register, :use
end
newtype :contact do
setparameters :contact_name, :alias, :contactgroups,
:host_notifications_enabled, :service_notifications_enabled,
:host_notification_period, :service_notification_period,
:host_notification_options, :service_notification_options,
:host_notification_commands, :service_notification_commands,
:email, :pager, :address1, :address2, :address3, :address4,
:address5, :address6, :can_submit_commands, :retain_status_information,
:retain_nonstatus_information,
:register, :use
setsuperior "person"
end
newtype :contactgroup do
setparameters :contactgroup_name, :alias, :members, :contactgroup_members,
:register, :use
end
# TODO - We should support generic time periods here eg "day 1 - 15"
newtype :timeperiod do
setparameters :timeperiod_name, :alias, :sunday, :monday, :tuesday,
:wednesday, :thursday, :friday, :saturday, :exclude,
:register, :use
end
newtype :command do
setparameters :command_name, :command_line
end
newtype :servicedependency do
auxiliary = true
setparameters :dependent_host_name, :dependent_hostgroup_name,
:dependent_service_description, :host_name, :hostgroup_name,
:service_description, :inherits_parent, :execution_failure_criteria,
:notification_failure_criteria, :dependency_period,
:register, :use,
:_naginator_name
setnamevar :_naginator_name
end
newtype :serviceescalation do
setparameters :host_name, :hostgroup_name, :servicegroup_name,
:service_description, :contacts, :contact_groups,
:first_notification, :last_notification, :notification_interval,
:escalation_period, :escalation_options,
:register, :use,
:_naginator_name
setnamevar :_naginator_name
end
newtype :hostdependency do
auxiliary = true
setparameters :dependent_host_name, :dependent_hostgroup_name, :host_name,
:hostgroup_name, :inherits_parent, :execution_failure_criteria,
:notification_failure_criteria, :dependency_period,
:register, :use,
:_naginator_name
setnamevar :_naginator_name
end
newtype :hostescalation do
setparameters :host_name, :hostgroup_name, :contacts, :contact_groups,
:first_notification, :last_notification, :notification_interval,
:escalation_period, :escalation_options,
:register, :use,
:_naginator_name
setnamevar :_naginator_name
end
newtype :hostextinfo do
auxiliary = true
setparameters :host_name, :notes, :notes_url, :icon_image, :icon_image_alt,
:vrml_image, :statusmap_image, "2d_coords".intern, "3d_coords".intern,
:register, :use
setnamevar :host_name
end
newtype :serviceextinfo do
auxiliary = true
setparameters :host_name, :service_description, :notes, :notes_url,
:action_url, :icon_image, :icon_image_alt,
:register, :use,
:_naginator_name
setnamevar :_naginator_name
end
end
|