From 85463c14d15a5b09b7467b667ba1661e0716b000 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Fri, 2 Sep 2005 00:24:30 +0000 Subject: Add build-nagios git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@3 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- build-nagios | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100755 build-nagios diff --git a/build-nagios b/build-nagios new file mode 100755 index 0000000..2d2c74b --- /dev/null +++ b/build-nagios @@ -0,0 +1,227 @@ +#!/usr/bin/ruby + +# Copyright (c) 2004, 2005 Peter Palfrader + +require "yaml" + +ORG="came" +CONTACTGROUP="cameadmins" + +class Nrpe + def initialize + @checks = {} + end + + def make_name( name, check ) + name = name.tr_s("^a-zA-Z", "_").gsub("process", "ps") + + result = "#{ ORG }_" + name[0,19] + + hash = '' + skew = '' + while (@checks.has_key?(result + hash)) + # hash it, so that we don't lose uniqeness by cutting it off + hash = (check+skew).crypt("$1$") + hash = hash[-5,5] # 5 chars are enough + hash.tr!("/", "_") + skew += ' ' # change it a bit so the hash changes + end + result += hash + return result # max of 32 or so chars + end + + def add( name, check ) + if @checks.has_value? check + @checks.each_pair{ |key, value| + return key if value == check + } + end + key = make_name(name, check) + @checks[ key ] = check + return key + end + + def checks + return @checks + end +end + +$nrpe = Nrpe.new() + + +def warn (msg) + STDERR.puts msg +end + + +config = YAML::load( File.open( 'nagios-master.cfg' ) ) + +%w{auto-hosts.cfg auto-hostgroups.cfg auto-services.cfg auto-dependencies.cfg}.each{ |file| + File.delete("#{file}") if FileTest.file?("#{file}") +} + +################################# +# create a few hostgroups +################################# + +config['hostgroups'].each_value{ |hg| + hg['members'] = [] +} + +config['servers'].each_pair{ |name, server| + server['hostgroups'].split(/,/).each{ |hg| + hg.strip! + config['hostgroups'][hg]['members'] << name + }; +} + +# create all +config['hostgroups']['all'] = {} +config['hostgroups']['all']['alias'] = "all servers" +config['hostgroups']['all']['members'] = [] +config['servers'].each_key{ |name| + config['hostgroups']['all']['members'] << name +} + + + +####### +# Hosts and Hostgroups +f = File.new("auto-hosts.cfg", "w") +config['servers'].each_pair{ |name, server| + f.puts "define host{" + f.puts " use generic-host" + f.puts " host_name #{ name }" + f.puts " alias #{ name }" + f.puts " address #{ server['ip'] }" + f.puts " check_command check-host-alive" + f.puts " parents #{ server['parents'] }" if server.has_key?('parents') + f.puts "}" + f.puts +} + +f = File.new("auto-hostgroups.cfg", "w") +config['hostgroups'].each_pair{ |name, hg| + next if hg['private'] + f.puts "define hostgroup{" + f.puts " hostgroup_name #{ name }" + f.puts " alias #{ hg['alias'] }" + f.puts " contact_groups #{ CONTACTGROUP }" + f.puts " members "+hg['members'].join(",") + f.puts "}" +} + + +####### +# Services and Dependencies +def addService(hosts, service, f, deps) + hosts_comma = hosts.join(',') + + f.puts "define service{" + f.puts " use generic-service" + f.puts " host_name #{ hosts_comma }" + f.puts " service_description #{ service['name'] }" + f.puts " check_command #{ service['check'] }" if service['check'] + if service['nrpe'] + check = $nrpe.add(service['name'], service['nrpe']) + f.puts " check_command #{ ORG }_check_nrpe!#{ check }" + + service['depends'] = 'process - nrpe' unless service['depends'] or service['name'] == 'process - nrpe' + end + # put additional keys into services + service.each_pair{ |key, value| + # known keys: + next if %w(name check hosts nrpe remotecheck hostgroups excludehosts depends runfrom).include? key + f.puts " #{key} #{value}" + + } + f.puts "}" + + if service['depends'] + service['depends'].each{ |prerequisite| + hosts.each{ |host| + prerequisite_host = host + pre = prerequisite + # split off a hostname if there's one + bananasplit = prerequisite.split(':') + if bananasplit.size == 2 + prerequisite_host = bananasplit[0] + pre = bananasplit[1] + elsif bananasplit.size > 2 + throw "Cannot prase prerequisite #{prerequisite} for service #{service['name']} into host:service" + end + deps.puts "define servicedependency{" + deps.puts " host_name #{ prerequisite_host }" + deps.puts " service_description #{ pre }" + deps.puts " dependent_host_name #{ host }" + deps.puts " dependent_service_description #{ service['name'] }" + deps.puts " execution_failure_criteria n" + deps.puts " notification_failure_criteria w,u,c" + deps.puts "}" + } + } + end +end + + +f = File.new("auto-services.cfg", "w") +deps = File.new("auto-dependencies.cfg", "w") +config['services'].each{ |service| + throw "Empty service or service not a hash" unless service.kind_of?(Hash) + throw "nrpe, check, and remotecheck are mutually exclusive in service #{service['name']}" if + (service['nrpe'] ? 1 : 0) + + (service['check'] ? 1 : 0) + + (service['remotecheck'] ? 1 : 0) >= 2 + # Figure out the hosts this service applies to + service['hosts'] = "" unless service['hosts'] + service['hosts'].delete!(" \t") + hosts = service['hosts'].split(/,/) + if service['hostgroups'] then + service['hostgroups'].delete(" \t").split(/,/).each{ |hg| + unless config['hostgroups'][hg] + throw "hostgroup #{hg} does not exist- used in service #{service['name']}" + end + hosts = hosts.concat config['hostgroups'][hg]['members'] + } + end + if service['excludehosts'] then + service['excludehosts'].delete(" \t").split(/,/).each{ |host| + if hosts.delete(host) == nil + throw "Cannot remove host #{host} from service #{service['name']}: it's not included anyway" + end + } + end + + throw "no hosts for service #{service['name']}" if hosts.empty? + + if service['runfrom'] + throw "need remotecheck with runfrom" unless service['remotecheck'] + relay = service['runfrom'] + + hosts.each{ |host| + host_ip = (config['servers'][host] ? config['servers'][host]['ip'] : gateway_name_to_ip[host] ) + check = $nrpe.add("#{host}_#{service['name']}", service['remotecheck'].gsub(/\$HOSTADDRESS\$/, host_ip)) + service['check'] = "#{ ORG }_check_nrpe_host!#{ config['servers'][ relay ]['ip'] }!#{ check }" + + if (service['depends'] == nil) + service['depends'] = [] + elsif service['depends'].kind_of?(String) + d = [ service['depends'] ]; + service['depends'] = d + end + service['depends'] << "#{ relay }:process - nrpe"; + + addService( [ host ], service, f, deps) + } + else + throw "must not remotecheck without runfrom" if service['remotecheck'] + addService(hosts, service, f, deps) + end +} + +f = File.new("nrpe_#{ ORG }.cfg", "w") +$nrpe.checks.each_pair{ |name, check| + f.puts "command[#{ name }]=#{ check }" +} + + -- cgit v1.2.3