Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.25 KB)

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

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

    
35
from vigiboard.controllers.plugins import VigiboardRequestPlugin
36

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

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

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

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

    
81
        eventdetails = {}
82
        for edname, edlink in enumerate(config['vigiboard_links.eventdetails']):
83

    
84
            if event.service:
85
                service = urllib.quote(event.service)
86
            else:
87
                service = None
88

    
89
            try:
90
                target = edlink[2]
91
            except IndexError:
92
                target = '_blank'
93
            else:
94
                if not target:
95
                    target = '_blank'
96

    
97
            eventdetails[unicode(edname)] = {
98
                'url': url(edlink[1]) % {
99
                    'idcorrevent': idcorrevent,
100
                    'host': urllib.quote(event.host),
101
                    'service': service,
102
                    'message': urllib.quote(event.message.encode('utf-8')),
103
                },
104
                'target': target
105
            }
106

    
107
        return dict(
108
                current_state = StateName.value_to_statename(
109
                                    event.current_state),
110
                initial_state = StateName.value_to_statename(
111
                                    event.initial_state),
112
                peak_state = StateName.value_to_statename(
113
                                    event.peak_state),
114
                idcorrevent = idcorrevent,
115
                host = event.host,
116
                service = event.service,
117
                eventdetails = eventdetails,
118
                idcause = event.idcause,
119
            )