Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.56 KB)

1
# -*- coding: utf-8 -*-
2
# vim:set expandtab tabstop=4 shiftwidth=4:
3
################################################################################
4
#
5
# Copyright (C) 2007-2012 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
from tg import config, url
28
from sqlalchemy.sql.expression import null as expr_null, union_all
29

    
30
from vigilo.models.session import DBSession
31
from vigilo.models.tables import Event, \
32
    CorrEvent, Host, LowLevelService, StateName
33

    
34
from vigiboard.controllers.plugins import VigiboardRequestPlugin
35

    
36
class PluginDetails(VigiboardRequestPlugin):
37
    """
38
    Plugin qui ajoute des liens vers les historiques et les applications
39
    externes.
40
    """
41

    
42
    def get_json_data(self, idcorrevent, *args, **kwargs):
43
        """
44
        Renvoie les éléments pour l'affichage de la fenêtre de dialogue
45
        contenant des détails sur un événement corrélé.
46

47
        @param idcorrevent: identifiant de l'événement corrélé.
48
        @type idcorrevent: C{int}
49
        """
50

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

    
80
        context = {
81
            'idcorrevent': idcorrevent,
82
            'host': event.host,
83
            'service': event.service,
84
            'message': event.message,
85
        }
86

    
87
        eventdetails = {}
88
        for edname, edlink in enumerate(config['vigiboard_links.eventdetails']):
89
            # Évite que les gardes ne se polluent entre elles.
90
            local_ctx = context.copy()
91

    
92
            # Les liens peuvent être conditionnés à l'aide
93
            # d'une expression ou d'un callable qui agira
94
            # comme un prédicat de test.
95
            if 'only_if' in edlink:
96
                if callable(edlink['only_if']):
97
                    display_link = edlink['only_if'](local_ctx)
98
                else:
99
                    display_link = edlink['only_if']
100
                if not display_link:
101
                    continue
102

    
103
            if callable(edlink['uri']):
104
                uri = edlink['uri'](local_ctx)
105
            else:
106
                uri = edlink['uri'] % local_ctx
107

    
108
            eventdetails[unicode(edname)] = {
109
                'url': url(uri),
110
                'target': edlink.get('target', '_blank')
111
            }
112

    
113
        return dict(
114
                current_state = StateName.value_to_statename(
115
                                    event.current_state),
116
                initial_state = StateName.value_to_statename(
117
                                    event.initial_state),
118
                peak_state = StateName.value_to_statename(
119
                                    event.peak_state),
120
                idcorrevent = idcorrevent,
121
                host = event.host,
122
                service = event.service,
123
                eventdetails = eventdetails,
124
                idcause = event.idcause,
125
            )