Merge branch 'master' of ssh://lausser,shinken@shinken.git.sourceforge.net/gitroot...
[shinken.git] / test / shinken_test.py
blobef393efeeed968f29079159c5e20b677efa4fac2
1 #!/usr/bin/env python2.6
4 # This file is used to test host- and service-downtimes.
7 import sys
8 import time
9 import datetime
10 import os
11 import string
12 import re
13 import random
14 import unittest
16 sys.path.append("..")
17 sys.path.append("../shinken")
18 #sys.path.append("../bin")
19 #sys.path.append(os.path.abspath("bin"))
22 import shinken
23 from shinken.objects.config import Config
24 from shinken.objects.command import Command
25 from shinken.objects.module import Module
27 from shinken.dispatcher import Dispatcher
28 from shinken.log import logger
29 from shinken.scheduler import Scheduler
30 from shinken.macroresolver import MacroResolver
31 from shinken.external_command import ExternalCommandManager, ExternalCommand
32 from shinken.check import Check
33 from shinken.message import Message
34 from shinken.arbiterlink import ArbiterLink
35 from shinken.schedulerlink import SchedulerLink
36 from shinken.pollerlink import PollerLink
37 from shinken.reactionnerlink import ReactionnerLink
38 from shinken.brokerlink import BrokerLink
39 from shinken.satellitelink import SatelliteLink
40 from shinken.notification import Notification
42 from shinken.brok import Brok
44 from shinken.daemons.schedulerdaemon import Shinken
45 from shinken.daemons.brokerdaemon import Broker
46 from shinken.daemons.arbiterdaemon import Arbiter
48 class ShinkenTest(unittest.TestCase):
49 def setUp(self):
50 self.setup_with_file('etc/nagios_1r_1h_1s.cfg')
52 def setup_with_file(self, path):
53 # i am arbiter-like
54 self.broks = {}
55 self.me = None
56 self.log = logger
57 self.log.load_obj(self)
58 self.config_files = [path]
59 self.conf = Config()
60 self.conf.read_config(self.config_files)
61 buf = self.conf.read_config(self.config_files)
62 raw_objects = self.conf.read_config_buf(buf)
63 self.conf.create_objects_for_type(raw_objects, 'arbiter')
64 self.conf.create_objects_for_type(raw_objects, 'module')
65 self.conf.early_arbiter_linking()
66 self.conf.create_objects(raw_objects)
67 self.conf.instance_id = 0
68 self.conf.instance_name = 'test'
69 self.conf.linkify_templates()
70 self.conf.apply_inheritance()
71 self.conf.explode()
72 self.conf.create_reversed_list()
73 self.conf.remove_twins()
74 self.conf.apply_implicit_inheritance()
75 self.conf.fill_default()
76 self.conf.clean_useless()
77 self.conf.pythonize()
78 self.conf.linkify()
79 self.conf.apply_dependancies()
80 self.conf.explode_global_conf()
81 self.conf.propagate_timezone_option()
82 self.conf.create_business_rules()
83 self.conf.create_business_rules_dependencies()
84 self.conf.is_correct()
85 self.confs = self.conf.cut_into_parts()
86 self.dispatcher = Dispatcher(self.conf, self.me)
88 scheddaemon = Shinken(None, False, False, False, None)
89 self.sched = Scheduler(scheddaemon)
91 scheddaemon.sched = self.sched
93 m = MacroResolver()
94 m.init(self.conf)
95 self.sched.load_conf(self.conf)
96 e = ExternalCommandManager(self.conf, 'applyer')
97 self.sched.external_command = e
98 e.load_scheduler(self.sched)
99 e2 = ExternalCommandManager(self.conf, 'dispatcher')
100 e2.load_arbiter(self)
101 self.external_command_dispatcher = e2
102 self.sched.schedule()
105 def add(self, b):
106 if isinstance(b, Brok):
107 self.broks[b.id] = b
108 return
109 if isinstance(b, ExternalCommand):
110 self.sched.run_external_command(b.cmd_line)
113 def fake_check(self, ref, exit_status, output="OK"):
114 #print "fake", ref
115 now = time.time()
116 ref.schedule(force=True)
117 #now checks are schedule and we get them in
118 #the action queue
119 check = ref.actions.pop()
120 self.sched.add(check) # check is now in sched.checks[]
121 # fake execution
122 check.check_time = now
124 elts_line1 = output.split('|')
125 #First line before | is output
126 check.output = elts_line1[0]
127 #After | is perfdata
128 if len(elts_line1) > 1:
129 check.perf_data = elts_line1[1]
130 else:
131 check.perf_data = ''
132 check.exit_status = exit_status
133 check.execution_time = 0.001
134 check.status = 'waitconsume'
135 self.sched.waiting_results.append(check)
138 def scheduler_loop(self, count, reflist, do_sleep=False, sleep_time=61):
139 for ref in reflist:
140 (obj, exit_status, output) = ref
141 obj.checks_in_progress = []
142 for loop in range(1, count + 1):
143 print "processing check", loop
144 for ref in reflist:
145 (obj, exit_status, output) = ref
146 obj.update_in_checking()
147 self.fake_check(obj, exit_status, output)
148 self.sched.manage_internal_checks()
149 self.sched.consume_results()
150 self.sched.get_new_actions()
151 self.sched.get_new_broks()
152 self.worker_loop()
153 for ref in reflist:
154 (obj, exit_status, output) = ref
155 obj.checks_in_progress = []
156 self.sched.update_downtimes_and_comments()
157 #time.sleep(ref.retry_interval * 60 + 1)
158 if do_sleep:
159 time.sleep(sleep_time)
162 def worker_loop(self):
163 self.sched.delete_zombie_checks()
164 self.sched.delete_zombie_actions()
165 checks = self.sched.get_to_run_checks(True, False, worker_name='tester')
166 actions = self.sched.get_to_run_checks(False, True, worker_name='tester')
167 #print "------------ worker loop checks ----------------"
168 #print checks
169 #print "------------ worker loop actions ----------------"
170 self.show_actions()
171 #print "------------ worker loop new ----------------"
172 for a in actions:
173 a.status = 'inpoller'
174 a.check_time = time.time()
175 a.exit_status = 0
176 self.sched.put_results(a)
177 self.show_actions()
178 #print "------------ worker loop end ----------------"
181 def show_logs(self):
182 print "--- logs <<<----------------------------------"
183 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
184 if brok.type == 'log':
185 print "LOG:", brok.data['log']
186 print "--- logs >>>----------------------------------"
189 def show_actions(self):
190 print "--- actions <<<----------------------------------"
191 for a in sorted(self.sched.actions.values(), lambda x, y: x.id - y.id):
192 if a.is_a == 'notification':
193 if a.ref.my_type == "host":
194 ref = "host: %s" % a.ref.get_name()
195 else:
196 ref = "host: %s svc: %s" % (a.ref.host.get_name(), a.ref.get_name())
197 print "NOTIFICATION %d %s %s %s %s" % (a.id, ref, a.type, time.asctime(time.localtime(a.t_to_go)), a.status)
198 elif a.is_a == 'eventhandler':
199 print "EVENTHANDLER:", a
200 print "--- actions >>>----------------------------------"
203 def show_and_clear_logs(self):
204 self.show_logs()
205 self.clear_logs()
208 def show_and_clear_actions(self):
209 self.show_actions()
210 self.clear_actions()
213 def count_logs(self):
214 return len([b for b in self.sched.broks.values() if b.type == 'log'])
217 def count_actions(self):
218 return len(self.sched.actions.values())
221 def clear_logs(self):
222 id_to_del = []
223 for b in self.sched.broks.values():
224 if b.type == 'log':
225 id_to_del.append(b.id)
226 for id in id_to_del:
227 del self.sched.broks[id]
230 def clear_actions(self):
231 self.sched.actions = {}
234 def log_match(self, index, pattern):
235 # log messages are counted 1...n, so index=1 for the first message
236 if index > self.count_logs():
237 return False
238 else:
239 regex = re.compile(pattern)
240 lognum = 1
241 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
242 if brok.type == 'log':
243 if index == lognum:
244 if re.search(regex, brok.data['log']):
245 return True
246 lognum += 1
247 return False
250 def any_log_match(self, pattern):
251 regex = re.compile(pattern)
252 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
253 if brok.type == 'log':
254 if re.search(regex, brok.data['log']):
255 return True
256 return False
259 def get_log_match(self, pattern):
260 regex = re.compile(pattern)
261 res = []
262 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
263 if brok.type == 'log':
264 if re.search(regex, brok.data['log']):
265 res.append(brok.data['log'])
266 return res
270 def print_header(self):
271 print "#" * 80 + "\n" + "#" + " " * 78 + "#"
272 print "#" + string.center(self.id(), 78) + "#"
273 print "#" + " " * 78 + "#\n" + "#" * 80 + "\n"
278 def xtest_conf_is_correct(self):
279 self.print_header()
280 self.assert_(self.conf.conf_is_correct)
284 if __name__ == '__main__':
285 unittest.main()