From 7becf6805a26955d75d7f0bb0251d2a7916c1119 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Wed, 12 Oct 2005 00:57:20 +0000 Subject: Support host and serviceextinfo git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@27 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- build-nagios | 118 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 76 insertions(+), 42 deletions(-) diff --git a/build-nagios b/build-nagios index 00691ad..5b534d2 100755 --- a/build-nagios +++ b/build-nagios @@ -9,11 +9,12 @@ SHORTORG="rela" CONTACTGROUP="weaselgroup" GENERATED_PREFIX="/etc/NOREPLY/generated/nagios/" -OUT_NRPE_CONFFILE = GENERATED_PREFIX+"nrpe_#{ ORG }.cfg" -OUT_AUTO_HOSTS = GENERATED_PREFIX+"auto-hosts.cfg" -OUT_AUTO_HOSTGROUPS = GENERATED_PREFIX+"auto-hostgroups.cfg" -OUT_AUTO_SERVICES = GENERATED_PREFIX+"auto-services.cfg" -OUT_AUTO_DEPENDENCIES = GENERATED_PREFIX+"auto-dependencies.cfg" +nagios_filename = {}; +%w(hosts hostgroups services dependencies hostextinfo serviceextinfo).each{ + |x| nagios_filename[x] = GENERATED_PREFIX+"auto-#{x}.cfg" +} +nagios_filename['nrpe'] = GENERATED_PREFIX+"nrpe_#{ ORG }.cfg" + MAX_CHECK_ATTEMPTS_DEFAULT=6 @@ -120,7 +121,7 @@ end # Add the service definition service to hosts # f is the file for service definitions, deps the file for dependencies -def addService(hosts, service, f, deps) +def addService(hosts, service, files) set_if_unset service, 'use' , SERVICE_TEMPLATE_NAME set_complain_if_set service, 'host_name' , hosts.join(',') , 'Service', service['service_description'] @@ -139,9 +140,9 @@ def addService(hosts, service, f, deps) service['depends'] << NRPE_PROCESS_SERVICE unless service['service_description'] == NRPE_PROCESS_SERVICE # Depend on NRPE unless we are it end - print_block f, 'service', service, %w(nrpe runfrom remotecheck - depends - hosts hostgroups excludehosts excludehostgroups) + print_block files['services'], 'service', service, %w(nrpe runfrom remotecheck + depends + hosts hostgroups excludehosts excludehostgroups) if service['depends'] service['depends'].each{ |prerequisite| @@ -164,10 +165,16 @@ def addService(hosts, service, f, deps) 'execution_failure_criteria' => 'n', 'notification_failure_criteria' => 'w,u,c' }; - print_block deps, 'servicedependency', dependency, %w() + print_block files['dependencies'], 'servicedependency', dependency, %w() } } end + + + set_complain_if_set service['_extinfo'], 'service_description' , service['service_description'], 'serviceextinfo', service['service_description'] + set_complain_if_set service['_extinfo'], 'host_name' , hosts.join(',') , 'serviceextinfo', service['service_description'] + + print_block files['serviceextinfo'], 'serviceextinfo', service['_extinfo'], %w() end # hostlists in services can be given as both, single hosts and hostgroups @@ -176,12 +183,12 @@ end # it also takes a prefix so that it can be used for excludelists as well def merge_hosts_and_hostgroups(service, servers, hostgroups, prefix) hosts = [] - hosts = service[prefix+'hosts'].delete(" \t").split(/,/) if service[prefix+'hosts'] + hosts = service[prefix+'hosts'].split(/,/).map{ |x| x.strip } if service[prefix+'hosts'] hosts.each{ |host| throw "host #{host} does not exist - used in service #{service['service_description']}" unless servers[host] }; if service[prefix+'hostgroups'] - service[prefix+'hostgroups'].delete(" \t").split(/,/).each{ |hg| + service[prefix+'hostgroups'].split(/,/).map{ |x| x.strip }.each{ |hg| throw "hostgroup #{hg} does not exist - used in service #{service['service_description']}" unless hostgroups[hg] hosts = hosts.concat hostgroups[hg]['_memberlist'] } @@ -206,6 +213,19 @@ def find_hosts(service, servers, hostgroups) return hosts end +# Move all elements that have a key that starts with "extinfo-" +# into the _extinfo subhash +def split_away_extinfo(hash) + hash['_extinfo'] = {} + hash.keys.each{ |key| + if key[0, 8] == 'extinfo-' + hash['_extinfo'][ key[8, key.length-8] ] = hash[key] + hash.delete(key); + end + } +end + + ############################################################################################# ############################################################################################# ############################################################################################# @@ -213,46 +233,44 @@ end # Load the config config = YAML::load( File.open( 'nagios-master.cfg' ) ) +files = {} # Remove old created files -[ OUT_AUTO_HOSTS, OUT_AUTO_HOSTGROUPS, OUT_AUTO_SERVICES, OUT_AUTO_DEPENDENCIES, OUT_NRPE_CONFFILE ].each{ |file| - File.delete(file) if File.exist?(file) +nagios_filename.each_pair{ |name, filename| + files[name] = File.new(filename, "w") } -f_hosts = File.new(OUT_AUTO_HOSTS, "w") -f_hostgroups = File.new(OUT_AUTO_HOSTGROUPS, "w") -f_services = File.new(OUT_AUTO_SERVICES, "w") -f_dependencies = File.new(OUT_AUTO_DEPENDENCIES, "w") -f_nrpe = File.new(OUT_NRPE_CONFFILE, "w") - ################################# # create a few hostgroups ################################# -config['hostgroups'].each_value{ |hg| +# create the "all" and "pingable" hostgroups +config['hostgroups']['all'] = {} +config['hostgroups']['all']['alias'] = "all servers" +config['hostgroups']['pingable'] = {} +config['hostgroups']['pingable']['alias'] = "pingable servers" + +config['hostgroups'].each_pair{ |name, hg| + throw "Empty hostgroup or hostgroup #{name} not a hash" unless hg.kind_of?(Hash) + split_away_extinfo hg + hg['_memberlist'] = [] } config['servers'].each_pair{ |name, server| + throw "Empty server or server #{name} not a hash" unless server.kind_of?(Hash) + + split_away_extinfo server + throw "No hostgroups defined for #{name}" unless server['hostgroups'] - server['hostgroups'].split(/,/).each{ |hg| - hg.strip! + server['_hostgroups'] = server['hostgroups'].split(/,/).map{ |x| x.strip }; + server['_hostgroups'] << 'all' + server['_hostgroups'] << 'pingable' unless server['pingable'] == false + + server['_hostgroups'].each{ |hg| throw "Hostgroup #{hg} is not defined" unless config['hostgroups'].has_key?(hg) config['hostgroups'][hg]['_memberlist'] << name }; } -# create the "all" and "pingable" hostgroups -config['hostgroups']['all'] = {} -config['hostgroups']['all']['alias'] = "all servers" -config['hostgroups']['all']['_memberlist'] = [] -config['hostgroups']['pingable'] = {} -config['hostgroups']['pingable']['alias'] = "pingable servers" -config['hostgroups']['pingable']['_memberlist'] = [] -config['servers'].each_key{ |name| - config['hostgroups']['all']['_memberlist'] << name - config['hostgroups']['pingable']['_memberlist'] << name unless (config['servers'][name]['pingable'] == false) -} - - ############## # HOSTS ############## @@ -265,13 +283,25 @@ config['servers'].each_pair{ |name, server| server.delete('ip'); end - set_complain_if_set server, 'host_name' , name, 'Host', name set_if_unset server, 'alias' , name set_if_unset server, 'use' , HOST_TEMPLATE_NAME set_if_unset server, 'check_command', HOST_ALIVE_CHECK unless server['pingable'] == false - print_block f_hosts, 'host', server, %w(hostgroups pingable) + print_block files['hosts'] , 'host' , server , %w(hostgroups pingable) + + + + # Handle hostextinfo + config['hostgroups'][ server['_hostgroups'].first ]['_extinfo'].each_pair{ |k, v| + set_if_unset server['_extinfo'], k ,v + } + + set_complain_if_set server['_extinfo'], 'host_name' , name, 'hostextinfo', name + set_if_unset server['_extinfo'], 'vrml_image' , server['_extinfo']['icon'] if server['_extinfo'].has_key?('icon') + set_if_unset server['_extinfo'], 'statusmap_image' , server['_extinfo']['icon'] if server['_extinfo'].has_key?('icon') + + print_block files['hostextinfo'], 'hostextinfo', server['_extinfo'], %w() } @@ -286,7 +316,7 @@ config['hostgroups'].each_pair{ |name, hg| set_complain_if_set hg, 'members' , hg['_memberlist'].join(","), 'Hostgroup', name set_if_unset hg, 'contact_groups', CONTACTGROUP - print_block f_hostgroups, 'hostgroup', hg, %w() + print_block files['hostgroups'], 'hostgroup', hg, %w() } @@ -295,6 +325,10 @@ config['hostgroups'].each_pair{ |name, hg| ############## config['services'].each{ |service| throw "Empty service or service not a hash" unless service.kind_of?(Hash) + + split_away_extinfo service + + # Both 'name' and 'service_description' are valid for a service's name # Internally we only use service_description as that's nagios' official term if service.has_key?('name') @@ -343,13 +377,13 @@ config['services'].each{ |service| # And append this new dependency service['depends'] << "#{ relay }:#{ NRPE_PROCESS_SERVICE }"; - addService( [ host ], service, f_services, f_dependencies) + addService( [ host ], service, files) } 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, f_services, f_dependencies) + addService(hosts, service, files) end } @@ -358,7 +392,7 @@ config['services'].each{ |service| # NRPE config file ############## $nrpe.checks.each_pair{ |name, check| - f_nrpe.puts "command[#{ name }]=#{ check }" + files['nrpe'].puts "command[#{ name }]=#{ check }" } -- cgit v1.2.3