Fix: missing customs values in host/service tables in livestatus and Thruk was not...
[shinken.git] / test / test_livestatus.py
blobc51498766daaee0fee2b7ab8eefb372556f08d19
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/>.
23 # This file is used to test host- and service-downtimes.
26 from shinken_test import *
27 import os
28 import re
29 import subprocess
30 import shutil
31 import time
33 sys.path.append("../shinken/modules/livestatus_broker")
34 from livestatus_broker import Livestatus_broker
35 sys.setcheckinterval(10000)
37 class TestConfig(ShinkenTest):
38 def contains_line(self, text, pattern):
39 regex = re.compile(pattern)
40 for line in text.splitlines():
41 if re.search(regex, line):
42 return True
43 return False
46 def scheduler_loop(self, count, reflist, do_sleep=False, sleep_time=61):
47 super(TestConfig, self).scheduler_loop(count, reflist, do_sleep, sleep_time)
48 if self.nagios_installed() and hasattr(self, 'nagios_started'):
49 self.nagios_loop(1, reflist)
52 def update_broker(self):
53 #The brok should be manage in the good order
54 ids = self.sched.broks.keys()
55 ids.sort()
56 for brok_id in ids:
57 brok = self.sched.broks[brok_id]
58 #print "Managing a brok type", brok.type, "of id", brok_id
59 #if brok.type == 'update_service_status':
60 # print "Problem?", brok.data['is_problem']
61 self.livestatus_broker.manage_brok(brok)
62 self.sched.broks = {}
65 def lines_equal(self, text1, text2):
66 # gets two multiline strings and compares the contents
67 # lifestatus output may not be in alphabetical order, so this
68 # function is used to compare unordered output with unordered
69 # expected output
70 # sometimes mklivestatus returns 0 or 1 on an empty result
71 text1 = text1.replace("200 1", "200 0")
72 text2 = text2.replace("200 1", "200 0")
73 text1 = text1.rstrip()
74 text2 = text2.rstrip()
75 #print "text1 //%s//" % text1
76 #print "text2 //%s//" % text2
77 sorted1 = "\n".join(sorted(text1.split("\n")))
78 sorted2 = "\n".join(sorted(text2.split("\n")))
79 len1 = len(text1.split("\n"))
80 len2 = len(text2.split("\n"))
81 #print "%s == %s text cmp %s" % (len1, len2, sorted1 == sorted2)
82 #print "text1 //%s//" % sorted(text1.split("\n"))
83 #print "text2 //%s//" % sorted(text2.split("\n"))
84 if sorted1 == sorted2 and len1 == len2:
85 return True
86 else:
87 # Maybe list members are different
88 # allhosts;test_host_0;test_ok_0;servicegroup_02,servicegroup_01,ok
89 # allhosts;test_host_0;test_ok_0;servicegroup_02,ok,servicegroup_01
90 # break it up to
91 # [['allhosts'], ['test_host_0'], ['test_ok_0'],
92 # ['ok', 'servicegroup_01', 'servicegroup_02']]
93 [line for line in sorted(text1.split("\n"))]
94 data1 = [[sorted(c.split(',')) for c in columns] for columns in [line.split(';') for line in sorted(text1.split("\n")) if line]]
95 data2 = [[sorted(c.split(',')) for c in columns] for columns in [line.split(';') for line in sorted(text2.split("\n")) if line]]
96 #print "text1 //%s//" % data1
97 #print "text2 //%s//" % data2
98 # cmp is clever enough to handle nested arrays
99 return cmp(data1, data2) == 0
102 def show_broks(self, title):
103 print
104 print "--- ", title
105 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
106 if re.compile('^service_').match(brok.type):
107 print "BROK:", brok.type
108 print "BROK ", brok.data['in_checking']
109 self.update_broker()
110 request = 'GET services\nColumns: service_description is_executing\n'
111 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
112 print response
115 def nagios_installed(self, path='/usr/local/nagios/bin/nagios', livestatus='/usr/local/nagios/lib/mk-livestatus/livestatus.o'):
116 return False
117 raise
118 if os.path.exists(path) and os.access(path, os.X_OK) and os.path.exists(livestatus):
119 self.nagios_path = path
120 self.livestatus_path = livestatus
121 return True
122 else:
123 return False
126 # shinkenize_nagios_config('nagios_1r_1h_1s')
127 # We assume that there is a nagios_1r_1h_1s.cfg and a nagios_1r_1h_1s directory for the objects
128 def unshinkenize_config(self, configname):
129 new_configname = configname + '_' + str(os.getpid())
130 config = open('etc/nagios_' + configname + '.cfg')
131 text = config.readlines()
132 config.close()
134 newconfig = open('etc/nagios_' + new_configname + '.cfg', 'w')
135 for line in text:
136 if re.search('^resource_file=', line):
137 newconfig.write("resource_file=etc/resource.cfg\n")
138 elif re.search('shinken\-specific\.cfg', line):
139 pass
140 elif re.search('enable_problem_impacts_states_change', line):
141 pass
142 elif re.search('cfg_dir=', line):
143 newconfig.write(re.sub(configname, new_configname, line))
144 elif re.search('cfg_file=', line):
145 newconfig.write(re.sub(configname, new_configname, line))
146 elif re.search('execute_host_checks=', line):
147 newconfig.write("execute_host_checks=0\n")
148 elif re.search('execute_service_checks=', line):
149 newconfig.write("execute_service_checks=0\n")
150 elif re.search('^debug_level=', line):
151 newconfig.write("debug_level=0\n")
152 elif re.search('^debug_verbosity=', line):
153 newconfig.write("debug_verbosity=0\n")
154 elif re.search('^status_update_interval=', line):
155 newconfig.write("status_update_interval=30\n")
156 elif re.search('^command_file=', line):
157 newconfig.write("command_file=var/nagios.cmd\n")
158 elif re.search('^command_check_interval=', line):
159 newconfig.write("command_check_interval=1s\n")
160 else:
161 newconfig.write(line)
162 newconfig.write('broker_module=/usr/local/nagios/lib/mk-livestatus/livestatus.o var/live' + "\n")
163 newconfig.close()
164 for dirfile in os.walk('etc/' + configname):
165 dirpath, dirlist, filelist = dirfile
166 newdirpath = re.sub(configname, new_configname, dirpath)
167 os.mkdir(newdirpath)
168 for file in [f for f in filelist if re.search('\.cfg$', f)]:
169 config = open(dirpath + '/' + file)
170 text = config.readlines()
171 config.close()
172 newconfig = open(newdirpath + '/' + file, 'w')
173 for line in text:
174 if re.search('^\s*criticity', line):
175 pass
176 elif re.search('enable_problem_impacts_states_change', line):
177 pass
178 else:
179 newconfig.write(line)
180 newconfig.close()
181 return new_configname
184 def start_nagios(self, config):
185 if os.path.exists('var/spool/checkresults'):
186 # Cleanup leftover checkresults
187 shutil.rmtree('var/spool/checkresults')
188 for dir in ['tmp', 'var/tmp', 'var/spool', 'var/spool/checkresults', 'var/archives']:
189 if not os.path.exists(dir):
190 os.mkdir(dir)
191 self.nagios_config = self.unshinkenize_config(config)
192 if os.path.exists('var/retention.dat'):
193 os.remove('var/retention.dat')
194 if os.path.exists('var/status.dat'):
195 os.remove('var/status.dat')
196 self.nagios_proc = subprocess.Popen([self.nagios_path, 'etc/nagios_' + self.nagios_config + '.cfg'], close_fds=True)
197 self.nagios_started = time.time()
198 time.sleep(2)
201 def stop_nagios(self):
202 if self.nagios_installed():
203 print "i stop nagios!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
204 time.sleep(5)
205 if hasattr(self, 'nagios_proc'):
206 attempt = 1
207 while self.nagios_proc.poll() == None and attempt < 4:
208 self.nagios_proc.terminate()
209 attempt += 1
210 time.sleep(1)
211 if self.nagios_proc.poll() == None:
212 self.nagios_proc.kill()
213 if os.path.exists('etc/' + self.nagios_config):
214 shutil.rmtree('etc/' + self.nagios_config)
215 if os.path.exists('etc/nagios_' + self.nagios_config + '.cfg'):
216 os.remove('etc/nagios_' + self.nagios_config + '.cfg')
219 def ask_nagios(self, request):
220 if time.time() - self.nagios_started < 2:
221 time.sleep(1)
222 if not request.endswith("\n"):
223 request = request + "\n"
224 unixcat = subprocess.Popen([os.path.dirname(self.nagios_path) + '/' + 'unixcat', 'var/live'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
225 tic = time.clock()
226 out, err = unixcat.communicate(request)
227 tac = time.clock()
228 print "mklivestatus duration %f" % (tac - tic)
229 attempt = 1
230 while unixcat.poll() == None and attempt < 4:
231 unixcat.terminate()
232 attempt += 1
233 time.sleep(1)
234 if unixcat.poll() == None:
235 unixcat.kill()
236 print "unixcat says", out
237 return out
240 def nagios_loop(self, count, reflist, do_sleep=False, sleep_time=61):
241 now = time.time()
242 buffer = open('var/pipebuffer', 'w')
243 for ref in reflist:
244 (obj, exit_status, output) = ref
245 if obj.my_type == 'service':
246 cmd = "[%lu] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n" % (now, obj.host_name, obj.service_description, exit_status, output)
247 print cmd
248 buffer.write(cmd)
249 else:
250 cmd = "[%lu] PROCESS_HOST_CHECK_RESULT;%s;%d;%s\n" % (now, obj.host_name, exit_status, output)
251 buffer.write(cmd)
252 buffer.close()
253 print "open pipe", self.conf.command_file
254 fifo = open('var/nagios.cmd', 'w')
255 cmd = "[%lu] PROCESS_FILE;%s;0\n" % (now, 'var/pipebuffer')
256 fifo.write(cmd)
257 fifo.flush()
258 fifo.close()
259 time.sleep(5)
262 def nagios_extcmd(self, cmd):
263 fifo = open('var/nagios.cmd', 'w')
264 fifo.write(cmd)
265 fifo.flush()
266 fifo.close()
267 time.sleep(5)
271 class TestConfigSmall(TestConfig):
272 def setUp(self):
273 self.setup_with_file('etc/nagios_1r_1h_1s.cfg')
274 self.livestatus_broker = Livestatus_broker('livestatus', '127.0.0.1', str(50000 + os.getpid()), 'live', 'tmp/livelogs.db' + str(os.getpid()), 'tmp/pnp4nagios_test' + str(os.getpid()))
275 self.livestatus_broker.properties = {
276 'to_queue' : 0,
277 'from_queue' : 0
280 self.livestatus_broker.init()
281 print "Cleaning old broks?"
282 self.sched.fill_initial_broks()
283 self.update_broker()
284 self.nagios_path = None
285 self.livestatus_path = None
286 self.nagios_config = None
289 def tearDown(self):
290 self.stop_nagios()
291 self.livestatus_broker.dbconn.close()
292 if os.path.exists('tmp/livelogs.db' + str(os.getpid())):
293 os.remove('tmp/livelogs.db' + str(os.getpid()))
294 if os.path.exists('tmp/pnp4nagios_test' + str(os.getpid())):
295 shutil.rmtree('tmp/pnp4nagios_test' + str(os.getpid()))
298 def test_childs(self):
299 if self.nagios_installed():
300 self.start_nagios('1r_1h_1s')
301 self.print_header()
302 now = time.time()
303 objlist = []
304 for host in self.sched.hosts:
305 objlist.append([host, 0, 'UP'])
306 for service in self.sched.services:
307 objlist.append([service, 0, 'OK'])
308 self.scheduler_loop(1, objlist)
309 self.update_broker()
310 request = """GET hosts
311 Columns: childs
312 Filter: name = test_host_0
313 OutputFormat:csv
314 KeepAlive: on
315 ResponseHeader: fixed16
317 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
318 print response
319 if self.nagios_installed():
320 nagresponse = self.ask_nagios(request)
321 print "nagresponse----------------------------------------------"
322 print nagresponse
323 self.assert_(self.lines_equal(response, nagresponse))
324 request = """GET hosts
325 Columns: childs
326 Filter: name = test_router_0
327 OutputFormat:csv
328 KeepAlive: on
329 ResponseHeader: fixed16
331 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
332 print response
333 if self.nagios_installed():
334 nagresponse = self.ask_nagios(request)
335 print "nagresponse----------------------------------------------"
336 print nagresponse
337 self.assert_(self.lines_equal(response, nagresponse))
341 def test_servicesbyhostgroup(self):
342 if self.nagios_installed():
343 self.start_nagios('1r_1h_1s')
344 self.print_header()
345 now = time.time()
346 objlist = []
347 for host in self.sched.hosts:
348 objlist.append([host, 0, 'UP'])
349 for service in self.sched.services:
350 objlist.append([service, 0, 'OK'])
351 self.scheduler_loop(1, objlist)
352 self.update_broker()
353 request = """GET servicesbyhostgroup
354 Filter: host_groups >= allhosts
355 Columns: hostgroup_name host_name service_description groups
356 OutputFormat: csv
357 KeepAlive: on
358 ResponseHeader: fixed16
360 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
361 print response
362 if self.nagios_installed():
363 nagresponse = self.ask_nagios(request)
364 print "nagresponse----------------------------------------------"
365 print nagresponse
366 self.assert_(self.lines_equal(response, nagresponse))
368 # Again, but without filter
369 request = """GET servicesbyhostgroup
370 Columns: hostgroup_name host_name service_description groups
371 OutputFormat: csv
372 KeepAlive: on
373 ResponseHeader: fixed16
375 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
376 print response
377 if self.nagios_installed():
378 nagresponse = self.ask_nagios(request)
379 print "nagresponse----------------------------------------------"
380 print nagresponse
381 self.assert_(self.lines_equal(response, nagresponse))
384 def test_hostsbygroup(self):
385 self.print_header()
386 if self.nagios_installed():
387 self.start_nagios('1r_1h_1s')
388 now = time.time()
389 objlist = []
390 for host in self.sched.hosts:
391 objlist.append([host, 0, 'UP'])
392 for service in self.sched.services:
393 objlist.append([service, 0, 'OK'])
394 self.scheduler_loop(1, objlist)
395 self.update_broker()
396 request = """GET hostsbygroup
397 ColumnHeaders: on
398 Columns: host_name hostgroup_name
399 Filter: groups >= allhosts
400 OutputFormat: csv
401 KeepAlive: on
402 ResponseHeader: fixed16
405 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
406 print response
407 if self.nagios_installed():
408 nagresponse = self.ask_nagios(request)
409 print "nagresponse----------------------------------------------"
410 print nagresponse
411 self.assert_(self.lines_equal(response, nagresponse))
414 def test_status(self):
415 self.print_header()
416 if self.nagios_installed():
417 self.start_nagios('1r_1h_1s')
418 now = time.time()
419 host = self.sched.hosts.find_by_name("test_host_0")
420 host.checks_in_progress = []
421 host.act_depend_of = [] # ignore the router
422 router = self.sched.hosts.find_by_name("test_router_0")
423 router.checks_in_progress = []
424 router.act_depend_of = [] # ignore the router
425 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
426 svc.checks_in_progress = []
427 svc.act_depend_of = [] # no hostchecks on critical checkresults
428 self.scheduler_loop(2, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 2, 'BAD']])
429 self.update_broker()
430 #---------------------------------------------------------------
431 # get the full hosts table
432 #---------------------------------------------------------------
433 request = 'GET hosts'
434 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
435 print response
436 if self.nagios_installed():
437 nagresponse = self.ask_nagios(request)
438 print "nagresponse----------------------------------------------"
439 print nagresponse
440 # todo 1 != 1.0000000000e+00
441 #self.assert_(self.lines_equal(response, nagresponse))
443 #---------------------------------------------------------------
444 # get only the host names and addresses
445 #---------------------------------------------------------------
446 request = 'GET hosts\nColumns: name address groups\nColumnHeaders: on'
447 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
448 print response
449 if self.nagios_installed():
450 nagresponse = self.ask_nagios(request)
451 print "nagresponse----------------------------------------------"
452 print nagresponse
453 self.assert_(self.lines_equal(response, nagresponse))
455 #---------------------------------------------------------------
456 # query_1
457 #---------------------------------------------------------------
458 request = 'GET contacts'
459 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
460 print 'query_1_______________\n%s\n%s\n' % (request, response)
461 if self.nagios_installed():
462 nagresponse = self.ask_nagios(request)
463 print "nagresponse----------------------------------------------"
464 print nagresponse
465 # There are some sick columns in the livestatus response like
466 # modified_attributes;modified_attributes_list
467 # These are not implemented in shinken-livestatus (never, i think)
468 #self.assert_(self.lines_equal(response, nagresponse))
470 #---------------------------------------------------------------
471 # query_2
472 #---------------------------------------------------------------
473 request = 'GET contacts\nColumns: name alias'
474 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
475 print 'query_2_______________\n%s\n%s\n' % (request, response)
476 if self.nagios_installed():
477 nagresponse = self.ask_nagios(request)
478 print "nagresponse----------------------------------------------"
479 print nagresponse
480 self.assert_(self.lines_equal(response, nagresponse))
482 #---------------------------------------------------------------
483 # query_3
484 #---------------------------------------------------------------
485 #self.scheduler_loop(3, svc, 2, 'BAD')
486 request = 'GET services\nColumns: host_name description state\nFilter: state = 2\nColumnHeaders: on'
487 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
488 print 'query_3_______________\n%s\n%s\n' % (request, response)
489 self.assert_(response == 'host_name;description;state\ntest_host_0;test_ok_0;2\n')
490 request = 'GET services\nColumns: host_name description state\nFilter: state = 2'
491 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
492 print 'query_3_______________\n%s\n%s\n' % (request, response)
493 self.assert_(response == 'test_host_0;test_ok_0;2\n')
494 request = 'GET services\nColumns: host_name description state\nFilter: state = 0'
495 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
496 print 'query_3_______________\n%s\n%s\n' % (request, response)
497 self.assert_(response == '\n')
498 duration = 180
499 now = time.time()
500 cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;0;0;%d;lausser;blablub" % (now, now, now + duration, duration)
501 self.sched.run_external_command(cmd)
502 self.update_broker()
503 self.scheduler_loop(1, [[svc, 0, 'OK']])
504 self.update_broker()
505 self.scheduler_loop(3, [[svc, 2, 'BAD']])
506 self.update_broker()
507 request = 'GET services\nColumns: host_name description scheduled_downtime_depth\nFilter: state = 2\nFilter: scheduled_downtime_depth = 1'
508 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
509 print 'query_3_______________\n%s\n%s\n' % (request, response)
510 self.assert_(response == 'test_host_0;test_ok_0;1\n')
512 #---------------------------------------------------------------
513 # query_4
514 #---------------------------------------------------------------
515 request = 'GET services\nColumns: host_name description state\nFilter: state = 2\nFilter: in_notification_period = 1\nAnd: 2\nFilter: state = 0\nOr: 2\nFilter: host_name = test_host_0\nFilter: description = test_ok_0\nAnd: 3\nFilter: contacts >= harri\nFilter: contacts >= test_contact\nOr: 3'
516 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
517 print 'query_4_______________\n%s\n%s\n' % (request, response)
518 self.assert_(response == 'test_host_0;test_ok_0;2\n')
520 #---------------------------------------------------------------
521 # query_6
522 #---------------------------------------------------------------
523 request = 'GET services\nStats: state = 0\nStats: state = 1\nStats: state = 2\nStats: state = 3'
524 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
525 print 'query_6_______________\n%s\n%s\n' % (request, response)
526 self.assert_(response == '0;0;1;0\n')
528 #---------------------------------------------------------------
529 # query_7
530 #---------------------------------------------------------------
531 request = 'GET services\nStats: state = 0\nStats: state = 1\nStats: state = 2\nStats: state = 3\nFilter: contacts >= test_contact'
532 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
533 print 'query_6_______________\n%s\n%s\n' % (request, response)
534 self.assert_(response == '0;0;1;0\n')
535 if self.nagios_installed():
536 nagresponse = self.ask_nagios(request)
537 print "nagresponse----------------------------------------------"
538 print nagresponse
539 # TODO looks like a timing problem with nagios
540 #self.assert_(self.lines_equal(response, nagresponse))
543 def test_json(self):
544 self.print_header()
545 print "got initial broks"
546 now = time.time()
547 host = self.sched.hosts.find_by_name("test_host_0")
548 host.checks_in_progress = []
549 host.act_depend_of = [] # ignore the router
550 router = self.sched.hosts.find_by_name("test_router_0")
551 router.checks_in_progress = []
552 router.act_depend_of = [] # ignore the router
553 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
554 svc.checks_in_progress = []
555 svc.act_depend_of = [] # no hostchecks on critical checkresults
556 self.scheduler_loop(2, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 2, 'BAD']])
557 self.update_broker()
558 request = 'GET services\nColumns: host_name description state\nOutputFormat: json'
559 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
560 print 'json wo headers__________\n%s\n%s\n' % (request, response)
561 self.assert_(response == '[["test_host_0","test_ok_0",2]]\n')
562 request = 'GET services\nColumns: host_name description state\nOutputFormat: json\nColumnHeaders: on'
563 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
564 print 'json with headers__________\n%s\n%s\n' % (request, response)
565 self.assert_(response == '[["host_name","description","state"],["test_host_0","test_ok_0",2]]\n')
566 #100% mklivesttaus: self.assert_(response == '[["host_name","description","state"],\n["test_host_0","test_ok_0",2]]\n')
569 def test_thruk(self):
570 self.print_header()
571 if self.nagios_installed():
572 self.start_nagios('1r_1h_1s')
573 now = time.time()
574 host = self.sched.hosts.find_by_name("test_host_0")
575 host.checks_in_progress = []
576 host.act_depend_of = [] # ignore the router
577 router = self.sched.hosts.find_by_name("test_router_0")
578 router.checks_in_progress = []
579 router.act_depend_of = [] # ignore the router
580 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
581 svc.checks_in_progress = []
582 svc.act_depend_of = [] # no hostchecks on critical checkresults
583 self.scheduler_loop(2, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 2, 'BAD']])
584 self.update_broker()
585 #---------------------------------------------------------------
586 # get the full hosts table
587 #---------------------------------------------------------------
588 request = 'GET status\nColumns: livestatus_version program_version accept_passive_host_checks accept_passive_service_checks check_external_commands check_host_freshness check_service_freshness enable_event_handlers enable_flap_detection enable_notifications execute_host_checks execute_service_checks last_command_check last_log_rotation nagios_pid obsess_over_hosts obsess_over_services process_performance_data program_start interval_length'
589 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
590 print response
592 request = """GET hosts
593 Stats: name !=
594 Stats: check_type = 0
595 Stats: check_type = 1
596 Stats: has_been_checked = 1
597 Stats: state = 0
598 StatsAnd: 2
599 Stats: has_been_checked = 1
600 Stats: state = 1
601 StatsAnd: 2
602 Stats: has_been_checked = 1
603 Stats: state = 2
604 StatsAnd: 2
605 Stats: has_been_checked = 0
606 Stats: has_been_checked = 0
607 Stats: active_checks_enabled = 0
608 StatsAnd: 2
609 Stats: has_been_checked = 0
610 Stats: scheduled_downtime_depth > 0
611 StatsAnd: 2
612 Stats: state = 0
613 Stats: has_been_checked = 1
614 Stats: active_checks_enabled = 0
615 StatsAnd: 3
616 Stats: state = 0
617 Stats: has_been_checked = 1
618 Stats: scheduled_downtime_depth > 0
619 StatsAnd: 3
620 Stats: state = 1
621 Stats: has_been_checked = 1
622 Stats: acknowledged = 1
623 StatsAnd: 3
624 Stats: state = 1
625 Stats: scheduled_downtime_depth > 0
626 Stats: has_been_checked = 1
627 StatsAnd: 3
628 Stats: state = 1
629 Stats: active_checks_enabled = 0
630 Stats: has_been_checked = 1
631 StatsAnd: 3
632 Stats: state = 1
633 Stats: active_checks_enabled = 1
634 Stats: acknowledged = 0
635 Stats: scheduled_downtime_depth = 0
636 Stats: has_been_checked = 1
637 StatsAnd: 5
638 Stats: state = 2
639 Stats: acknowledged = 1
640 Stats: has_been_checked = 1
641 StatsAnd: 3
642 Stats: state = 2
643 Stats: scheduled_downtime_depth > 0
644 Stats: has_been_checked = 1
645 StatsAnd: 3
646 Stats: state = 2
647 Stats: active_checks_enabled = 0
648 StatsAnd: 2
649 Stats: state = 2
650 Stats: active_checks_enabled = 1
651 Stats: acknowledged = 0
652 Stats: scheduled_downtime_depth = 0
653 Stats: has_been_checked = 1
654 StatsAnd: 5
655 Stats: is_flapping = 1
656 Stats: flap_detection_enabled = 0
657 Stats: notifications_enabled = 0
658 Stats: event_handler_enabled = 0
659 Stats: active_checks_enabled = 0
660 Stats: accept_passive_checks = 0
661 Stats: state = 1
662 Stats: childs !=
663 StatsAnd: 2
664 Separators: 10 59 44 124
665 ResponseHeader: fixed16"""
666 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
667 print response
668 if self.nagios_installed():
669 nagresponse = self.ask_nagios(request)
670 print "nagresponse----------------------------------------------"
671 print nagresponse
672 # TODO timing problem?
673 #self.assert_(self.lines_equal(response, nagresponse))
675 request = """GET comments
676 Columns: host_name source type author comment entry_time entry_type expire_time
677 Filter: service_description ="""
678 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
679 print response
680 if self.nagios_installed():
681 nagresponse = self.ask_nagios(request)
682 print "nagresponse----------------------------------------------"
683 print nagresponse
684 self.assert_(self.lines_equal(response, nagresponse))
686 request = """GET hosts
687 Columns: comments has_been_checked state name address acknowledged notifications_enabled active_checks_enabled is_flapping scheduled_downtime_depth is_executing notes_url_expanded action_url_expanded icon_image_expanded icon_image_alt last_check last_state_change plugin_output next_check long_plugin_output
688 Separators: 10 59 44 124
689 ResponseHeader: fixed16"""
690 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
691 print response
693 duration = 180
694 now = time.time()
695 cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_warning_00;%d;%d;0;0;%d;lausser;blablubsvc" % (now, now, now + duration, duration)
696 print cmd
697 self.sched.run_external_command(cmd)
698 if self.nagios_installed():
699 self.nagios_extcmd(cmd)
700 cmd = "[%lu] SCHEDULE_HOST_DOWNTIME;test_host_0;%d;%d;0;0;%d;lausser;blablubhost" % (now, now, now + duration, duration)
701 print cmd
702 self.sched.run_external_command(cmd)
703 if self.nagios_installed():
704 self.nagios_extcmd(cmd)
705 self.update_broker()
706 self.scheduler_loop(1, [[svc, 0, 'OK']])
707 self.update_broker()
708 self.scheduler_loop(3, [[svc, 2, 'BAD']])
709 self.update_broker()
710 request = """GET downtimes
711 Filter: service_description =
712 Columns: author comment end_time entry_time fixed host_name id start_time
713 Separators: 10 59 44 124
714 ResponseHeader: fixed16"""
715 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
716 print response
717 if self.nagios_installed():
718 #time.sleep(10)
719 nagresponse = self.ask_nagios(request)
720 print "nagresponse----------------------------------------------"
721 print nagresponse
722 #TODO the entry_times are different. find a way to round the numbers
723 # so that they are equal
724 #self.assert_(self.lines_equal(response, nagresponse))
726 request = """GET comments
727 Filter: service_description =
728 Columns: author comment
729 Separators: 10 59 44 124
730 ResponseHeader: fixed16"""
731 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
732 print response
733 if self.nagios_installed():
734 time.sleep(10)
735 nagresponse = self.ask_nagios(request)
736 print "nagresponse----------------------------------------------"
737 print nagresponse
738 #self.assert_(self.lines_equal(response, nagresponse))
740 request = """GET services
741 Filter: has_been_checked = 1
742 Filter: check_type = 0
743 Stats: sum has_been_checked
744 Stats: sum latency
745 Separators: 10 59 44 124
746 ResponseHeader: fixed16"""
747 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
748 print response
750 request = """GET services
751 Filter: has_been_checked = 1
752 Filter: check_type = 0
753 Stats: sum has_been_checked
754 Stats: sum latency
755 Stats: sum execution_time
756 Stats: min latency
757 Stats: min execution_time
758 Stats: max latency
759 Stats: max execution_time
760 Separators: 10 59 44 124
761 ResponseHeader: fixed16"""
762 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
763 print response
765 request = """GET services\nFilter: has_been_checked = 1\nFilter: check_type = 0\nStats: sum has_been_checked as has_been_checked\nStats: sum latency as latency_sum\nStats: sum execution_time as execution_time_sum\nStats: min latency as latency_min\nStats: min execution_time as execution_time_min\nStats: max latency as latency_max\nStats: max execution_time as execution_time_max\n\nResponseHeader: fixed16"""
766 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
767 print response
769 request = """GET hostgroups\nColumnHeaders: on\nResponseHeader: fixed16"""
770 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
771 print response
772 if self.nagios_installed():
773 nagresponse = self.ask_nagios(request)
774 print "nagresponse----------------------------------------------"
775 print nagresponse
776 # TODO members_with_state
777 #self.assert_(self.lines_equal(response, nagresponse))
779 request = """GET hosts\nColumns: name groups\nColumnHeaders: on\nResponseHeader: fixed16"""
780 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
781 print response
782 if self.nagios_installed():
783 nagresponse = self.ask_nagios(request)
784 print "nagresponse----------------------------------------------"
785 print nagresponse
786 self.assert_(self.lines_equal(response, nagresponse))
788 request = """GET hostgroups\nColumns: name num_services num_services_ok\nColumnHeaders: on\nResponseHeader: fixed16"""
789 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
790 print response
791 if self.nagios_installed():
792 nagresponse = self.ask_nagios(request)
793 print "nagresponse----------------------------------------------"
794 print nagresponse
795 self.assert_(self.lines_equal(response, nagresponse))
797 request = """GET hostgroups\nColumns: name num_services_pending num_services_ok num_services_warning num_services_critical num_services_unknown worst_service_state worst_service_hard_state\nColumnHeaders: on\nResponseHeader: fixed16"""
798 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
799 print response
801 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']])
802 self.update_broker()
803 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 1, 'WARNING']])
804 self.update_broker()
806 print "WARNING SOFT;1"
807 # worst_service_state 1, worst_service_hard_state 0
808 request = """GET hostgroups\nColumns: name num_services_pending num_services_ok num_services_warn num_services_crit num_services_unknown worst_service_state worst_service_hard_state\nColumnHeaders: on\nResponseHeader: fixed16"""
809 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
810 print response
811 self.scheduler_loop(3, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 1, 'WARNING']])
812 self.update_broker()
813 print "WARNING HARD;3"
814 # worst_service_state 1, worst_service_hard_state 1
815 request = """GET hostgroups\nColumns: name num_services_pending num_services_ok num_services_warn num_services_crit num_services_unknown worst_service_state worst_service_hard_state\nColumnHeaders: on\nResponseHeader: fixed16"""
816 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
817 print response
818 for s in self.livestatus_broker.livestatus.services.values():
819 print "%s %d %s;%d" % (s.state, s.state_id, s.state_type, s.attempt)
822 def test_thruk_comments(self):
823 self.print_header()
824 host = self.sched.hosts.find_by_name("test_host_0")
825 host.checks_in_progress = []
826 host.act_depend_of = [] # ignore the router
827 router = self.sched.hosts.find_by_name("test_router_0")
828 router.checks_in_progress = []
829 router.act_depend_of = [] # ignore the router
830 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
831 svc.checks_in_progress = []
832 svc.act_depend_of = [] # no hostchecks on critical checkresults
833 duration = 600
834 now = time.time()
835 # downtime valid for the next 2 minutes
836 cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now, now, now + duration, duration)
837 self.sched.run_external_command(cmd)
838 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
839 svc.checks_in_progress = []
840 svc.act_depend_of = [] # no hostchecks on critical checkresults
841 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
843 print "downtime was scheduled. check its activity and the comment"
844 self.assert_(len(self.sched.downtimes) == 1)
845 self.assert_(len(svc.downtimes) == 1)
846 self.assert_(svc.downtimes[0] in self.sched.downtimes.values())
847 self.assert_(svc.downtimes[0].fixed)
848 self.assert_(svc.downtimes[0].is_in_effect)
849 self.assert_(not svc.downtimes[0].can_be_deleted)
850 self.assert_(len(self.sched.comments) == 1)
851 self.assert_(len(svc.comments) == 1)
852 self.assert_(svc.comments[0] in self.sched.comments.values())
853 self.assert_(svc.downtimes[0].comment_id == svc.comments[0].id)
855 now = time.time()
856 cmd = "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
857 self.sched.run_external_command(cmd)
858 #cmd = "[%lu] ADD_HOST_COMMENT;test_host_0;1;lausser;hcomment" % now
859 #self.sched.run_external_command(cmd)
860 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
861 self.assert_(len(self.sched.comments) == 2)
862 self.assert_(len(svc.comments) == 2)
864 self.update_broker()
865 svc_comment_list = (',').join([str(c.id) for c in svc.comments])
867 #request = """GET comments\nColumns: host_name service_description id source type author comment entry_time entry_type persistent expire_time expires\nFilter: service_description !=\nResponseHeader: fixed16\nOutputFormat: json\n"""
868 request = """GET services\nColumns: comments host_comments host_is_executing is_executing\nFilter: service_description !=\nResponseHeader: fixed16\nOutputFormat: json\n"""
869 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
870 print response
871 good_response = """200 17
872 [[[""" + svc_comment_list +"""],[],0,0]]
874 self.assert_(response == good_response) # json
876 request = """GET services\nColumns: comments host_comments host_is_executing is_executing\nFilter: service_description !=\nResponseHeader: fixed16\n"""
877 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
878 print response
879 good_response = """200 9
880 """ + svc_comment_list + """;;0;0
882 self.assert_(response == good_response) # csv
885 def test_thruk_logs(self):
886 self.print_header()
887 start = time.time()
888 host = self.sched.hosts.find_by_name("test_host_0")
889 host.checks_in_progress = []
890 host.act_depend_of = [] # ignore the router
891 router = self.sched.hosts.find_by_name("test_router_0")
892 router.checks_in_progress = []
893 router.act_depend_of = [] # ignore the router
894 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
895 svc.checks_in_progress = []
896 svc.act_depend_of = [] # no hostchecks on critical checkresults
897 self.scheduler_loop(3, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 1, 'WARNING']])
898 self.update_broker()
899 duration = 600
900 now = time.time()
901 # downtime valid for the next 2 minutes
902 cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now, now, now + duration, duration)
903 self.sched.run_external_command(cmd)
904 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
905 svc.checks_in_progress = []
906 svc.act_depend_of = [] # no hostchecks on critical checkresults
907 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
908 now = time.time()
909 cmd = "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
910 self.sched.run_external_command(cmd)
911 time.sleep(1)
912 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
913 self.update_broker()
914 time.sleep(1)
915 self.scheduler_loop(3, [[host, 2, 'DOWN'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
916 self.update_broker()
917 time.sleep(1)
918 self.scheduler_loop(3, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
919 self.update_broker()
920 end = time.time()
922 # show history for service
923 request = """GET log
924 Columns: time type options state
925 Filter: time >= """ + str(int(start)) + """
926 Filter: time <= """ + str(int(end)) + """
927 Filter: type = SERVICE ALERT
928 Filter: type = HOST ALERT
929 Filter: type = SERVICE FLAPPING ALERT
930 Filter: type = HOST FLAPPING ALERT
931 Filter: type = SERVICE DOWNTIME ALERT
932 Filter: type = HOST DOWNTIME ALERT
933 Or: 6
934 Filter: host_name = test_host_0
935 Filter: service_description = test_ok_0
936 And: 3
937 Filter: type ~ starting...
938 Filter: type ~ shutting down...
939 Or: 3
940 Filter: current_service_description !=
942 Filter: service_description =
943 Filter: host_name !=
944 And: 2
945 Filter: service_description =
946 Filter: host_name =
947 And: 2
948 Or: 3"""
950 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
951 print response
952 self.assert_(self.contains_line(response, 'SERVICE DOWNTIME ALERT;test_host_0;test_ok_0;STARTED; Service has entered a period of scheduled downtime'))
955 def test_thruk_logs_alerts_summary(self):
956 self.print_header()
957 start = time.time()
958 host = self.sched.hosts.find_by_name("test_host_0")
959 host.checks_in_progress = []
960 host.act_depend_of = [] # ignore the router
961 router = self.sched.hosts.find_by_name("test_router_0")
962 router.checks_in_progress = []
963 router.act_depend_of = [] # ignore the router
964 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
965 svc.checks_in_progress = []
966 svc.act_depend_of = [] # no hostchecks on critical checkresults
967 self.scheduler_loop(3, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 1, 'WARNING']])
968 self.update_broker()
969 duration = 600
970 now = time.time()
971 # downtime valid for the next 2 minutes
972 cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now, now, now + duration, duration)
973 self.sched.run_external_command(cmd)
974 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
975 svc.checks_in_progress = []
976 svc.act_depend_of = [] # no hostchecks on critical checkresults
977 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
978 now = time.time()
979 cmd = "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
980 self.sched.run_external_command(cmd)
981 time.sleep(1)
982 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
983 self.update_broker()
984 time.sleep(1)
985 self.scheduler_loop(3, [[host, 2, 'DOWN'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
986 self.update_broker()
987 time.sleep(1)
988 self.scheduler_loop(3, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
989 self.update_broker()
990 end = time.time()
992 # is this an error in thruk?
994 request = """GET log
995 Filter: options ~ ;HARD;
996 Filter: type = HOST ALERT
997 Filter: time >= 1284056080
998 Filter: time <= 1284660880
999 Filter: current_service_description !=
1000 Filter: service_description =
1001 Filter: host_name !=
1002 And: 2
1003 Filter: service_description =
1004 Filter: host_name =
1005 And: 2
1006 Or: 3
1007 Columns: time state state_type host_name service_description current_host_groups current_service_groups plugin_output"""
1009 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1010 print response
1013 def test_thruk_logs_current(self):
1014 self.print_header()
1015 start = time.time()
1016 host = self.sched.hosts.find_by_name("test_host_0")
1017 host.checks_in_progress = []
1018 host.act_depend_of = [] # ignore the router
1019 router = self.sched.hosts.find_by_name("test_router_0")
1020 router.checks_in_progress = []
1021 router.act_depend_of = [] # ignore the router
1022 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1023 svc.checks_in_progress = []
1024 svc.act_depend_of = [] # no hostchecks on critical checkresults
1025 self.scheduler_loop(3, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 1, 'WARNING']])
1026 self.update_broker()
1027 duration = 600
1028 now = time.time()
1029 # downtime valid for the next 2 minutes
1030 cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now, now, now + duration, duration)
1031 self.sched.run_external_command(cmd)
1032 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1033 svc.checks_in_progress = []
1034 svc.act_depend_of = [] # no hostchecks on critical checkresults
1035 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
1036 now = time.time()
1037 cmd = "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
1038 self.sched.run_external_command(cmd)
1039 time.sleep(1)
1040 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
1041 self.update_broker()
1042 time.sleep(1)
1043 self.scheduler_loop(3, [[host, 2, 'DOWN'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
1044 self.update_broker()
1045 time.sleep(1)
1046 self.scheduler_loop(3, [[host, 0, 'UUP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
1047 self.update_broker()
1048 # time.sleep(1)
1049 # self.scheduler_loop(3, [[host, 0, 'UP'], [router, 2, 'DOWN'], [svc, 0, 'OK']], do_sleep=False)
1050 # self.update_broker()
1051 end = time.time()
1053 # show history for service
1054 request = """GET log
1055 Columns: time type options state current_host_name
1056 Filter: time >= """ + str(int(start)) + """
1057 Filter: time <= """ + str(int(end)) + """
1058 Filter: type = SERVICE ALERT
1059 Filter: type = HOST ALERT
1060 Filter: type = SERVICE FLAPPING ALERT
1061 Filter: type = HOST FLAPPING ALERT
1062 Filter: type = SERVICE DOWNTIME ALERT
1063 Filter: type = HOST DOWNTIME ALERT
1064 Or: 6
1065 Filter: current_host_name = test_host_0
1066 Filter: current_service_description = test_ok_0
1067 And: 2"""
1068 request = """GET log
1069 Columns: time type options state current_host_name
1070 Filter: time >= """ + str(int(start)) + """
1071 Filter: time <= """ + str(int(end)) + """
1072 Filter: current_host_name = test_host_0
1073 Filter: current_service_description = test_ok_0
1074 And: 2"""
1076 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1077 print response
1080 def test_thruk_tac_svc(self):
1081 self.print_header()
1082 if self.nagios_installed():
1083 self.start_nagios('1r_1h_1s')
1084 self.update_broker()
1086 start = time.time()
1087 host = self.sched.hosts.find_by_name("test_host_0")
1088 host.checks_in_progress = []
1089 host.act_depend_of = [] # ignore the router
1090 router = self.sched.hosts.find_by_name("test_router_0")
1091 router.checks_in_progress = []
1092 router.act_depend_of = [] # ignore the router
1093 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1094 svc.checks_in_progress = []
1095 svc.act_depend_of = [] # no hostchecks on critical checkresults
1096 self.scheduler_loop(3, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 1, 'WARNING']])
1097 self.update_broker()
1098 duration = 600
1099 now = time.time()
1100 # downtime valid for the next 2 minutes
1101 cmd = "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now, now, now + duration, duration)
1102 self.sched.run_external_command(cmd)
1103 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1104 svc.checks_in_progress = []
1105 svc.act_depend_of = [] # no hostchecks on critical checkresults
1106 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
1107 now = time.time()
1108 cmd = "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
1109 self.sched.run_external_command(cmd)
1110 time.sleep(1)
1111 self.scheduler_loop(1, [[host, 0, 'UP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
1112 self.update_broker()
1113 time.sleep(1)
1114 self.scheduler_loop(3, [[host, 2, 'DOWN'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
1115 self.update_broker()
1116 time.sleep(1)
1117 self.scheduler_loop(3, [[host, 0, 'UUP'], [router, 0, 'UP'], [svc, 0, 'OK']], do_sleep=False)
1118 self.update_broker()
1119 # time.sleep(1)
1120 # self.scheduler_loop(3, [[host, 0, 'UP'], [router, 2, 'DOWN'], [svc, 0, 'OK']], do_sleep=False)
1121 # self.update_broker()
1122 end = time.time()
1124 # show history for service
1125 request = """GET services
1126 Filter: has_been_checked = 1
1127 Filter: check_type = 0
1128 Stats: sum has_been_checked
1129 Stats: sum latency
1130 Stats: sum execution_time
1131 Stats: min latency
1132 Stats: min execution_time
1133 Stats: max latency
1134 Stats: max execution_time"""
1136 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1137 print response
1138 # nagios comparison makes no sense, because the latencies/execution times will surely differ
1139 if self.nagios_installed():
1140 nagresponse = self.ask_nagios(request)
1141 # print nagresponse
1142 # self.assert_(self.lines_equal(response, nagresponse))
1145 def test_columns(self):
1146 self.print_header()
1147 self.update_broker()
1148 #---------------------------------------------------------------
1149 # get the columns meta-table
1150 #---------------------------------------------------------------
1151 request = """GET columns"""
1152 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1153 print response
1156 def test_scheduler_table(self):
1157 self.print_header()
1158 self.update_broker()
1160 creation_tab = {'scheduler_name' : 'scheduler-1', 'address' : 'localhost', 'spare' : '0'}
1161 schedlink = SchedulerLink(creation_tab)
1162 schedlink.pythonize()
1163 schedlink.alive = True
1164 b = schedlink.get_initial_status_brok()
1165 self.sched.add(b)
1166 creation_tab = {'scheduler_name' : 'scheduler-2', 'address' : 'othernode', 'spare' : '1'}
1167 schedlink = SchedulerLink(creation_tab)
1168 schedlink.pythonize()
1169 schedlink.alive = True
1170 b2 = schedlink.get_initial_status_brok()
1171 self.sched.add(b2)
1173 self.update_broker()
1174 #---------------------------------------------------------------
1175 # get the columns meta-table
1176 #---------------------------------------------------------------
1177 request = """GET schedulers"""
1178 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1179 print response
1180 good_response = """address;alive;name;port;spare;weight
1181 othernode;1;scheduler-2;7768;1;1
1182 localhost;1;scheduler-1;7768;0;1
1184 print response, 'FUCK'
1185 print "FUCK", response, "TOTO"
1186 self.assert_(self.lines_equal(response, good_response))
1188 #Now we update a scheduler state and we check
1189 #here the N2
1190 schedlink.alive = False
1191 b = schedlink.get_update_status_brok()
1192 self.sched.add(b)
1193 self.update_broker()
1194 request = """GET schedulers"""
1195 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1196 good_response = """address;alive;name;port;spare;weight
1197 othernode;0;scheduler-2;7768;1;1
1198 localhost;1;scheduler-1;7768;0;1
1200 self.assert_(self.lines_equal(response, good_response))
1204 def test_reactionner_table(self):
1205 self.print_header()
1206 self.update_broker()
1207 creation_tab = {'reactionner_name' : 'reactionner-1', 'address' : 'localhost', 'spare' : '0'}
1208 reac = ReactionnerLink(creation_tab)
1209 reac.pythonize()
1210 reac.alive = True
1211 b = reac.get_initial_status_brok()
1212 self.sched.add(b)
1213 creation_tab = {'reactionner_name' : 'reactionner-2', 'address' : 'othernode', 'spare' : '1'}
1214 reac = ReactionnerLink(creation_tab)
1215 reac.pythonize()
1216 reac.alive = True
1217 b2 = reac.get_initial_status_brok()
1218 self.sched.add(b2)
1220 self.update_broker()
1221 #---------------------------------------------------------------
1222 # get the columns meta-table
1223 #---------------------------------------------------------------
1224 request = """GET reactionners"""
1225 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1226 print response
1227 good_response = """address;alive;name;port;spare
1228 localhost;1;reactionner-1;7769;0
1229 othernode;1;reactionner-2;7769;1
1231 print response == good_response
1232 self.assert_(self.lines_equal(response, good_response))
1234 #Now the update part
1235 reac.alive = False
1236 b2 = reac.get_update_status_brok()
1237 self.sched.add(b2)
1238 self.update_broker()
1239 request = """GET reactionners"""
1240 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1241 print response
1242 good_response = """address;alive;name;port;spare
1243 localhost;1;reactionner-1;7769;0
1244 othernode;0;reactionner-2;7769;1
1246 print response == good_response
1247 self.assert_(self.lines_equal(response, good_response))
1251 def test_poller_table(self):
1252 self.print_header()
1253 self.update_broker()
1255 creation_tab = {'poller_name' : 'poller-1', 'address' : 'localhost', 'spare' : '0'}
1256 pol = PollerLink(creation_tab)
1257 pol.pythonize()
1258 pol.alive = True
1259 b = pol.get_initial_status_brok()
1260 self.sched.add(b)
1261 creation_tab = {'poller_name' : 'poller-2', 'address' : 'othernode', 'spare' : '1'}
1262 pol = PollerLink(creation_tab)
1263 pol.pythonize()
1264 pol.alive = True
1265 b2 = pol.get_initial_status_brok()
1266 self.sched.add(b2)
1268 self.update_broker()
1269 #---------------------------------------------------------------
1270 # get the columns meta-table
1271 #---------------------------------------------------------------
1272 request = """GET pollers"""
1273 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1274 print response
1275 good_response = """address;alive;name;port;spare
1276 localhost;1;poller-1;7771;0
1277 othernode;1;poller-2;7771;1
1279 print response == good_response
1280 self.assert_(self.lines_equal(response, good_response))
1282 #Now the update part
1283 pol.alive = False
1284 b2 = pol.get_update_status_brok()
1285 self.sched.add(b2)
1287 self.update_broker()
1288 #---------------------------------------------------------------
1289 # get the columns meta-table
1290 #---------------------------------------------------------------
1291 request = """GET pollers"""
1292 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1293 print response
1294 good_response = """address;alive;name;port;spare
1295 localhost;1;poller-1;7771;0
1296 othernode;0;poller-2;7771;1
1298 print response == good_response
1299 self.assert_(self.lines_equal(response, good_response))
1303 def test_broker_table(self):
1304 self.print_header()
1305 self.update_broker()
1307 creation_tab = {'broker_name' : 'broker-1', 'address' : 'localhost', 'spare' : '0'}
1308 pol = BrokerLink(creation_tab)
1309 pol.pythonize()
1310 pol.alive = True
1311 b = pol.get_initial_status_brok()
1312 self.sched.add(b)
1313 creation_tab = {'broker_name' : 'broker-2', 'address' : 'othernode', 'spare' : '1'}
1314 pol = BrokerLink(creation_tab)
1315 pol.pythonize()
1316 pol.alive = True
1317 b2 = pol.get_initial_status_brok()
1318 self.sched.add(b2)
1320 self.update_broker()
1321 #---------------------------------------------------------------
1322 # get the columns meta-table
1323 #---------------------------------------------------------------
1324 request = """GET brokers"""
1325 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1326 print response
1327 good_response = """address;alive;name;port;spare
1328 localhost;1;broker-1;7772;0
1329 othernode;1;broker-2;7772;1
1331 print response == good_response
1332 self.assert_(response == good_response)
1334 #Now the update part
1335 pol.alive = False
1336 b2 = pol.get_initial_status_brok()
1337 self.sched.add(b2)
1339 self.update_broker()
1340 #---------------------------------------------------------------
1341 # get the columns meta-table
1342 #---------------------------------------------------------------
1343 request = """GET brokers"""
1344 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1345 print response
1346 good_response = """address;alive;name;port;spare
1347 localhost;1;broker-1;7772;0
1348 othernode;0;broker-2;7772;1
1350 print response == good_response
1351 self.assert_(response == good_response)
1355 def test_problems_table(self):
1356 self.print_header()
1357 self.update_broker()
1358 host = self.sched.hosts.find_by_name("test_host_0")
1359 host.checks_in_progress = []
1360 host.act_depend_of = [] # ignore the router
1361 router = self.sched.hosts.find_by_name("test_router_0")
1362 router.checks_in_progress = []
1363 router.act_depend_of = [] # ignore the router
1364 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1365 svc.checks_in_progress = []
1366 svc.act_depend_of = [] # no hostchecks on critical checkresults
1367 self.scheduler_loop(4, [[host, 2, 'DOWN'], [router, 2, 'DOWN'], [svc, 2, 'BAD']])
1368 print "Is router a problem?", router.is_problem, router.state, router.state_type
1369 print "Is host a problem?", host.is_problem, host.state, host.state_type
1370 print "Is service a problem?", svc.is_problem, svc.state, svc.state_type
1371 self.update_broker()
1372 print "All", self.livestatus_broker.hosts
1373 for h in self.livestatus_broker.hosts.values():
1374 print h.get_dbg_name(), h.is_problem
1376 #---------------------------------------------------------------
1377 # get the columns meta-table
1378 #---------------------------------------------------------------
1379 request = """GET problems"""
1380 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1381 print "FUCK", response
1382 good_response = """impacts;source
1383 test_host_0,test_host_0/test_ok_0;test_router_0
1385 print response == good_response
1386 self.assert_(response == good_response)
1390 def test_limit(self):
1391 self.print_header()
1392 if self.nagios_installed():
1393 self.start_nagios('1r_1h_1s')
1394 now = time.time()
1395 self.update_broker()
1396 #---------------------------------------------------------------
1397 # get the full hosts table
1398 #---------------------------------------------------------------
1399 request = 'GET hosts\nColumns: host_name\n'
1400 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1401 print response
1402 good_response = """test_host_0
1403 test_router_0
1405 self.assert_(self.lines_equal(response, good_response))
1407 request = 'GET hosts\nColumns: host_name\nLimit: 1\n'
1408 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1409 print response
1410 good_response = """test_host_0
1412 # it must be test_host_0 because with Limit: the output is
1413 # alphabetically ordered
1414 self.assert_(response == good_response)
1415 # TODO look whats wrong
1416 if self.nagios_installed():
1417 nagresponse = self.ask_nagios(request)
1418 # print nagresponse
1419 # self.assert_(self.lines_equal(response, nagresponse))
1423 def test_problem_impact_in_host_service(self):
1424 self.print_header()
1425 now = time.time()
1426 self.update_broker()
1428 host_router_0 = self.sched.hosts.find_by_name("test_router_0")
1429 host_router_0.checks_in_progress = []
1431 #Then initialize host under theses routers
1432 host_0 = self.sched.hosts.find_by_name("test_host_0")
1433 host_0.checks_in_progress = []
1435 all_hosts = [host_router_0, host_0]
1436 all_routers = [host_router_0]
1437 all_servers = [host_0]
1439 print "- 4 x UP -------------------------------------"
1440 self.scheduler_loop(1, [[host_router_0, 0, 'UP'], [host_0, 0, 'UP']], do_sleep=False)
1441 self.scheduler_loop(1, [[host_router_0, 1, 'DOWN']], do_sleep=False)
1442 self.scheduler_loop(1, [[host_router_0, 1, 'DOWN']], do_sleep=False)
1443 self.scheduler_loop(1, [[host_router_0, 1, 'DOWN']], do_sleep=False)
1444 self.scheduler_loop(1, [[host_router_0, 1, 'DOWN']], do_sleep=False)
1445 self.scheduler_loop(1, [[host_router_0, 1, 'DOWN']], do_sleep=False)
1447 #Max attempt is reach, should be HARD now
1448 for h in all_routers:
1449 self.assert_(h.state == 'DOWN')
1450 self.assert_(h.state_type == 'HARD')
1452 for b in self.sched.broks.values():
1453 print "All broks", b.type, b
1454 if b.type == 'update_host_status':
1455 print "***********"
1456 print "Impacts", b.data['impacts']
1457 print "Sources", b.data['source_problems']
1459 for b in host_router_0.broks:
1460 print " host_router_0.broks", b
1462 self.update_broker()
1464 print "source de host_0", host_0.source_problems
1465 for i in host_0.source_problems:
1466 print "source", i.get_name()
1467 print "impacts de host_router_0", host_router_0.impacts
1468 for i in host_router_0.impacts:
1469 print "impact", i.get_name()
1471 #---------------------------------------------------------------
1472 # get the full hosts table
1473 #---------------------------------------------------------------
1474 print "Got source problems"
1475 request = 'GET hosts\nColumns: host_name is_impact source_problems\n'
1476 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1477 print "moncullulu2", response
1478 good_response = """test_router_0;0;
1479 test_host_0;1;test_router_0
1481 #self.assert_(self.lines_equal(response, good_response))
1483 print "Now got impact"
1484 request = 'GET hosts\nColumns: host_name is_problem impacts\n'
1485 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1486 print "moncululu", response
1487 good_response = """test_router_0;1;test_host_0,test_host_0/test_ok_0
1488 test_host_0;0;"""
1489 self.assert_(self.lines_equal(response.strip(), good_response.strip()))
1491 request = 'GET hosts\nColumns: host_name\nLimit: 1\n'
1492 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1493 print response
1494 good_response = """test_host_0
1496 # it must be test_host_0 because with Limit: the output is
1497 # alphabetically ordered
1498 self.assert_(response == good_response)
1502 def test_thruk_servicegroup(self):
1503 self.print_header()
1504 now = time.time()
1505 self.update_broker()
1506 #---------------------------------------------------------------
1507 # get services of a certain servicegroup
1508 # test_host_0/test_ok_0 is in
1509 # servicegroup_01,ok via service.servicegroups
1510 # servicegroup_02 via servicegroup.members
1511 #---------------------------------------------------------------
1512 request = """GET services
1513 Columns: host_name service_description
1514 Filter: groups >= servicegroup_01
1515 OutputFormat: csv
1516 ResponseHeader: fixed16
1518 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1519 self.assert_(response == """200 22
1520 test_host_0;test_ok_0
1521 """)
1522 request = """GET services
1523 Columns: host_name service_description
1524 Filter: groups >= servicegroup_02
1525 OutputFormat: csv
1526 ResponseHeader: fixed16
1528 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1529 self.assert_(response == """200 22
1530 test_host_0;test_ok_0
1531 """)
1535 def test_is_executing(self):
1536 self.print_header()
1537 #---------------------------------------------------------------
1538 # make sure that the is_executing flag is updated regularly
1539 #---------------------------------------------------------------
1540 now = time.time()
1541 host = self.sched.hosts.find_by_name("test_host_0")
1542 host.checks_in_progress = []
1543 host.act_depend_of = [] # ignore the router
1544 router = self.sched.hosts.find_by_name("test_router_0")
1545 router.checks_in_progress = []
1546 router.act_depend_of = [] # ignore the router
1547 svc = self.sched.services.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1548 svc.checks_in_progress = []
1549 svc.act_depend_of = [] # no hostchecks on critical checkresults
1551 for loop in range(1, 2):
1552 print "processing check", loop
1553 self.show_broks("update_in_checking")
1554 svc.update_in_checking()
1555 self.show_broks("fake_check")
1556 self.fake_check(svc, 2, 'BAD')
1557 self.show_broks("sched.consume_results")
1558 self.sched.consume_results()
1559 self.show_broks("sched.get_new_actions")
1560 self.sched.get_new_actions()
1561 self.show_broks("sched.get_new_broks")
1562 self.sched.get_new_broks()
1563 self.show_broks("sched.delete_zombie_checks")
1564 self.sched.delete_zombie_checks()
1565 self.show_broks("sched.delete_zombie_actions")
1566 self.sched.delete_zombie_actions()
1567 self.show_broks("sched.get_to_run_checks")
1568 checks = self.sched.get_to_run_checks(True, False)
1569 self.show_broks("sched.get_to_run_checks")
1570 actions = self.sched.get_to_run_checks(False, True)
1571 #self.show_actions()
1572 for a in actions:
1573 a.status = 'inpoller'
1574 a.check_time = time.time()
1575 a.exit_status = 0
1576 self.sched.put_results(a)
1577 #self.show_actions()
1579 svc.checks_in_progress = []
1580 self.show_broks("sched.update_downtimes_and_comments")
1581 self.sched.update_downtimes_and_comments()
1582 time.sleep(5)
1584 print "-------------------------------------------------"
1585 for brok in sorted(self.sched.broks.values(), lambda x, y: x.id - y.id):
1586 if re.compile('^service_').match(brok.type):
1587 print "BROK:", brok.type
1588 print "BROK ", brok.data['in_checking']
1589 self.update_broker()
1590 print "-------------------------------------------------"
1591 request = 'GET services\nColumns: service_description is_executing\n'
1592 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1593 print response
1597 def test_pnp_path(self):
1598 self.print_header()
1599 now = time.time()
1600 self.update_broker()
1601 #---------------------------------------------------------------
1602 # pnp_path is a parameter for the module
1603 # column pnpgraph_present checks if a file
1604 # <pnp_path>/host/service.xml
1605 # <pnp_path>/host/_HOST_.xml
1606 # exists
1607 #---------------------------------------------------------------
1608 pnp_path = 'tmp/pnp4nagios_test' + str(os.getpid())
1609 try:
1610 os.removedirs(pnp_path)
1611 except:
1612 pass
1613 else:
1614 print "there is no spool dir", pnp_path
1616 request = """GET services
1617 Columns: host_name service_description pnpgraph_present
1618 OutputFormat: csv
1619 ResponseHeader: fixed16
1621 requesth = """GET hosts
1622 Columns: host_name pnpgraph_present
1623 OutputFormat: csv
1624 ResponseHeader: fixed16
1626 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1627 print response
1628 self.assert_(response == """200 24
1629 test_host_0;test_ok_0;0
1630 """)
1631 #self.assert_(not self.livestatus_broker.livestatus.pnp_path)
1633 try:
1634 os.makedirs(pnp_path)
1635 print "there is an empty spool dir", pnp_path
1636 except:
1637 pass
1639 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1640 print response
1641 self.assert_(response == """200 24
1642 test_host_0;test_ok_0;0
1643 """)
1644 self.assert_(self.livestatus_broker.livestatus.pnp_path == pnp_path + '/')
1646 try:
1647 os.makedirs(pnp_path + '/test_host_0')
1648 open(pnp_path + '/test_host_0/_HOST_.xml', 'w').close()
1649 open(pnp_path + '/test_host_0/test_ok_0.xml', 'w').close()
1650 print "there is a spool dir with data", pnp_path
1651 except:
1652 pass
1654 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1655 print response
1656 self.assert_(response == """200 24
1657 test_host_0;test_ok_0;1
1658 """)
1659 response, keepalive = self.livestatus_broker.livestatus.handle_request(requesth)
1660 print response
1661 self.assert_(response == """200 30
1662 test_router_0;0
1663 test_host_0;1
1664 """)
1667 def test_thruk_action_notes_url(self):
1668 self.print_header()
1669 now = time.time()
1670 self.update_broker()
1671 request = """GET services
1672 Columns: host_name service_description action_url
1673 Filter: host_name = test_host_0
1674 Filter: service_description = test_ok_0
1675 OutputFormat: csv
1676 ResponseHeader: fixed16
1678 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1679 self.assert_(response == """200 78
1680 test_host_0;test_ok_0;/nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
1681 """)
1683 request = """GET services
1684 Columns: host_name service_description action_url_expanded
1685 Filter: host_name = test_host_0
1686 Filter: service_description = test_ok_0
1687 OutputFormat: csv
1688 ResponseHeader: fixed16
1690 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1691 self.assert_(response == """200 75
1692 test_host_0;test_ok_0;/nagios/pnp/index.php?host=test_host_0&srv=test_ok_0
1693 """)
1695 request = """GET hosts
1696 Columns: host_name action_url_expanded notes_url_expanded
1697 Filter: host_name = test_host_0
1698 OutputFormat: csv
1699 ResponseHeader: fixed16
1701 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1702 self.assert_(response == """200 85
1703 test_host_0;/nagios/pnp/index.php?host=test_host_0;/nagios/wiki/doku.php/test_host_0
1704 """)
1708 def test_thruk_custom_variables(self):
1709 self.print_header()
1710 now = time.time()
1711 self.update_broker()
1712 request = """GET hosts
1713 Columns: host_name custom_variable_names custom_variable_values
1714 Filter: host_name = test_host_0
1715 OutputFormat: csv
1716 ResponseHeader: fixed16
1718 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1719 self.assert_(response == """200 42
1720 test_host_0;OSLICENSE,OSTYPE;gpl,gnulinux
1721 """)
1723 request = """GET services
1724 Columns: host_name service_description custom_variable_names custom_variable_values
1725 Filter: host_name = test_host_0
1726 Filter: service_description = test_ok_0
1727 OutputFormat: csv
1728 ResponseHeader: fixed16
1730 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1731 self.assert_(response == """200 41
1732 test_host_0;test_ok_0;CUSTNAME;custvalue
1733 """)
1737 class TestConfigBig(TestConfig):
1738 def setUp(self):
1739 self.setup_with_file('etc/nagios_5r_100h_2000s.cfg')
1740 self.livestatus_broker = Livestatus_broker('livestatus', '127.0.0.1', str(50000 + os.getpid()), 'live', 'tmp/livelogs.db' + str(os.getpid()), 'tmp/pnp4nagios_test' + str(os.getpid()))
1741 self.livestatus_broker.properties = {
1742 'to_queue' : 0,
1743 'from_queue' : 0
1746 self.livestatus_broker.init()
1747 print "Cleaning old broks?"
1748 self.sched.fill_initial_broks()
1749 self.update_broker()
1752 def tearDown(self):
1753 self.stop_nagios()
1754 self.livestatus_broker.dbconn.close()
1755 if os.path.exists('tmp/livelogs.db' + str(os.getpid())):
1756 os.remove('tmp/livelogs.db' + str(os.getpid()))
1759 def test_stats(self):
1760 self.print_header()
1761 if self.nagios_installed():
1762 self.start_nagios('5r_100h_2000s')
1763 now = time.time()
1764 objlist = []
1765 for host in self.sched.hosts:
1766 objlist.append([host, 0, 'UP'])
1767 for service in self.sched.services:
1768 objlist.append([service, 0, 'OK'])
1769 self.scheduler_loop(1, objlist)
1770 self.update_broker()
1771 svc1 = self.sched.services.find_srv_by_name_and_hostname("test_host_005", "test_ok_00")
1772 print svc1
1773 svc2 = self.sched.services.find_srv_by_name_and_hostname("test_host_005", "test_ok_15")
1774 print svc2
1775 svc3 = self.sched.services.find_srv_by_name_and_hostname("test_host_005", "test_ok_16")
1776 print svc3
1777 svc4 = self.sched.services.find_srv_by_name_and_hostname("test_host_007", "test_ok_05")
1778 print svc4
1779 svc5 = self.sched.services.find_srv_by_name_and_hostname("test_host_007", "test_ok_11")
1780 svc6 = self.sched.services.find_srv_by_name_and_hostname("test_host_025", "test_ok_01")
1781 svc7 = self.sched.services.find_srv_by_name_and_hostname("test_host_025", "test_ok_03")
1782 self.scheduler_loop(1, [[svc1, 1, 'W'], [svc2, 1, 'W'], [svc3, 1, 'W'], [svc4, 2, 'C'], [svc5, 3, 'U'], [svc6, 2, 'C'], [svc7, 2, 'C']])
1783 self.update_broker()
1784 # 1993O, 3xW, 3xC, 1xU
1786 request = """GET services
1787 Filter: contacts >= test_contact
1788 Stats: state != 9999
1789 Stats: state = 0
1790 Stats: state = 1
1791 Stats: state = 2
1792 Stats: state = 3"""
1793 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1794 print 'query_6_______________\n%s\n%s\n' % (request, response)
1795 self.assert_(response == '2000;1993;3;3;1\n')
1796 if self.nagios_installed():
1797 nagresponse = self.ask_nagios(request)
1798 print nagresponse
1799 self.assert_(self.lines_equal(response, nagresponse))
1802 def test_statsgroupby(self):
1803 self.print_header()
1804 if self.nagios_installed():
1805 self.start_nagios('5r_100h_2000s')
1806 now = time.time()
1807 objlist = []
1808 for host in self.sched.hosts:
1809 objlist.append([host, 0, 'UP'])
1810 for service in self.sched.services:
1811 objlist.append([service, 0, 'OK'])
1812 self.scheduler_loop(1, objlist)
1813 self.update_broker()
1814 svc1 = self.sched.services.find_srv_by_name_and_hostname("test_host_005", "test_ok_00")
1815 print svc1
1816 svc2 = self.sched.services.find_srv_by_name_and_hostname("test_host_005", "test_ok_15")
1817 print svc2
1818 svc3 = self.sched.services.find_srv_by_name_and_hostname("test_host_005", "test_ok_16")
1819 print svc3
1820 svc4 = self.sched.services.find_srv_by_name_and_hostname("test_host_007", "test_ok_05")
1821 print svc4
1822 svc5 = self.sched.services.find_srv_by_name_and_hostname("test_host_007", "test_ok_11")
1823 svc6 = self.sched.services.find_srv_by_name_and_hostname("test_host_025", "test_ok_01")
1824 svc7 = self.sched.services.find_srv_by_name_and_hostname("test_host_025", "test_ok_03")
1825 self.scheduler_loop(1, [[svc1, 1, 'W'], [svc2, 1, 'W'], [svc3, 1, 'W'], [svc4, 2, 'C'], [svc5, 3, 'U'], [svc6, 2, 'C'], [svc7, 2, 'C']])
1826 self.update_broker()
1827 # 1993O, 3xW, 3xC, 1xU
1829 request = 'GET services\nFilter: contacts >= test_contact\nStats: state != 9999\nStats: state = 0\nStats: state = 1\nStats: state = 2\nStats: state = 3\nStatsGroupBy: host_name'
1830 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1831 print response
1832 self.assert_(self.contains_line(response, 'test_host_005;20;17;3;0;0'))
1833 self.assert_(self.contains_line(response, 'test_host_007;20;18;0;1;1'))
1834 self.assert_(self.contains_line(response, 'test_host_025;20;18;0;2;0'))
1835 self.assert_(self.contains_line(response, 'test_host_026;20;20;0;0;0'))
1837 request = """GET services
1838 Stats: state != 9999
1839 StatsGroupBy: state
1841 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1842 print response
1843 self.assert_(self.contains_line(response, '0;1993'))
1844 self.assert_(self.contains_line(response, '1;3'))
1845 self.assert_(self.contains_line(response, '2;3'))
1846 self.assert_(self.contains_line(response, '3;1'))
1847 if self.nagios_installed():
1848 nagresponse = self.ask_nagios(request)
1849 print nagresponse
1850 self.assert_(self.lines_equal(response, nagresponse))
1853 def test_hostsbygroup(self):
1854 self.print_header()
1855 if self.nagios_installed():
1856 self.start_nagios('5r_100h_2000s')
1857 now = time.time()
1858 objlist = []
1859 for host in self.sched.hosts:
1860 objlist.append([host, 0, 'UP'])
1861 for service in self.sched.services:
1862 objlist.append([service, 0, 'OK'])
1863 self.scheduler_loop(1, objlist)
1864 self.update_broker()
1865 request = """GET hostsbygroup
1866 ColumnHeaders: on
1867 Columns: host_name hostgroup_name
1868 OutputFormat: csv
1869 KeepAlive: on
1870 ResponseHeader: fixed16
1873 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1874 print response
1875 if self.nagios_installed():
1876 nagresponse = self.ask_nagios(request)
1877 print nagresponse
1878 self.assert_(self.lines_equal(response, nagresponse))
1881 def test_servicesbyhostgroup(self):
1882 self.print_header()
1883 if self.nagios_installed():
1884 self.start_nagios('5r_100h_2000s')
1885 now = time.time()
1886 objlist = []
1887 for host in self.sched.hosts:
1888 objlist.append([host, 0, 'UP'])
1889 for service in self.sched.services:
1890 objlist.append([service, 0, 'OK'])
1891 self.scheduler_loop(1, objlist)
1892 self.update_broker()
1893 request = """GET servicesbyhostgroup
1894 Filter: host_groups >= up
1895 Stats: has_been_checked = 0
1896 Stats: state = 0
1897 Stats: has_been_checked != 0
1898 Stats: scheduled_downtime_depth = 0
1899 Stats: host_scheduled_downtime_depth = 0
1900 StatsAnd: 4
1901 Stats: state = 0
1902 Stats: scheduled_downtime_depth > 0
1903 Stats: host_scheduled_downtime_depth > 0
1904 StatsAnd: 3
1905 Stats: state = 1
1906 Stats: acknowledged = 0
1907 Stats: host_acknowledged = 0
1908 Stats: scheduled_downtime_depth = 0
1909 Stats: host_scheduled_downtime_depth = 0
1910 StatsAnd: 5
1911 Stats: state = 1
1912 Stats: acknowledged = 1
1913 Stats: host_acknowledged = 1
1914 StatsOr: 2
1915 StatsAnd: 2
1916 Stats: state = 1
1917 Stats: scheduled_downtime_depth > 0
1918 Stats: host_scheduled_downtime_depth > 0
1919 StatsOr: 2
1920 StatsAnd: 2
1921 Stats: state = 2
1922 Stats: acknowledged = 0
1923 Stats: host_acknowledged = 0
1924 Stats: scheduled_downtime_depth = 0
1925 Stats: host_scheduled_downtime_depth = 0
1926 StatsAnd: 5
1927 Stats: state = 2
1928 Stats: acknowledged = 1
1929 Stats: host_acknowledged = 1
1930 StatsOr: 2
1931 StatsAnd: 2
1932 Stats: state = 2
1933 Stats: scheduled_downtime_depth > 0
1934 Stats: host_scheduled_downtime_depth > 0
1935 StatsOr: 2
1936 StatsAnd: 2
1937 Stats: state = 3
1938 Stats: acknowledged = 0
1939 Stats: host_acknowledged = 0
1940 Stats: scheduled_downtime_depth = 0
1941 Stats: host_scheduled_downtime_depth = 0
1942 StatsAnd: 5
1943 Stats: state = 3
1944 Stats: acknowledged = 1
1945 Stats: host_acknowledged = 1
1946 StatsOr: 2
1947 StatsAnd: 2
1948 Stats: state = 3
1949 Stats: scheduled_downtime_depth > 0
1950 Stats: host_scheduled_downtime_depth > 0
1951 StatsOr: 2
1952 StatsAnd: 2
1953 StatsGroupBy: hostgroup_name
1954 OutputFormat: csv
1955 KeepAlive: on
1956 ResponseHeader: fixed16
1958 tic = time.clock()
1959 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
1960 tac = time.clock()
1961 print "livestatus duration %f" % (tac - tic)
1962 print response
1963 if self.nagios_installed():
1964 nagresponse = self.ask_nagios(request)
1965 print nagresponse
1966 self.assert_(self.lines_equal(response, nagresponse))
1968 # Again, without Filter:
1969 request = """GET servicesbyhostgroup
1970 Stats: has_been_checked = 0
1971 Stats: state = 0
1972 Stats: has_been_checked != 0
1973 Stats: scheduled_downtime_depth = 0
1974 Stats: host_scheduled_downtime_depth = 0
1975 StatsAnd: 4
1976 Stats: state = 0
1977 Stats: scheduled_downtime_depth > 0
1978 Stats: host_scheduled_downtime_depth > 0
1979 StatsAnd: 3
1980 Stats: state = 1
1981 Stats: acknowledged = 0
1982 Stats: host_acknowledged = 0
1983 Stats: scheduled_downtime_depth = 0
1984 Stats: host_scheduled_downtime_depth = 0
1985 StatsAnd: 5
1986 Stats: state = 1
1987 Stats: acknowledged = 1
1988 Stats: host_acknowledged = 1
1989 StatsOr: 2
1990 StatsAnd: 2
1991 Stats: state = 1
1992 Stats: scheduled_downtime_depth > 0
1993 Stats: host_scheduled_downtime_depth > 0
1994 StatsOr: 2
1995 StatsAnd: 2
1996 Stats: state = 2
1997 Stats: acknowledged = 0
1998 Stats: host_acknowledged = 0
1999 Stats: scheduled_downtime_depth = 0
2000 Stats: host_scheduled_downtime_depth = 0
2001 StatsAnd: 5
2002 Stats: state = 2
2003 Stats: acknowledged = 1
2004 Stats: host_acknowledged = 1
2005 StatsOr: 2
2006 StatsAnd: 2
2007 Stats: state = 2
2008 Stats: scheduled_downtime_depth > 0
2009 Stats: host_scheduled_downtime_depth > 0
2010 StatsOr: 2
2011 StatsAnd: 2
2012 Stats: state = 3
2013 Stats: acknowledged = 0
2014 Stats: host_acknowledged = 0
2015 Stats: scheduled_downtime_depth = 0
2016 Stats: host_scheduled_downtime_depth = 0
2017 StatsAnd: 5
2018 Stats: state = 3
2019 Stats: acknowledged = 1
2020 Stats: host_acknowledged = 1
2021 StatsOr: 2
2022 StatsAnd: 2
2023 Stats: state = 3
2024 Stats: scheduled_downtime_depth > 0
2025 Stats: host_scheduled_downtime_depth > 0
2026 StatsOr: 2
2027 StatsAnd: 2
2028 StatsGroupBy: hostgroup_name
2029 OutputFormat: csv
2030 KeepAlive: on
2031 ResponseHeader: fixed16
2033 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
2034 print response
2035 if self.nagios_installed():
2036 nagresponse = self.ask_nagios(request)
2037 print nagresponse
2038 self.assert_(self.lines_equal(response, nagresponse))
2041 def test_childs(self):
2042 if self.nagios_installed():
2043 self.start_nagios('5r_100h_2000s')
2044 self.print_header()
2045 now = time.time()
2046 objlist = []
2047 for host in self.sched.hosts:
2048 objlist.append([host, 0, 'UP'])
2049 for service in self.sched.services:
2050 objlist.append([service, 0, 'OK'])
2051 self.scheduler_loop(1, objlist)
2052 self.update_broker()
2053 request = """GET hosts
2054 Columns: childs
2055 Filter: name = test_host_0
2056 OutputFormat:csv
2057 KeepAlive: on
2058 ResponseHeader: fixed16
2060 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
2061 print response
2062 if self.nagios_installed():
2063 nagresponse = self.ask_nagios(request)
2064 print "nagresponse----------------------------------------------"
2065 print nagresponse
2066 self.assert_(self.lines_equal(response, nagresponse))
2067 request = """GET hosts
2068 Columns: childs
2069 Filter: name = test_router_0
2070 OutputFormat:csv
2071 KeepAlive: on
2072 ResponseHeader: fixed16
2074 response, keepalive = self.livestatus_broker.livestatus.handle_request(request)
2075 print response
2076 if self.nagios_installed():
2077 nagresponse = self.ask_nagios(request)
2078 print "nagresponse----------------------------------------------"
2079 print nagresponse
2080 self.assert_(self.lines_equal(response, nagresponse))
2083 if __name__ == '__main__':
2084 #import cProfile
2085 command = """unittest.main()"""
2086 unittest.main()
2087 #cProfile.runctx( command, globals(), locals(), filename="Thruk.profile" )