aboutsummaryrefslogtreecommitdiffstats
path: root/add-syslog
blob: d24de7a4ef474f149720d0c322cbc802454bad68 (plain)
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
#!/usr/bin/perl
# rpm helper scriptlet to add an entry into default syslog implementation
# $Id$
use Getopt::Std;
use strict;

my @facilities = qw/auth authpriv cron daemon \
                    kern lpr mail mark news syslog \
                    user uucp local0 local1 local2 \
                    local3 local4 local5 local6 local7/;
my %facilities = map { $_ => 1 } @facilities;
my @priorities = qw/debug info notice warning err crit alert emerg/;
my $i;
my %priorities = map { $_ => $i++ } @priorities;

main(@ARGV) unless caller();

sub main {
    my %opts = ( 
        s => '/dev/log',
        m => 'debug',
        M => 'emerg'
    );
    getopts('s:m:M:', \%opts);
    my ($source, $min, $max) = @opts{qw/s m M/};

    die <<EOF if @ARGV < 4;
usage: $0 [options] <pkg> <nb> <facility> <dest>
Available options:
-s <source>   source (default: /dev/log)
-m <priority> min priority (default: debug)
-M <priority> max priority (default: emerg)
EOF
    my ($package, $number, $dest, $facility) = @ARGV;

    # don't do anything for upgrade
    exit(0) if $number == 2;

    # check arguments
    die "invalid facility '$facility'" if $facility && !$facilities{$facility};

    die "invalid min priority '$min'" if $min && ! defined $priorities{$min};
    die "invalid max priority '$max'" if $max && ! defined $priorities{$max};
    die "maximum priority '$max' lower than minimum priority '$min'"
        if $min && $max && ($priorities{$max} < $priorities{$min});

    my $release_file = '/etc/release';
    open(my $fh, '<', $release_file)
        or die "can't open $release_file: $!";
    my $line = <$fh>;
    $line =~ /^Mandriva Linux release (\d\d\d\d\.\d)/;
    my $release = $1;
    close($fh);

    # add an entry to default syslog implementation, if installed
    if ($release && version->parse($release) < version->parse("2010.1")) {
        add_sysklogd_entry($package, $source, $dest, $facility, $min, $max)
            if -f '/etc/init.d/syslog';
    } else {
        add_rsyslog_entry($package, $source, $dest, $facility, $min, $max)
            if -f '/etc/init.d/rsyslog';
    }
}

sub add_sysklogd_entry {
    my ($package, $source, $dest, $facility, $min, $max) = @_;

    # ensure source is configured
    add_new_source($source, '/etc/sysconfig/syslog')
        if $source ne '/dev/log';

    # compute selector
    my $selector = get_selector($facility, $min, $max);

    # compute spacing to keep default configuration file formatting
    my $tabs = length($selector) < 48 ?
        ((48 - length($selector)) / 8) :
        1;

    # append entry
    open(my $out, '>>', '/etc/syslog.conf')
        or die "Can't open /etc/syslog.conf for appending: $!";
    print $out "# BEGIN: Automatically added by $package installation\n";
    print $out "$selector" . ("\t" x $tabs) . "-$dest\n";
    print $out "# END\n";
    close($out);

    # relaunch syslog
    system('service syslog condrestart 2>&1 >/dev/null');
}

sub add_rsyslog_entry {
    my ($package, $source, $dest, $facility, $min, $max) = @_;

    # compute selector
    my $selector = get_selector($facility, $min, $max);

    # append entry
    open(my $out, '>', "/etc/rsyslog.d/$package.conf")
        or die "Can't open /etc/rsyslog.d/$package.conf for writing: $!";
    print $out "# Automatically added by $package installation\n";
    print $out "\$AddUnixListenSocket $source\n" if $source ne '/dev/log';
    print $out "$selector\t-$dest\n";
    close($out);

    # relaunch rsyslog
    system('service rsyslog condrestart 2>&1 >/dev/null');
}

sub add_new_source {
    my ($source, $file) = @_;

    my ($content, $changed);
    open(my $in, '<', $file)
        or die "Can't open $file for reading: $!";

    while (my $line = <$in>) {
        if ($line =~ /^SYSLOGD_OPTIONS=(.*)/) {
            my $options = $1;
            if ($options) {
                my $quote;
                if ($options !~ /-a\s+$source/) {
                    if ($options =~ /^(["'])(.*)\1$/) {
                        $quote = $1;
                        $options = $2;
                    } else {
                        $quote = '"';
                    }
                    $options = $quote . $options . " -a $source" . $quote;
                    $changed = 1;
                }
            } else {
                $options = "\"-a $source\"";
                $changed = 1;
            }

            $content .= "SYSLOGD_OPTIONS=$options\n";
        } else {
            $content .= $line;
        }
    }
    close($in);

    if ($changed) {
        open(my $out, '>', $file)
            or die "Can't open $file for writing: $!";
        print $out $content;
        close($out);
    }
}

sub get_selector {
    my ($facility, $min, $max) = @_;

    my $selector;
    if ($max eq 'emerg') {
        if ($min eq 'debug') {
            $selector = "$facility.*";
        } else {
            $selector = "$facility.$min";
        }
    } else {
        for my $i ($priorities{$min} .. $priorities{$max}) {
            $selector .= ';' if $selector;
            $selector .= "$facility.=$priorities[$i]";
        }
    }

    return $selector;
}

1;