summaryrefslogtreecommitdiff
path: root/build-nagios
blob: 87432095f46000d8b239c17a90b68e6538dd5f59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/ruby

# Copyright (c) 2004, 2005 Peter Palfrader <peter@palfrader.org>

require "yaml"

ORG="relativity"
SHORTORG="rela"
CONTACTGROUP="weaselgroup"
GENERATED_PREFIX="/etc/NOREPLY/generated/nagios/"

class Nrpe
	def initialize
		@checks = {}
	end

	def make_name( name, check )
		name = name.tr_s("^a-zA-Z", "_").gsub("process", "ps")

		result = "#{ SHORTORG }_" + 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(GENERATED_PREFIX+"#{file}") if FileTest.file?(GENERATED_PREFIX+"#{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(GENERATED_PREFIX+"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(GENERATED_PREFIX+"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(GENERATED_PREFIX+"auto-services.cfg", "w")
deps = File.new(GENERATED_PREFIX+"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(GENERATED_PREFIX+"nrpe_#{ ORG }.cfg", "w")
$nrpe.checks.each_pair{ |name, check|
	f.puts "command[#{ name }]=#{ check }"
}