summaryrefslogtreecommitdiffstats
path: root/os-plugins/plugins/syslog/OpenSLX/OSPlugin/syslog.pm
blob: c011bf664e6e15c470e4aa0f10a5ea456668e178 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright (c) 2008 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# syslog.pm
#    - implementation of the 'syslog' plugin, which installs  
#     all needed information for a displaymanager and for the syslog. 
# -----------------------------------------------------------------------------
package OpenSLX::OSPlugin::syslog;

use strict;
use warnings;

use base qw(OpenSLX::OSPlugin::Base);

use File::Basename;
use File::Path;

use OpenSLX::Basics;
use OpenSLX::Utils;

sub new
{
    my $class = shift;

    my $self = {
        name => 'syslog',
    };

    return bless $self, $class;
}

sub getInfo
{
    my $self = shift;

    return {
        description => unshiftHereDoc(<<'        End-of-Here'),
            Sets up system log service for SLX-clients.
        End-of-Here
        precedence => 50,
    };
}

sub getAttrInfo
{
    my $self = shift;

    return {
        'syslog::active' => {
            applies_to_systems => 1,
            applies_to_clients => 1,
            description => unshiftHereDoc(<<'            End-of-Here'),
                should the 'syslog'-plugin be executed during boot?
            End-of-Here
            content_regex => qr{^(0|1)$},
            content_descr => '1 means active - 0 means inactive',
            default => '1',
        },
        'syslog::kind' => {
            applies_to_vendor_os => 1,
            description => unshiftHereDoc(<<'            End-of-Here'),
                kind of syslog to use \(syslogd-ng or old-style syslog\)
            End-of-Here
            content_regex => qr{^(syslog-ng|syslogd|rsyslog)$},
            content_descr => 'allowed: syslog-ng, syslogd, rsyslog',
            default => 'rsyslog',
        },
        'syslog::host' => {
            applies_to_systems => 1,
            applies_to_clients => 1,
            description => unshiftHereDoc(<<'            End-of-Here'),
                name or IP-address of host where syslog shall be sent to
            End-of-Here
            content_regex => undef,
            content_descr => 'a hostname or an IP address',
            default => undef,
        },
        'syslog::port' => {
            applies_to_systems => 1,
            applies_to_clients => 1,
            description => unshiftHereDoc(<<'            End-of-Here'),
                port number (UDP) where syslog shall be sent to
            End-of-Here
            content_regex => undef,
            content_descr => 'a port number',
            default => 514,
        },
        'syslog::file' => {
            applies_to_systems => 1,
            applies_to_clients => 1,
            description => unshiftHereDoc(<<'            End-of-Here'),
                name of file where all log messages shall be written
            End-of-Here
            content_regex => undef,
            content_descr => 'a complete file path',
            default => '/var/log/messages',
        },
    };
}

sub installationPhase
{
    my $self = shift;
    my $info = shift;
    
    $self->{pluginRepositoryPath} = $info->{'plugin-repo-path'};
    $self->{pluginTempPath}       = $info->{'plugin-temp-path'};
    $self->{openslxBasePath}      = $info->{'openslx-base-path'};
    $self->{openslxConfigPath}    = $info->{'openslx-config-path'};
    $self->{attrs}                = $info->{'plugin-attrs'};

    # We are going to change some of the stage1 attributes during installation
    # (basically we are filling the ones that are not defined). Since the result
    # of these changes might change between invocations, we do not want to store
    # the resulting values, but we want to store the original (undef).
    # In order to do so, we copy all stage1 attributes directly into the
    # object hash and change them there.
    $self->{kind}  = lc($self->{attrs}->{'syslog::kind'});
    
    my $engine = $self->{'os-plugin-engine'};
    
    if ($self->{kind} eq 'rsyslog' && !isInPath($self->{distro}->getBinaryName('rsyslog'))) {
        $engine->installPackages($self->{distro}->getPackageName('rsyslog'));
    }
    
    if ($self->{kind} eq 'syslog-ng' && !isInPath('syslog-ng')) {
        $engine->installPackages($self->{distro}->getPackageName($self->{distro}->getBinaryName('syslog-ng')));
    }

    # not sure if we ever should setup this service
    if ($self->{kind} eq 'syslogd' && !isInPath($self->{distro}->getBinaryName('syslogd'))) {
        $engine->installPackages($self->{distro}->getPackageName('syslogd'));
    }

    if (!$self->{kind}) {
        if (qx{which $self->{distro}->getBinaryName('rsyslog')}) {
            $self->{kind} = 'rsyslog';
        }
        elsif (qx{which $self->{distro}->getBinaryName('syslog-ng')}) {
            $self->{kind} = 'syslog-ng';
        }
        elsif (qx{which $self->{distro}->getBinaryName('syslogd')}) {
            $self->{kind} = 'syslogd';
        }
        else {
            die _tr(
                "no syslog daemon available, plugin 'syslog' wouldn't work!"
            );
        }
        print _tr("selecting %s as syslog kind\n", $self->{kind});
    }

    # start to actually do something - according to current stage1 attributes
    if ($self->{kind} eq 'rsyslog') {
        $self->_setupRsyslog($self->{attrs});
    }
    elsif ($self->{kind} eq 'syslog-ng') {
        $self->_setupSyslogNG($self->{attrs});
    }
    elsif ($self->{kind} eq 'syslogd') {
        $self->_setupSyslogd($self->{attrs});
    }
    else {
        die _tr(
            'unknown kind "%s" given, only "rsyslog", "syslog-ng" and "syslogd" are supported!',
            $self->{kind}
        );
    }

    return;
}

sub removalPhase
{
    my $self = shift;
    my $info = shift;
    
    return;
}

sub _setupRsyslog
{
    my $self  = shift;
    my $attrs = shift;

    my $repoPath = $self->{pluginRepositoryPath};

    my $kind = lc($attrs->{'syslog::kind'});
    my $rlInfo = $self->{distro}->runlevelInfo($kind);

    my $conf = unshiftHereDoc(<<"    End-of-Here");
        #!/bin/sh
        # written by OpenSLX-plugin 'syslog'

        testmkd /mnt/var/run/rsyslog
        rllinker $rlInfo->{scriptName} $rlInfo->{startAt} $rlInfo->{stopAt}

    End-of-Here
    spitFile("$repoPath/syslog.sh", $conf);
    
    return;    
}

sub _setupSyslogNG
{
    my $self  = shift;
    my $attrs = shift;
    
    my $repoPath = $self->{pluginRepositoryPath};

    my $kind = lc($attrs->{'syslog::kind'});
    my $rlInfo = $self->{distro}->runlevelInfo($kind);

    my $conf = unshiftHereDoc(<<"    End-of-Here");
        #!/bin/sh
        # written by OpenSLX-plugin 'syslog'

        cat >/mnt/etc/syslog-ng/syslog-ng.conf <<END
        # written by OpenSLX-plugin 'syslog'
        source all {
            file("/proc/kmsg");
            unix-dgram("/dev/log");
            internal();
        };
        destination console_all {
            file("/dev/tty10");
        };
        log {
            source(all);
            destination(console_all);
        };
        END
        
        if [ -n "\${syslog_host}" ]; then
        [ -z \${syslog_port} ] && syslog_port=514
        cat >>/mnt/etc/syslog-ng/syslog-ng.conf <<END
        destination loghost {
            udp( "\${syslog_host}" port(\${syslog_port}) );
        };
        log {
            source(all);
            destination(loghost);
        };
        END
        fi

        if [ -n "\${syslog_file}" ]; then
        cat >>/mnt/etc/syslog-ng/syslog-ng.conf <<END
        destination allmessages {
            file("\${syslog_file}");
        };
        log {
            source(all);
            destination(allmessages);
        };
        END
        fi

        rllinker $rlInfo->{scriptName} $rlInfo->{startAt} $rlInfo->{stopAt}

    End-of-Here
    spitFile("$repoPath/syslog.sh", $conf);
    
    return;    
}

sub _setupSyslogd
{
    my $self  = shift;
    my $attrs = shift;
    
    my $repoPath = $self->{pluginRepositoryPath};

    my $kind = lc($attrs->{'syslog::kind'});
    my $rlInfo = $self->{distro}->runlevelInfo($kind);

    my $conf = unshiftHereDoc(<<'    End-of-Here');
        #!/bin/sh
        # written by OpenSLX-plugin 'syslog'

        cat >/mnt/etc/syslog.conf <<END
        # written by OpenSLX-plugin 'syslog'
         *.=debug;\
            auth,authpriv.none;\
            news.none;mail.none     -/var/log/debug
         *.=info;*.=notice;*.=warn;\
            auth,authpriv.none;\
            cron,daemon.none;\
            mail,news.none          -/var/log/messages

        END
        
        if [ -n "\${syslog_host}" ]; then
        [ -z \${syslog_port} ] && syslog_port=514
        cat >/mnt/etc/syslog.conf <<END
         *.*                        @${syslog_host}
        END
        fi

        if [ -n "\${syslog_file}" ]; then
        cat >/mnt/etc/syslog.conf <<END
         *.*                        ${syslog_file}
        };
        END
        fi

        rllinker $rlInfo->{scriptName} $rlInfo->{startAt} $rlInfo->{stopAt}

    End-of-Here
    spitFile("$repoPath/syslog.sh", $conf);
    
    return;    
}

1;