X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=build-nagios;h=8475da8200f75b512c019e69c95108b83c3c46ca;hb=34816cfa599726fb4d7c33e02091a14b22069312;hp=9e2fc48e3ede0960c4601382903eff52cdef05bb;hpb=50504670d6b7bf259a143ba1413e90e8e257210c;p=mirror%2Fdsa-nagios.git diff --git a/build-nagios b/build-nagios index 9e2fc48..8475da8 100755 --- a/build-nagios +++ b/build-nagios @@ -4,13 +4,12 @@ require "yaml" -ORG="debianorg" -SHORTORG="do" -CONTACTGROUP="debianadmin" -GENERATED_PREFIX="/etc/NOREPLY/generated/nagios/" +ORG="dsa" +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" @@ -118,13 +117,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 @@ -140,9 +147,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| @@ -245,8 +258,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) @@ -271,6 +286,8 @@ config['servers'].each_pair{ |name, server| }; } +config['servicegroups'] = {} unless config.has_key? 'servicegroups' + ############## # HOSTS ############## @@ -293,12 +310,17 @@ config['servers'].each_pair{ |name, server| # Handle hostextinfo - config['hostgroups'][ server['_hostgroups'].first ]['_extinfo'].each_pair{ |k, v| - # substitute hostname into the notes_url - v = sprintf(v,name) if k == 'notes_url' - - set_if_unset server['_extinfo'], k ,v - } + #config['hostgroups'][ server['_hostgroups'].first ]['_extinfo'].each_pair{ |k, v| + # find the first hostgroup that has extinfo + extinfo = server['_hostgroups'].collect{ |hgname | config['hostgroups'][hgname]['_extinfo'] }.delete_if{ |ei| ei.size == 0 }.first + if extinfo then + extinfo.each_pair do |k, v| + # substitute hostname into the notes_url + v = sprintf(v,name) if k == 'notes_url' + + set_if_unset server['_extinfo'], k ,v + end + end set_complain_if_set server['_extinfo'], 'host_name' , name, 'hostextinfo', name set_if_unset server['_extinfo'], 'vrml_image' , server['_extinfo']['icon_image'] if server['_extinfo'].has_key?('icon_image') @@ -317,7 +339,6 @@ config['hostgroups'].each_pair{ |name, hg| set_complain_if_set hg, 'hostgroup_name', name , 'Hostgroup', name set_complain_if_set hg, 'members' , hg['_memberlist'].join(","), 'Hostgroup', name - set_if_unset hg, 'contact_groups', CONTACTGROUP print_block files['hostgroups'], 'hostgroup', hg, %w() } @@ -381,17 +402,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 ##############