2 from __future__ import unicode_literals
8 from ido_mysql import run_query
12 TYPE_PASSIVE_CHECK = 1
14 CHECK_INTERVAL = 300 # seconds
15 CHECKRESULT_READ_INTERVAL = 5 # seconds
16 CHECKRESULT_LOCATION = '/tmp/icinga2/checkresults'
17 CHECKRESULT_TEMPLATE = """
18 host_name=%(hostname)s
19 service_description=%(servicename)s
20 check_type=%(check_type)s
25 start_time=%(start_time)s
26 finish_time=%(finish_time)s
28 exited_ok=%(excited_ok)s
29 return_code=%(return_code)s
35 # We need to wait a bit first as Icinga processes a
36 # checkresult only if its newer than the last check
37 query = 'select unix_timestamp(s.last_check) as last_check ' \
38 'from icinga_servicestatus as s ' \
39 'inner join icinga_services as c ' \
40 'on s.service_object_id = c.service_object_id ' \
41 "where c.display_name = 'PassiveService1'"
42 state_time = float(next(iter(run_query(query)), {}).get('last_check', '0'))
44 print '"PassiveService1" seems not to have been checked yet'
47 if (state_time + CHECK_INTERVAL) - time.time() < 30:
50 # Now pass the checkresult in
51 with open(os.path.join(CHECKRESULT_LOCATION, 'cfoobar'), 'w') as f:
52 f.write(CHECKRESULT_TEMPLATE % {
53 'hostname': 'nsca-ng',
54 'servicename': 'PassiveService1',
55 'check_type': TYPE_PASSIVE_CHECK,
56 'start_time': time.time(),
57 'finish_time': time.time(),
59 'return_code': STATE_OK,
60 'output': 'Passing in CheckResult header files works!'
63 # And notfiy Icinga that the file has been completely written...
64 with open(os.path.join(CHECKRESULT_LOCATION, 'cfoobar.ok'), 'w') as f:
67 # Lastly check whether the service changed its state
68 time.sleep(CHECKRESULT_READ_INTERVAL * 2)
70 query = 'select s.output ' \
71 'from icinga_servicestatus as s ' \
72 'inner join icinga_services as c ' \
73 'on s.service_object_id = c.service_object_id ' \
74 "where c.display_name = 'PassiveService1'"
75 output = next(iter(run_query(query)), {}).get('output', '')
76 if output != 'Passing in CheckResult header files works!':
77 print 'Checkresult header files seem not to be processed properly'
80 print 'Checkresult header files are processed properly'
84 if __name__ == '__main__':