Add : (Denis GERMAIN) check_shinken plugin and interface in the arbiter to get data.
[shinken.git] / test / shinken_test.py
blob4aa8b7afefd0b2fc53102966fc13fbd2c361dda1
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
46 class ShinkenTest(unittest.TestCase):
47 def setUp(self):
48 self.setup_with_file('etc/nagios_1r_1h_1s.cfg')
50 def setup_with_file(self, path):
51 # i am arbiter-like
52 self.broks = {}
53 self.me = None
54 self.log = logger
55 self.log.load_obj(self)
56 self.config_files = [path]
57 self.conf = Config()
58 self.conf.read_config(self.config_files)
59 buf = self.conf.read_config(self.config_files)
60 raw_objects = self.conf.read_config_buf(buf)
61 self.conf.create_objects_for_type(raw_objects, 'arbiter')
62 self.conf.create_objects_for_type(raw_objects, 'module')
63 self.conf.early_arbiter_linking()
64 self.conf.create_objects(raw_objects)
65 self.conf.instance_id = 0
66 self.conf.instance_name = 'test'
67 self.conf.linkify_templates()
68 self.conf.apply_inheritance()
69 self.conf.explode()
70 self.conf.create_reversed_list()
71 self.conf.remove_twins()
72 self.conf.apply_implicit_inheritance()
73 self.conf.fill_default()
74 self.conf.clean_useless()
75 self.conf.pythonize()
76 self.conf.linkify()
77 self.conf.apply_dependancies()
78 self.conf.explode_global_conf()
79 self.conf.propagate_timezone_option()
80 self.conf.create_business_rules()
81 self.conf.create_business_rules_dependencies()
82 self.conf.is_correct()
83 self.confs = self.conf.cut_into_parts()
84 self.dispatcher = Dispatcher(self.conf, self.me)
86 scheddaemon = Shinken(None, False, False, False, None)
87 self.sched = Scheduler(scheddaemon)
89 scheddaemon.sched = self.sched
91 m = MacroResolver()
92 m.init(self.conf)
93 self.sched.load_conf(self.conf)
94 e = ExternalCommandManager(self.conf, 'applyer')
95 self.sched.external_command = e
96 e.load_scheduler(self.sched)
97 e2 = ExternalCommandManager(self.conf, 'dispatcher')
98 e2.load_arbiter(self)
99 self.external_command_dispatcher = e2
100 self.sched.schedule()
103 def add(self, b):
104 if isinstance(b, Brok):
105 self.broks[b.id] = b
106 return
107 if isinstance(b, ExternalCommand):
108 self.sched.run_external_command(b.cmd_line)
111 def fake_check(self, ref, exit_status, output="OK"):
112 #print "fake", ref
113 now = time.time()
114 ref.schedule(force=True)
115 #now checks are schedule and we get them in
116 #the action queue
117 check = ref.actions.pop()
118 self.sched.add(check) # check is now in sched.checks[]
119 # fake execution
120 check.check_time = now
122 elts_line1 = output.split('|')
123 #First line before | is output
124 check.output = elts_line1[0]
125 #After | is perfdata
126 if len(elts_line1) > 1:
127 check.perf_data = elts_line1[1]
128 else:
129 check.perf_data = ''
130 check.exit_status = exit_status
131 check.execution_time = 0.001
132 check.status = 'waitconsume'
133 self.sched.waiting_results.append(check)
136 def scheduler_loop(self, count, reflist, do_sleep=False, sleep_time=61):
137 for ref in reflist:
138 (obj, exit_status, output) = ref
139 obj.checks_in_progress = []
140 for loop in range(1, count + 1):
141 print "processing check", loop
142 for ref in reflist:
143 (obj, exit_status, output) = ref
144 obj.update_in_checking()
145 self.fake_check(obj, exit_status, output)
146 self.sched.manage_internal_checks()
147 self.sched.consume_results()
148 self.sched.get_new_actions()
149 self.sched.get_new_broks()
150 self.worker_loop()
151 for ref in reflist:
152 (obj, exit_status, output) = ref
153 obj.checks_in_progress = []
154 self.sched.update_downtimes_and_comments()
155 #time.sleep(ref.retry_interval * 60 + 1)
156 if do_sleep:
157 time.sleep(sleep_time)
160 def worker_loop(self):
161 self.sched.delete_zombie_checks()
162 self.sched.delete_zombie_actions()
163 checks = self.sched.get_to_run_checks(True, False, worker_name='tester')
164 actions = self.sched.get_to_run_checks(False, True, worker_name='tester')
165 #print "------------ worker loop checks ----------------"
166 #print checks
167 #print "------------ worker loop actions ----------------"
168 self.show_actions()
169 #print "------------ worker loop new ----------------"
170 for a in actions:
171 a.status = 'inpoller'
172 a.check_time = time.time()
173 a.exit_status = 0
174 self.sched.put_results(a)
175 self.show_actions()
176 #print "------------ worker loop end ----------------"
179 def show_logs(self):
180 print "--- logs <<<----------------------------------"
181 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
182 if brok.type == 'log':
183 print "LOG:", brok.data['log']
184 print "--- logs >>>----------------------------------"
187 def show_actions(self):
188 print "--- actions <<<----------------------------------"
189 for a in sorted(self.sched.actions.values(), lambda x, y: x.id - y.id):
190 if a.is_a == 'notification':
191 if a.ref.my_type == "host":
192 ref = "host: %s" % a.ref.get_name()
193 else:
194 ref = "host: %s svc: %s" % (a.ref.host.get_name(), a.ref.get_name())
195 print "NOTIFICATION %d %s %s %s %s" % (a.id, ref, a.type, time.asctime(time.localtime(a.t_to_go)), a.status)
196 elif a.is_a == 'eventhandler':
197 print "EVENTHANDLER:", a
198 print "--- actions >>>----------------------------------"
201 def show_and_clear_logs(self):
202 self.show_logs()
203 self.clear_logs()
206 def show_and_clear_actions(self):
207 self.show_actions()
208 self.clear_actions()
211 def count_logs(self):
212 return len([b for b in self.sched.broks.values() if b.type == 'log'])
215 def count_actions(self):
216 return len(self.sched.actions.values())
219 def clear_logs(self):
220 id_to_del = []
221 for b in self.sched.broks.values():
222 if b.type == 'log':
223 id_to_del.append(b.id)
224 for id in id_to_del:
225 del self.sched.broks[id]
228 def clear_actions(self):
229 self.sched.actions = {}
232 def log_match(self, index, pattern):
233 # log messages are counted 1...n, so index=1 for the first message
234 if index > self.count_logs():
235 return False
236 else:
237 regex = re.compile(pattern)
238 lognum = 1
239 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
240 if brok.type == 'log':
241 if index == lognum:
242 if re.search(regex, brok.data['log']):
243 return True
244 lognum += 1
245 return False
248 def any_log_match(self, pattern):
249 regex = re.compile(pattern)
250 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
251 if brok.type == 'log':
252 if re.search(regex, brok.data['log']):
253 return True
254 return False
257 def get_log_match(self, pattern):
258 regex = re.compile(pattern)
259 res = []
260 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
261 if brok.type == 'log':
262 if re.search(regex, brok.data['log']):
263 res.append(brok.data['log'])
264 return res
268 def print_header(self):
269 print "#" * 80 + "\n" + "#" + " " * 78 + "#"
270 print "#" + string.center(self.id(), 78) + "#"
271 print "#" + " " * 78 + "#\n" + "#" * 80 + "\n"
276 def xtest_conf_is_correct(self):
277 self.print_header()
278 self.assert_(self.conf.conf_is_correct)
282 if __name__ == '__main__':
283 unittest.main()