vigiboard / vigiboard / controllers / plugins / details.py @ dc005588
History | View | Annotate | Download (6.35 KB)
1 | 15b98053 | Francois POIROTTE | # -*- coding: utf-8 -*-
|
---|---|---|---|
2 | 4febadf0 | Francois POIROTTE | # vim:set expandtab tabstop=4 shiftwidth=4:
|
3 | a77de887 | Francois POIROTTE | ################################################################################
|
4 | #
|
||
5 | 3b537383 | Francois POIROTTE | # Copyright (C) 2007-2011 CS-SI
|
6 | a77de887 | Francois POIROTTE | #
|
7 | # This program is free software; you can redistribute it and/or modify
|
||
8 | # it under the terms of the GNU General Public License version 2 as
|
||
9 | # published by the Free Software Foundation.
|
||
10 | #
|
||
11 | # This program is distributed in the hope that it will be useful,
|
||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | # GNU General Public License for more details.
|
||
15 | #
|
||
16 | # You should have received a copy of the GNU General Public License
|
||
17 | # along with this program; if not, write to the Free Software
|
||
18 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||
19 | ################################################################################
|
||
20 | |||
21 | 15b98053 | Francois POIROTTE | """
|
22 | 4febadf0 | Francois POIROTTE | Un plugin pour VigiBoard qui ajoute une colonne avec les liens vers les
|
23 | entrées d'historiques liées à l'événement, ainsi que les liens vers les
|
||
24 | applications externes.
|
||
25 | 15b98053 | Francois POIROTTE | """
|
26 | |||
27 | import urllib |
||
28 | dc005588 | Francois POIROTTE | from tg import config, url, request |
29 | 03e5dc9e | Vincent QUEMENER | from sqlalchemy.sql.expression import null as expr_null, union_all |
30 | 03059edd | Francois POIROTTE | |
31 | dc005588 | Francois POIROTTE | from repoze.what.predicates import has_permission, in_group |
32 | from vigilo.turbogears.helpers import get_current_user |
||
33 | |||
34 | 03e5dc9e | Vincent QUEMENER | from vigilo.models.session import DBSession |
35 | dc005588 | Francois POIROTTE | from vigilo.models.tables import Event, CorrEvent, Host, LowLevelService, \ |
36 | StateName, Map, MapNode, MapNodeHost, MapGroup |
||
37 | from vigilo.models.tables.secondary_tables import MAP_GROUP_TABLE |
||
38 | 03e5dc9e | Vincent QUEMENER | |
39 | 94f31908 | Francois POIROTTE | from vigiboard.controllers.plugins import VigiboardRequestPlugin |
40 | 15b98053 | Francois POIROTTE | |
41 | 94f31908 | Francois POIROTTE | class PluginDetails(VigiboardRequestPlugin): |
42 | 4febadf0 | Francois POIROTTE | """
|
43 | Plugin qui ajoute des liens vers les historiques et les applications
|
||
44 | externes.
|
||
45 | """
|
||
46 | |||
47 | cf3c2494 | Vincent QUEMENER | def get_json_data(self, idcorrevent, *args, **kwargs): |
48 | 15b98053 | Francois POIROTTE | """
|
49 | Renvoie les éléments pour l'affichage de la fenêtre de dialogue
|
||
50 | 94f31908 | Francois POIROTTE | contenant des détails sur un événement corrélé.
|
51 | 15b98053 | Francois POIROTTE |
|
52 | 94f31908 | Francois POIROTTE | @param idcorrevent: identifiant de l'événement corrélé.
|
53 | @type idcorrevent: C{int}
|
||
54 | 15b98053 | Francois POIROTTE | """
|
55 | |||
56 | # Obtention de données sur l'événement et sur son historique
|
||
57 | 03e5dc9e | Vincent QUEMENER | host_query = DBSession.query( |
58 | Host.idhost.label("idsupitem"),
|
||
59 | dc005588 | Francois POIROTTE | Host.idhost.label("idhost"),
|
60 | 03e5dc9e | Vincent QUEMENER | Host.name.label("host"),
|
61 | expr_null().label("service"),
|
||
62 | 15b98053 | Francois POIROTTE | ) |
63 | 03e5dc9e | Vincent QUEMENER | lls_query = DBSession.query( |
64 | LowLevelService.idservice.label("idsupitem"),
|
||
65 | dc005588 | Francois POIROTTE | Host.idhost.label("idhost"),
|
66 | 03e5dc9e | Vincent QUEMENER | Host.name.label("host"),
|
67 | LowLevelService.servicename.label("service"),
|
||
68 | ).join( |
||
69 | (Host, Host.idhost == LowLevelService.idhost), |
||
70 | ) |
||
71 | supitems = union_all(lls_query, host_query, correlate=False).alias()
|
||
72 | event = DBSession.query( |
||
73 | CorrEvent.idcorrevent, |
||
74 | CorrEvent.idcause, |
||
75 | dc005588 | Francois POIROTTE | supitems.c.idhost, |
76 | 03e5dc9e | Vincent QUEMENER | supitems.c.host, |
77 | supitems.c.service, |
||
78 | Event.message, |
||
79 | Event.initial_state, |
||
80 | Event.current_state, |
||
81 | Event.peak_state |
||
82 | ).join( |
||
83 | (Event, Event.idevent == CorrEvent.idcause), |
||
84 | (supitems, supitems.c.idsupitem == Event.idsupitem), |
||
85 | ).filter(CorrEvent.idcorrevent == idcorrevent |
||
86 | ).first() |
||
87 | 15b98053 | Francois POIROTTE | |
88 | dc005588 | Francois POIROTTE | # On détermine les cartes auxquelles cet utilisateur a accès.
|
89 | user_maps = {} |
||
90 | max_maps = int(config['max_maps']) |
||
91 | is_manager = in_group('managers').is_met(request.environ)
|
||
92 | if max_maps != 0 and (is_manager or |
||
93 | has_permission('vigimap-access').is_met(request.environ)):
|
||
94 | items = DBSession.query( |
||
95 | Map.idmap, |
||
96 | Map.title, |
||
97 | ).distinct( |
||
98 | ).join( |
||
99 | (MAP_GROUP_TABLE, MAP_GROUP_TABLE.c.idmap == Map.idmap), |
||
100 | (MapGroup, MapGroup.idgroup == MAP_GROUP_TABLE.c.idgroup), |
||
101 | (MapNodeHost, MapNodeHost.idmap == Map.idmap), |
||
102 | ).order_by(Map.title.asc() |
||
103 | ).filter(MapNodeHost.idhost == event.idhost) |
||
104 | |||
105 | if not is_manager: |
||
106 | mapgroups = get_current_user().mapgroups(only_direct=True)
|
||
107 | # pylint: disable-msg=E1103
|
||
108 | items = items.filter(MapGroup.idgroup.in_(mapgroups)) |
||
109 | |||
110 | # La valeur -1 supprime la limite.
|
||
111 | if max_maps > 0: |
||
112 | # On limite au nombre maximum de cartes demandés + 1.
|
||
113 | # Un message sera affiché s'il y a effectivement plus
|
||
114 | # de cartes que la limite configurée.
|
||
115 | items = items.limit(max_maps + 1)
|
||
116 | |||
117 | user_maps = dict([(m.idmap, m.title) for m in items.all()]) |
||
118 | |||
119 | 147b6daa | Francois POIROTTE | context = { |
120 | 'idcorrevent': idcorrevent,
|
||
121 | 'host': event.host,
|
||
122 | 'service': event.service,
|
||
123 | 'message': event.message,
|
||
124 | dc005588 | Francois POIROTTE | 'maps': user_maps,
|
125 | 147b6daa | Francois POIROTTE | } |
126 | |||
127 | 15b98053 | Francois POIROTTE | eventdetails = {} |
128 | 65383903 | Francois POIROTTE | for edname, edlink in enumerate(config['vigiboard_links.eventdetails']): |
129 | 147b6daa | Francois POIROTTE | # Évite que les gardes ne se polluent entre elles.
|
130 | local_ctx = context.copy() |
||
131 | 15b98053 | Francois POIROTTE | |
132 | 147b6daa | Francois POIROTTE | # Les liens peuvent être conditionnés à l'aide
|
133 | # d'une expression ou d'un callable qui agira
|
||
134 | # comme un prédicat de test.
|
||
135 | if 'only_if' in edlink: |
||
136 | if callable(edlink['only_if']): |
||
137 | display_link = edlink['only_if'](local_ctx)
|
||
138 | else:
|
||
139 | display_link = edlink['only_if']
|
||
140 | if not display_link: |
||
141 | continue
|
||
142 | bcf87133 | Francois POIROTTE | |
143 | 147b6daa | Francois POIROTTE | if callable(edlink['uri']): |
144 | uri = edlink['uri'](local_ctx)
|
||
145 | baf08f7e | Francois POIROTTE | else:
|
146 | 147b6daa | Francois POIROTTE | uri = edlink['uri'] % local_ctx
|
147 | baf08f7e | Francois POIROTTE | |
148 | eventdetails[unicode(edname)] = {
|
||
149 | 062a58e7 | Francois POIROTTE | 'url': url(uri),
|
150 | 147b6daa | Francois POIROTTE | 'target': edlink.get('target', '_blank') |
151 | 15b98053 | Francois POIROTTE | } |
152 | |||
153 | return dict( |
||
154 | current_state = StateName.value_to_statename( |
||
155 | 03e5dc9e | Vincent QUEMENER | event.current_state), |
156 | 15b98053 | Francois POIROTTE | initial_state = StateName.value_to_statename( |
157 | 03e5dc9e | Vincent QUEMENER | event.initial_state), |
158 | 15b98053 | Francois POIROTTE | peak_state = StateName.value_to_statename( |
159 | 03e5dc9e | Vincent QUEMENER | event.peak_state), |
160 | 15b98053 | Francois POIROTTE | idcorrevent = idcorrevent, |
161 | 03e5dc9e | Vincent QUEMENER | host = event.host, |
162 | service = event.service, |
||
163 | 15b98053 | Francois POIROTTE | eventdetails = eventdetails, |
164 | dc005588 | Francois POIROTTE | maps = user_maps, |
165 | 03e5dc9e | Vincent QUEMENER | idcause = event.idcause, |
166 | 15b98053 | Francois POIROTTE | ) |