-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathdatamanager.py
1139 lines (964 loc) · 47.8 KB
/
datamanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2009-2014:
# Gabes Jean, [email protected]
# Gerhard Lausser, [email protected]
# Gregory Starck, [email protected]
# Hartmut Goebel, [email protected]
# Frederic Mohier, [email protected]
# Guillaume Subiron, [email protected]
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
import re
import itertools
import time
from shinken.log import logger
from shinken.misc.datamanager import DataManager
# Import all objects we will need
from shinken.objects.host import Host, Hosts
from shinken.objects.hostgroup import Hostgroup, Hostgroups
from shinken.objects.service import Service, Services
from shinken.objects.servicegroup import Servicegroup, Servicegroups
from shinken.objects.contact import Contact, Contacts
from shinken.objects.contactgroup import Contactgroup, Contactgroups
from shinken.objects.notificationway import NotificationWay, NotificationWays
from shinken.objects.timeperiod import Timeperiod, Timeperiods
from shinken.objects.command import Command, Commands
from shinken.misc.sorter import worse_first, last_state_change_earlier
# Search patterns like: isnot:0 isnot:ack isnot:"downtime fred" name "vm fred"
SEARCH_QUERY_PATTERNS = r'''
# 1/ Search a key:value pattern.
(?P<key>\w+): # Key consists of only a word followed by a colon
(?P<quote2>["']?) # Optional quote character.
(?P<value>.*?) # Value is a non greedy match
(?P=quote2) # Closing quote equals the first.
($|\s) # Entry ends with whitespace or end of string
| # OR
# 2/ Search a single string quoted or not
(?P<quote>["']?) # Optional quote character.
(?P<name>.*?) # Name is a non greedy match
(?P=quote) # Closing quote equals the opening one.
($|\s) # Entry ends with whitespace or end of string
'''
class WebUIDataManager(DataManager):
def __init__(self, rg=None, problems_business_impact=0, disable_inner_problems_computation=0):
super(WebUIDataManager, self).__init__()
self.rg = rg
self.problems_business_impact = problems_business_impact
self.disable_inner_problems_computation = disable_inner_problems_computation
@property
def is_initialized(self):
return len(self.get_contacts()) > 0
@staticmethod
def _is_related_to(item, user):
"""
"""
# if no user or user is an admin, always consider there is a relation
if not user or user.is_administrator():
return item
logger.debug("[WebUI - relation], DM _is_related_to: %s", item.__class__)
return user._is_related_to(item)
@staticmethod
def _only_related_to(items, user):
""" This function is just a wrapper to _is_related_to for a list.
:returns: List of elements related to the user
"""
# if no user or user is an admin, always consider there is a relation
if not user or user.is_administrator():
return items
try:
logger.debug("[WebUI - relation], DM _only_related_to: %s", items)
return [item for item in items if user._is_related_to(item)]
except TypeError:
return items if user._is_related_to(items) else None
##
# Hosts
##
def get_hosts(self, user=None, get_impacts=False, extra_search=''):
""" Get a list of all hosts.
Default search is to get all hosts. extra_search allows to add some more
search criteria
:param user: concerned user
:param get_impacts: should impact hosts be included in the list?
:param extra_search: extra search criteria
:returns: list of all hosts
"""
search = 'type:host'
if get_impacts:
search = search + ' is:impact'
if "bi:" not in search:
search = search + " bi:>=%d " % (self.problems_business_impact)
if extra_search:
search = search + " " + extra_search
return self.search_hosts_and_services(search, user)
def get_host(self, name, user=None):
""" Get a host by its hostname. """
hosts = self.search_hosts_and_services('type:host host:^%s$' % (name), user=user)
return hosts[0] if hosts else None
def get_host_services(self, hname, user):
""" Get host services by its hostname. """
return self.search_hosts_and_services('type:service host:%s' % (hname), user=user)
def get_percentage_hosts_state(self, user=None, problem=False):
""" Get percentage of hosts not in (or in) problems.
:param problem: False to return the % of hosts not in problems,
True to return the % of hosts in problems.
False by default
"""
h = self.get_hosts_synthesis(None, user)
if not h['nb_elts']:
return 0
count = h['nb_elts'] - h['nb_problems']
if problem:
count = h['nb_problems']
logger.debug("Hosts count: %s / %s / %s", count, h['nb_problems'], h['nb_elts'])
return round(100.0 * (count / h['nb_elts']), 1)
def get_hosts_synthesis(self, elts=None, user=None, extra_search=''):
if elts is not None:
hosts = [item for item in elts if item.__class__.my_type == 'host']
else:
hosts = self.get_hosts(user=user, extra_search=extra_search)
logger.debug("[WebUI - datamanager] get_hosts_synthesis, %d hosts", len(hosts))
h = dict()
h['nb_elts'] = len(hosts)
if hosts:
h['bi'] = max(h.business_impact for h in hosts)
for state in 'up', 'pending':
h['nb_' + state] = sum(1 for host in hosts if host.state == state.upper())
h['pct_' + state] = round(100.0 * h['nb_' + state] / h['nb_elts'], 1)
for state in 'down', 'unreachable', 'unknown':
h['nb_' + state] = sum(1 for host in hosts if host.state == state.upper()
and not (host.problem_has_been_acknowledged or host.in_scheduled_downtime))
h['pct_' + state] = round(100.0 * h['nb_' + state] / h['nb_elts'], 1)
# Our own computation !
# ------
# Shinken/Alignak does not always reflect the "problem" state from a user point of view ...
# To make UI more consistent, build our own problems counter!
if not self.disable_inner_problems_computation:
h['nb_ack'] = 0
h['nb_problems'] = 0
h['nb_impacts'] = 0
for host in hosts:
if host.state_type.upper() not in ['HARD']:
continue
# An host is a problem if it is in a HARD DOWN or UNKNOWN state
if host.state.lower() in ['down', 'unnown']:
host.is_problem = True
# An host is impacted if it is UNREACHABLE
if host.state.lower() in ['unreachable']:
host.is_impact = True
if host.is_problem and host.problem_has_been_acknowledged:
h['nb_ack'] += 1
if host.is_problem and not host.problem_has_been_acknowledged:
h['nb_problems'] += 1
if host.is_impact:
h['nb_impacts'] += 1
else:
h['nb_problems'] = sum(1 for host in hosts if host.is_problem
and not host.problem_has_been_acknowledged)
h['nb_impacts'] = sum(1 for host in hosts if host.is_problem
and not host.problem_has_been_acknowledged and host.is_impact)
h['nb_ack'] = sum(1 for host in hosts if host.is_problem and host.problem_has_been_acknowledged)
h['pct_problems'] = round(100.0 * h['nb_problems'] / h['nb_elts'], 1)
h['pct_ack'] = round(100.0 * h['nb_ack'] / h['nb_elts'], 1)
h['nb_downtime'] = sum(1 for host in hosts if host.in_scheduled_downtime)
h['pct_downtime'] = round(100.0 * h['nb_downtime'] / h['nb_elts'], 1)
else:
h['bi'] = 0
for state in 'up', 'down', 'unreachable', 'pending', 'unknown', 'ack', 'downtime', 'problems':
h['nb_' + state] = 0
h['pct_' + state] = 0
logger.debug("[WebUI - datamanager] get_hosts_synthesis: %s", h)
return h
##
# Services
##
def get_services(self, user, get_impacts=False, extra_search=''):
""" Get a list of all services.
:param user: concerned user
:param get_impacts: should impact services be included in the list?
:returns: list of all services
"""
search = 'type:service'
if get_impacts:
search = search + ' is:impact'
if "bi:" not in search:
search = search + " bi:>=%d " % (self.problems_business_impact)
if extra_search:
search = search + " " + extra_search
return self.search_hosts_and_services(search, user)
def get_service(self, hname, sname, user):
""" Get a service by its hostname and service description. """
services = self.search_hosts_and_services('type:service host:^%s$ service:"^%s$"' % (hname, sname), user=user)
return services[0] if services else None
def get_percentage_service_state(self, user=None, problem=False):
""" Get percentage of services not in (or in) problems.
:param problem: False to return the % of services not in problems,
True to return the % of services in problems.
False by default
"""
s = self.get_services_synthesis(None, user)
if not s['nb_elts']:
return 0
# Services not in problem
count = s['nb_elts'] - s['nb_problems']
if problem:
count = s['nb_problems']
logger.debug("Services count: %s / %s / %s", count, s['nb_problems'], s['nb_elts'])
return round(100.0 * (count / s['nb_elts']), 1)
def get_services_synthesis(self, elts=None, user=None, extra_search=''):
if elts is not None:
services = [item for item in elts if item.__class__.my_type == 'service']
else:
services = self.get_services(user=user, extra_search=extra_search)
logger.debug("[WebUI - datamanager] get_services_synthesis, %d services", len(services))
s = dict()
s['nb_elts'] = len(services)
if services:
s['bi'] = max(s.business_impact for s in services)
for state in 'ok', 'pending':
s['nb_' + state] = sum(1 for service in services if service.state == state.upper())
s['pct_' + state] = round(100.0 * s['nb_' + state] / s['nb_elts'], 1)
for state in 'warning', 'critical', 'unreachable', 'unknown':
s['nb_' + state] = sum(1 for service in services if service.state == state.upper()
and not (service.problem_has_been_acknowledged or service.in_scheduled_downtime))
s['pct_' + state] = round(100.0 * s['nb_' + state] / s['nb_elts'], 1)
s['nb_impacts'] = 0
# Our own computation !
# ------
# Shinken/Alignak does not always reflect the "problem" state from a user point of view ...
# To make UI more consistent, build our own problems counter!
if not self.disable_inner_problems_computation:
s['nb_ack'] = 0
s['nb_problems'] = 0
for service in services:
if service.state_type.upper() not in ['HARD']:
continue
# A service is a problem if it is in a HARD WARNING, CRITICAL or UNKNOWN state
if service.state.lower() in ['warning', 'critical', 'unknown']:
service.is_problem = True
# A service is impacted if its host is not UP
if service.host.state not in ['up']:
service.is_impact = True
# A service is impacted if it is UNREACHABLE
if service.state.lower() in ['unreachable']:
service.is_impact = True
if service.is_problem and service.problem_has_been_acknowledged:
s['nb_ack'] += 1
if service.is_problem and not service.problem_has_been_acknowledged:
s['nb_problems'] += 1
if service.is_impact:
s['nb_impacts'] += 1
else:
s['nb_problems'] = sum(1 for service in services if service.is_problem
and not service.problem_has_been_acknowledged)
s['nb_impacts'] = sum(1 for service in services if service.is_problem
and not service.problem_has_been_acknowledged and service.is_impact)
s['nb_ack'] = sum(1 for service in services if service.is_problem
and service.problem_has_been_acknowledged)
s['pct_problems'] = round(100.0 * s['nb_problems'] / s['nb_elts'], 1)
s['pct_ack'] = round(100.0 * s['nb_ack'] / s['nb_elts'], 1)
s['nb_downtime'] = sum(1 for service in services if service.in_scheduled_downtime)
s['pct_downtime'] = round(100.0 * s['nb_downtime'] / s['nb_elts'], 1)
else:
s['bi'] = 0
for state in ['ok', 'warning', 'critical',
'pending', 'unreachable', 'unknown',
'ack', 'downtime', 'problems']:
s['nb_' + state] = 0
s['pct_' + state] = 0
logger.debug("[WebUI - datamanager] get_services_synthesis: %s", s)
return s
##
# Elements
##
def get_element(self, name, user):
""" Get an element by its name.
:name: Must be "host" or "host/service"
"""
if '/' in name:
return self.get_service(name.split('/')[0], '/'.join(name.split('/')[1:]), user)
host = self.get_host(name, user)
if not host:
return self.get_contact(name=name, user=user)
return host
##
# Searching
##
def search_hosts_and_services(self, search, user, get_impacts=True, sorter=None, important=False):
""" Search hosts and services.
This method is the heart of the datamanager. All other methods should be based on this one.
If important is True, only the most important items are filtered using the important
problems business impact (important_problems_business_impact) rather than the
default business impact (problems_business_impact).
Todo: the get_impacts parameter is not used into this function, should be removed!
:search: Search string
:user: concerned user
:get_impacts: should impacts be included in the list?
:sorter: function to sort the items. default=None (means no sorting)
:important: only get the most important items
:returns: list of hosts and services
"""
# Make user an User object ... simple protection.
# pylint: disable=undefined-variable
# Because unicode...
if isinstance(user, (unicode, str)):
user = self.rg.contacts.find_by_name(user)
items = []
items.extend(self._only_related_to(super(WebUIDataManager, self).get_hosts(), user))
items.extend(self._only_related_to(super(WebUIDataManager, self).get_services(), user))
logger.debug("[WebUI - datamanager] search_hosts_and_services, search for %s in %d items", search, len(items))
# Search patterns like: isnot:0 isnot:ack isnot:"downtime fred" name "vm fred"
regex = re.compile(SEARCH_QUERY_PATTERNS, re.VERBOSE)
# Replace "NOT foo" by "^((?!foo).)*$" to ignore foo
search = re.sub(r'NOT ([^\ ]*)', r'^((?!\1).)*$', search)
search = re.sub(r'not ([^\ ]*)', r'^((?!\1).)*$', search)
patterns = []
for match in regex.finditer(search):
if match.group('name'):
patterns.append(('name', match.group('name')))
elif match.group('key'):
patterns.append((match.group('key'), match.group('value')))
logger.debug("[WebUI - datamanager] search patterns: %s", patterns)
for t, s in patterns:
t = t.lower()
logger.debug("[WebUI - datamanager] searching for %s %s", t, s)
if t == 'name':
# Case insensitive
pat = re.compile(s, re.IGNORECASE)
new_items = []
# Ordered search in all the items for: displa_name, alias, name and notes
for i in items:
if (pat.search(getattr(i, 'display_name', '')) or pat.search(getattr(i, 'alias', ''))
or pat.search(i.get_full_name()) or pat.search(getattr(i, 'notes', ''))):
new_items.append(i)
# Nothing found in all the items
if not new_items:
for i in items:
# Search in the last check output
if pat.search(i.output):
new_items.append(i)
if not new_items:
# Nothing found in the checks output
for i in items:
# Search in the impacts and source problems last check output
for j in i.impacts + i.source_problems:
if pat.search(j.output):
new_items.append(i)
items = new_items
if (t in ['h', 'host']) and s.lower() != 'all':
logger.debug("[WebUI - datamanager] searching for an host %s", s)
# Case sensitive
pat = re.compile(s)
new_items = []
for i in items:
if i.__class__.my_type == 'host' and pat.search(i.get_name()):
new_items.append(i)
if i.__class__.my_type == 'service' and pat.search(i.host_name):
new_items.append(i)
items = new_items
logger.debug("[WebUI - datamanager] host:%s, %d matching items", s, len(items))
for item in items:
logger.debug("[WebUI - datamanager] item %s is %s", item.get_name(), item.__class__)
if (t in ['s', 'service']) and s.lower() != 'all':
logger.debug("[WebUI - datamanager] searching for a service %s", s)
pat = re.compile(s)
new_items = []
for i in items:
if i.__class__.my_type == 'service' and pat.search(i.get_name()):
new_items.append(i)
items = new_items
logger.debug("[WebUI - datamanager] service:%s, %d matching items", s, len(items))
for item in items:
logger.debug("[WebUI - datamanager] item %s is %s", item.get_name(), item.__class__)
if (t in ['c', 'contact']) and s.lower() != 'all':
logger.debug("[WebUI - datamanager] searching for a contact %s", s)
pat = re.compile(s)
new_items = []
for i in items:
if i.__class__.my_type == 'contact' and pat.search(i.get_name()):
new_items.append(i)
if i.__class__.my_type == 'host':
# :TODO:maethor:171012:
pass
if i.__class__.my_type == 'service':
# :TODO:maethor:171012:
pass
items = new_items
if (t in ['hg', 'hgroup', 'hostgroup']) and s.lower() != 'all':
logger.debug("[WebUI - datamanager] searching for items in the hostgroup %s", s)
group = self.get_hostgroup(s)
if group:
logger.debug("[WebUI - datamanager] found the group: %s", group.get_name())
# This filters items that are related with the hostgroup only
# if the item has an hostgroups property
items = [i for i in items if getattr(i, 'get_hostgroups') and
group.get_name() in [g.get_name() for g in i.get_hostgroups()]]
if (t in ['sg', 'sgroup', 'servicegroup']) and s.lower() != 'all':
logger.debug("[WebUI - datamanager] searching for items in the servicegroup %s", s)
group = self.get_servicegroup(s)
if group:
logger.debug("[WebUI - datamanager] found the group: %s", group.get_name())
# Only the items that have a servicegroups property
items = [i for i in items if getattr(i, 'servicegroups') and
group.get_name() in [g.get_name() for g in i.servicegroups]]
if (t in ['cg', 'cgroup', 'contactgroup']) and s.lower() != 'all':
logger.debug("[WebUI - datamanager] searching for items related with the contactgroup %s", s)
group = self.get_contactgroup(s, user)
if group:
logger.debug("[WebUI - datamanager] found the group: %s", group.get_name())
contacts = [c for c in self.get_contacts(user=user) if c in group.members]
logger.info("[WebUI - datamanager] contacts: %s", contacts)
items = list(set(itertools.chain(*[self._only_related_to(items,
self.rg.contacts.find_by_name(c))
for c in contacts])))
if t == 'realm':
r = self.get_realm(s)
if not r:
return [] # :TODO:maethor:150716: raise an error
items = [i for i in items if i.get_realm() == r]
if t == 'htag' and s.lower() != 'all':
items = [i for i in items if getattr(i, 'get_host_tags') and s in i.get_host_tags()]
if t == 'stag' and s.lower() != 'all':
items = [i for i in items if getattr(i, 'get_service_tags') and s in i.get_service_tags()]
if t == 'ctag' and s.lower() != 'all':
contacts = [c for c in self.get_contacts(user=user) if s in c.tags]
items = list(set(itertools.chain(*[self._only_related_to(items, c) for c in contacts])))
if t == 'type' and s.lower() != 'all':
items = [i for i in items if i.__class__.my_type == s]
logger.debug("[WebUI - datamanager] type:%s, %d matching items", s, len(items))
for item in items:
logger.debug("[WebUI - datamanager] item %s is %s", item.get_name(), item.__class__)
if t in ['bp', 'bi']:
try:
if s.startswith('>='):
items = [i for i in items if i.business_impact >= int(s[2:])]
elif s.startswith('<='):
items = [i for i in items if i.business_impact <= int(s[2:])]
elif s.startswith('>'):
items = [i for i in items if i.business_impact > int(s[1:])]
elif s.startswith('<'):
items = [i for i in items if i.business_impact < int(s[1:])]
else:
if s.startswith('='):
s = s[1:]
items = [i for i in items if i.business_impact == int(s)]
except ValueError:
items = []
if t in ['duration', 'last_check']:
seconds_per_unit = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}
if t == 'duration':
times = [(i, time.time() - int(i.last_state_change)) for i in items]
else:
times = [(i, time.time() - int(i.last_chk)) for i in items]
try:
if s.startswith('>='):
s = int(s[2:-1]) * seconds_per_unit[s[-1].lower()]
items = [i[0] for i in times if i[1] >= s]
elif s.startswith('<='):
s = int(s[2:-1]) * seconds_per_unit[s[-1].lower()]
items = [i[0] for i in times if i[1] <= s]
elif s.startswith('>'):
s = int(s[1:-1]) * seconds_per_unit[s[-1].lower()]
items = [i[0] for i in times if i[1] > s]
elif s.startswith('<'):
s = int(s[1:-1]) * seconds_per_unit[s[-1].lower()]
items = [i[0] for i in times if i[1] < s]
else:
items = []
except Exception:
items = []
if t == 'is':
if s.lower() == 'ack':
items = [i for i in items if i.__class__.my_type == 'service'
or i.problem_has_been_acknowledged]
items = [i for i in items if i.__class__.my_type == 'host'
or (i.problem_has_been_acknowledged or i.host.problem_has_been_acknowledged)]
elif s.lower() == 'downtime':
items = [i for i in items if i.__class__.my_type == 'service'
or i.in_scheduled_downtime]
items = [i for i in items if i.__class__.my_type == 'host'
or (i.in_scheduled_downtime or i.host.in_scheduled_downtime)]
elif s.lower() == 'impact':
items = [i for i in items if i.is_impact]
elif s.lower() == 'flapping':
items = [i for i in items if i.is_flapping]
elif s.lower() == 'soft':
items = [i for i in items if i.state_type != 'HARD']
elif s.lower() == 'hard':
items = [i for i in items if i.state_type == 'HARD']
else:
# Manage SOFT & HARD state
# :COMMENT:maethor:171006: Kept for retrocompatility
if s.startswith('s'):
s = s[1:]
if len(s) == 1:
items = [i for i in items if i.state_id == int(s) and i.state_type != 'HARD']
else:
items = [i for i in items if i.state == s.upper() and i.state_type != 'HARD']
elif s.startswith('h'):
s = s[1:]
if len(s) == 1:
items = [i for i in items if i.state_id != int(s) and i.state_type == 'HARD']
else:
items = [i for i in items if i.state != s.upper() and i.state_type == 'HARD']
else:
if len(s) == 1:
items = [i for i in items if i.state_id == int(s)]
else:
items = [i for i in items if i.state == s.upper()]
if t == 'isnot':
if s.lower() == 'ack':
items = [i for i in items if i.__class__.my_type == 'service'
or not i.problem_has_been_acknowledged]
items = [i for i in items if i.__class__.my_type == 'host'
or (not i.problem_has_been_acknowledged and not i.host.problem_has_been_acknowledged)]
elif s.lower() == 'downtime':
items = [i for i in items if i.__class__.my_type == 'service'
or not i.in_scheduled_downtime]
items = [i for i in items if i.__class__.my_type == 'host'
or (not i.in_scheduled_downtime and not i.host.in_scheduled_downtime)]
elif s.lower() == 'impact':
items = [i for i in items if not i.is_impact]
elif s.lower() == 'flapping':
items = [i for i in items if not i.is_flapping]
elif s.lower() == 'soft':
items = [i for i in items if not i.state_type != 'HARD']
elif s.lower() == 'hard':
items = [i for i in items if not i.state_type == 'HARD']
else:
# Manage soft & hard state
if s.startswith('s'):
s = s[1:]
if len(s) == 1:
items = [i for i in items if i.state_id != int(s) and i.state_type != 'HARD']
else:
items = [i for i in items if i.state != s.upper() and i.state_type != 'HARD']
elif s.startswith('h'):
s = s[1:]
if len(s) == 1:
items = [i for i in items if i.state_id != int(s) and i.state_type == 'HARD']
else:
items = [i for i in items if i.state != s.upper() and i.state_type == 'HARD']
else:
if len(s) == 1:
items = [i for i in items if i.state_id != int(s)]
else:
items = [i for i in items if i.state != s.upper()]
# :COMMENT:maethor:150616: Legacy filters, kept for bookmarks compatibility
if t == 'ack':
if s.lower() == 'false' or s.lower() == 'no':
patterns.append(("isnot", "ack"))
if s.lower() == 'true' or s.lower() == 'yes':
patterns.append(("is", "ack"))
if t == 'downtime':
if s.lower() == 'false' or s.lower() == 'no':
patterns.append(("isnot", "downtime"))
if s.lower() == 'true' or s.lower() == 'yes':
patterns.append(("is", "downtime"))
if t == 'crit':
patterns.append(("is", "critical"))
if sorter is not None:
items.sort(sorter)
logger.debug("[WebUI - datamanager] search_hosts_and_services, found %d matching items", len(items))
logger.debug("[WebUI - datamanager] ----------------------------------------")
for item in items:
logger.debug("[WebUI - datamanager] item %s is %s", item.get_name(), item.__class__)
logger.debug("[WebUI - datamanager] ----------------------------------------")
return items
##
# Timeperiods
##
def get_timeperiods(self, user=None, name=None):
""" Get a list of known time periods
:param user: concerned user
:param name: only this element
:returns: List of elements related to the user
"""
logger.debug("[WebUI - datamanager] get_timeperiods, name: %s, user: %s", name, user)
items = self.rg.timeperiods
logger.debug("[WebUI - datamanager] got %d timeperiods", len(items))
if name:
return items.find_by_name(name)
return self._only_related_to(items, user)
def get_timeperiod(self, name):
try:
name = name.decode('utf8', 'ignore')
except UnicodeEncodeError:
pass
return self.get_timeperiods(name=name)
##
# Commands
##
def get_commands(self, user=None, name=None):
""" Get a list of known commands
:param user: concerned user
:param name: only this element
:returns: List of elements related to the user
"""
logger.debug("[WebUI - datamanager] get_commands, name: %s, user: %s", name, user)
items = self.rg.commands
logger.debug("[WebUI - datamanager] got %d commands", len(items))
if name:
return items.find_by_name(name)
return self._only_related_to(items, user)
def get_command(self, name):
try:
name = name.decode('utf8', 'ignore')
except UnicodeEncodeError:
pass
return self.get_commands(name=name)
##
# Contacts
##
def get_contacts(self, user=None, name=None):
""" Get a list of known contacts
:param user: concerned user
:param name: only this element
:returns: List of elements related to the user
"""
logger.debug("[WebUI - datamanager] get_contacts, name: %s", name)
items = self.rg.contacts
logger.debug("[WebUI - datamanager] got %d contacts", len(items))
if name:
return items.find_by_name(name)
return self._only_related_to(items, user)
def get_contact(self, name=None, user=None):
try:
name = name.decode('utf8', 'ignore')
except UnicodeEncodeError:
pass
except AttributeError:
pass
logger.debug("[WebUI - datamanager] get_contact, name: %s, user: %s", name, user)
return self.get_contacts(user=user, name=name)
##
# Contacts groups
##
def set_contactgroups_level(self, user):
# All known contactgroups are level 0 groups ...
for group in self.get_contactgroups(user=user):
logger.debug("[WebUI - datamanager] set_contactgroups_level, group: %s", group)
if not hasattr(group, 'level'):
self.set_contactgroup_level(group, 0, user)
def set_contactgroup_level(self, group, level, user):
logger.debug("[WebUI - datamanager] set_contactgroup_level, group: %s, level: %d", group, level)
setattr(group, 'level', level)
for g in sorted(group.contactgroup_members):
if not g:
continue
logger.debug("[WebUI - datamanager] set_contactgroup_level, g: %s", g)
try:
child_group = self.get_contactgroup(g, user=user)
self.set_contactgroup_level(child_group, level + 1, user)
except AttributeError:
pass
def get_contactgroups(self, user=None, name=None, parent=None, members=False):
""" Get a list of known contacts groups
:param user: concerned user
:param name: only this element
:returns: List of elements related to the user
"""
logger.debug("[WebUI - datamanager] get_contactgroups, name: %s, members: %s", name, members)
items = []
if parent:
group = self.get_contactgroups(user=user, name=parent)
if group:
items = [self.get_contactgroup(g) for g in group.contactgroup_members]
else:
return items
else:
items = self.rg.contactgroups
logger.debug("[WebUI - datamanager] got %d contactgroups", len(items))
if name:
return items.find_by_name(name)
return self._only_related_to(items, user)
def get_contactgroup(self, name, user=None, members=False):
""" Get a specific contacts group
:param name: searched contacts group name
:param user: concerned user
:returns: group which name matches else None
"""
try:
name = name.decode('utf8', 'ignore')
except UnicodeEncodeError:
pass
logger.debug("[WebUI - datamanager] get_contactgroup, name: %s", name)
return self._only_related_to(self.get_contactgroups(user=user, name=name, members=members), user)
##
# Hosts groups
##
def set_hostgroups_level(self, user):
# All known hostgroups are level 0 groups ...
for group in self.get_hostgroups(user=user):
logger.debug("[WebUI - datamanager] set_hostgroups_level, group: %s", group)
if not hasattr(group, 'level'):
self.set_hostgroup_level(group, 0, user)
def set_hostgroup_level(self, group, level, user):
setattr(group, 'level', level)
logger.debug("[WebUI - datamanager] set_hostgroup_level, group: %s, level: %d", group, level)
for g in sorted(group.hostgroup_members, key=lambda g: g.hostgroup_name):
if not g:
continue
logger.debug("[WebUI - datamanager] set_hostgroup_level, g: %s", g.get_name())
try:
child_group = self.get_hostgroup(g.get_name(), user=user)
self.set_hostgroup_level(child_group, level + 1, user)
except AttributeError:
pass
def get_hostgroups(self, user=None, name=None, parent=None):
""" Get a list of known hosts groups
:param user: concerned user
:param name: only this element
:returns: List of elements related to the user
"""
logger.debug("[WebUI - datamanager] get_hostgroups, name: %s", name)
items = []
if parent:
group = self.get_hostgroups(user=user, name=parent)
if group:
items = [self.get_hostgroup(g.get_name()) for g in group.hostgroup_members]
else:
return items
else:
items = self.rg.hostgroups
logger.debug("[WebUI - datamanager] got %d hostgroups", len(items))
if name:
return items.find_by_name(name)
return self._only_related_to(items, user)
def get_hostgroup(self, name, user=None):
""" Get a specific hosts group
:param name: searched hosts group name
:param user: concerned user
:returns: group which name matches else None
"""
try:
name = name.decode('utf8', 'ignore')
except UnicodeEncodeError:
pass
# group = self.get_hostgroups(user=user, name=name)
return self._is_related_to(self.get_hostgroups(user=user, name=name), user)
##
# Services groups
##
def set_servicegroups_level(self, user):
# All known hostgroups are level 0 groups ...
for group in self.get_servicegroups(user=user):
if not hasattr(group, 'level'):
self.set_servicegroup_level(group, 0, user)
def set_servicegroup_level(self, group, level, user):
setattr(group, 'level', level)
for g in sorted(group.servicegroup_members):
try:
child_group = self.get_servicegroup(g)
self.set_servicegroup_level(child_group, level + 1, user)
except AttributeError:
pass
def get_servicegroups(self, user=None, name=None, parent=None, members=False):
""" Get a list of known services groups
:param user: concerned user
:param name: only this element
:param parent: only the sub groups of this group
:returns: List of elements related to the user
"""
logger.debug("[WebUI - datamanager] get_servicegroups, name: %s", user)
items = []
if parent:
group = self.get_servicegroups(user=user, name=parent)
if group:
items = [self.get_servicegroup(g) for g in group.servicegroup_members]
else:
return items
else:
items = self.rg.servicegroups
logger.debug("[WebUI - datamanager] got %d servicegroups", len(items))
if name:
return items.find_by_name(name)
return self._only_related_to(items, user)
def get_servicegroup(self, name, user=None, parent=None, members=False):
""" Get a specific hosts group
:param name: searched hosts group name
:param user: concerned user
:returns: group which name matches else None
"""
try:
name = name.decode('utf8', 'ignore')
except UnicodeEncodeError:
pass
return self._is_related_to(self.get_servicegroups(user=user, name=name, members=members), user)
##
# Hosts tags
##
def get_host_tags(self):
''' Get the hosts tags sorted by names. '''
logger.debug("[WebUI - datamanager] get_host_tags")
items = []
names = self.rg.tags.keys()
names.sort()
for name in names:
items.append((name, self.rg.tags[name]))
logger.debug("[WebUI - datamanager] got %d hosts tags", len(items))
return items
def get_hosts_tagged_with(self, tag, user):
''' Get the hosts tagged with a specific tag. '''
return self.search_hosts_and_services('type:host htag:%s' % tag, user)
##
# Services tags
##
def get_service_tags(self):
''' Get the services tags sorted by names. '''
items = []
names = self.rg.services_tags.keys()
names.sort()
for name in names:
items.append((name, self.rg.services_tags[name]))
logger.debug("[WebUI - datamanager] got %d services tags", len(items))
return items
def get_services_tagged_with(self, tag, user):
''' Get the services tagged with a specific tag. '''
return self.search_hosts_and_services('type:service stag:%s' % tag, user)
##
# Realms
##
def get_realms(self, user=None, name=None, parent=None):
return self._only_related_to(self.rg.realms, user)
def get_realm(self, name, user=None):
try:
name = name.decode('utf8', 'ignore')
except UnicodeEncodeError:
pass
return self._is_related_to(self.get_realms(user=user, name=name), user)
##
# Shinken program and daemons
##
def get_configs(self):
"""Return the scheduler configurations received during the initialisation phase"""