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 *
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
):
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()
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
)
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
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
:
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
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
):
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']
110 request
= 'GET services\nColumns: service_description is_executing\n'
111 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
115 def nagios_installed(self
, path
='/usr/local/nagios/bin/nagios', livestatus
='/usr/local/nagios/lib/mk-livestatus/livestatus.o'):
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
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()
134 newconfig
= open('etc/nagios_' + new_configname
+ '.cfg', 'w')
136 if re
.search('^resource_file=', line
):
137 newconfig
.write("resource_file=etc/resource.cfg\n")
138 elif re
.search('shinken\-specific\.cfg', line
):
140 elif re
.search('enable_problem_impacts_states_change', line
):
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")
161 newconfig
.write(line
)
162 newconfig
.write('broker_module=/usr/local/nagios/lib/mk-livestatus/livestatus.o var/live' + "\n")
164 for dirfile
in os
.walk('etc/' + configname
):
165 dirpath
, dirlist
, filelist
= dirfile
166 newdirpath
= re
.sub(configname
, new_configname
, dirpath
)
168 for file in [f
for f
in filelist
if re
.search('\.cfg$', f
)]:
169 config
= open(dirpath
+ '/' + file)
170 text
= config
.readlines()
172 newconfig
= open(newdirpath
+ '/' + file, 'w')
174 if re
.search('^\s*criticity', line
):
176 elif re
.search('enable_problem_impacts_states_change', line
):
179 newconfig
.write(line
)
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):
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()
201 def stop_nagios(self
):
202 if self
.nagios_installed():
203 print "i stop nagios!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
205 if hasattr(self
, 'nagios_proc'):
207 while self
.nagios_proc
.poll() == None and attempt
< 4:
208 self
.nagios_proc
.terminate()
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:
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
)
226 out
, err
= unixcat
.communicate(request
)
228 print "mklivestatus duration %f" % (tac
- tic
)
230 while unixcat
.poll() == None and attempt
< 4:
234 if unixcat
.poll() == None:
236 print "unixcat says", out
240 def nagios_loop(self
, count
, reflist
, do_sleep
=False, sleep_time
=61):
242 buffer = open('var/pipebuffer', 'w')
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
)
250 cmd
= "[%lu] PROCESS_HOST_CHECK_RESULT;%s;%d;%s\n" % (now
, obj
.host_name
, exit_status
, output
)
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')
262 def nagios_extcmd(self
, cmd
):
263 fifo
= open('var/nagios.cmd', 'w')
271 class TestConfigSmall(TestConfig
):
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
= {
280 self
.livestatus_broker
.init()
281 print "Cleaning old broks?"
282 self
.sched
.fill_initial_broks()
284 self
.nagios_path
= None
285 self
.livestatus_path
= None
286 self
.nagios_config
= None
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')
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
)
310 request
= """GET hosts
312 Filter: name = test_host_0
315 ResponseHeader: fixed16
317 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
319 if self
.nagios_installed():
320 nagresponse
= self
.ask_nagios(request
)
321 print "nagresponse----------------------------------------------"
323 self
.assert_(self
.lines_equal(response
, nagresponse
))
324 request
= """GET hosts
326 Filter: name = test_router_0
329 ResponseHeader: fixed16
331 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
333 if self
.nagios_installed():
334 nagresponse
= self
.ask_nagios(request
)
335 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')
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
)
353 request
= """GET servicesbyhostgroup
354 Filter: host_groups >= allhosts
355 Columns: hostgroup_name host_name service_description groups
358 ResponseHeader: fixed16
360 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
362 if self
.nagios_installed():
363 nagresponse
= self
.ask_nagios(request
)
364 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
373 ResponseHeader: fixed16
375 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
377 if self
.nagios_installed():
378 nagresponse
= self
.ask_nagios(request
)
379 print "nagresponse----------------------------------------------"
381 self
.assert_(self
.lines_equal(response
, nagresponse
))
384 def test_hostsbygroup(self
):
386 if self
.nagios_installed():
387 self
.start_nagios('1r_1h_1s')
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
)
396 request
= """GET hostsbygroup
398 Columns: host_name hostgroup_name
399 Filter: groups >= allhosts
402 ResponseHeader: fixed16
405 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
407 if self
.nagios_installed():
408 nagresponse
= self
.ask_nagios(request
)
409 print "nagresponse----------------------------------------------"
411 self
.assert_(self
.lines_equal(response
, nagresponse
))
414 def test_status(self
):
416 if self
.nagios_installed():
417 self
.start_nagios('1r_1h_1s')
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']])
430 #---------------------------------------------------------------
431 # get the full hosts table
432 #---------------------------------------------------------------
433 request
= 'GET hosts'
434 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
436 if self
.nagios_installed():
437 nagresponse
= self
.ask_nagios(request
)
438 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
)
449 if self
.nagios_installed():
450 nagresponse
= self
.ask_nagios(request
)
451 print "nagresponse----------------------------------------------"
453 self
.assert_(self
.lines_equal(response
, nagresponse
))
455 #---------------------------------------------------------------
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----------------------------------------------"
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 #---------------------------------------------------------------
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----------------------------------------------"
480 self
.assert_(self
.lines_equal(response
, nagresponse
))
482 #---------------------------------------------------------------
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')
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
)
503 self
.scheduler_loop(1, [[svc
, 0, 'OK']])
505 self
.scheduler_loop(3, [[svc
, 2, 'BAD']])
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 #---------------------------------------------------------------
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 #---------------------------------------------------------------
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 #---------------------------------------------------------------
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----------------------------------------------"
539 # TODO looks like a timing problem with nagios
540 #self.assert_(self.lines_equal(response, nagresponse))
545 print "got initial broks"
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']])
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
):
571 if self
.nagios_installed():
572 self
.start_nagios('1r_1h_1s')
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']])
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
)
592 request
= """GET hosts
594 Stats: check_type = 0
595 Stats: check_type = 1
596 Stats: has_been_checked = 1
599 Stats: has_been_checked = 1
602 Stats: has_been_checked = 1
605 Stats: has_been_checked = 0
606 Stats: has_been_checked = 0
607 Stats: active_checks_enabled = 0
609 Stats: has_been_checked = 0
610 Stats: scheduled_downtime_depth > 0
613 Stats: has_been_checked = 1
614 Stats: active_checks_enabled = 0
617 Stats: has_been_checked = 1
618 Stats: scheduled_downtime_depth > 0
621 Stats: has_been_checked = 1
622 Stats: acknowledged = 1
625 Stats: scheduled_downtime_depth > 0
626 Stats: has_been_checked = 1
629 Stats: active_checks_enabled = 0
630 Stats: has_been_checked = 1
633 Stats: active_checks_enabled = 1
634 Stats: acknowledged = 0
635 Stats: scheduled_downtime_depth = 0
636 Stats: has_been_checked = 1
639 Stats: acknowledged = 1
640 Stats: has_been_checked = 1
643 Stats: scheduled_downtime_depth > 0
644 Stats: has_been_checked = 1
647 Stats: active_checks_enabled = 0
650 Stats: active_checks_enabled = 1
651 Stats: acknowledged = 0
652 Stats: scheduled_downtime_depth = 0
653 Stats: has_been_checked = 1
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
664 Separators: 10 59 44 124
665 ResponseHeader: fixed16"""
666 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
668 if self
.nagios_installed():
669 nagresponse
= self
.ask_nagios(request
)
670 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
)
680 if self
.nagios_installed():
681 nagresponse
= self
.ask_nagios(request
)
682 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
)
695 cmd
= "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_warning_00;%d;%d;0;0;%d;lausser;blablubsvc" % (now
, now
, now
+ duration
, duration
)
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
)
702 self
.sched
.run_external_command(cmd
)
703 if self
.nagios_installed():
704 self
.nagios_extcmd(cmd
)
706 self
.scheduler_loop(1, [[svc
, 0, 'OK']])
708 self
.scheduler_loop(3, [[svc
, 2, 'BAD']])
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
)
717 if self
.nagios_installed():
719 nagresponse
= self
.ask_nagios(request
)
720 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
)
733 if self
.nagios_installed():
735 nagresponse
= self
.ask_nagios(request
)
736 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
745 Separators: 10 59 44 124
746 ResponseHeader: fixed16"""
747 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
750 request
= """GET services
751 Filter: has_been_checked = 1
752 Filter: check_type = 0
753 Stats: sum has_been_checked
755 Stats: sum execution_time
757 Stats: min execution_time
759 Stats: max execution_time
760 Separators: 10 59 44 124
761 ResponseHeader: fixed16"""
762 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
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
)
769 request
= """GET hostgroups\nColumnHeaders: on\nResponseHeader: fixed16"""
770 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
772 if self
.nagios_installed():
773 nagresponse
= self
.ask_nagios(request
)
774 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
)
782 if self
.nagios_installed():
783 nagresponse
= self
.ask_nagios(request
)
784 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
)
791 if self
.nagios_installed():
792 nagresponse
= self
.ask_nagios(request
)
793 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
)
801 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']])
803 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
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
)
811 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
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
)
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
):
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
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)
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)
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
)
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
)
879 good_response
= """200 9
880 """ + svc_comment_list
+ """;;0;0
882 self
.assert_(response
== good_response
) # csv
885 def test_thruk_logs(self
):
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']])
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)
909 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
910 self
.sched
.run_external_command(cmd
)
912 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
915 self
.scheduler_loop(3, [[host
, 2, 'DOWN'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
918 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
922 # show history for service
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
934 Filter: host_name = test_host_0
935 Filter: service_description = test_ok_0
937 Filter: type ~ starting...
938 Filter: type ~ shutting down...
940 Filter: current_service_description !=
942 Filter: service_description =
945 Filter: service_description =
950 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
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
):
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']])
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)
979 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
980 self
.sched
.run_external_command(cmd
)
982 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
985 self
.scheduler_loop(3, [[host
, 2, 'DOWN'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
988 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
992 # is this an error in thruk?
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 !=
1003 Filter: service_description =
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
)
1013 def test_thruk_logs_current(self
):
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()
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)
1037 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
1038 self
.sched
.run_external_command(cmd
)
1040 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1041 self
.update_broker()
1043 self
.scheduler_loop(3, [[host
, 2, 'DOWN'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1044 self
.update_broker()
1046 self
.scheduler_loop(3, [[host
, 0, 'UUP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1047 self
.update_broker()
1049 # self.scheduler_loop(3, [[host, 0, 'UP'], [router, 2, 'DOWN'], [svc, 0, 'OK']], do_sleep=False)
1050 # self.update_broker()
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
1065 Filter: current_host_name = test_host_0
1066 Filter: current_service_description = test_ok_0
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
1076 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1080 def test_thruk_tac_svc(self
):
1082 if self
.nagios_installed():
1083 self
.start_nagios('1r_1h_1s')
1084 self
.update_broker()
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()
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)
1108 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
1109 self
.sched
.run_external_command(cmd
)
1111 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1112 self
.update_broker()
1114 self
.scheduler_loop(3, [[host
, 2, 'DOWN'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1115 self
.update_broker()
1117 self
.scheduler_loop(3, [[host
, 0, 'UUP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1118 self
.update_broker()
1120 # self.scheduler_loop(3, [[host, 0, 'UP'], [router, 2, 'DOWN'], [svc, 0, 'OK']], do_sleep=False)
1121 # self.update_broker()
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
1130 Stats: sum execution_time
1132 Stats: min execution_time
1134 Stats: max execution_time"""
1136 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
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
)
1142 # self.assert_(self.lines_equal(response, nagresponse))
1145 def test_columns(self
):
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
)
1156 def test_scheduler_table(self
):
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()
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()
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
)
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
1190 schedlink
.alive
= False
1191 b
= schedlink
.get_update_status_brok()
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
):
1206 self
.update_broker()
1207 creation_tab
= {'reactionner_name' : 'reactionner-1', 'address' : 'localhost', 'spare' : '0'}
1208 reac
= ReactionnerLink(creation_tab
)
1211 b
= reac
.get_initial_status_brok()
1213 creation_tab
= {'reactionner_name' : 'reactionner-2', 'address' : 'othernode', 'spare' : '1'}
1214 reac
= ReactionnerLink(creation_tab
)
1217 b2
= reac
.get_initial_status_brok()
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
)
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
1236 b2
= reac
.get_update_status_brok()
1238 self
.update_broker()
1239 request
= """GET reactionners"""
1240 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
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
):
1253 self
.update_broker()
1255 creation_tab
= {'poller_name' : 'poller-1', 'address' : 'localhost', 'spare' : '0'}
1256 pol
= PollerLink(creation_tab
)
1259 b
= pol
.get_initial_status_brok()
1261 creation_tab
= {'poller_name' : 'poller-2', 'address' : 'othernode', 'spare' : '1'}
1262 pol
= PollerLink(creation_tab
)
1265 b2
= pol
.get_initial_status_brok()
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
)
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
1284 b2
= pol
.get_update_status_brok()
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
)
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
):
1305 self
.update_broker()
1307 creation_tab
= {'broker_name' : 'broker-1', 'address' : 'localhost', 'spare' : '0'}
1308 pol
= BrokerLink(creation_tab
)
1311 b
= pol
.get_initial_status_brok()
1313 creation_tab
= {'broker_name' : 'broker-2', 'address' : 'othernode', 'spare' : '1'}
1314 pol
= BrokerLink(creation_tab
)
1317 b2
= pol
.get_initial_status_brok()
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
)
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
1336 b2
= pol
.get_initial_status_brok()
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
)
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
):
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
):
1392 if self
.nagios_installed():
1393 self
.start_nagios('1r_1h_1s')
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
)
1402 good_response
= """test_host_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
)
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
)
1419 # self.assert_(self.lines_equal(response, nagresponse))
1423 def test_problem_impact_in_host_service(self
):
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':
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
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
)
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
):
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
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
1522 request
= """GET services
1523 Columns: host_name service_description
1524 Filter: groups >= servicegroup_02
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
1535 def test_is_executing(self
):
1537 #---------------------------------------------------------------
1538 # make sure that the is_executing flag is updated regularly
1539 #---------------------------------------------------------------
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()
1573 a
.status
= 'inpoller'
1574 a
.check_time
= time
.time()
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()
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
)
1597 def test_pnp_path(self
):
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
1607 #---------------------------------------------------------------
1608 pnp_path
= 'tmp/pnp4nagios_test' + str(os
.getpid())
1610 os
.removedirs(pnp_path
)
1614 print "there is no spool dir", pnp_path
1616 request
= """GET services
1617 Columns: host_name service_description pnpgraph_present
1619 ResponseHeader: fixed16
1621 requesth
= """GET hosts
1622 Columns: host_name pnpgraph_present
1624 ResponseHeader: fixed16
1626 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1628 self
.assert_(response
== """200 24
1629 test_host_0;test_ok_0;0
1631 #self.assert_(not self.livestatus_broker.livestatus.pnp_path)
1634 os
.makedirs(pnp_path
)
1635 print "there is an empty spool dir", pnp_path
1639 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1641 self
.assert_(response
== """200 24
1642 test_host_0;test_ok_0;0
1644 self
.assert_(self
.livestatus_broker
.livestatus
.pnp_path
== pnp_path
+ '/')
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
1654 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1656 self
.assert_(response
== """200 24
1657 test_host_0;test_ok_0;1
1659 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(requesth
)
1661 self
.assert_(response
== """200 30
1667 def test_thruk_action_notes_url(self
):
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
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$
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
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
1695 request
= """GET hosts
1696 Columns: host_name action_url_expanded notes_url_expanded
1697 Filter: host_name = test_host_0
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
1708 def test_thruk_custom_variables(self
):
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
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
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
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
1737 class TestConfigBig(TestConfig
):
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
= {
1746 self
.livestatus_broker
.init()
1747 print "Cleaning old broks?"
1748 self
.sched
.fill_initial_broks()
1749 self
.update_broker()
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
):
1761 if self
.nagios_installed():
1762 self
.start_nagios('5r_100h_2000s')
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")
1773 svc2
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_15")
1775 svc3
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_16")
1777 svc4
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_007", "test_ok_05")
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
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
)
1799 self
.assert_(self
.lines_equal(response
, nagresponse
))
1802 def test_statsgroupby(self
):
1804 if self
.nagios_installed():
1805 self
.start_nagios('5r_100h_2000s')
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")
1816 svc2
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_15")
1818 svc3
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_16")
1820 svc4
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_007", "test_ok_05")
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
)
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
1841 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
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
)
1850 self
.assert_(self
.lines_equal(response
, nagresponse
))
1853 def test_hostsbygroup(self
):
1855 if self
.nagios_installed():
1856 self
.start_nagios('5r_100h_2000s')
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
1867 Columns: host_name hostgroup_name
1870 ResponseHeader: fixed16
1873 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1875 if self
.nagios_installed():
1876 nagresponse
= self
.ask_nagios(request
)
1878 self
.assert_(self
.lines_equal(response
, nagresponse
))
1881 def test_servicesbyhostgroup(self
):
1883 if self
.nagios_installed():
1884 self
.start_nagios('5r_100h_2000s')
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
1897 Stats: has_been_checked != 0
1898 Stats: scheduled_downtime_depth = 0
1899 Stats: host_scheduled_downtime_depth = 0
1902 Stats: scheduled_downtime_depth > 0
1903 Stats: host_scheduled_downtime_depth > 0
1906 Stats: acknowledged = 0
1907 Stats: host_acknowledged = 0
1908 Stats: scheduled_downtime_depth = 0
1909 Stats: host_scheduled_downtime_depth = 0
1912 Stats: acknowledged = 1
1913 Stats: host_acknowledged = 1
1917 Stats: scheduled_downtime_depth > 0
1918 Stats: host_scheduled_downtime_depth > 0
1922 Stats: acknowledged = 0
1923 Stats: host_acknowledged = 0
1924 Stats: scheduled_downtime_depth = 0
1925 Stats: host_scheduled_downtime_depth = 0
1928 Stats: acknowledged = 1
1929 Stats: host_acknowledged = 1
1933 Stats: scheduled_downtime_depth > 0
1934 Stats: host_scheduled_downtime_depth > 0
1938 Stats: acknowledged = 0
1939 Stats: host_acknowledged = 0
1940 Stats: scheduled_downtime_depth = 0
1941 Stats: host_scheduled_downtime_depth = 0
1944 Stats: acknowledged = 1
1945 Stats: host_acknowledged = 1
1949 Stats: scheduled_downtime_depth > 0
1950 Stats: host_scheduled_downtime_depth > 0
1953 StatsGroupBy: hostgroup_name
1956 ResponseHeader: fixed16
1959 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1961 print "livestatus duration %f" % (tac
- tic
)
1963 if self
.nagios_installed():
1964 nagresponse
= self
.ask_nagios(request
)
1966 self
.assert_(self
.lines_equal(response
, nagresponse
))
1968 # Again, without Filter:
1969 request
= """GET servicesbyhostgroup
1970 Stats: has_been_checked = 0
1972 Stats: has_been_checked != 0
1973 Stats: scheduled_downtime_depth = 0
1974 Stats: host_scheduled_downtime_depth = 0
1977 Stats: scheduled_downtime_depth > 0
1978 Stats: host_scheduled_downtime_depth > 0
1981 Stats: acknowledged = 0
1982 Stats: host_acknowledged = 0
1983 Stats: scheduled_downtime_depth = 0
1984 Stats: host_scheduled_downtime_depth = 0
1987 Stats: acknowledged = 1
1988 Stats: host_acknowledged = 1
1992 Stats: scheduled_downtime_depth > 0
1993 Stats: host_scheduled_downtime_depth > 0
1997 Stats: acknowledged = 0
1998 Stats: host_acknowledged = 0
1999 Stats: scheduled_downtime_depth = 0
2000 Stats: host_scheduled_downtime_depth = 0
2003 Stats: acknowledged = 1
2004 Stats: host_acknowledged = 1
2008 Stats: scheduled_downtime_depth > 0
2009 Stats: host_scheduled_downtime_depth > 0
2013 Stats: acknowledged = 0
2014 Stats: host_acknowledged = 0
2015 Stats: scheduled_downtime_depth = 0
2016 Stats: host_scheduled_downtime_depth = 0
2019 Stats: acknowledged = 1
2020 Stats: host_acknowledged = 1
2024 Stats: scheduled_downtime_depth > 0
2025 Stats: host_scheduled_downtime_depth > 0
2028 StatsGroupBy: hostgroup_name
2031 ResponseHeader: fixed16
2033 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2035 if self
.nagios_installed():
2036 nagresponse
= self
.ask_nagios(request
)
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')
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
2055 Filter: name = test_host_0
2058 ResponseHeader: fixed16
2060 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2062 if self
.nagios_installed():
2063 nagresponse
= self
.ask_nagios(request
)
2064 print "nagresponse----------------------------------------------"
2066 self
.assert_(self
.lines_equal(response
, nagresponse
))
2067 request
= """GET hosts
2069 Filter: name = test_router_0
2072 ResponseHeader: fixed16
2074 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2076 if self
.nagios_installed():
2077 nagresponse
= self
.ask_nagios(request
)
2078 print "nagresponse----------------------------------------------"
2080 self
.assert_(self
.lines_equal(response
, nagresponse
))
2083 if __name__
== '__main__':
2085 command
= """unittest.main()"""
2087 #cProfile.runctx( command, globals(), locals(), filename="Thruk.profile" )