Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / plugins / details.py @ 25892058

History | View | Annotate | Download (6.81 KB)

1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4:
3
################################################################################
4
#
5
# Copyright (C) 2007-2013 CS-SI
6
#
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
"""
22
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
"""
26

    
27
import urllib
28
from tg import config, url, request
29
from sqlalchemy.sql.expression import null as expr_null, union_all
30
from sqlalchemy import func
31

    
32
from repoze.what.predicates import has_permission, in_group
33
from vigilo.turbogears.helpers import get_current_user
34

    
35
from vigilo.models.session import DBSession
36
from vigilo.models.tables import Event, CorrEvent, Host, LowLevelService, \
37
    StateName, Map, MapNode, MapNodeHost, MapGroup
38
from vigilo.models.tables.secondary_tables import MAP_GROUP_TABLE
39

    
40
from vigiboard.controllers.plugins import VigiboardRequestPlugin
41

    
42
class PluginDetails(VigiboardRequestPlugin):
43
    """
44
    Plugin qui ajoute des liens vers les historiques et les applications
45
    externes.
46
    """
47

    
48
    def get_json_data(self, idcorrevent, *args, **kwargs):
49
        """
50
        Renvoie les éléments pour l'affichage de la fenêtre de dialogue
51
        contenant des détails sur un événement corrélé.
52

53
        @param idcorrevent: identifiant de l'événement corrélé.
54
        @type idcorrevent: C{int}
55
        """
56

    
57
        # Obtention de données sur l'événement et sur son historique
58
        host_query = DBSession.query(
59
            Host.idhost.label("idsupitem"),
60
            Host.idhost.label("idhost"),
61
            Host.name.label("host"),
62
            expr_null().label("service"),
63
        )
64
        lls_query = DBSession.query(
65
            LowLevelService.idservice.label("idsupitem"),
66
            Host.idhost.label("idhost"),
67
            Host.name.label("host"),
68
            LowLevelService.servicename.label("service"),
69
        ).join(
70
            (Host, Host.idhost == LowLevelService.idhost),
71
        )
72
        supitems = union_all(lls_query, host_query, correlate=False).alias()
73
        event = DBSession.query(
74
            CorrEvent.idcorrevent,
75
            CorrEvent.idcause,
76
            supitems.c.idhost,
77
            supitems.c.host,
78
            supitems.c.service,
79
            Event.message,
80
            Event.initial_state,
81
            Event.current_state,
82
            Event.peak_state
83
        ).join(
84
            (Event, Event.idevent == CorrEvent.idcause),
85
            (supitems, supitems.c.idsupitem == Event.idsupitem),
86
        ).filter(CorrEvent.idcorrevent == idcorrevent
87
        ).first()
88

    
89
        # On détermine les cartes auxquelles cet utilisateur a accès.
90
        user_maps = []
91
        max_maps = int(config['max_maps'])
92
        is_manager = in_group('managers').is_met(request.environ)
93
        if max_maps != 0 and (is_manager or
94
            has_permission('vigimap-access').is_met(request.environ)):
95
            items = DBSession.query(
96
                    Map.idmap,
97
                    Map.title,
98
                ).distinct(
99
                ).join(
100
                    (MAP_GROUP_TABLE, MAP_GROUP_TABLE.c.idmap == Map.idmap),
101
                    (MapGroup, MapGroup.idgroup == MAP_GROUP_TABLE.c.idgroup),
102
                    (MapNodeHost, MapNodeHost.idmap == Map.idmap),
103
                ).order_by(func.lower(Map.title).asc()
104
                ).filter(MapNodeHost.idhost == event.idhost)
105

    
106
            if not is_manager:
107
                mapgroups = get_current_user().mapgroups(only_direct=True)
108
                # pylint: disable-msg=E1103
109
                items = items.filter(MapGroup.idgroup.in_(mapgroups))
110

    
111
            # La valeur -1 supprime la limite.
112
            if max_maps > 0:
113
                # On limite au nombre maximum de cartes demandés + 1.
114
                # Un message sera affiché s'il y a effectivement plus
115
                # de cartes que la limite configurée.
116
                items = items.limit(max_maps + 1)
117

    
118
            user_maps = [(m.idmap, m.title) for m in items.all()]
119

    
120
        context = {
121
            'idcorrevent': idcorrevent,
122
            'host': event.host,
123
            'service': event.service,
124
            'message': event.message,
125
            'maps': user_maps,
126
        }
127

    
128
        eventdetails = {}
129
        for edname, edlink in enumerate(config['vigiboard_links.eventdetails']):
130
            # Évite que les gardes ne se polluent entre elles.
131
            local_ctx = context.copy()
132

    
133
            # Les liens peuvent être conditionnés à l'aide
134
            # d'une expression ou d'un callable qui agira
135
            # comme un prédicat de test.
136
            if 'only_if' in edlink:
137
                if callable(edlink['only_if']):
138
                    display_link = edlink['only_if'](local_ctx)
139
                else:
140
                    display_link = edlink['only_if']
141
                if not display_link:
142
                    continue
143

    
144
            if callable(edlink['uri']):
145
                uri = edlink['uri'](local_ctx)
146
            else:
147
                uri = edlink['uri'] % local_ctx
148

    
149
            eventdetails[unicode(edname)] = {
150
                'url': url(uri),
151
                'target': edlink.get('target', '_blank')
152
            }
153

    
154
        return dict(
155
                current_state = StateName.value_to_statename(
156
                                    event.current_state),
157
                initial_state = StateName.value_to_statename(
158
                                    event.initial_state),
159
                peak_state = StateName.value_to_statename(
160
                                    event.peak_state),
161
                idcorrevent = idcorrevent,
162
                host = event.host,
163
                service = event.service,
164
                eventdetails = eventdetails,
165
                maps = user_maps,
166
                idcause = event.idcause,
167
            )
168

    
169
    def get_data(self, event):
170
        state = StateName.value_to_statename(event[0].cause.current_state)
171
        peak_state = StateName.value_to_statename(event[0].cause.peak_state)
172
        init_state = StateName.value_to_statename(event[0].cause.initial_state)
173
        return {
174
            'state': state,
175
            'peak_state': peak_state,
176
            'initial_state': init_state,
177
            'id': event[0].idcorrevent,
178
        }