X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=build-nagios;h=6395e803283232d86f3d031c383422f5c7392919;hb=0bdfaad3366c22fdf69d3695e9fc73e28a2278e6;hp=3e3e09eaa46009fd05071cdab3fd9fe897102aaa;hpb=a46df3797d1eb579701f86cb294261ef61212541;p=mirror%2Fdsa-nagios.git diff --git a/build-nagios b/build-nagios index 3e3e09e..6395e80 100755 --- a/build-nagios +++ b/build-nagios @@ -1,6 +1,28 @@ #!/usr/bin/ruby -# Copyright (c) 2004, 2005, 2006, 2007, 2008 Peter Palfrader +# build nagios and nrpe config from a single master config file + +# Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009 Peter Palfrader +# +# 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" @@ -9,7 +31,7 @@ SHORTORG="dsa" GENERATED_PREFIX="./generated/" nagios_filename = {}; -%w(hosts hostgroups services dependencies hostextinfo serviceextinfo).each{ +%w(hosts hostgroups services dependencies hostextinfo serviceextinfo servicegroups).each{ |x| nagios_filename[x] = GENERATED_PREFIX+"auto-#{x}.cfg" } nagios_filename['nrpe'] = GENERATED_PREFIX+"nrpe_#{ ORG }.cfg" @@ -117,13 +139,21 @@ 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 -def addService(hosts, service, files) +def addService(hosts, service, files, servers) set_if_unset service, 'use' , SERVICE_TEMPLATE_NAME - set_complain_if_set service, 'host_name' , hosts.join(',') , 'Service', service['service_description'] set_if_unset service, 'max_check_attempts', MAX_CHECK_ATTEMPTS_DEFAULT service['max_check_attempts'] = MAX_CHECK_ATTEMPTS_DEFAULT + service['max_check_attempts'] if service['max_check_attempts'] < 0 @@ -139,9 +169,15 @@ def addService(hosts, service, files) service['depends'] << NRPE_PROCESS_SERVICE unless service['service_description'] == NRPE_PROCESS_SERVICE # Depend on NRPE unless we are it end - print_block files['services'], 'service', service, %w(nrpe runfrom remotecheck - depends - hosts hostgroups excludehosts excludehostgroups) + hosts.each{ |host| + s = service.clone + set_complain_if_set s, 'host_name', host, 'Service', s['service_description'] + merge_contacts(servers[host], s) + + print_block files['services'], 'service', s, %w(nrpe runfrom remotecheck + depends + hosts hostgroups excludehosts excludehostgroups) + } if service['depends'] service['depends'].each{ |prerequisite| @@ -244,8 +280,10 @@ nagios_filename.each_pair{ |name, filename| # create the "all" and "pingable" hostgroups config['hostgroups']['all'] = {} config['hostgroups']['all']['alias'] = "all servers" +config['hostgroups']['all']['private'] = true config['hostgroups']['pingable'] = {} config['hostgroups']['pingable']['alias'] = "pingable servers" +config['hostgroups']['pingable']['private'] = true config['hostgroups'].each_pair{ |name, hg| throw "Empty hostgroup or hostgroup #{name} not a hash" unless hg.kind_of?(Hash) @@ -270,6 +308,8 @@ config['servers'].each_pair{ |name, server| }; } +config['servicegroups'] = {} unless config.has_key? 'servicegroups' + ############## # HOSTS ############## @@ -375,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 @@ -384,17 +427,28 @@ config['services'].each{ |service| # And append this new dependency hostservice['depends'] << "#{ relay }:#{ NRPE_PROCESS_SERVICE }"; - addService( [ host ], hostservice, files) + addService( [ host ], hostservice, files, config['servers']) } elsif service['runfrom'] || service['remotecheck'] throw "runfrom and remotecheck must either appear both or not at all in service #{service['service_description']}" throw "must not remotecheck without runfrom" if service['remotecheck'] else - addService(hosts, service, files) + addService(hosts, service, files, config['servers']) end } + +############## +# SERVICEGROUPS +############## +config['servicegroups'].each_pair{ |name, sg| + set_complain_if_set sg, 'servicegroup_name', name , 'Servicegroup', name + + print_block files['servicegroups'], 'servicegroup', sg, %w() +} + + ############## # NRPE config file ##############