1 #!/usr/bin/env python2.6
2 # -*- coding: utf-8 -*-
4 #Copyright (C) 2009-2010 :
5 # Gabes Jean, naparuba@gmail.com
6 # Gerhard Lausser, Gerhard.Lausser@consol.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
25 # This file is used to test host- and service-downtimes.
28 from shinken_test
import *
36 from shinken
.brok
import Brok
37 from shinken
.objects
.timeperiod
import Timeperiod
38 from shinken
.objects
.module
import Module
40 from shinken
.modules
import livestatus_broker
41 from shinken
.modules
.livestatus_broker
import Livestatus_broker
43 sys
.setcheckinterval(10000)
46 modconf
.module_name
= "livestatus"
47 modconf
.module_type
= livestatus_broker
.properties
['type']
48 modconf
.properties
= livestatus_broker
.properties
.copy()
50 class TestConfig(ShinkenTest
):
51 def contains_line(self
, text
, pattern
):
52 regex
= re
.compile(pattern
)
53 for line
in text
.splitlines():
54 if re
.search(regex
, line
):
59 def scheduler_loop(self
, count
, reflist
, do_sleep
=False, sleep_time
=61):
60 super(TestConfig
, self
).scheduler_loop(count
, reflist
, do_sleep
, sleep_time
)
61 if self
.nagios_installed() and hasattr(self
, 'nagios_started'):
62 self
.nagios_loop(1, reflist
)
65 def update_broker(self
):
66 #The brok should be manage in the good order
67 ids
= self
.sched
.broks
.keys()
70 brok
= self
.sched
.broks
[brok_id
]
71 #print "Managing a brok type", brok.type, "of id", brok_id
72 #if brok.type == 'update_service_status':
73 # print "Problem?", brok.data['is_problem']
74 self
.livestatus_broker
.manage_brok(brok
)
78 def lines_equal(self
, text1
, text2
):
79 # gets two multiline strings and compares the contents
80 # lifestatus output may not be in alphabetical order, so this
81 # function is used to compare unordered output with unordered
83 # sometimes mklivestatus returns 0 or 1 on an empty result
84 text1
= text1
.replace("200 1", "200 0")
85 text2
= text2
.replace("200 1", "200 0")
86 text1
= text1
.rstrip()
87 text2
= text2
.rstrip()
88 #print "text1 //%s//" % text1
89 #print "text2 //%s//" % text2
90 sorted1
= "\n".join(sorted(text1
.split("\n")))
91 sorted2
= "\n".join(sorted(text2
.split("\n")))
92 len1
= len(text1
.split("\n"))
93 len2
= len(text2
.split("\n"))
94 #print "%s == %s text cmp %s" % (len1, len2, sorted1 == sorted2)
95 #print "text1 //%s//" % sorted(text1.split("\n"))
96 #print "text2 //%s//" % sorted(text2.split("\n"))
97 if sorted1
== sorted2
and len1
== len2
:
100 # Maybe list members are different
101 # allhosts;test_host_0;test_ok_0;servicegroup_02,servicegroup_01,ok
102 # allhosts;test_host_0;test_ok_0;servicegroup_02,ok,servicegroup_01
104 # [['allhosts'], ['test_host_0'], ['test_ok_0'],
105 # ['ok', 'servicegroup_01', 'servicegroup_02']]
106 [line
for line
in sorted(text1
.split("\n"))]
107 data1
= [[sorted(c
.split(',')) for c
in columns
] for columns
in [line
.split(';') for line
in sorted(text1
.split("\n")) if line
]]
108 data2
= [[sorted(c
.split(',')) for c
in columns
] for columns
in [line
.split(';') for line
in sorted(text2
.split("\n")) if line
]]
109 #print "text1 //%s//" % data1
110 #print "text2 //%s//" % data2
111 # cmp is clever enough to handle nested arrays
112 return cmp(data1
, data2
) == 0
115 def show_broks(self
, title
):
118 for brok
in sorted(self
.sched
.broks
.values(), lambda x
, y
: x
.id - y
.id):
119 if re
.compile('^service_').match(brok
.type):
120 print "BROK:", brok
.type
121 print "BROK ", brok
.data
['in_checking']
123 request
= 'GET services\nColumns: service_description is_executing\n'
124 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
128 def nagios_installed(self
, path
='/usr/local/nagios/bin/nagios', livestatus
='/usr/local/nagios/lib/mk-livestatus/livestatus.o'):
131 if os
.path
.exists(path
) and os
.access(path
, os
.X_OK
) and os
.path
.exists(livestatus
):
132 self
.nagios_path
= path
133 self
.livestatus_path
= livestatus
139 # shinkenize_nagios_config('nagios_1r_1h_1s')
140 # We assume that there is a nagios_1r_1h_1s.cfg and a nagios_1r_1h_1s directory for the objects
141 def unshinkenize_config(self
, configname
):
142 new_configname
= configname
+ '_' + str(os
.getpid())
143 config
= open('etc/nagios_' + configname
+ '.cfg')
144 text
= config
.readlines()
147 newconfig
= open('etc/nagios_' + new_configname
+ '.cfg', 'w')
149 if re
.search('^resource_file=', line
):
150 newconfig
.write("resource_file=etc/resource.cfg\n")
151 elif re
.search('shinken\-specific\.cfg', line
):
153 elif re
.search('enable_problem_impacts_states_change', line
):
155 elif re
.search('cfg_dir=', line
):
156 newconfig
.write(re
.sub(configname
, new_configname
, line
))
157 elif re
.search('cfg_file=', line
):
158 newconfig
.write(re
.sub(configname
, new_configname
, line
))
159 elif re
.search('execute_host_checks=', line
):
160 newconfig
.write("execute_host_checks=0\n")
161 elif re
.search('execute_service_checks=', line
):
162 newconfig
.write("execute_service_checks=0\n")
163 elif re
.search('^debug_level=', line
):
164 newconfig
.write("debug_level=0\n")
165 elif re
.search('^debug_verbosity=', line
):
166 newconfig
.write("debug_verbosity=0\n")
167 elif re
.search('^status_update_interval=', line
):
168 newconfig
.write("status_update_interval=30\n")
169 elif re
.search('^command_file=', line
):
170 newconfig
.write("command_file=var/nagios.cmd\n")
171 elif re
.search('^command_check_interval=', line
):
172 newconfig
.write("command_check_interval=1s\n")
174 newconfig
.write(line
)
175 newconfig
.write('broker_module=/usr/local/nagios/lib/mk-livestatus/livestatus.o var/live' + "\n")
177 for dirfile
in os
.walk('etc/' + configname
):
178 dirpath
, dirlist
, filelist
= dirfile
179 newdirpath
= re
.sub(configname
, new_configname
, dirpath
)
181 for file in [f
for f
in filelist
if re
.search('\.cfg$', f
)]:
182 config
= open(dirpath
+ '/' + file)
183 text
= config
.readlines()
185 newconfig
= open(newdirpath
+ '/' + file, 'w')
187 if re
.search('^\s*criticity', line
):
189 elif re
.search('enable_problem_impacts_states_change', line
):
192 newconfig
.write(line
)
194 return new_configname
197 def start_nagios(self
, config
):
198 if os
.path
.exists('var/spool/checkresults'):
199 # Cleanup leftover checkresults
200 shutil
.rmtree('var/spool/checkresults')
201 for dir in ['tmp', 'var/tmp', 'var/spool', 'var/spool/checkresults', 'var/archives']:
202 if not os
.path
.exists(dir):
204 self
.nagios_config
= self
.unshinkenize_config(config
)
205 if os
.path
.exists('var/nagios.log'):
206 os
.remove('var/nagios.log')
207 if os
.path
.exists('var/retention.dat'):
208 os
.remove('var/retention.dat')
209 if os
.path
.exists('var/status.dat'):
210 os
.remove('var/status.dat')
211 self
.nagios_proc
= subprocess
.Popen([self
.nagios_path
, 'etc/nagios_' + self
.nagios_config
+ '.cfg'], close_fds
=True)
212 self
.nagios_started
= time
.time()
216 def stop_nagios(self
):
217 if self
.nagios_installed():
218 print "i stop nagios!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
220 if hasattr(self
, 'nagios_proc'):
222 while self
.nagios_proc
.poll() == None and attempt
< 4:
223 self
.nagios_proc
.terminate()
226 if self
.nagios_proc
.poll() == None:
227 self
.nagios_proc
.kill()
228 if os
.path
.exists('etc/' + self
.nagios_config
):
229 shutil
.rmtree('etc/' + self
.nagios_config
)
230 if os
.path
.exists('etc/nagios_' + self
.nagios_config
+ '.cfg'):
231 os
.remove('etc/nagios_' + self
.nagios_config
+ '.cfg')
234 def ask_nagios(self
, request
):
235 if time
.time() - self
.nagios_started
< 2:
237 if not request
.endswith("\n"):
238 request
= request
+ "\n"
239 unixcat
= subprocess
.Popen([os
.path
.dirname(self
.nagios_path
) + '/' + 'unixcat', 'var/live'], stdin
=subprocess
.PIPE
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
)
241 out
, err
= unixcat
.communicate(request
)
243 print "mklivestatus duration %f" % (tac
- tic
)
245 while unixcat
.poll() == None and attempt
< 4:
249 if unixcat
.poll() == None:
251 print "unixcat says", out
255 def nagios_loop(self
, count
, reflist
, do_sleep
=False, sleep_time
=61):
257 buffer = open('var/pipebuffer', 'w')
259 (obj
, exit_status
, output
) = ref
260 if obj
.my_type
== 'service':
261 cmd
= "[%lu] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%d;%s\n" % (now
, obj
.host_name
, obj
.service_description
, exit_status
, output
)
265 cmd
= "[%lu] PROCESS_HOST_CHECK_RESULT;%s;%d;%s\n" % (now
, obj
.host_name
, exit_status
, output
)
268 print "open pipe", self
.conf
.command_file
269 fifo
= open('var/nagios.cmd', 'w')
270 cmd
= "[%lu] PROCESS_FILE;%s;0\n" % (now
, 'var/pipebuffer')
277 def nagios_extcmd(self
, cmd
):
278 fifo
= open('var/nagios.cmd', 'w')
286 class TestConfigSmall(TestConfig
):
288 self
.setup_with_file('etc/nagios_1r_1h_1s.cfg')
289 self
.testid
= str(os
.getpid() + random
.randint(1, 1000))
290 self
.livelogs
= 'tmp/livelogs.db' + self
.testid
291 self
.pnp4nagios
= 'tmp/pnp4nagios_test' + self
.testid
292 self
.livestatus_broker
= Livestatus_broker(modconf
, '127.0.0.1', str(50000 + os
.getpid()), 'live', [], self
.livelogs
, 365, self
.pnp4nagios
)
293 self
.livestatus_broker
.create_queues()
294 #self.livestatus_broker.properties = {
299 self
.livestatus_broker
.init()
300 print "Cleaning old broks?"
301 self
.sched
.fill_initial_broks()
303 self
.nagios_path
= None
304 self
.livestatus_path
= None
305 self
.nagios_config
= None
310 self
.livestatus_broker
.dbconn
.commit()
311 self
.livestatus_broker
.dbconn
.close()
312 if os
.path
.exists(self
.livelogs
):
313 os
.remove(self
.livelogs
)
314 if os
.path
.exists(self
.pnp4nagios
):
315 shutil
.rmtree(self
.pnp4nagios
)
318 def test_childs(self
):
319 if self
.nagios_installed():
320 self
.start_nagios('1r_1h_1s')
324 for host
in self
.sched
.hosts
:
325 objlist
.append([host
, 0, 'UP'])
326 for service
in self
.sched
.services
:
327 objlist
.append([service
, 0, 'OK'])
328 self
.scheduler_loop(1, objlist
)
330 request
= """GET hosts
332 Filter: name = test_host_0
335 ResponseHeader: fixed16
337 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
339 if self
.nagios_installed():
340 nagresponse
= self
.ask_nagios(request
)
341 print "nagresponse----------------------------------------------"
343 self
.assert_(self
.lines_equal(response
, nagresponse
))
344 request
= """GET hosts
346 Filter: name = test_router_0
349 ResponseHeader: fixed16
351 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
353 if self
.nagios_installed():
354 nagresponse
= self
.ask_nagios(request
)
355 print "nagresponse----------------------------------------------"
357 self
.assert_(self
.lines_equal(response
, nagresponse
))
361 def test_servicesbyhostgroup(self
):
362 if self
.nagios_installed():
363 self
.start_nagios('1r_1h_1s')
367 for host
in self
.sched
.hosts
:
368 objlist
.append([host
, 0, 'UP'])
369 for service
in self
.sched
.services
:
370 objlist
.append([service
, 0, 'OK'])
371 self
.scheduler_loop(1, objlist
)
373 request
= """GET servicesbyhostgroup
374 Filter: host_groups >= allhosts
375 Columns: hostgroup_name host_name service_description groups
378 ResponseHeader: fixed16
380 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
382 if self
.nagios_installed():
383 nagresponse
= self
.ask_nagios(request
)
384 print "nagresponse----------------------------------------------"
386 self
.assert_(self
.lines_equal(response
, nagresponse
))
388 # Again, but without filter
389 request
= """GET servicesbyhostgroup
390 Columns: hostgroup_name host_name service_description groups
393 ResponseHeader: fixed16
395 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
397 if self
.nagios_installed():
398 nagresponse
= self
.ask_nagios(request
)
399 print "nagresponse----------------------------------------------"
401 self
.assert_(self
.lines_equal(response
, nagresponse
))
404 def test_hostsbygroup(self
):
406 if self
.nagios_installed():
407 self
.start_nagios('1r_1h_1s')
410 for host
in self
.sched
.hosts
:
411 objlist
.append([host
, 0, 'UP'])
412 for service
in self
.sched
.services
:
413 objlist
.append([service
, 0, 'OK'])
414 self
.scheduler_loop(1, objlist
)
416 request
= """GET hostsbygroup
418 Columns: host_name hostgroup_name
419 Filter: groups >= allhosts
422 ResponseHeader: fixed16
425 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
427 if self
.nagios_installed():
428 nagresponse
= self
.ask_nagios(request
)
429 print "nagresponse----------------------------------------------"
431 self
.assert_(self
.lines_equal(response
, nagresponse
))
434 def test_status(self
):
436 if self
.nagios_installed():
437 self
.start_nagios('1r_1h_1s')
439 host
= self
.sched
.hosts
.find_by_name("test_host_0")
440 host
.checks_in_progress
= []
441 host
.act_depend_of
= [] # ignore the router
442 router
= self
.sched
.hosts
.find_by_name("test_router_0")
443 router
.checks_in_progress
= []
444 router
.act_depend_of
= [] # ignore the router
445 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
446 svc
.checks_in_progress
= []
447 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
448 self
.scheduler_loop(2, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 2, 'BAD']])
450 #---------------------------------------------------------------
451 # get the full hosts table
452 #---------------------------------------------------------------
453 request
= 'GET hosts'
454 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
456 if self
.nagios_installed():
457 nagresponse
= self
.ask_nagios(request
)
458 print "nagresponse----------------------------------------------"
460 # todo 1 != 1.0000000000e+00
461 #self.assert_(self.lines_equal(response, nagresponse))
463 #---------------------------------------------------------------
464 # get only the host names and addresses
465 #---------------------------------------------------------------
466 request
= 'GET hosts\nColumns: name address groups\nColumnHeaders: on'
467 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
469 if self
.nagios_installed():
470 nagresponse
= self
.ask_nagios(request
)
471 print "nagresponse----------------------------------------------"
473 self
.assert_(self
.lines_equal(response
, nagresponse
))
475 #---------------------------------------------------------------
477 #---------------------------------------------------------------
478 request
= 'GET contacts'
479 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
480 print 'query_1_______________\n%s\n%s\n' % (request
, response
)
481 if self
.nagios_installed():
482 nagresponse
= self
.ask_nagios(request
)
483 print "nagresponse----------------------------------------------"
485 # There are some sick columns in the livestatus response like
486 # modified_attributes;modified_attributes_list
487 # These are not implemented in shinken-livestatus (never, i think)
488 #self.assert_(self.lines_equal(response, nagresponse))
490 #---------------------------------------------------------------
492 #---------------------------------------------------------------
493 request
= 'GET contacts\nColumns: name alias'
494 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
495 print 'query_2_______________\n%s\n%s\n' % (request
, response
)
496 if self
.nagios_installed():
497 nagresponse
= self
.ask_nagios(request
)
498 print "nagresponse----------------------------------------------"
500 self
.assert_(self
.lines_equal(response
, nagresponse
))
502 #---------------------------------------------------------------
504 #---------------------------------------------------------------
505 #self.scheduler_loop(3, svc, 2, 'BAD')
506 request
= 'GET services\nColumns: host_name description state\nFilter: state = 2\nColumnHeaders: on'
507 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
508 print 'query_3_______________\n%s\n%s\n' % (request
, response
)
509 self
.assert_(response
== 'host_name;description;state\ntest_host_0;test_ok_0;2\n')
510 request
= 'GET services\nColumns: host_name description state\nFilter: state = 2'
511 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
512 print 'query_3_______________\n%s\n%s\n' % (request
, response
)
513 self
.assert_(response
== 'test_host_0;test_ok_0;2\n')
514 request
= 'GET services\nColumns: host_name description state\nFilter: state = 0'
515 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
516 print 'query_3_______________\n%s\n%s\n' % (request
, response
)
517 self
.assert_(response
== '\n')
520 cmd
= "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;0;0;%d;lausser;blablub" % (now
, now
, now
+ duration
, duration
)
521 self
.sched
.run_external_command(cmd
)
523 self
.scheduler_loop(1, [[svc
, 0, 'OK']])
525 self
.scheduler_loop(3, [[svc
, 2, 'BAD']])
527 request
= 'GET services\nColumns: host_name description scheduled_downtime_depth\nFilter: state = 2\nFilter: scheduled_downtime_depth = 1'
528 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
529 print 'query_3_______________\n%s\n%s\n' % (request
, response
)
530 self
.assert_(response
== 'test_host_0;test_ok_0;1\n')
532 #---------------------------------------------------------------
534 #---------------------------------------------------------------
535 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'
536 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
537 print 'query_4_______________\n%s\n%s\n' % (request
, response
)
538 self
.assert_(response
== 'test_host_0;test_ok_0;2\n')
540 #---------------------------------------------------------------
542 #---------------------------------------------------------------
543 request
= 'GET services\nStats: state = 0\nStats: state = 1\nStats: state = 2\nStats: state = 3'
544 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
545 print 'query_6_______________\n%s\n%s\n' % (request
, response
)
546 self
.assert_(response
== '0;0;1;0\n')
548 #---------------------------------------------------------------
550 #---------------------------------------------------------------
551 request
= 'GET services\nStats: state = 0\nStats: state = 1\nStats: state = 2\nStats: state = 3\nFilter: contacts >= test_contact'
552 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
553 print 'query_6_______________\n%s\n%s\n' % (request
, response
)
554 self
.assert_(response
== '0;0;1;0\n')
555 if self
.nagios_installed():
556 nagresponse
= self
.ask_nagios(request
)
557 print "nagresponse----------------------------------------------"
559 # TODO looks like a timing problem with nagios
560 #self.assert_(self.lines_equal(response, nagresponse))
565 print "got initial broks"
567 host
= self
.sched
.hosts
.find_by_name("test_host_0")
568 host
.checks_in_progress
= []
569 host
.act_depend_of
= [] # ignore the router
570 router
= self
.sched
.hosts
.find_by_name("test_router_0")
571 router
.checks_in_progress
= []
572 router
.act_depend_of
= [] # ignore the router
573 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
574 svc
.checks_in_progress
= []
575 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
576 self
.scheduler_loop(2, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 2, 'BAD']])
578 request
= 'GET services\nColumns: host_name description state\nOutputFormat: json'
579 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
580 print 'json wo headers__________\n%s\n%s\n' % (request
, response
)
581 self
.assert_(response
== '[["test_host_0","test_ok_0",2]]\n')
582 request
= 'GET services\nColumns: host_name description state\nOutputFormat: json\nColumnHeaders: on'
583 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
584 print 'json with headers__________\n%s\n%s\n' % (request
, response
)
585 self
.assert_(response
== '[["host_name","description","state"],["test_host_0","test_ok_0",2]]\n')
586 #100% mklivesttaus: self.assert_(response == '[["host_name","description","state"],\n["test_host_0","test_ok_0",2]]\n')
589 def test_thruk(self
):
591 if self
.nagios_installed():
592 self
.start_nagios('1r_1h_1s')
594 host
= self
.sched
.hosts
.find_by_name("test_host_0")
595 host
.checks_in_progress
= []
596 host
.act_depend_of
= [] # ignore the router
597 router
= self
.sched
.hosts
.find_by_name("test_router_0")
598 router
.checks_in_progress
= []
599 router
.act_depend_of
= [] # ignore the router
600 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
601 svc
.checks_in_progress
= []
602 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
603 self
.scheduler_loop(2, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 2, 'BAD']])
605 #---------------------------------------------------------------
606 # get the full hosts table
607 #---------------------------------------------------------------
608 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'
609 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
612 request
= """GET hosts
614 Stats: check_type = 0
615 Stats: check_type = 1
616 Stats: has_been_checked = 1
619 Stats: has_been_checked = 1
622 Stats: has_been_checked = 1
625 Stats: has_been_checked = 0
626 Stats: has_been_checked = 0
627 Stats: active_checks_enabled = 0
629 Stats: has_been_checked = 0
630 Stats: scheduled_downtime_depth > 0
633 Stats: has_been_checked = 1
634 Stats: active_checks_enabled = 0
637 Stats: has_been_checked = 1
638 Stats: scheduled_downtime_depth > 0
641 Stats: has_been_checked = 1
642 Stats: acknowledged = 1
645 Stats: scheduled_downtime_depth > 0
646 Stats: has_been_checked = 1
649 Stats: active_checks_enabled = 0
650 Stats: has_been_checked = 1
653 Stats: active_checks_enabled = 1
654 Stats: acknowledged = 0
655 Stats: scheduled_downtime_depth = 0
656 Stats: has_been_checked = 1
659 Stats: acknowledged = 1
660 Stats: has_been_checked = 1
663 Stats: scheduled_downtime_depth > 0
664 Stats: has_been_checked = 1
667 Stats: active_checks_enabled = 0
670 Stats: active_checks_enabled = 1
671 Stats: acknowledged = 0
672 Stats: scheduled_downtime_depth = 0
673 Stats: has_been_checked = 1
675 Stats: is_flapping = 1
676 Stats: flap_detection_enabled = 0
677 Stats: notifications_enabled = 0
678 Stats: event_handler_enabled = 0
679 Stats: active_checks_enabled = 0
680 Stats: accept_passive_checks = 0
684 Separators: 10 59 44 124
685 ResponseHeader: fixed16"""
686 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
688 if self
.nagios_installed():
689 nagresponse
= self
.ask_nagios(request
)
690 print "nagresponse----------------------------------------------"
692 # TODO timing problem?
693 #self.assert_(self.lines_equal(response, nagresponse))
695 request
= """GET comments
696 Columns: host_name source type author comment entry_time entry_type expire_time
697 Filter: service_description ="""
698 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
700 if self
.nagios_installed():
701 nagresponse
= self
.ask_nagios(request
)
702 print "nagresponse----------------------------------------------"
704 self
.assert_(self
.lines_equal(response
, nagresponse
))
706 request
= """GET hosts
707 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
708 Separators: 10 59 44 124
709 ResponseHeader: fixed16"""
710 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
715 cmd
= "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_warning_00;%d;%d;0;0;%d;lausser;blablubsvc" % (now
, now
, now
+ duration
, duration
)
717 self
.sched
.run_external_command(cmd
)
718 if self
.nagios_installed():
719 self
.nagios_extcmd(cmd
)
720 cmd
= "[%lu] SCHEDULE_HOST_DOWNTIME;test_host_0;%d;%d;0;0;%d;lausser;blablubhost" % (now
, now
, now
+ duration
, duration
)
722 self
.sched
.run_external_command(cmd
)
723 if self
.nagios_installed():
724 self
.nagios_extcmd(cmd
)
726 self
.scheduler_loop(1, [[svc
, 0, 'OK']])
728 self
.scheduler_loop(3, [[svc
, 2, 'BAD']])
730 request
= """GET downtimes
731 Filter: service_description =
732 Columns: author comment end_time entry_time fixed host_name id start_time
733 Separators: 10 59 44 124
734 ResponseHeader: fixed16"""
735 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
737 if self
.nagios_installed():
739 nagresponse
= self
.ask_nagios(request
)
740 print "nagresponse----------------------------------------------"
742 #TODO the entry_times are different. find a way to round the numbers
743 # so that they are equal
744 #self.assert_(self.lines_equal(response, nagresponse))
746 request
= """GET comments
747 Filter: service_description =
748 Columns: author comment
749 Separators: 10 59 44 124
750 ResponseHeader: fixed16"""
751 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
753 if self
.nagios_installed():
755 nagresponse
= self
.ask_nagios(request
)
756 print "nagresponse----------------------------------------------"
758 #self.assert_(self.lines_equal(response, nagresponse))
760 request
= """GET services
761 Filter: has_been_checked = 1
762 Filter: check_type = 0
763 Stats: sum has_been_checked
765 Separators: 10 59 44 124
766 ResponseHeader: fixed16"""
767 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
770 request
= """GET services
771 Filter: has_been_checked = 1
772 Filter: check_type = 0
773 Stats: sum has_been_checked
775 Stats: sum execution_time
777 Stats: min execution_time
779 Stats: max execution_time
780 Separators: 10 59 44 124
781 ResponseHeader: fixed16"""
782 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
785 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"""
786 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
789 request
= """GET hostgroups\nColumnHeaders: on\nResponseHeader: fixed16"""
790 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
792 if self
.nagios_installed():
793 nagresponse
= self
.ask_nagios(request
)
794 print "nagresponse----------------------------------------------"
796 # TODO members_with_state
797 #self.assert_(self.lines_equal(response, nagresponse))
799 request
= """GET hosts\nColumns: name groups\nColumnHeaders: on\nResponseHeader: fixed16"""
800 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
802 if self
.nagios_installed():
803 nagresponse
= self
.ask_nagios(request
)
804 print "nagresponse----------------------------------------------"
806 self
.assert_(self
.lines_equal(response
, nagresponse
))
808 request
= """GET hostgroups\nColumns: name num_services num_services_ok\nColumnHeaders: on\nResponseHeader: fixed16"""
809 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
811 if self
.nagios_installed():
812 nagresponse
= self
.ask_nagios(request
)
813 print "nagresponse----------------------------------------------"
815 self
.assert_(self
.lines_equal(response
, nagresponse
))
817 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"""
818 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
821 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']])
823 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
826 print "WARNING SOFT;1"
827 # worst_service_state 1, worst_service_hard_state 0
828 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"""
829 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
831 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
833 print "WARNING HARD;3"
834 # worst_service_state 1, worst_service_hard_state 1
835 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"""
836 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
838 for s
in self
.livestatus_broker
.livestatus
.services
.values():
839 print "%s %d %s;%d" % (s
.state
, s
.state_id
, s
.state_type
, s
.attempt
)
842 def test_thruk_comments(self
):
844 host
= self
.sched
.hosts
.find_by_name("test_host_0")
845 host
.checks_in_progress
= []
846 host
.act_depend_of
= [] # ignore the router
847 router
= self
.sched
.hosts
.find_by_name("test_router_0")
848 router
.checks_in_progress
= []
849 router
.act_depend_of
= [] # ignore the router
850 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
851 svc
.checks_in_progress
= []
852 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
855 # downtime valid for the next 2 minutes
856 cmd
= "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now
, now
, now
+ duration
, duration
)
857 self
.sched
.run_external_command(cmd
)
858 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
859 svc
.checks_in_progress
= []
860 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
861 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
863 print "downtime was scheduled. check its activity and the comment"
864 self
.assert_(len(self
.sched
.downtimes
) == 1)
865 self
.assert_(len(svc
.downtimes
) == 1)
866 self
.assert_(svc
.downtimes
[0] in self
.sched
.downtimes
.values())
867 self
.assert_(svc
.downtimes
[0].fixed
)
868 self
.assert_(svc
.downtimes
[0].is_in_effect
)
869 self
.assert_(not svc
.downtimes
[0].can_be_deleted
)
870 self
.assert_(len(self
.sched
.comments
) == 1)
871 self
.assert_(len(svc
.comments
) == 1)
872 self
.assert_(svc
.comments
[0] in self
.sched
.comments
.values())
873 self
.assert_(svc
.downtimes
[0].comment_id
== svc
.comments
[0].id)
876 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
877 self
.sched
.run_external_command(cmd
)
878 #cmd = "[%lu] ADD_HOST_COMMENT;test_host_0;1;lausser;hcomment" % now
879 #self.sched.run_external_command(cmd)
880 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
881 self
.assert_(len(self
.sched
.comments
) == 2)
882 self
.assert_(len(svc
.comments
) == 2)
885 svc_comment_list
= (',').join([str(c
.id) for c
in svc
.comments
])
887 #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"""
888 request
= """GET services\nColumns: comments host_comments host_is_executing is_executing\nFilter: service_description !=\nResponseHeader: fixed16\nOutputFormat: json\n"""
889 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
891 good_response
= """200 17
892 [[[""" + svc_comment_list
+"""],[],0,0]]
894 self
.assert_(response
== good_response
) # json
896 request
= """GET services\nColumns: comments host_comments host_is_executing is_executing\nFilter: service_description !=\nResponseHeader: fixed16\n"""
897 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
899 good_response
= """200 9
900 """ + svc_comment_list
+ """;;0;0
902 self
.assert_(response
== good_response
) # csv
905 def test_thruk_logs(self
):
908 host
= self
.sched
.hosts
.find_by_name("test_host_0")
909 host
.checks_in_progress
= []
910 host
.act_depend_of
= [] # ignore the router
911 router
= self
.sched
.hosts
.find_by_name("test_router_0")
912 router
.checks_in_progress
= []
913 router
.act_depend_of
= [] # ignore the router
914 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
915 svc
.checks_in_progress
= []
916 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
917 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
921 # downtime valid for the next 2 minutes
922 cmd
= "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now
, now
, now
+ duration
, duration
)
923 self
.sched
.run_external_command(cmd
)
924 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
925 svc
.checks_in_progress
= []
926 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
927 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
929 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
930 self
.sched
.run_external_command(cmd
)
932 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
935 self
.scheduler_loop(3, [[host
, 2, 'DOWN'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
938 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
942 # show history for service
944 Columns: time type options state
945 Filter: time >= """ + str(int(start
)) + """
946 Filter: time <= """ + str(int(end
)) + """
947 Filter: type = SERVICE ALERT
948 Filter: type = HOST ALERT
949 Filter: type = SERVICE FLAPPING ALERT
950 Filter: type = HOST FLAPPING ALERT
951 Filter: type = SERVICE DOWNTIME ALERT
952 Filter: type = HOST DOWNTIME ALERT
954 Filter: host_name = test_host_0
955 Filter: service_description = test_ok_0
957 Filter: type ~ starting...
958 Filter: type ~ shutting down...
960 Filter: current_service_description !=
962 Filter: service_description =
965 Filter: service_description =
970 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
972 self
.assert_(self
.contains_line(response
, 'SERVICE DOWNTIME ALERT;test_host_0;test_ok_0;STARTED; Service has entered a period of scheduled downtime'))
975 def test_thruk_logs_alerts_summary(self
):
978 host
= self
.sched
.hosts
.find_by_name("test_host_0")
979 host
.checks_in_progress
= []
980 host
.act_depend_of
= [] # ignore the router
981 router
= self
.sched
.hosts
.find_by_name("test_router_0")
982 router
.checks_in_progress
= []
983 router
.act_depend_of
= [] # ignore the router
984 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
985 svc
.checks_in_progress
= []
986 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
987 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
991 # downtime valid for the next 2 minutes
992 cmd
= "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now
, now
, now
+ duration
, duration
)
993 self
.sched
.run_external_command(cmd
)
994 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
995 svc
.checks_in_progress
= []
996 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
997 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
999 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
1000 self
.sched
.run_external_command(cmd
)
1002 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1003 self
.update_broker()
1005 self
.scheduler_loop(3, [[host
, 2, 'DOWN'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1006 self
.update_broker()
1008 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1009 self
.update_broker()
1012 # is this an error in thruk?
1014 request
= """GET log
1015 Filter: options ~ ;HARD;
1016 Filter: type = HOST ALERT
1017 Filter: time >= 1284056080
1018 Filter: time <= 1284660880
1019 Filter: current_service_description !=
1020 Filter: service_description =
1021 Filter: host_name !=
1023 Filter: service_description =
1027 Columns: time state state_type host_name service_description current_host_groups current_service_groups plugin_output"""
1029 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1033 def test_thruk_logs_current(self
):
1036 host
= self
.sched
.hosts
.find_by_name("test_host_0")
1037 host
.checks_in_progress
= []
1038 host
.act_depend_of
= [] # ignore the router
1039 router
= self
.sched
.hosts
.find_by_name("test_router_0")
1040 router
.checks_in_progress
= []
1041 router
.act_depend_of
= [] # ignore the router
1042 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1043 svc
.checks_in_progress
= []
1044 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
1045 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
1046 self
.update_broker()
1049 # downtime valid for the next 2 minutes
1050 cmd
= "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now
, now
, now
+ duration
, duration
)
1051 self
.sched
.run_external_command(cmd
)
1052 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1053 svc
.checks_in_progress
= []
1054 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
1055 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1057 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
1058 self
.sched
.run_external_command(cmd
)
1060 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1061 self
.update_broker()
1063 self
.scheduler_loop(3, [[host
, 2, 'DOWN'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1064 self
.update_broker()
1066 self
.scheduler_loop(3, [[host
, 0, 'UUP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1067 self
.update_broker()
1069 # self.scheduler_loop(3, [[host, 0, 'UP'], [router, 2, 'DOWN'], [svc, 0, 'OK']], do_sleep=False)
1070 # self.update_broker()
1073 # show history for service
1074 request
= """GET log
1075 Columns: time type options state current_host_name
1076 Filter: time >= """ + str(int(start
)) + """
1077 Filter: time <= """ + str(int(end
)) + """
1078 Filter: type = SERVICE ALERT
1079 Filter: type = HOST ALERT
1080 Filter: type = SERVICE FLAPPING ALERT
1081 Filter: type = HOST FLAPPING ALERT
1082 Filter: type = SERVICE DOWNTIME ALERT
1083 Filter: type = HOST DOWNTIME ALERT
1085 Filter: current_host_name = test_host_0
1086 Filter: current_service_description = test_ok_0
1088 request
= """GET log
1089 Columns: time type options state current_host_name
1090 Filter: time >= """ + str(int(start
)) + """
1091 Filter: time <= """ + str(int(end
)) + """
1092 Filter: current_host_name = test_host_0
1093 Filter: current_service_description = test_ok_0
1096 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1100 def test_thruk_tac_svc(self
):
1102 if self
.nagios_installed():
1103 self
.start_nagios('1r_1h_1s')
1104 self
.update_broker()
1107 host
= self
.sched
.hosts
.find_by_name("test_host_0")
1108 host
.checks_in_progress
= []
1109 host
.act_depend_of
= [] # ignore the router
1110 router
= self
.sched
.hosts
.find_by_name("test_router_0")
1111 router
.checks_in_progress
= []
1112 router
.act_depend_of
= [] # ignore the router
1113 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1114 svc
.checks_in_progress
= []
1115 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
1116 self
.scheduler_loop(3, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
1117 self
.update_broker()
1120 # downtime valid for the next 2 minutes
1121 cmd
= "[%lu] SCHEDULE_SVC_DOWNTIME;test_host_0;test_ok_0;%d;%d;1;0;%d;lausser;blablub" % (now
, now
, now
+ duration
, duration
)
1122 self
.sched
.run_external_command(cmd
)
1123 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1124 svc
.checks_in_progress
= []
1125 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
1126 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1128 cmd
= "[%lu] ADD_SVC_COMMENT;test_host_0;test_ok_0;1;lausser;comment" % now
1129 self
.sched
.run_external_command(cmd
)
1131 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1132 self
.update_broker()
1134 self
.scheduler_loop(3, [[host
, 2, 'DOWN'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1135 self
.update_broker()
1137 self
.scheduler_loop(3, [[host
, 0, 'UUP'], [router
, 0, 'UP'], [svc
, 0, 'OK']], do_sleep
=False)
1138 self
.update_broker()
1140 # self.scheduler_loop(3, [[host, 0, 'UP'], [router, 2, 'DOWN'], [svc, 0, 'OK']], do_sleep=False)
1141 # self.update_broker()
1144 # show history for service
1145 request
= """GET services
1146 Filter: has_been_checked = 1
1147 Filter: check_type = 0
1148 Stats: sum has_been_checked
1150 Stats: sum execution_time
1152 Stats: min execution_time
1154 Stats: max execution_time"""
1156 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1158 # nagios comparison makes no sense, because the latencies/execution times will surely differ
1159 if self
.nagios_installed():
1160 nagresponse
= self
.ask_nagios(request
)
1162 # self.assert_(self.lines_equal(response, nagresponse))
1165 def test_columns(self
):
1167 self
.update_broker()
1168 #---------------------------------------------------------------
1169 # get the columns meta-table
1170 #---------------------------------------------------------------
1171 request
= """GET columns"""
1172 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1176 def test_scheduler_table(self
):
1178 self
.update_broker()
1180 creation_tab
= {'scheduler_name' : 'scheduler-1', 'address' : 'localhost', 'spare' : '0'}
1181 schedlink
= SchedulerLink(creation_tab
)
1182 schedlink
.pythonize()
1183 schedlink
.alive
= True
1184 b
= schedlink
.get_initial_status_brok()
1186 creation_tab
= {'scheduler_name' : 'scheduler-2', 'address' : 'othernode', 'spare' : '1'}
1187 schedlink
= SchedulerLink(creation_tab
)
1188 schedlink
.pythonize()
1189 schedlink
.alive
= True
1190 b2
= schedlink
.get_initial_status_brok()
1193 self
.update_broker()
1194 #---------------------------------------------------------------
1195 # get the columns meta-table
1196 #---------------------------------------------------------------
1197 request
= """GET schedulers"""
1198 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1200 good_response
= """address;alive;name;port;spare;weight
1201 othernode;1;scheduler-2;7768;1;1
1202 localhost;1;scheduler-1;7768;0;1
1204 print response
, 'FUCK'
1205 print "FUCK", response
, "TOTO"
1206 self
.assert_(self
.lines_equal(response
, good_response
))
1208 #Now we update a scheduler state and we check
1210 schedlink
.alive
= False
1211 b
= schedlink
.get_update_status_brok()
1213 self
.update_broker()
1214 request
= """GET schedulers"""
1215 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1216 good_response
= """address;alive;name;port;spare;weight
1217 othernode;0;scheduler-2;7768;1;1
1218 localhost;1;scheduler-1;7768;0;1
1220 self
.assert_(self
.lines_equal(response
, good_response
))
1224 def test_reactionner_table(self
):
1226 self
.update_broker()
1227 creation_tab
= {'reactionner_name' : 'reactionner-1', 'address' : 'localhost', 'spare' : '0'}
1228 reac
= ReactionnerLink(creation_tab
)
1231 b
= reac
.get_initial_status_brok()
1233 creation_tab
= {'reactionner_name' : 'reactionner-2', 'address' : 'othernode', 'spare' : '1'}
1234 reac
= ReactionnerLink(creation_tab
)
1237 b2
= reac
.get_initial_status_brok()
1240 self
.update_broker()
1241 #---------------------------------------------------------------
1242 # get the columns meta-table
1243 #---------------------------------------------------------------
1244 request
= """GET reactionners"""
1245 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1247 good_response
= """address;alive;name;port;spare
1248 localhost;1;reactionner-1;7769;0
1249 othernode;1;reactionner-2;7769;1
1251 print response
== good_response
1252 self
.assert_(self
.lines_equal(response
, good_response
))
1254 #Now the update part
1256 b2
= reac
.get_update_status_brok()
1258 self
.update_broker()
1259 request
= """GET reactionners"""
1260 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1262 good_response
= """address;alive;name;port;spare
1263 localhost;1;reactionner-1;7769;0
1264 othernode;0;reactionner-2;7769;1
1266 print response
== good_response
1267 self
.assert_(self
.lines_equal(response
, good_response
))
1271 def test_poller_table(self
):
1273 self
.update_broker()
1275 creation_tab
= {'poller_name' : 'poller-1', 'address' : 'localhost', 'spare' : '0'}
1276 pol
= PollerLink(creation_tab
)
1279 b
= pol
.get_initial_status_brok()
1281 creation_tab
= {'poller_name' : 'poller-2', 'address' : 'othernode', 'spare' : '1'}
1282 pol
= PollerLink(creation_tab
)
1285 b2
= pol
.get_initial_status_brok()
1288 self
.update_broker()
1289 #---------------------------------------------------------------
1290 # get the columns meta-table
1291 #---------------------------------------------------------------
1292 request
= """GET pollers"""
1293 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1295 good_response
= """address;alive;name;port;spare
1296 localhost;1;poller-1;7771;0
1297 othernode;1;poller-2;7771;1
1299 print response
== good_response
1300 self
.assert_(self
.lines_equal(response
, good_response
))
1302 #Now the update part
1304 b2
= pol
.get_update_status_brok()
1307 self
.update_broker()
1308 #---------------------------------------------------------------
1309 # get the columns meta-table
1310 #---------------------------------------------------------------
1311 request
= """GET pollers"""
1312 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1314 good_response
= """address;alive;name;port;spare
1315 localhost;1;poller-1;7771;0
1316 othernode;0;poller-2;7771;1
1318 print response
== good_response
1319 self
.assert_(self
.lines_equal(response
, good_response
))
1323 def test_broker_table(self
):
1325 self
.update_broker()
1327 creation_tab
= {'broker_name' : 'broker-1', 'address' : 'localhost', 'spare' : '0'}
1328 pol
= BrokerLink(creation_tab
)
1331 b
= pol
.get_initial_status_brok()
1333 creation_tab
= {'broker_name' : 'broker-2', 'address' : 'othernode', 'spare' : '1'}
1334 pol
= BrokerLink(creation_tab
)
1337 b2
= pol
.get_initial_status_brok()
1340 self
.update_broker()
1341 #---------------------------------------------------------------
1342 # get the columns meta-table
1343 #---------------------------------------------------------------
1344 request
= """GET brokers"""
1345 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1347 good_response
= """address;alive;name;port;spare
1348 localhost;1;broker-1;7772;0
1349 othernode;1;broker-2;7772;1
1351 print response
== good_response
1352 self
.assert_(self
.lines_equal(response
, good_response
))
1354 #Now the update part
1356 b2
= pol
.get_initial_status_brok()
1359 self
.update_broker()
1360 #---------------------------------------------------------------
1361 # get the columns meta-table
1362 #---------------------------------------------------------------
1363 request
= """GET brokers"""
1364 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1366 good_response
= """address;alive;name;port;spare
1367 localhost;1;broker-1;7772;0
1368 othernode;0;broker-2;7772;1
1370 print response
== good_response
1371 self
.assert_(self
.lines_equal(response
, good_response
))
1375 def test_problems_table(self
):
1377 self
.update_broker()
1378 host
= self
.sched
.hosts
.find_by_name("test_host_0")
1379 host
.checks_in_progress
= []
1380 host
.act_depend_of
= [] # ignore the router
1381 router
= self
.sched
.hosts
.find_by_name("test_router_0")
1382 router
.checks_in_progress
= []
1383 router
.act_depend_of
= [] # ignore the router
1384 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1385 svc
.checks_in_progress
= []
1386 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
1387 self
.scheduler_loop(4, [[host
, 2, 'DOWN'], [router
, 2, 'DOWN'], [svc
, 2, 'BAD']])
1388 print "Is router a problem?", router
.is_problem
, router
.state
, router
.state_type
1389 print "Is host a problem?", host
.is_problem
, host
.state
, host
.state_type
1390 print "Is service a problem?", svc
.is_problem
, svc
.state
, svc
.state_type
1391 self
.update_broker()
1392 print "All", self
.livestatus_broker
.hosts
1393 for h
in self
.livestatus_broker
.hosts
.values():
1394 print h
.get_dbg_name(), h
.is_problem
1396 #---------------------------------------------------------------
1397 # get the columns meta-table
1398 #---------------------------------------------------------------
1399 request
= """GET problems"""
1400 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1401 print "FUCK", response
1402 good_response
= """impacts;source
1403 test_host_0,test_host_0/test_ok_0;test_router_0
1405 print response
== good_response
1406 self
.assert_(response
== good_response
)
1409 def test_parent_childs_dep_lists(self
):
1411 self
.update_broker()
1412 host
= self
.sched
.hosts
.find_by_name("test_host_0")
1413 router
= self
.sched
.hosts
.find_by_name("test_router_0")
1414 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1416 #---------------------------------------------------------------
1417 # get the columns meta-table
1418 #---------------------------------------------------------------
1419 # first test if test_router_0 is in the host parent list
1420 request
= 'GET hosts\nColumns: host_name parent_dependencies\nFilter: host_name = test_host_0\n'
1421 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1422 good_response
= """test_host_0;test_router_0"""
1423 self
.assert_(response
.strip() == good_response
.strip())
1425 # Now check if host is in the child router list
1426 request
= 'GET hosts\nColumns: host_name child_dependencies\nFilter: host_name = test_router_0\n'
1427 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1428 good_response
= """test_router_0;test_host_0"""
1429 self
.assert_(response
.strip() == good_response
.strip())
1431 # Now check with the service
1432 request
= 'GET hosts\nColumns: host_name child_dependencies\nFilter: host_name = test_host_0\n'
1433 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1434 good_response
= """test_host_0;test_host_0/test_ok_0"""
1435 self
.assert_(response
.strip() == good_response
.strip())
1437 # And check the parent for the service
1438 request
= 'GET services\nColumns: parent_dependencies\nFilter: host_name = test_host_0\n'
1439 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1440 good_response
= """test_host_0"""
1441 self
.assert_(response
.strip() == good_response
.strip())
1444 def test_limit(self
):
1446 if self
.nagios_installed():
1447 self
.start_nagios('1r_1h_1s')
1449 self
.update_broker()
1450 #---------------------------------------------------------------
1451 # get the full hosts table
1452 #---------------------------------------------------------------
1453 request
= 'GET hosts\nColumns: host_name\n'
1454 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1456 good_response
= """test_host_0
1459 self
.assert_(self
.lines_equal(response
, good_response
))
1461 request
= 'GET hosts\nColumns: host_name\nLimit: 1\n'
1462 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1464 good_response
= """test_host_0
1466 # it must be test_host_0 because with Limit: the output is
1467 # alphabetically ordered
1468 self
.assert_(response
== good_response
)
1469 # TODO look whats wrong
1470 if self
.nagios_installed():
1471 nagresponse
= self
.ask_nagios(request
)
1473 # self.assert_(self.lines_equal(response, nagresponse))
1477 def test_problem_impact_in_host_service(self
):
1480 self
.update_broker()
1482 host_router_0
= self
.sched
.hosts
.find_by_name("test_router_0")
1483 host_router_0
.checks_in_progress
= []
1485 #Then initialize host under theses routers
1486 host_0
= self
.sched
.hosts
.find_by_name("test_host_0")
1487 host_0
.checks_in_progress
= []
1489 all_hosts
= [host_router_0
, host_0
]
1490 all_routers
= [host_router_0
]
1491 all_servers
= [host_0
]
1493 print "- 4 x UP -------------------------------------"
1494 self
.scheduler_loop(1, [[host_router_0
, 0, 'UP'], [host_0
, 0, 'UP']], do_sleep
=False)
1495 self
.scheduler_loop(1, [[host_router_0
, 1, 'DOWN']], do_sleep
=False)
1496 self
.scheduler_loop(1, [[host_router_0
, 1, 'DOWN']], do_sleep
=False)
1497 self
.scheduler_loop(1, [[host_router_0
, 1, 'DOWN']], do_sleep
=False)
1498 self
.scheduler_loop(1, [[host_router_0
, 1, 'DOWN']], do_sleep
=False)
1499 self
.scheduler_loop(1, [[host_router_0
, 1, 'DOWN']], do_sleep
=False)
1501 #Max attempt is reach, should be HARD now
1502 for h
in all_routers
:
1503 self
.assert_(h
.state
== 'DOWN')
1504 self
.assert_(h
.state_type
== 'HARD')
1506 for b
in self
.sched
.broks
.values():
1507 print "All broks", b
.type, b
1508 if b
.type == 'update_host_status':
1510 print "Impacts", b
.data
['impacts']
1511 print "Sources", b
.data
['source_problems']
1513 for b
in host_router_0
.broks
:
1514 print " host_router_0.broks", b
1516 self
.update_broker()
1518 print "source de host_0", host_0
.source_problems
1519 for i
in host_0
.source_problems
:
1520 print "source", i
.get_name()
1521 print "impacts de host_router_0", host_router_0
.impacts
1522 for i
in host_router_0
.impacts
:
1523 print "impact", i
.get_name()
1525 #---------------------------------------------------------------
1526 # get the full hosts table
1527 #---------------------------------------------------------------
1528 print "Got source problems"
1529 request
= 'GET hosts\nColumns: host_name is_impact source_problems\n'
1530 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1531 print "moncullulu2", response
1532 good_response
= """test_router_0;0;
1533 test_host_0;1;test_router_0
1535 #self.assert_(self.lines_equal(response, good_response))
1537 print "Now got impact"
1538 request
= 'GET hosts\nColumns: host_name is_problem impacts\n'
1539 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1540 print "moncululu", response
1541 good_response
= """test_router_0;1;test_host_0,test_host_0/test_ok_0
1543 self
.assert_(self
.lines_equal(response
.strip(), good_response
.strip()))
1545 request
= 'GET hosts\nColumns: host_name\nLimit: 1\n'
1546 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1548 good_response
= """test_host_0
1550 # it must be test_host_0 because with Limit: the output is
1551 # alphabetically ordered
1552 self
.assert_(response
== good_response
)
1556 def test_thruk_servicegroup(self
):
1559 self
.update_broker()
1560 #---------------------------------------------------------------
1561 # get services of a certain servicegroup
1562 # test_host_0/test_ok_0 is in
1563 # servicegroup_01,ok via service.servicegroups
1564 # servicegroup_02 via servicegroup.members
1565 #---------------------------------------------------------------
1566 request
= """GET services
1567 Columns: host_name service_description
1568 Filter: groups >= servicegroup_01
1570 ResponseHeader: fixed16
1572 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1573 self
.assert_(response
== """200 22
1574 test_host_0;test_ok_0
1576 request
= """GET services
1577 Columns: host_name service_description
1578 Filter: groups >= servicegroup_02
1580 ResponseHeader: fixed16
1582 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1583 self
.assert_(response
== """200 22
1584 test_host_0;test_ok_0
1589 def test_is_executing(self
):
1591 #---------------------------------------------------------------
1592 # make sure that the is_executing flag is updated regularly
1593 #---------------------------------------------------------------
1595 host
= self
.sched
.hosts
.find_by_name("test_host_0")
1596 host
.checks_in_progress
= []
1597 host
.act_depend_of
= [] # ignore the router
1598 router
= self
.sched
.hosts
.find_by_name("test_router_0")
1599 router
.checks_in_progress
= []
1600 router
.act_depend_of
= [] # ignore the router
1601 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1602 svc
.checks_in_progress
= []
1603 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
1605 for loop
in range(1, 2):
1606 print "processing check", loop
1607 self
.show_broks("update_in_checking")
1608 svc
.update_in_checking()
1609 self
.show_broks("fake_check")
1610 self
.fake_check(svc
, 2, 'BAD')
1611 self
.show_broks("sched.consume_results")
1612 self
.sched
.consume_results()
1613 self
.show_broks("sched.get_new_actions")
1614 self
.sched
.get_new_actions()
1615 self
.show_broks("sched.get_new_broks")
1616 self
.sched
.get_new_broks()
1617 self
.show_broks("sched.delete_zombie_checks")
1618 self
.sched
.delete_zombie_checks()
1619 self
.show_broks("sched.delete_zombie_actions")
1620 self
.sched
.delete_zombie_actions()
1621 self
.show_broks("sched.get_to_run_checks")
1622 checks
= self
.sched
.get_to_run_checks(True, False)
1623 self
.show_broks("sched.get_to_run_checks")
1624 actions
= self
.sched
.get_to_run_checks(False, True)
1625 #self.show_actions()
1627 a
.status
= 'inpoller'
1628 a
.check_time
= time
.time()
1630 self
.sched
.put_results(a
)
1631 #self.show_actions()
1633 svc
.checks_in_progress
= []
1634 self
.show_broks("sched.update_downtimes_and_comments")
1635 self
.sched
.update_downtimes_and_comments()
1638 print "-------------------------------------------------"
1639 for brok
in sorted(self
.sched
.broks
.values(), lambda x
, y
: x
.id - y
.id):
1640 if re
.compile('^service_').match(brok
.type):
1641 print "BROK:", brok
.type
1642 print "BROK ", brok
.data
['in_checking']
1643 self
.update_broker()
1644 print "-------------------------------------------------"
1645 request
= 'GET services\nColumns: service_description is_executing\n'
1646 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1651 def test_pnp_path(self
):
1654 self
.update_broker()
1655 #---------------------------------------------------------------
1656 # pnp_path is a parameter for the module
1657 # column pnpgraph_present checks if a file
1658 # <pnp_path>/host/service.xml
1659 # <pnp_path>/host/_HOST_.xml
1661 #---------------------------------------------------------------
1662 pnp_path
= self
.pnp4nagios
1664 os
.removedirs(pnp_path
)
1668 print "there is no spool dir", pnp_path
1670 request
= """GET services
1671 Columns: host_name service_description pnpgraph_present
1673 ResponseHeader: fixed16
1675 requesth
= """GET hosts
1676 Columns: host_name pnpgraph_present
1678 ResponseHeader: fixed16
1680 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1682 self
.assert_(response
== """200 24
1683 test_host_0;test_ok_0;0
1685 #self.assert_(not self.livestatus_broker.livestatus.pnp_path)
1688 os
.makedirs(pnp_path
)
1689 print "there is an empty spool dir", pnp_path
1693 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1695 self
.assert_(response
== """200 24
1696 test_host_0;test_ok_0;0
1698 self
.assert_(self
.livestatus_broker
.livestatus
.pnp_path
== pnp_path
+ '/')
1701 os
.makedirs(pnp_path
+ '/test_host_0')
1702 open(pnp_path
+ '/test_host_0/_HOST_.xml', 'w').close()
1703 open(pnp_path
+ '/test_host_0/test_ok_0.xml', 'w').close()
1704 print "there is a spool dir with data", pnp_path
1708 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1710 self
.assert_(response
== """200 24
1711 test_host_0;test_ok_0;1
1713 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(requesth
)
1715 goodresponse
= """200 30
1719 self
.assert_(self
.lines_equal(response
, goodresponse
))
1722 def test_thruk_action_notes_url(self
):
1725 self
.update_broker()
1726 request
= """GET services
1727 Columns: host_name service_description action_url
1728 Filter: host_name = test_host_0
1729 Filter: service_description = test_ok_0
1731 ResponseHeader: fixed16
1733 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1734 self
.assert_(response
== """200 78
1735 test_host_0;test_ok_0;/nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
1738 request
= """GET services
1739 Columns: host_name service_description action_url_expanded
1740 Filter: host_name = test_host_0
1741 Filter: service_description = test_ok_0
1743 ResponseHeader: fixed16
1745 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1746 self
.assert_(response
== """200 75
1747 test_host_0;test_ok_0;/nagios/pnp/index.php?host=test_host_0&srv=test_ok_0
1750 request
= """GET hosts
1751 Columns: host_name action_url_expanded notes_url_expanded
1752 Filter: host_name = test_host_0
1754 ResponseHeader: fixed16
1756 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1757 self
.assert_(response
== """200 85
1758 test_host_0;/nagios/pnp/index.php?host=test_host_0;/nagios/wiki/doku.php/test_host_0
1762 def test_thruk_custom_variables(self
):
1765 self
.update_broker()
1766 request
= """GET hosts
1767 Columns: host_name custom_variable_names custom_variable_values
1768 Filter: host_name = test_host_0
1770 ResponseHeader: fixed16
1772 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1773 self
.assert_(response
== """200 42
1774 test_host_0;OSLICENSE,OSTYPE;gpl,gnulinux
1777 request
= """GET services
1778 Columns: host_name service_description custom_variable_names custom_variable_values
1779 Filter: host_name = test_host_0
1780 Filter: service_description = test_ok_0
1782 ResponseHeader: fixed16
1784 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1785 self
.assert_(response
== """200 41
1786 test_host_0;test_ok_0;CUSTNAME;custvalue
1790 def test_multisite_in_check_period(self
):
1792 self
.update_broker()
1793 # timeperiods must be manipulated in the broker, because status-broks
1794 # contain timeperiod names, not objects.
1795 lshost
= self
.livestatus_broker
.find_host("test_host_0")
1797 localnow
= time
.localtime(now
)
1798 if localnow
[5] > 45:
1800 nextminute
= time
.localtime(time
.time() + 60)
1801 tonextminute
= '%s 00:00-%02d:%02d' % (['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'][nextminute
[6]], nextminute
[3], nextminute
[4])
1802 fromnextminute
= '%s %02d:%02d-23:59' % (['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'][nextminute
[6]], nextminute
[3], nextminute
[4])
1804 lshost
.notification_period
= Timeperiod()
1805 lshost
.notification_period
.resolve_daterange(lshost
.notification_period
.dateranges
, tonextminute
)
1806 lshost
.check_period
= Timeperiod()
1807 lshost
.check_period
.resolve_daterange(lshost
.check_period
.dateranges
, fromnextminute
)
1808 self
.update_broker()
1809 print "now it is", time
.asctime()
1810 print "notification_period is", tonextminute
1811 print "check_period is", fromnextminute
1812 request
= """GET hosts
1813 Columns: host_name in_notification_period in_check_period
1814 Filter: host_name = test_host_0
1816 ResponseHeader: fixed16
1819 # inside notification_period, outside check_period
1821 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1822 self
.assert_(response
== """200 16
1826 # a minute later it's the other way round
1827 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1828 self
.assert_(response
== """200 16
1833 def test_thruk_log_current_groups(self
):
1836 b
= Brok('log', {'log' : "[%lu] EXTERNAL COMMAND: [%lu] DISABLE_NOTIFICATIONS" % (now
, now
) })
1837 self
.livestatus_broker
.manage_log_brok(b
)
1838 b
= Brok('log', {'log' : "[%lu] EXTERNAL COMMAND: [%lu] STOP_EXECUTING_SVC_CHECKS" % (now
, now
) })
1839 self
.livestatus_broker
.manage_log_brok(b
)
1840 self
.update_broker()
1841 host
= self
.sched
.hosts
.find_by_name("test_host_0")
1842 host
.checks_in_progress
= []
1843 host
.act_depend_of
= [] # ignore the router
1844 router
= self
.sched
.hosts
.find_by_name("test_router_0")
1845 router
.checks_in_progress
= []
1846 router
.act_depend_of
= [] # ignore the router
1847 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1848 svc
.checks_in_progress
= []
1849 svc
.act_depend_of
= [] # no hostchecks on critical checkresults
1850 self
.update_broker()
1851 self
.scheduler_loop(1, [[host
, 0, 'UP'], [router
, 0, 'UP'], [svc
, 1, 'WARNING']])
1852 self
.update_broker()
1853 # select messages which are not host or service related. current_service_groups must be an empty list
1854 request
= """GET log
1855 Filter: current_host_name =
1856 Filter: current_service_description =
1858 Columns: message current_service_groups
1860 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1861 request
= """GET log
1862 Filter: current_host_name =
1863 Filter: current_service_description =
1865 Columns: message current_service_groups
1868 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1869 good_response
= "[[\"[%lu] EXTERNAL COMMAND: [%lu] DISABLE_NOTIFICATIONS\",[]],[\"[%lu] EXTERNAL COMMAND: [%lu] STOP_EXECUTING_SVC_CHECKS\",[]]]\n" % (now
, now
, now
, now
)
1871 self
.assert_(response
== good_response
)
1873 request
= """GET log
1874 Columns: time current_host_name current_service_description current_host_groups current_service_groups
1875 Filter: time >= """ + str(int(now
)) + """
1876 Filter: current_host_name = test_host_0
1877 Filter: current_service_description = test_ok_0
1879 good_response
= """1234567890;test_host_0;test_ok_0;hostgroup_01,allhosts,up;servicegroup_02,ok,servicegroup_01
1881 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1882 # remove the timestamps
1883 good_response
= ';'.join(good_response
.split(';')[1:])
1884 response
= ';'.join(response
.split(';')[1:])
1886 self
.assert_(self
.lines_equal(response
, good_response
))
1889 def test_thruk_empty_stats(self
):
1891 self
.update_broker()
1892 # surely no host object matches with this filter
1893 # nonetheless there must be a line of output
1894 request
= """GET hosts
1895 Filter: has_been_checked = 10
1896 Filter: check_type = 10
1897 Stats: sum percent_state_change
1898 Stats: min percent_state_change
1899 Stats: max percent_state_change
1900 OutputFormat: csv"""
1902 good_response
= """0;0;0"""
1904 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1906 self
.assert_(self
.lines_equal(response
, good_response
))
1909 def test_thruk_host_parents(self
):
1911 self
.update_broker()
1912 # surely no host object matches with this filter
1913 # nonetheless there must be a line of output
1914 request
= """GET hosts
1915 Columns: host_name parents
1916 OutputFormat: csv"""
1918 good_response
= """test_router_0;
1919 test_host_0;test_router_0
1922 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1924 self
.assert_(self
.lines_equal(response
, good_response
))
1928 def test_statsgroupby(self
):
1932 for host
in self
.sched
.hosts
:
1933 objlist
.append([host
, 0, 'UP'])
1934 for service
in self
.sched
.services
:
1935 objlist
.append([service
, 0, 'OK'])
1936 self
.scheduler_loop(1, objlist
)
1937 self
.update_broker()
1938 svc1
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1940 self
.scheduler_loop(1, [[svc1
, 1, 'W']])
1941 self
.update_broker()
1943 request
= """GET services
1944 Filter: contacts >= test_contact
1945 Stats: state != 9999
1950 StatsGroupBy: host_name"""
1951 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1953 self
.assert_(self
.contains_line(response
, 'test_host_0;1;0;1;0;0'))
1955 request
= """GET services
1956 Stats: state != 9999
1959 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
1961 # does not show null-values
1962 #self.assert_(self.contains_line(response, '0;0'))
1963 self
.assert_(self
.contains_line(response
, '1;1'))
1964 #self.assert_(self.contains_line(response, '2;0'))
1965 #self.assert_(self.contains_line(response, '3;0'))
1968 def test_multisite_column_groupby(self
):
1972 for host
in self
.sched
.hosts
:
1973 objlist
.append([host
, 0, 'UP'])
1974 for service
in self
.sched
.services
:
1975 objlist
.append([service
, 0, 'OK'])
1976 self
.scheduler_loop(1, objlist
)
1977 self
.update_broker()
1978 router
= self
.sched
.hosts
.find_by_name("test_router_0")
1979 host
= self
.sched
.hosts
.find_by_name("test_host_0")
1980 svc
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_0", "test_ok_0")
1981 host
.act_depend_of
= []
1982 router
.act_depend_of
= []
1983 self
.scheduler_loop(4, [[router
, 1, 'D'], [host
, 1, 'D'], [svc
, 1, 'W']])
1984 self
.update_broker()
1985 self
.scheduler_loop(1, [[router
, 0, 'U'], [host
, 0, 'U'], [svc
, 0, 'O']])
1986 self
.update_broker()
1987 self
.scheduler_loop(1, [[router
, 1, 'D'], [host
, 0, 'U'], [svc
, 2, 'C']])
1988 self
.update_broker()
1990 request
= """GET log
1991 Columns: host_name service_description
1992 Filter: log_time >= 1292256802
2001 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2003 self
.assert_(self
.contains_line(response
, 'test_host_0;;1;3;0;0;3'))
2004 self
.assert_(self
.contains_line(response
, 'test_router_0;;1;4;0;0;4'))
2005 self
.assert_(self
.contains_line(response
, 'test_host_0;test_ok_0;1;2;1;0;3'))
2008 # does not show null-values
2009 #self.assert_(self.contains_line(response, '0;0'))
2010 #self.assert_(self.contains_line(response, '1;1'))
2011 #self.assert_(self.contains_line(response, '2;0'))
2012 #self.assert_(self.contains_line(response, '3;0'))
2016 class TestConfigBig(TestConfig
):
2018 self
.setup_with_file('etc/nagios_5r_100h_2000s.cfg')
2019 self
.testid
= str(os
.getpid() + random
.randint(1, 1000))
2020 self
.livelogs
= 'tmp/livelogs.db' + self
.testid
2021 self
.pnp4nagios
= 'tmp/pnp4nagios_test' + self
.testid
2022 self
.livestatus_broker
= Livestatus_broker(modconf
, '127.0.0.1', str(50000 + os
.getpid()), 'live', [], self
.livelogs
, 365, self
.pnp4nagios
)
2023 self
.livestatus_broker
.create_queues()
2024 #self.livestatus_broker.properties = {
2028 self
.livestatus_broker
.init()
2029 print "Cleaning old broks?"
2030 self
.sched
.fill_initial_broks()
2031 self
.update_broker()
2036 self
.livestatus_broker
.dbconn
.commit()
2037 self
.livestatus_broker
.dbconn
.close()
2038 if os
.path
.exists(self
.livelogs
):
2039 os
.remove(self
.livelogs
)
2040 if os
.path
.exists(self
.pnp4nagios
):
2041 shutil
.rmtree(self
.pnp4nagios
)
2044 def test_stats(self
):
2046 if self
.nagios_installed():
2047 self
.start_nagios('5r_100h_2000s')
2050 for host
in self
.sched
.hosts
:
2051 objlist
.append([host
, 0, 'UP'])
2052 for service
in self
.sched
.services
:
2053 objlist
.append([service
, 0, 'OK'])
2054 self
.scheduler_loop(1, objlist
)
2055 self
.update_broker()
2056 svc1
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_00")
2058 svc2
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_15")
2060 svc3
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_16")
2062 svc4
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_007", "test_ok_05")
2064 svc5
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_007", "test_ok_11")
2065 svc6
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_025", "test_ok_01")
2066 svc7
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_025", "test_ok_03")
2067 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']])
2068 self
.update_broker()
2069 # 1993O, 3xW, 3xC, 1xU
2071 request
= """GET services
2072 Filter: contacts >= test_contact
2073 Stats: state != 9999
2078 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2079 print 'query_6_______________\n%s\n%s\n' % (request
, response
)
2080 self
.assert_(response
== '2000;1993;3;3;1\n')
2081 if self
.nagios_installed():
2082 nagresponse
= self
.ask_nagios(request
)
2084 self
.assert_(self
.lines_equal(response
, nagresponse
))
2087 def test_statsgroupby(self
):
2089 if self
.nagios_installed():
2090 self
.start_nagios('5r_100h_2000s')
2093 for host
in self
.sched
.hosts
:
2094 objlist
.append([host
, 0, 'UP'])
2095 for service
in self
.sched
.services
:
2096 objlist
.append([service
, 0, 'OK'])
2097 self
.scheduler_loop(1, objlist
)
2098 self
.update_broker()
2099 svc1
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_00")
2101 svc2
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_15")
2103 svc3
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_005", "test_ok_16")
2105 svc4
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_007", "test_ok_05")
2107 svc5
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_007", "test_ok_11")
2108 svc6
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_025", "test_ok_01")
2109 svc7
= self
.sched
.services
.find_srv_by_name_and_hostname("test_host_025", "test_ok_03")
2110 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']])
2111 self
.update_broker()
2112 # 1993O, 3xW, 3xC, 1xU
2114 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'
2115 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2117 self
.assert_(self
.contains_line(response
, 'test_host_005;20;17;3;0;0'))
2118 self
.assert_(self
.contains_line(response
, 'test_host_007;20;18;0;1;1'))
2119 self
.assert_(self
.contains_line(response
, 'test_host_025;20;18;0;2;0'))
2120 self
.assert_(self
.contains_line(response
, 'test_host_026;20;20;0;0;0'))
2122 request
= """GET services
2123 Stats: state != 9999
2126 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2128 self
.assert_(self
.contains_line(response
, '0;1993'))
2129 self
.assert_(self
.contains_line(response
, '1;3'))
2130 self
.assert_(self
.contains_line(response
, '2;3'))
2131 self
.assert_(self
.contains_line(response
, '3;1'))
2132 if self
.nagios_installed():
2133 nagresponse
= self
.ask_nagios(request
)
2135 self
.assert_(self
.lines_equal(response
, nagresponse
))
2138 def test_hostsbygroup(self
):
2140 if self
.nagios_installed():
2141 self
.start_nagios('5r_100h_2000s')
2144 for host
in self
.sched
.hosts
:
2145 objlist
.append([host
, 0, 'UP'])
2146 for service
in self
.sched
.services
:
2147 objlist
.append([service
, 0, 'OK'])
2148 self
.scheduler_loop(1, objlist
)
2149 self
.update_broker()
2150 request
= """GET hostsbygroup
2152 Columns: host_name hostgroup_name
2155 ResponseHeader: fixed16
2158 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2160 if self
.nagios_installed():
2161 nagresponse
= self
.ask_nagios(request
)
2163 self
.assert_(self
.lines_equal(response
, nagresponse
))
2166 def test_servicesbyhostgroup(self
):
2168 if self
.nagios_installed():
2169 self
.start_nagios('5r_100h_2000s')
2172 for host
in self
.sched
.hosts
:
2173 objlist
.append([host
, 0, 'UP'])
2174 for service
in self
.sched
.services
:
2175 objlist
.append([service
, 0, 'OK'])
2176 self
.scheduler_loop(1, objlist
)
2177 self
.update_broker()
2178 request
= """GET servicesbyhostgroup
2179 Filter: host_groups >= up
2180 Stats: has_been_checked = 0
2182 Stats: has_been_checked != 0
2183 Stats: scheduled_downtime_depth = 0
2184 Stats: host_scheduled_downtime_depth = 0
2187 Stats: scheduled_downtime_depth > 0
2188 Stats: host_scheduled_downtime_depth > 0
2191 Stats: acknowledged = 0
2192 Stats: host_acknowledged = 0
2193 Stats: scheduled_downtime_depth = 0
2194 Stats: host_scheduled_downtime_depth = 0
2197 Stats: acknowledged = 1
2198 Stats: host_acknowledged = 1
2202 Stats: scheduled_downtime_depth > 0
2203 Stats: host_scheduled_downtime_depth > 0
2207 Stats: acknowledged = 0
2208 Stats: host_acknowledged = 0
2209 Stats: scheduled_downtime_depth = 0
2210 Stats: host_scheduled_downtime_depth = 0
2213 Stats: acknowledged = 1
2214 Stats: host_acknowledged = 1
2218 Stats: scheduled_downtime_depth > 0
2219 Stats: host_scheduled_downtime_depth > 0
2223 Stats: acknowledged = 0
2224 Stats: host_acknowledged = 0
2225 Stats: scheduled_downtime_depth = 0
2226 Stats: host_scheduled_downtime_depth = 0
2229 Stats: acknowledged = 1
2230 Stats: host_acknowledged = 1
2234 Stats: scheduled_downtime_depth > 0
2235 Stats: host_scheduled_downtime_depth > 0
2238 StatsGroupBy: hostgroup_name
2241 ResponseHeader: fixed16
2244 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2246 print "livestatus duration %f" % (tac
- tic
)
2248 if self
.nagios_installed():
2249 nagresponse
= self
.ask_nagios(request
)
2251 self
.assert_(self
.lines_equal(response
, nagresponse
))
2253 # Again, without Filter:
2254 request
= """GET servicesbyhostgroup
2255 Stats: has_been_checked = 0
2257 Stats: has_been_checked != 0
2258 Stats: scheduled_downtime_depth = 0
2259 Stats: host_scheduled_downtime_depth = 0
2262 Stats: scheduled_downtime_depth > 0
2263 Stats: host_scheduled_downtime_depth > 0
2266 Stats: acknowledged = 0
2267 Stats: host_acknowledged = 0
2268 Stats: scheduled_downtime_depth = 0
2269 Stats: host_scheduled_downtime_depth = 0
2272 Stats: acknowledged = 1
2273 Stats: host_acknowledged = 1
2277 Stats: scheduled_downtime_depth > 0
2278 Stats: host_scheduled_downtime_depth > 0
2282 Stats: acknowledged = 0
2283 Stats: host_acknowledged = 0
2284 Stats: scheduled_downtime_depth = 0
2285 Stats: host_scheduled_downtime_depth = 0
2288 Stats: acknowledged = 1
2289 Stats: host_acknowledged = 1
2293 Stats: scheduled_downtime_depth > 0
2294 Stats: host_scheduled_downtime_depth > 0
2298 Stats: acknowledged = 0
2299 Stats: host_acknowledged = 0
2300 Stats: scheduled_downtime_depth = 0
2301 Stats: host_scheduled_downtime_depth = 0
2304 Stats: acknowledged = 1
2305 Stats: host_acknowledged = 1
2309 Stats: scheduled_downtime_depth > 0
2310 Stats: host_scheduled_downtime_depth > 0
2313 StatsGroupBy: hostgroup_name
2316 ResponseHeader: fixed16
2318 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2320 if self
.nagios_installed():
2321 nagresponse
= self
.ask_nagios(request
)
2323 self
.assert_(self
.lines_equal(response
, nagresponse
))
2326 def test_childs(self
):
2327 if self
.nagios_installed():
2328 self
.start_nagios('5r_100h_2000s')
2332 for host
in self
.sched
.hosts
:
2333 objlist
.append([host
, 0, 'UP'])
2334 for service
in self
.sched
.services
:
2335 objlist
.append([service
, 0, 'OK'])
2336 self
.scheduler_loop(1, objlist
)
2337 self
.update_broker()
2338 request
= """GET hosts
2340 Filter: name = test_host_0
2343 ResponseHeader: fixed16
2345 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2347 if self
.nagios_installed():
2348 nagresponse
= self
.ask_nagios(request
)
2349 print "nagresponse----------------------------------------------"
2351 self
.assert_(self
.lines_equal(response
, nagresponse
))
2352 request
= """GET hosts
2354 Filter: name = test_router_0
2357 ResponseHeader: fixed16
2359 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2361 if self
.nagios_installed():
2362 nagresponse
= self
.ask_nagios(request
)
2363 print "nagresponse----------------------------------------------"
2365 self
.assert_(self
.lines_equal(response
, nagresponse
))
2368 def test_thruk_servicegroup(self
):
2371 self
.update_broker()
2372 #---------------------------------------------------------------
2373 # get services of a certain servicegroup
2374 # test_host_0/test_ok_0 is in
2375 # servicegroup_01,ok via service.servicegroups
2376 # servicegroup_02 via servicegroup.members
2377 #---------------------------------------------------------------
2378 request
= """GET services
2379 Columns: host_name service_description
2380 Filter: groups >= servicegroup_01
2382 ResponseHeader: fixed16
2384 # 400 services => 400 lines + header + empty last line
2385 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2387 self
.assert_(len(response
.split("\n")) == 402)
2389 request
= """GET servicegroups
2390 Columns: name members
2391 Filter: name = servicegroup_01
2394 # 400 services => 400 lines
2395 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2397 # take first line, take members column, count list elements = 400 services
2398 self
.assert_(len(((response
.split("\n")[0]).split(';')[1]).split(',')) == 400)
2401 class TestConfigComplex(TestConfig
):
2403 self
.setup_with_file('etc/nagios_problem_impact.cfg')
2404 self
.testid
= str(os
.getpid() + random
.randint(1, 1000))
2405 self
.livelogs
= 'tmp/livelogs.db' + self
.testid
2406 self
.pnp4nagios
= 'tmp/pnp4nagios_test' + self
.testid
2407 self
.livestatus_broker
= Livestatus_broker(modconf
, '127.0.0.1', str(50000 + os
.getpid()), 'live', [], self
.livelogs
, 365, self
.pnp4nagios
)
2408 self
.livestatus_broker
.create_queues()
2410 self
.livestatus_broker
.properties
= {
2415 self
.livestatus_broker
.init()
2416 print "Cleaning old broks?"
2417 self
.sched
.fill_initial_broks()
2418 self
.update_broker()
2419 self
.nagios_path
= None
2420 self
.livestatus_path
= None
2421 self
.nagios_config
= None
2426 self
.livestatus_broker
.dbconn
.commit()
2427 self
.livestatus_broker
.dbconn
.close()
2428 if os
.path
.exists(self
.livelogs
):
2429 os
.remove(self
.livelogs
)
2430 if os
.path
.exists(self
.pnp4nagios
):
2431 shutil
.rmtree(self
.pnp4nagios
)
2434 # test_host_0 has parents test_router_0,test_router_1
2435 def test_thruk_parents(self
):
2439 for host
in self
.sched
.hosts
:
2440 objlist
.append([host
, 0, 'UP'])
2441 for service
in self
.sched
.services
:
2442 objlist
.append([service
, 0, 'OK'])
2443 self
.scheduler_loop(1, objlist
)
2444 self
.update_broker()
2445 request
= """GET hosts
2446 Columns: host_name parents childs
2449 good_response
= """test_router_0;;test_host_0,test_host_1
2450 test_router_1;;test_host_0,test_host_1
2451 test_host_0;test_router_0,test_router_1;
2452 test_host_1;test_router_0,test_router_1;
2454 response
, keepalive
= self
.livestatus_broker
.livestatus
.handle_request(request
)
2456 self
.assert_(self
.lines_equal(response
, good_response
))
2460 if __name__
== '__main__':
2462 command
= """unittest.main()"""
2464 #cProfile.runctx( command, globals(), locals(), filename="Thruk.profile" )