~ubuntu-branches/ubuntu/vivid/zentyal-core/vivid-proposed

« back to all changes in this revision

Viewing changes to src/EBox/Events/Model/LogWatcherConfiguration.pm

  • Committer: Package Import Robot
  • Author(s): Jorge Salamero Sanz
  • Date: 2012-08-28 10:03:33 UTC
  • Revision ID: package-import@ubuntu.com-20120828100333-oz3n5kpav4z0tl27
Tags: 2.3.21+quantal1
New upstream release for Quantal

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2012 eBox Technologies S.L.
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License, version 2, as
 
5
# published by the Free Software Foundation.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
 
 
16
# Class: EBox::Events::Model::LogWatcherConfiguration
 
17
#
 
18
# This class is the model to configurate Log watcher. It has as many
 
19
# rows as logger exist in eBox
 
20
#
 
21
# The fields are the following:
 
22
#
 
23
#    - name - the logger name (i18ned)
 
24
#    - filtering - model to configure as many filters as you may need
 
25
#    - enabled - enabled the event notification for that logger
 
26
#
 
27
 
 
28
package EBox::Events::Model::LogWatcherConfiguration;
 
29
use strict;
 
30
use warnings;
 
31
 
 
32
use base 'EBox::Model::DataTable';
 
33
 
 
34
use EBox::Exceptions::DataNotFound;
 
35
use EBox::Events::Model::LogFiltering;
 
36
use EBox::Gettext;
 
37
use EBox::Global;
 
38
use EBox::Model::Manager;
 
39
use EBox::Types::HasMany;
 
40
use EBox::Types::Text;
 
41
 
 
42
# Core modules
 
43
use Error qw(:try);
 
44
 
 
45
# Constants
 
46
use constant FILTERING_MODEL_NAME => 'LogWatcherFiltering';
 
47
 
 
48
# Group: Public methods
 
49
 
 
50
# Method: setUpModels
 
51
#
 
52
#     Set in model manager the dynamic models for log watcher
 
53
#     configuration per domain
 
54
#
 
55
#     The result of this method is to include in model manager the
 
56
#     dynamic models (one per log domain)
 
57
#
 
58
sub setUpModels
 
59
{
 
60
    my ($self) = @_;
 
61
 
 
62
    my $manager = EBox::Model::Manager->instance();
 
63
 
 
64
    my $readOnly = $self->parentModule()->isReadOnly();
 
65
    my $logs = EBox::Global->getInstance($readOnly)->modInstance('logs');
 
66
    my $logDomainTables = $logs->getAllTables(1);
 
67
    if (defined ( $logDomainTables)) {
 
68
        while (my ($domain, $tableInfo) = each %{$logDomainTables}) {
 
69
            next if ($domain eq 'events'); # avoid observe recursively itself!
 
70
            $manager->addModel($self->_createFilteringModel($domain, $tableInfo));
 
71
        }
 
72
    }
 
73
}
 
74
 
 
75
# Method: syncRows
 
76
#
 
77
# Overrides:
 
78
#
 
79
#        <EBox::Model::DataTable::syncRows>
 
80
#
 
81
#   It is overriden because this table is kind of different in
 
82
#   comparation to the normal use of generic data tables.
 
83
#
 
84
#   - The user does not add rows. When we detect the table is
 
85
#   empty we populate the table with the available log domains.
 
86
#
 
87
#   - We check if we have to add/remove one the log domains. That happens
 
88
#   when a new module is installed or an existing one is removed.
 
89
#
 
90
sub syncRows
 
91
{
 
92
    my ($self, $currentIds) = @_;
 
93
 
 
94
    my $anyChange = undef;
 
95
    my $logs = EBox::Global->modInstance('logs');
 
96
 
 
97
    # Set up every dynamic model
 
98
    $self->setUpModels();
 
99
 
 
100
    # Fetch the current log domains stored in conf
 
101
    my %storedLogDomains;
 
102
    foreach my $id (@{$currentIds}) {
 
103
        my $row = $self->row($id);
 
104
        $storedLogDomains{$row->valueByName('domain')} = 1;
 
105
    }
 
106
 
 
107
    # Fetch the current available log domains
 
108
    my %currentLogDomains;
 
109
    my $currentTables = $logs->getAllTables(1);
 
110
    foreach my $table (keys (%{$currentTables})) {
 
111
        next if ($table eq 'events'); # ignore events table
 
112
        $currentLogDomains{$table} = 1;
 
113
    }
 
114
 
 
115
    # Add new domains to conf
 
116
    foreach my $domain (keys %currentLogDomains) {
 
117
        next if (exists $storedLogDomains{$domain});
 
118
        $self->addRow('domain' => $domain, 'enabled' => 0);
 
119
        $anyChange = 1;
 
120
    }
 
121
 
 
122
    # Remove non-existing domains from conf
 
123
    foreach my $id (@{$currentIds}) {
 
124
        my $row = $self->row($id);
 
125
        my $domain = $row->valueByName('domain');
 
126
        next if (exists $currentLogDomains{$domain});
 
127
        $self->removeRow($id);
 
128
        $self->_removeFilteringModel($domain);
 
129
        $anyChange = 1;
 
130
    }
 
131
 
 
132
    return $anyChange;
 
133
}
 
134
 
 
135
# Method: updatedRowNotify
 
136
#
 
137
# Overrides:
 
138
#
 
139
#   <EBox::Model::DataTable::updatedRowNotify>
 
140
#
 
141
sub updatedRowNotify
 
142
{
 
143
    my ($self, $row, $oldRow, $force) = @_;
 
144
 
 
145
    # Warn if the parent log observer is not enabled
 
146
    if ($row->valueByName('enabled')) {
 
147
        my $eventModel = EBox::Global->modInstance('events')->model('ConfigureWatchers');
 
148
        my $logConfRow = $eventModel->findValue(eventWatcher => 'EBox::Event::Watcher::Log');
 
149
        unless ($logConfRow->valueByName('enabled')) {
 
150
            $self->setMessage(__('Warning! The log watcher is not enabled. '
 
151
                                 . 'Enable to be notified when logs happen. '
 
152
                                 . $self->message()));
 
153
        }
 
154
    }
 
155
}
 
156
 
 
157
# Method: addedRowNotify
 
158
#
 
159
# Overrides:
 
160
#
 
161
#     <EBox::Model::DataTable::addedRowNotify>
 
162
#
 
163
sub addedRowNotify
 
164
{
 
165
    my ($self, $row, $force) = @_;
 
166
 
 
167
    # Warn if the parent log observer is not enabled
 
168
    if ( $row->valueByName('enabled') ) {
 
169
        my $eventModel = EBox::Global->modInstance('events')->model('ConfigureWatchers');
 
170
        my $logConfRow = $eventModel->findValue( eventWatcher => 'EBox::Event::Watcher::Log' );
 
171
        unless ( $logConfRow->valueByName('enabled') ) {
 
172
            $self->setMessage(__('Warning! The log watcher is not enabled. '
 
173
                                 . 'Enable to be notified when logs happen. '
 
174
                                 . $self->message()));
 
175
        }
 
176
    }
 
177
}
 
178
 
 
179
 
 
180
# Group: Protected methods
 
181
 
 
182
# Method: _table
 
183
#
 
184
# Overrides:
 
185
#
 
186
#     <EBox::Model::DataTable::_table>
 
187
#
 
188
sub _table
 
189
{
 
190
    my @tableDesc =
 
191
        (
 
192
         new EBox::Types::Text(
 
193
             fieldName     => 'domain',
 
194
             printableName => __('Domain'),
 
195
             editable      => 0,
 
196
             ),
 
197
         new EBox::Types::HasMany(
 
198
             fieldName     => 'filters',
 
199
             printableName => __('Filtering'),
 
200
             foreignModelAcquirer => \&acquireFilteringModel,
 
201
             backView      => '/Events/View/LogWatcherConfiguration?directory=Log',
 
202
             ),
 
203
        );
 
204
 
 
205
    my $dataForm = {
 
206
        tableName           => 'LogWatcherConfiguration',
 
207
        printableTableName  => __('Configure log watchers'),
 
208
        modelDomain         => 'Events',
 
209
        printableRowName    => __('Log watcher'),
 
210
        defaultActions      => [ 'editField', 'changeView' ],
 
211
        tableDescription    => \@tableDesc,
 
212
        class               => 'dataTable',
 
213
        help                => '',
 
214
        enableProperty      => 1,
 
215
        defaultEnabledValue => 0,
 
216
    };
 
217
 
 
218
    return $dataForm;
 
219
}
 
220
 
 
221
# Group: Callback functions
 
222
 
 
223
# Function: acquireFilteringModel
 
224
#
 
225
#       Callback function used to gather the foreignModel and its view
 
226
#       in order to configure the log event watcher filters
 
227
#
 
228
# Parameters:
 
229
#
 
230
#       row - hash ref with the content what is stored in GConf
 
231
#       regarding to this row.
 
232
#
 
233
# Returns:
 
234
#
 
235
#      String - the foreign model to configurate the filters
 
236
#      associated to the log event watcher
 
237
#
 
238
sub acquireFilteringModel
 
239
{
 
240
    my ($row) = @_;
 
241
 
 
242
    my $logDomain = $row->valueByName('domain');
 
243
 
 
244
    return 'events/' . FILTERING_MODEL_NAME . "_$logDomain";
 
245
}
 
246
 
 
247
# Group: Private methods
 
248
 
 
249
# Create a new filtering model given a
 
250
# log domain and notify this new model to model manager
 
251
sub _createFilteringModel
 
252
{
 
253
    my ($self, $domain, $domainTableInfo) = @_;
 
254
 
 
255
    if (not defined $domainTableInfo) {
 
256
        my $logs = EBox::Global->modInstance('logs');
 
257
        $domainTableInfo = $logs->getTableInfo($domain);
 
258
    }
 
259
 
 
260
    my $filteringModel = new EBox::Events::Model::LogFiltering(confmodule => $self->{confmodule},
 
261
                                                               directory  => $self->{confdir},
 
262
                                                               tableInfo  => $domainTableInfo);
 
263
    return $filteringModel;
 
264
}
 
265
 
 
266
# Remove an existing filtering model given a
 
267
# log domain and notify this removal to model manager
 
268
sub _removeFilteringModel
 
269
{
 
270
    my ($self, $domain) = @_;
 
271
 
 
272
    my $modelManager = EBox::Model::Manager->instance();
 
273
    $modelManager->removeModel('events/' . FILTERING_MODEL_NAME . "_$domain");
 
274
}
 
275
 
 
276
# Method: viewCustomizer
 
277
#
 
278
#   Overrides <EBox::Model::DataTable::viewCustomizer> to
 
279
#   provide a custom HTML title with breadcrumbs
 
280
#
 
281
sub viewCustomizer
 
282
{
 
283
    my ($self) = @_;
 
284
 
 
285
    my $custom =  $self->SUPER::viewCustomizer();
 
286
    $custom->setHTMLTitle([
 
287
                {
 
288
                    title => __('Events'),
 
289
                    link  => '/Events/Composite/General',
 
290
                },
 
291
                {
 
292
                    title => __('Log Observer Watcher'),
 
293
                    link  => ''
 
294
                }
 
295
            ]);
 
296
 
 
297
    return $custom;
 
298
}
 
299
 
 
300
1;