Decommission agnesi (RT#3697)
[mirror/dsa-nagios.git] / config / build-nagios
1 #!/usr/bin/ruby
2
3 # build nagios and nrpe config from a single master config file
4
5 # Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Peter Palfrader <peter@palfrader.org>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
14 #
15 # The above copyright notice and this permission notice shall be
16 # included in all copies or substantial portions of the Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26
27 require "yaml"
28
29 ORG="dsa"
30 SHORTORG="dsa"
31 GENERATED_PREFIX="./generated/"
32
33 nagios_filename = {};
34 %w(hosts hostgroups services dependencies hostextinfo serviceextinfo servicegroups).each{
35         |x| nagios_filename[x] = GENERATED_PREFIX+"auto-#{x}.cfg"
36 }
37 nagios_filename['nrpe'] = GENERATED_PREFIX+"nrpe_#{ ORG }.cfg"
38
39
40 MAX_CHECK_ATTEMPTS_DEFAULT=6
41
42 NRPE_CHECKNAME="#{ ORG }_check_nrpe"           # check that takes one argument:  service name to be checked
43 NRPE_CHECKNAME_HOST="#{ ORG }_check_nrpe_host" # check that takes two arguments: relay host on which to run check, service name to be checked
44
45 HOST_TEMPLATE_NAME='generic-host'          # host templates that all our host definitions use
46 SERVICE_TEMPLATE_NAME='generic-service'    # host templates that all our host definitions use
47 HOST_ALIVE_CHECK='check-host-alive'        # host alive check if server is pingable
48 NRPE_PROCESS_SERVICE='process - nrpe'      # nrpe checks will depend on this one
49
50
51 def warn (msg)
52         STDERR.puts msg
53 end
54 def set_if_unset(hash, key, value)
55         hash[key] = value unless hash.has_key?(key)
56 end
57 def set_complain_if_set(hash, key, value, type, name)
58         throw "#{type} definition '#{name}' has '#{key}' already defined" if hash.has_key?(key)
59         hash[key] = value
60 end
61
62 # Make an array out of something.  If there is nothing, create an empty array
63 # if it is just a string, make a list with just that element, if it already is
64 # an array keep it.
65 def ensure_array(something)
66         if (something == nil)
67                 result = []
68         elsif something.kind_of?(String)
69                 result = [ something ]
70         elsif something.kind_of?(Array)
71                 result = something
72         else
73                 throw "Do now know how to make an array out of #{something}: " + something.to_yaml
74         end
75         return result
76 end
77
78
79 # This class keeps track of the checks done via NRPE and makes sure
80 # each gets a unique name.
81 #
82 # Unforutunately NRPE limits check names to some 30 characters, so
83 # we need to mangle service names near the end.
84 class Nrpe
85         def initialize
86                 @checks = {}
87         end
88
89         def make_name( name, check )
90                 name = name.tr_s("^a-zA-Z", "_").gsub("process", "ps")
91
92                 result = "#{ SHORTORG }_" + name[0,19]
93
94                 hash = ''
95                 skew = ''
96                 while (@checks.has_key?(result + hash))
97                         # hash it, so that we don't lose uniqeness by cutting it off
98                         hash = (check+skew).crypt("$1$")
99                         hash = hash[-5,5]  # 5 chars are enough
100                         hash.tr!("/", "_")
101                         skew += ' ' # change it a bit so the hash changes
102                 end
103                 result += hash
104                 return result      # max of 32 or so chars
105         end
106
107         def add( name, check )
108                 if @checks.has_value? check
109                         @checks.each_pair{ |key, value|
110                                 return key if value == check
111                         }
112                 end
113                 key = make_name(name, check)
114                 @checks[ key ] = check
115                 return key
116         end
117
118         def checks
119                 return @checks
120         end
121 end
122 $nrpe = Nrpe.new()
123
124
125 # Prints the keys and values of hash to a file
126 # This is the function that prints the bodies of most our
127 # host/service/etc definitions
128 #
129 # It skips over such keys as are listed in exclude_keys
130 # and also skips private keys (those starting with an underscore)
131 def print_block(fd, kind, hash, exclude_keys)
132         fd.puts "define #{kind} {"
133         hash.each_pair{ |key, value|
134                 next if key == '_extinfo'
135                 next if key == '_hostgroups'
136                 next if key == '_memberlist'
137                 next if exclude_keys.include? key
138                 fd.puts "       #{key}          #{value}"
139         }
140         fd.puts "}"
141         fd.puts
142 end
143
144 def merge_contacts(host, service)
145         %w{contacts contact_groups}.each{ |k|
146                 contacts = []
147                 [host, service].each{ |source|
148                         contacts.push source[k] if source.has_key?(k)
149                 }
150                 service[k] = contacts.join(",") unless contacts.empty?
151         }
152 end
153
154 # Add the service definition service to hosts
155 # f is the file for service definitions, deps the file for dependencies
156 def addService(hosts, service, files, servers)
157
158         set_if_unset        service, 'use'               , SERVICE_TEMPLATE_NAME
159         set_if_unset        service, 'max_check_attempts', MAX_CHECK_ATTEMPTS_DEFAULT
160
161         service['max_check_attempts'] = MAX_CHECK_ATTEMPTS_DEFAULT + service['max_check_attempts'] if service['max_check_attempts'] < 0
162
163         if service['nrpe']
164                 throw "We already have a check_command (#{service['check_command']}) but we are in the NRPE block (nrpe: #{service['nrpe']})."+
165                         "  This should have been caught much earlier" if service.has_key?('check_command');
166
167                 check = $nrpe.add(service['service_description'], service['nrpe'])
168                 service['check_command'] = "#{ NRPE_CHECKNAME }!#{ check }"
169
170                 service['depends'] = ensure_array( service['depends'] )
171                 service['depends'] << NRPE_PROCESS_SERVICE unless service['service_description'] == NRPE_PROCESS_SERVICE  # Depend on NRPE unless we are it
172         end
173
174         hosts.each{ |host|
175                 s = service.clone
176                 set_complain_if_set s, 'host_name', host, 'Service', s['service_description']
177                 merge_contacts(servers[host], s)
178
179                 print_block files['services'], 'service', s, %w(nrpe runfrom remotecheck
180                                                                 depends
181                                                                 hosts hostgroups excludehosts excludehostgroups)
182         }
183
184         if service['depends']
185                 service['depends'].each{ |prerequisite|
186                         hosts.each{ |host|
187                                 prerequisite_host = host
188                                 pre = prerequisite
189                                 # split off a hostname if there's one
190                                 bananasplit = prerequisite.split(':')
191                                 if bananasplit.size == 2
192                                         prerequisite_host = bananasplit[0]
193                                         pre = bananasplit[1]
194                                 elsif bananasplit.size > 2
195                                         throw "Cannot prase prerequisite #{prerequisite} for service #{service['service_description']} into host:service"
196                                 end
197                                 dependency = {
198                                         'host_name'                     => prerequisite_host,
199                                         'service_description'           => pre,
200                                         'dependent_host_name'           => host,
201                                         'dependent_service_description' => service['service_description'],
202                                         'execution_failure_criteria'    => 'n',
203                                         'notification_failure_criteria' => 'w,u,c'
204                                 };
205                                 print_block files['dependencies'], 'servicedependency', dependency, %w()
206                         }
207                 }
208         end
209
210
211         set_complain_if_set service['_extinfo'], 'service_description' , service['service_description'], 'serviceextinfo', service['service_description']
212         set_complain_if_set service['_extinfo'], 'host_name'           , hosts.join(',')               , 'serviceextinfo', service['service_description']
213
214         print_block files['serviceextinfo'], 'serviceextinfo', service['_extinfo'], %w()
215 end
216
217 # hostlists in services can be given as both, single hosts and hostgroups
218 # This functinn merges hostgroups and a simple list of hosts
219 #
220 # it also takes a prefix so that it can be used for excludelists as well
221 def merge_hosts_and_hostgroups(service, servers, hostgroups, prefix)
222         hosts = []
223         hosts = service[prefix+'hosts'].split(/,/).map{ |x| x.strip } if service[prefix+'hosts']
224         hosts.each{ |host|
225                 throw "host #{host} does not exist - used in service #{service['service_description']}" unless servers[host]
226         };
227         if service[prefix+'hostgroups']
228                 service[prefix+'hostgroups'].split(/,/).map{ |x| x.strip }.each{ |hg|
229                         throw "hostgroup #{hg} does not exist - used in service #{service['service_description']}" unless hostgroups[hg]
230                         hosts = hosts.concat hostgroups[hg]['_memberlist']
231                 }
232         end
233
234         return hosts
235 end
236
237 # Figure out the hosts a given service applies to
238 #
239 # For a given service find the list of hosts minus excluded hosts that this service runs on
240 def find_hosts(service, servers, hostgroups)
241         hosts        = merge_hosts_and_hostgroups service, servers, hostgroups, ''
242         excludehosts = merge_hosts_and_hostgroups service, servers, hostgroups, 'exclude'
243
244         excludehosts.uniq.each{ |host|
245                 if hosts.delete(host) == nil
246                         puts "info: [service #{service['service_description']}] not excluding host #{host} - it's not included anyway."
247                 end
248         }
249
250         return hosts
251 end
252
253 # Move all elements that have a key that starts with "extinfo-"
254 # into the _extinfo subhash
255 def split_away_extinfo(hash)
256         hash['_extinfo'] = {}
257         hash.keys.each{ |key|
258                 if key[0, 8] == 'extinfo-'
259                         hash['_extinfo'][ key[8, key.length-8] ] = hash[key]
260                         hash.delete(key);
261                 end
262         }
263 end
264
265
266 #############################################################################################
267 #############################################################################################
268 #############################################################################################
269
270 # Load the config
271 config = YAML::load( File.open( 'nagios-master.cfg' ) )
272
273 files = {}
274 # Remove old created files
275 nagios_filename.each_pair{ |name, filename|
276         files[name] = File.new(filename, "w")
277 }
278
279 #################################
280 # create a few hostgroups
281 #################################
282 # create the "all" and "pingable" hostgroups
283 config['hostgroups']['all'] = {}
284 config['hostgroups']['all']['alias'] = "all servers"
285 config['hostgroups']['all']['private'] = true
286 config['hostgroups']['pingable'] = {}
287 config['hostgroups']['pingable']['alias'] = "pingable servers"
288 config['hostgroups']['pingable']['private'] = true
289
290 config['hostgroups'].each_pair{ |name, hg|
291         throw "Empty hostgroup or hostgroup #{name} not a hash" unless hg.kind_of?(Hash)
292         split_away_extinfo hg
293
294         hg['_memberlist'] = []
295 }
296
297 config['servers'].each_pair{ |name, server|
298         throw "Empty server or server #{name} not a hash" unless server.kind_of?(Hash)
299
300         split_away_extinfo server
301
302         throw "No hostgroups defined for #{name}" unless server['hostgroups']
303         server['_hostgroups'] = server['hostgroups'].split(/,/).map{ |x| x.strip };
304         server['_hostgroups'] << 'all'
305         server['_hostgroups'] << 'pingable' unless server['pingable'] == false
306
307         server['_hostgroups'].each{ |hg|
308                 throw "Hostgroup #{hg} is not defined" unless config['hostgroups'].has_key?(hg)
309                 config['hostgroups'][hg]['_memberlist'] << name
310         };
311 }
312
313 config['servicegroups'] = {} unless config.has_key? 'servicegroups'
314
315 ##############
316 # HOSTS
317 ##############
318 config['servers'].each_pair{ |name, server|
319         # Formerly we used 'ip' instead of 'address' in our source file
320         # Handle this change but warn                                   XXX
321         if server.has_key?('ip')
322                 STDERR.puts("Host definition for #{name} has an 'ip' field.  Please use 'address' instead");
323                 server['address'] = server['ip'];
324                 server.delete('ip');
325         end
326
327         set_complain_if_set server, 'host_name'    , name, 'Host', name
328         set_if_unset        server, 'alias'        , name
329         set_if_unset        server, 'use'          , HOST_TEMPLATE_NAME
330         set_if_unset        server, 'check_command', HOST_ALIVE_CHECK    unless server['pingable'] == false
331
332         print_block files['hosts']      , 'host'       , server            , %w(hostgroups pingable)
333
334
335
336         # Handle hostextinfo
337         #config['hostgroups'][  server['_hostgroups'].first  ]['_extinfo'].each_pair{ |k, v|
338         # find the first hostgroup that has extinfo
339         extinfo = server['_hostgroups'].collect{ |hgname | config['hostgroups'][hgname]['_extinfo'] }.delete_if{ |ei| ei.size == 0 }.first
340         if extinfo then
341                 extinfo.each_pair do |k, v|
342                         # substitute hostname into the notes_url
343                         v = sprintf(v,name) if k == 'notes_url'
344
345                         set_if_unset server['_extinfo'], k ,v
346                 end
347         end
348
349         set_complain_if_set server['_extinfo'], 'host_name'       , name, 'hostextinfo', name
350         set_if_unset        server['_extinfo'], 'vrml_image'      , server['_extinfo']['icon_image'] if server['_extinfo'].has_key?('icon_image')
351         set_if_unset        server['_extinfo'], 'statusmap_image' , server['_extinfo']['icon_image'] if server['_extinfo'].has_key?('icon_image')
352
353         print_block files['hostextinfo'], 'hostextinfo', server['_extinfo'], %w()
354 }
355
356
357
358 ##############
359 # HOSTGROUPS
360 ##############
361 config['hostgroups'].each_pair{ |name, hg|
362         next if hg['private']
363         throw "Empty hostgroup #{name}" if hg['_memberlist'].length == 0
364
365         set_complain_if_set hg, 'hostgroup_name', name                       , 'Hostgroup', name
366         set_complain_if_set hg, 'members'       , hg['_memberlist'].join(","), 'Hostgroup', name
367
368         print_block files['hostgroups'], 'hostgroup', hg, %w()
369 }
370
371
372 ##############
373 # SERVICES and DEPENDENCIES
374 ##############
375 config['services'].each{ |service|
376         throw "Empty service or service not a hash" unless service.kind_of?(Hash)
377
378         split_away_extinfo service
379
380
381         # Both 'name' and 'service_description' are valid for a service's name
382         # Internally we only use service_description as that's nagios' official term
383         if service.has_key?('name')
384                 throw "Service definition has both a name (#{service['name']})" +
385                       "and a service_description (#{service['service_description']})" if service.has_key?('service_description')
386                 #STDERR.puts("Service definition #{service['name']} has a 'name' field.  Please use 'service_description' instead");
387                 service['service_description'] = service['name'];
388                 service.delete('name');
389         end
390         # Both 'check' and 'check_command' are valid for a service's check command
391         # Internally we only use check_command as that's nagios' official term
392         if service.has_key?('check')
393                 throw "Service definition has both a check (#{service['check']})" +
394                       "and a check_command (#{service['check_command']})" if service.has_key?('check_command')
395                 #STDERR.puts("Service definition #{service['service_description']} has a 'check' field.  Please use 'check_command' instead");
396                 service['check_command'] = service['check'];
397                 service.delete('check');
398         end
399
400
401         hosts = find_hosts service, config['servers'], config['hostgroups']
402         throw "no hosts for service #{service['service_description']}" if hosts.empty?
403
404         throw "nrpe, check, and remotecheck are mutually exclusive in service #{service['service_description']}" if 
405                 (service['nrpe'] ? 1 : 0) +
406                 (service['check_command'] ? 1 : 0) +
407                 (service['remotecheck'] ? 1 : 0)  >= 2
408
409         if service['runfrom'] && service['remotecheck']
410                 # If the service check is to be run from a remote monitor server ("relay")
411                 # add that as an NRPE check to be run on the relay and make this
412                 # service also depend on NRPE on the relay
413                 relay = service['runfrom']
414
415                 hosts.each{ |host|
416                         # how to recursively copy this thing?
417                         hostservice = YAML::load( service.to_yaml )
418                         host_ip = config['servers'][host]['address']
419
420                         remotecheck = hostservice['remotecheck']
421                         if remotecheck.include?('$HOSTADDRESS$') and not host_ip
422                                 throw "For some reason I do not have an address for #{host} yet I need it for the remote check.  This shouldn't be."
423                         end
424                         remotecheck.gsub!(/\$HOSTADDRESS\$/, host_ip) if host_ip
425                         remotecheck.gsub!(/\$HOSTNAME\$/, host)
426                         check = $nrpe.add("#{host}_#{hostservice['service_description']}", remotecheck)
427                         hostservice['check_command'] = "#{NRPE_CHECKNAME_HOST}!#{ config['servers'][ relay ]['address'] }!#{ check }"
428
429                         # Make sure dependencies are an array.  If there are none, create an empty array
430                         # if depends is just a string, make a list with just that element
431                         hostservice['depends'] = ensure_array( hostservice['depends'] )
432                         # And append this new dependency
433                         hostservice['depends'] << "#{ relay }:#{ NRPE_PROCESS_SERVICE }";
434
435                         addService( [ host ], hostservice, files, config['servers'])
436                 }
437         elsif service['runfrom'] || service['remotecheck']
438                 throw "runfrom and remotecheck must either appear both or not at all in service #{service['service_description']}"
439                 throw "must not remotecheck without runfrom" if service['remotecheck']
440         else
441                 addService(hosts, service, files, config['servers'])
442         end
443 }
444
445
446
447 ##############
448 # SERVICEGROUPS
449 ##############
450 config['servicegroups'].each_pair{ |name, sg|
451         set_complain_if_set sg, 'servicegroup_name', name                       , 'Servicegroup', name
452
453         print_block files['servicegroups'], 'servicegroup', sg, %w()
454 }
455
456
457 ##############
458 # NRPE config file
459 ##############
460 $nrpe.checks.each_pair{ |name, check|
461         files['nrpe'].puts "command[#{ name }]=#{ check }"
462 }
463
464