]> granicus.if.org Git - icinga2/blob - test/jenkins/files/ido_tests.py
a6d3131e10c901454cb08f3dfc17b48835b4a1f3
[icinga2] / test / jenkins / files / ido_tests.py
1 from __future__ import unicode_literals
2
3 from datetime import datetime, timedelta
4
5
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
9
10 TABLE_PREFIX = 'icinga_'
11 TABLES = [
12     # Central tables
13     'instances',
14     'objects',
15     # Debugging tables
16     'conninfo',
17     # Historical tables
18     'acknowledgements',
19     'commenthistory',
20     'contactnotifications',
21     'dbversion',
22     'downtimehistory',
23     'eventhandlers',
24     'externalcommands',
25     'flappinghistory',
26     'hostchecks',
27     'logentries',
28     'notifications',
29     'processevents',
30     'servicechecks',
31     'statehistory',
32     'systemcommands',
33     # Current status tables
34     'comments',
35     'customvariablestatus',
36     'hoststatus',
37     'programstatus',
38     'runtimevariables',
39     'scheduleddowntime',
40     'servicestatus',
41     'contactstatus',
42     # Configuration tables
43     'commands',
44     'configfiles',
45     'configfilevariables',
46     'contact_addresses',
47     'contact_notificationcommands',
48     'contactgroup_members',
49     'contactgroups',
50     'contactnotificationmethods',
51     'contacts',
52     'customvariables',
53     'host_contactgroups',
54     'host_contacts',
55     'host_parenthosts',
56     'hostdependencies',
57     'hostescalation_contactgroups',
58     'hostescalation_contacts',
59     'hostescalations',
60     'hostgroup_members',
61     'hostgroups',
62     'hosts',
63     'service_contactgroups',
64     'service_contacts',
65     'servicedependencies',
66     'serviceescalation_contactgroups',
67     'serviceescalation_contacts',
68     'serviceescalations',
69     'servicegroup_members',
70     'servicegroups',
71     'services',
72     'timeperiod_timeranges',
73     'timeperiods'
74     ]
75 EXAMPLE_CONFIG = {
76     'localhost': ['disk', 'http', 'icinga', 'load', 'ping4',
77                   'ping6', 'processes', 'ssh', 'users'],
78     'nsca-ng': ['PassiveService1', 'PassiveService2']
79 }
80
81
82 def validate_tables(tables):
83     """
84     Return whether all tables of the IDO database scheme exist in
85     the given table listing
86
87     """
88     missing = [n for n in TABLES if TABLE_PREFIX + n not in tables]
89     if missing:
90         print 'Some tables are missing in the IDO'
91         print 'Missing tables: ' + ', '.join(missing)
92         return False
93
94     print 'All tables were found in the IDO'
95     return True
96
97
98 def verify_host_config(config_data):
99     """
100     Return whether the example hosts exist in the given "hosts" table
101
102     """
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'
105         return True
106
107     print 'Some example hosts are missing in the IDO'
108     return False
109
110
111 def verify_service_config(config_data):
112     """
113     Return whether the example services exist in the given "services" table
114
115     """
116     for hostname, servicename in ((h, s) for h, ss in EXAMPLE_CONFIG.iteritems()
117                                          for s in ss):
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'
123             return False
124
125     print 'The service config stored in the IDO is correct'
126     return True
127
128
129 def check_last_host_status_update(check_info):
130     """
131     Return whether the example hosts are checked as scheduled
132
133     """
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,
138                                                        seconds=10):
139                 print 'The last status update of host "localhost"' \
140                       ' was more than {0} minutes ago'.format(CHECK_INTERVAL)
141                 return False
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'
146                 return False
147
148     print 'The updates of both example hosts are processed as configured'
149     return True
150
151
152 def check_last_service_status_update(check_info):
153     """
154     Return whether the example services are checked as scheduled
155
156     """
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,
161                                                        seconds=10):
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'],
165                                 CHECK_INTERVAL)
166                 return False
167
168     print 'The updates of all example services are processed as configured'
169     return True
170
171
172 def check_logentries(logentry_info):
173     """
174     Return whether the given logentry originates from host "localhost"
175     and refers to its very last hard status change
176
177     """
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'
184             return False
185     else:
186         print 'No logs found in the IDO for host "localhost"'
187         return False
188
189     print 'The last hard state of host "localhost" was properly logged'
190     return True
191