Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.43 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 a2744508 Aurelien BOMPARD
# Copyright (C) 2007-2012 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 3 colonnes au tableau des événements :
23 e181e86c Francois POIROTTE
    -   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 15b98053 Francois POIROTTE
"""
29 a2fa6a5b Francois POIROTTE
import urllib
30
import tg
31 27140946 Francois POIROTTE
import tw.forms as twf
32
from pylons.i18n import lazy_ugettext as l_
33
34 a2fa6a5b Francois POIROTTE
from vigilo.models.tables import CorrEvent, StateName
35 27140946 Francois POIROTTE
from vigilo.models.functions import sql_escape_like
36 86662bc9 Francois POIROTTE
from vigiboard.controllers.plugins import VigiboardRequestPlugin, ITEMS
37 15b98053 Francois POIROTTE
38
class PluginStatus(VigiboardRequestPlugin):
39 4febadf0 Francois POIROTTE
    """
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 07b62a01 Francois POIROTTE
    def get_generated_columns_count(self):
45 4febadf0 Francois POIROTTE
        """
46
        Renvoie le nombre de colonnes que ce plugin ajoute.
47 f744bc14 Francois POIROTTE
        Ce plugin en ajoute 4, au lieu de 1 comme la plupart des plugins.
48 4febadf0 Francois POIROTTE
        """
49 f744bc14 Francois POIROTTE
        return 4
50 27140946 Francois POIROTTE
51
    def get_search_fields(self):
52 f3eff455 Francois POIROTTE
        options = [
53
            ('', l_('All alerts')),
54 8ba2de75 Francois POIROTTE
            # 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 f3eff455 Francois POIROTTE
        ]
60
61 27140946 Francois POIROTTE
        return [
62
            twf.TextField(
63
                'trouble_ticket',
64
                label_text=l_('Trouble Ticket'),
65
                validator=twf.validators.String(if_missing=None),
66 f3eff455 Francois POIROTTE
            ),
67
            twf.SingleSelectField(
68 8ba2de75 Francois POIROTTE
                'ack',
69 f3eff455 Francois POIROTTE
                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 27140946 Francois POIROTTE
        ]
78
79 86662bc9 Francois POIROTTE
    def handle_search_fields(self, query, search, state, subqueries):
80
        if state != ITEMS:
81
            return
82
83 27140946 Francois POIROTTE
        if search.get('trouble_ticket'):
84
            tt = sql_escape_like(search['trouble_ticket'])
85
            query.add_filter(CorrEvent.trouble_ticket.ilike(tt))
86 f3eff455 Francois POIROTTE
87 8ba2de75 Francois POIROTTE
        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 a2fa6a5b Francois POIROTTE
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
        }