Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / controllers / plugins / status.py @ a2fa6a5b

History | View | Annotate | Download (4.43 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 3 colonnes au tableau des événements :
23
    -   la première colonne contient l'état d'acquittement de l'événement.
24
    -   la seconde colonne contient un lien permettant d'éditer certaines
25
        propriétés associées à l'événement corrélé.
26
    -   la dernière colonne permet de (dé)sélectionner l'événement pour
27
        effectuer un traitement par lot.
28
"""
29
import urllib
30
import tg
31
import tw.forms as twf
32
from pylons.i18n import lazy_ugettext as l_
33

    
34
from vigilo.models.tables import CorrEvent, StateName
35
from vigilo.models.functions import sql_escape_like
36
from vigiboard.controllers.plugins import VigiboardRequestPlugin, ITEMS
37

    
38
class PluginStatus(VigiboardRequestPlugin):
39
    """
40
    Ajoute des colonnes permettant de voir le statut d'acquittement
41
    d'un événement corrélé et de modifier certaines de ses propriétés.
42
    """
43

    
44
    def get_generated_columns_count(self):
45
        """
46
        Renvoie le nombre de colonnes que ce plugin ajoute.
47
        Ce plugin en ajoute 4, au lieu de 1 comme la plupart des plugins.
48
        """
49
        return 4
50

    
51
    def get_search_fields(self):
52
        options = [
53
            ('', l_('All alerts')),
54
            # On doit passer un type basestring pour les options.
55
            # Donc, on convertit les constantes (entiers) en type str.
56
            (str(CorrEvent.ACK_NONE),   l_('New alerts')),
57
            (str(CorrEvent.ACK_KNOWN),  l_('Alerts marked as Acknowledged')),
58
            (str(CorrEvent.ACK_CLOSED), l_('Alerts marked as Closed')),
59
        ]
60

    
61
        return [
62
            twf.TextField(
63
                'trouble_ticket',
64
                label_text=l_('Trouble Ticket'),
65
                validator=twf.validators.String(if_missing=None),
66
            ),
67
            twf.SingleSelectField(
68
                'ack',
69
                label_text=l_('Acknowledgement Status'),
70
                options=options,
71
                validator=twf.validators.OneOf(
72
                    dict(options).keys(),
73
                    if_invalid=None,
74
                    if_missing=None,
75
                ),
76
            ),
77
        ]
78

    
79
    def handle_search_fields(self, query, search, state, subqueries):
80
        if state != ITEMS:
81
            return
82

    
83
        if search.get('trouble_ticket'):
84
            tt = sql_escape_like(search['trouble_ticket'])
85
            query.add_filter(CorrEvent.trouble_ticket.ilike(tt))
86

    
87
        if search.get('ack'):
88
            try:
89
                query.add_filter(CorrEvent.ack == int(search['ack']))
90
            except (ValueError, TypeError):
91
                # On ignore silencieusement le critère de recherche erroné.
92
                pass
93

    
94
    def get_data(self, event):
95
        cause = event[0].cause
96
        ack = event[0].ack
97
        state = StateName.value_to_statename(cause.current_state)
98

    
99
        trouble_ticket_id = None
100
        trouble_ticket_link = None
101
        if event[0].trouble_ticket:
102
            trouble_ticket_id = event[0].trouble_ticket
103
            trouble_ticket_link = tg.config['vigiboard_links.tt'] % {
104
                'id': event[0].idcorrevent,
105
                'host': event[1] and urllib.quote(event[1], '') or event[1],
106
                'service': event[2] and urllib.quote(event[2], '') or event[2],
107
                'tt': trouble_ticket_id and \
108
                        urllib.quote(trouble_ticket_id, '') or \
109
                        trouble_ticket_id,
110
            }
111

    
112
        return {
113
            'trouble_ticket_link': trouble_ticket_link,
114
            'trouble_ticket_id': trouble_ticket_id,
115
            'state': state,
116
            'id': event[0].idcorrevent,
117
            'ack': ack,
118
        }