[project @ peter@palfrader.org-20090329163538-9ilfxhn9e6tk3jbt]
[mirror/dsa-nagios.git] / build-nagios
index e7bc540..6395e80 100755 (executable)
@@ -1,6 +1,28 @@
 #!/usr/bin/ruby
 
-# Copyright (c) 2004, 2005, 2006, 2007, 2008 Peter Palfrader <peter@palfrader.org>
+# build nagios and nrpe config from a single master config file
+
+# Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009 Peter Palfrader <peter@palfrader.org>
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
 
 require "yaml"
 
@@ -117,6 +139,15 @@ def print_block(fd, kind, hash, exclude_keys)
        fd.puts
 end
 
+def merge_contacts(host, service)
+       %w{contacts contact_groups}.each{ |k|
+               contacts = []
+               [host, service].each{ |source|
+                       contacts.push source[k] if source.has_key?(k)
+               }
+               service[k] = contacts.join(",") unless contacts.empty?
+       }
+end
 
 # Add the service definition service to hosts
 # f is the file for service definitions, deps the file for dependencies
@@ -141,9 +172,7 @@ def addService(hosts, service, files, servers)
        hosts.each{ |host|
                s = service.clone
                set_complain_if_set s, 'host_name', host, 'Service', s['service_description']
-               %w{contacts contact_groups}.each{ |k|
-                       set_if_unset(s, k, servers[host][k]) if servers[host].has_key? k
-               }
+               merge_contacts(servers[host], s)
 
                print_block files['services'], 'service', s, %w(nrpe runfrom remotecheck
                                                                depends
@@ -386,7 +415,10 @@ config['services'].each{ |service|
                        host_ip = config['servers'][host]['address']
                        throw "For some reason I do not have an address for #{host}.  This shouldn't be." unless host_ip
 
-                       check = $nrpe.add("#{host}_#{hostservice['service_description']}", hostservice['remotecheck'].gsub(/\$HOSTADDRESS\$/, host_ip))
+                       remotecheck = hostservice['remotecheck']
+                       remotecheck.gsub!(/\$HOSTADDRESS\$/, host_ip)
+                       remotecheck.gsub!(/\$HOSTNAME\$/, host)
+                       check = $nrpe.add("#{host}_#{hostservice['service_description']}", remotecheck)
                        hostservice['check_command'] = "#{NRPE_CHECKNAME_HOST}!#{ config['servers'][ relay ]['address'] }!#{ check }"
 
                        # Make sure dependencies are an array.  If there are none, create an empty array