1 #!/usr/bin/env python2.6
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 # This file is used to test reading and processing of config files
26 from shinken_test
import *
29 class GoodArbiter(ArbiterLink
):
30 # To lie about satellites
32 print "Dummy OK for", self
.get_name()
35 def have_conf(self
, i
):
42 class GoodScheduler(SchedulerLink
):
43 # To lie about satellites
45 print "Dummy OK for", self
.get_name()
48 def have_conf(self
, i
):
51 def put_conf(self
, conf
):
55 class BadScheduler(SchedulerLink
):
57 print "Dummy bad ping", self
.get_name()
58 self
.add_failed_check_attempt()
60 def have_conf(self
, i
):
63 class GoodPoller(PollerLink
):
64 # To lie about satellites
66 print "Dummy OK for", self
.get_name()
69 def put_conf(self
, conf
):
73 class BadPoller(PollerLink
):
75 print "Dummy bad ping", self
.get_name()
76 self
.add_failed_check_attempt()
78 class GoodReactionner(ReactionnerLink
):
79 # To lie about satellites
81 print "Dummy OK for", self
.get_name()
84 def put_conf(self
, conf
):
87 class BadReactionner(ReactionnerLink
):
89 print "Dummy bad ping", self
.get_name()
90 self
.add_failed_check_attempt()
92 class GoodBroker(BrokerLink
):
93 # To lie about satellites
95 print "Dummy OK for", self
.get_name()
98 def put_conf(self
, conf
):
101 class BadBroker(BrokerLink
):
103 print "Dummy bad ping", self
.get_name()
104 self
.add_failed_check_attempt()
107 class TestPassivePoller(ShinkenTest
):
108 #Uncomment this is you want to use a specific configuration
111 self
.setup_with_file('etc/nagios_passive_pollers.cfg')
115 def test_simple_passive_pollers(self
):
116 print "The dispatcher", self
.dispatcher
117 # dummy for the arbiter
118 for a
in self
.conf
.arbiterlinks
:
119 a
.__class
__ = GoodArbiter
120 print "Preparing schedulers"
121 scheduler1
= self
.conf
.schedulerlinks
.find_by_name('scheduler-all-1')
122 self
.assert_(scheduler1
is not None)
123 scheduler1
.__class__
= GoodScheduler
124 scheduler2
= self
.conf
.schedulerlinks
.find_by_name('scheduler-all-2')
125 self
.assert_(scheduler2
is not None)
126 scheduler2
.__class__
= BadScheduler
128 # Poller 1 is normal, 2 and 3 are passives
129 print "Preparing pollers"
130 poller1
= self
.conf
.pollers
.find_by_name('poller-all-1')
131 self
.assert_(poller1
is not None)
132 poller1
.__class__
= GoodPoller
133 print poller1
.__dict__
134 self
.assert_(poller1
.passive
== False)
135 poller2
= self
.conf
.pollers
.find_by_name('poller-all-2')
136 self
.assert_(poller2
is not None)
137 poller2
.__class__
= GoodPoller
138 self
.assert_(poller2
.passive
== True)
139 poller3
= self
.conf
.pollers
.find_by_name('poller-all-3')
140 self
.assert_(poller3
is not None)
141 poller3
.__class__
= GoodPoller
142 self
.assert_(poller3
.passive
== True)
144 print "Preparing reactionners"
145 reactionner1
= self
.conf
.reactionners
.find_by_name('reactionner-all-1')
146 self
.assert_(reactionner1
is not None)
147 reactionner1
.__class__
= GoodReactionner
148 reactionner2
= self
.conf
.reactionners
.find_by_name('reactionner-all-2')
149 self
.assert_(reactionner2
is not None)
150 reactionner2
.__class__
= BadReactionner
152 print "Preparing brokers"
153 broker1
= self
.conf
.brokers
.find_by_name('broker-all-1')
154 self
.assert_(broker1
is not None)
155 broker1
.__class__
= GoodBroker
156 broker2
= self
.conf
.brokers
.find_by_name('broker-all-2')
157 self
.assert_(broker2
is not None)
158 broker2
.__class__
= BadBroker
160 # Ping all elements. Should have 1 as OK, 2 as
161 # one bad attempt (3 max)
162 self
.dispatcher
.check_alive()
165 self
.assert_(scheduler1
.alive
== True)
166 self
.assert_(scheduler1
.attempt
== 0)
167 self
.assert_(scheduler1
.reachable
== True)
168 # still alive, just unreach
169 self
.assert_(scheduler2
.alive
== True)
170 self
.assert_(scheduler2
.attempt
== 1)
171 self
.assert_(scheduler2
.reachable
== False)
173 #and others satellites too
174 self
.assert_(poller1
.alive
== True)
175 self
.assert_(poller1
.attempt
== 0)
176 self
.assert_(poller1
.reachable
== True)
177 # still alive, just unreach
178 self
.assert_(poller2
.alive
== True)
179 self
.assert_(poller2
.attempt
== 0)
180 self
.assert_(poller2
.reachable
== True)
182 #and others satellites too
183 self
.assert_(reactionner1
.alive
== True)
184 self
.assert_(reactionner1
.attempt
== 0)
185 self
.assert_(reactionner1
.reachable
== True)
186 # still alive, just unreach
187 self
.assert_(reactionner2
.alive
== True)
188 self
.assert_(reactionner2
.attempt
== 1)
189 self
.assert_(reactionner2
.reachable
== False)
191 #and others satellites too
192 self
.assert_(broker1
.alive
== True)
193 self
.assert_(broker1
.attempt
== 0)
194 self
.assert_(broker1
.reachable
== True)
195 # still alive, just unreach
196 self
.assert_(broker2
.alive
== True)
197 self
.assert_(broker2
.attempt
== 1)
198 self
.assert_(broker2
.reachable
== False)
200 ### Now add another attempt, still alive, but attemp=2/3
201 self
.dispatcher
.check_alive()
204 self
.assert_(scheduler1
.alive
== True)
205 self
.assert_(scheduler1
.attempt
== 0)
206 self
.assert_(scheduler1
.reachable
== True)
207 # still alive, just unreach
208 self
.assert_(scheduler2
.alive
== True)
209 self
.assert_(scheduler2
.attempt
== 2)
210 self
.assert_(scheduler2
.reachable
== False)
212 #and others satellites too
213 self
.assert_(poller1
.alive
== True)
214 self
.assert_(poller1
.attempt
== 0)
215 self
.assert_(poller1
.reachable
== True)
216 # still alive, just unreach
217 self
.assert_(poller2
.alive
== True)
218 self
.assert_(poller2
.attempt
== 0)
219 self
.assert_(poller2
.reachable
== True)
221 #and others satellites too
222 self
.assert_(reactionner1
.alive
== True)
223 self
.assert_(reactionner1
.attempt
== 0)
224 self
.assert_(reactionner1
.reachable
== True)
225 # still alive, just unreach
226 self
.assert_(reactionner2
.alive
== True)
227 self
.assert_(reactionner2
.attempt
== 2)
228 self
.assert_(reactionner2
.reachable
== False)
230 #and others satellites too
231 self
.assert_(broker1
.alive
== True)
232 self
.assert_(broker1
.attempt
== 0)
233 self
.assert_(broker1
.reachable
== True)
234 # still alive, just unreach
235 self
.assert_(broker2
.alive
== True)
236 self
.assert_(broker2
.attempt
== 2)
237 self
.assert_(broker2
.reachable
== False)
239 ### Now we get BAD, We go DEAD for N2 !
240 self
.dispatcher
.check_alive()
243 self
.assert_(scheduler1
.alive
== True)
244 self
.assert_(scheduler1
.attempt
== 0)
245 self
.assert_(scheduler1
.reachable
== True)
246 # still alive, just unreach
247 self
.assert_(scheduler2
.alive
== False)
248 self
.assert_(scheduler2
.attempt
== 3)
249 self
.assert_(scheduler2
.reachable
== False)
251 #and others satellites too
252 self
.assert_(poller1
.alive
== True)
253 self
.assert_(poller1
.attempt
== 0)
254 self
.assert_(poller1
.reachable
== True)
255 # still alive, just unreach
256 self
.assert_(poller2
.alive
== True)
257 self
.assert_(poller2
.attempt
== 0)
258 self
.assert_(poller2
.reachable
== True)
260 #and others satellites too
261 self
.assert_(reactionner1
.alive
== True)
262 self
.assert_(reactionner1
.attempt
== 0)
263 self
.assert_(reactionner1
.reachable
== True)
264 # still alive, just unreach
265 self
.assert_(reactionner2
.alive
== False)
266 self
.assert_(reactionner2
.attempt
== 3)
267 self
.assert_(reactionner2
.reachable
== False)
269 #and others satellites too
270 self
.assert_(broker1
.alive
== True)
271 self
.assert_(broker1
.attempt
== 0)
272 self
.assert_(broker1
.reachable
== True)
273 # still alive, just unreach
274 self
.assert_(broker2
.alive
== False)
275 self
.assert_(broker2
.attempt
== 3)
276 self
.assert_(broker2
.reachable
== False)
278 # Now we check how we should dispatch confs
279 self
.dispatcher
.check_dispatch()
280 # the conf should not be in a good shape
281 self
.assert_(self
.dispatcher
.dispatch_ok
== False)
283 # Now we really dispatch them!
284 self
.dispatcher
.dispatch()
285 self
.assert_(self
.any_log_match('Dispatch OK of for conf in scheduler scheduler-all-1'))
286 self
.assert_(self
.any_log_match('Dispatch OK of for configuration 0 to reactionner reactionner-all-1'))
287 self
.assert_(self
.any_log_match('Dispatch OK of for configuration 0 to poller poller-all-1'))
288 self
.assert_(self
.any_log_match('Dispatch OK of for configuration 0 to broker broker-all-1'))
291 # And look if we really dispatch conf as we should
292 for r
in self
.conf
.realms
:
293 for cfg
in r
.confs
.values():
294 self
.assert_(cfg
.is_assigned
== True)
295 self
.assert_(cfg
.assigned_to
== scheduler1
)
298 if __name__
== '__main__':