1 from __future__ import unicode_literals
3 from datetime import datetime, timedelta
6 CHECK_INTERVAL = 10 # minutes; The actual interval are 5 minutes but as other
7 # tests might restart Icinga we need to take any
8 # rescheduling into account
10 TABLE_PREFIX = 'icinga_'
20 'contactnotifications',
33 # Current status tables
35 'customvariablestatus',
42 # Configuration tables
45 'configfilevariables',
47 'contact_notificationcommands',
48 'contactgroup_members',
50 'contactnotificationmethods',
57 'hostescalation_contactgroups',
58 'hostescalation_contacts',
63 'service_contactgroups',
65 'servicedependencies',
66 'serviceescalation_contactgroups',
67 'serviceescalation_contacts',
69 'servicegroup_members',
72 'timeperiod_timeranges',
76 'localhost': ['disk', 'http', 'icinga', 'load', 'ping4',
77 'ping6', 'processes', 'ssh', 'users'],
78 'nsca-ng': ['PassiveService1', 'PassiveService2']
82 def validate_tables(tables):
84 Return whether all tables of the IDO database scheme exist in
85 the given table listing
88 missing = [n for n in TABLES if TABLE_PREFIX + n not in tables]
90 print 'Some tables are missing in the IDO'
91 print 'Missing tables: ' + ', '.join(missing)
94 print 'All tables were found in the IDO'
98 def verify_host_config(config_data):
100 Return whether the example hosts exist in the given "hosts" table
103 if len([1 for e in config_data if e['alias'] in EXAMPLE_CONFIG]) == 2:
104 print 'All example hosts are stored in the IDO'
107 print 'Some example hosts are missing in the IDO'
111 def verify_service_config(config_data):
113 Return whether the example services exist in the given "services" table
116 for hostname, servicename in ((h, s) for h, ss in EXAMPLE_CONFIG.iteritems()
118 # Not very efficient, but suitable for just two hosts...
119 if not any(1 for c in config_data
120 if c['alias'] == hostname and
121 c['display_name'] == servicename):
122 print 'The config stored in the IDO is missing some services'
125 print 'The service config stored in the IDO is correct'
129 def check_last_host_status_update(check_info):
131 Return whether the example hosts are checked as scheduled
134 for info in check_info:
135 if info['alias'] == 'localhost':
136 last_check = datetime.fromtimestamp(float(info['last_check']))
137 if datetime.now() - last_check > timedelta(minutes=CHECK_INTERVAL,
139 print 'The last status update of host "localhost"' \
140 ' was more than {0} minutes ago'.format(CHECK_INTERVAL)
142 elif info['alias'] == 'nsca-ng':
143 if float(info['last_check']) > 0:
144 print 'The host "nsca-ng" was checked even though' \
145 ' it should not be actively checked'
148 print 'The updates of both example hosts are processed as configured'
152 def check_last_service_status_update(check_info):
154 Return whether the example services are checked as scheduled
157 for info in check_info:
158 if info['display_name'] in EXAMPLE_CONFIG.get(info['alias'], []):
159 last_check = datetime.fromtimestamp(float(info['last_check']))
160 if datetime.now() - last_check > timedelta(minutes=CHECK_INTERVAL,
162 print 'The last status update of service "{0}" of' \
163 ' host "{1}" was more than {2} minutes ago' \
164 ''.format(info['display_name'], info['alias'],
168 print 'The updates of all example services are processed as configured'
172 def check_logentries(logentry_info):
174 Return whether the given logentry originates from host "localhost"
175 and refers to its very last hard status change
178 if logentry_info and logentry_info[0]['alias'] == 'localhost':
179 entry_time = datetime.fromtimestamp(float(logentry_info[0]['entry_time']))
180 state_time = datetime.fromtimestamp(float(logentry_info[0]['state_time']))
181 if entry_time - state_time > timedelta(seconds=10):
182 print 'The last hard state of host "localhost"' \
183 ' seems not to have been logged'
186 print 'No logs found in the IDO for host "localhost"'
189 print 'The last hard state of host "localhost" was properly logged'