diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2009-04-06 21:32:29 -0700 |
---|---|---|
committer | Stephen Hemminger <shemminger@vyatta.com> | 2009-04-08 15:33:37 -0700 |
commit | 3f6410042b54db636eac390657a8a35583515ce4 (patch) | |
tree | 53d912db2b97adce4ac22f09abd4686a305885b5 /scripts/system | |
parent | b565e19b831a92a054e9281624f51482d3d9566e (diff) | |
download | vyatta-cfg-system-3f6410042b54db636eac390657a8a35583515ce4.tar.gz vyatta-cfg-system-3f6410042b54db636eac390657a8a35583515ce4.zip |
Rewrite existing syslog configuration update
Do most of the work in the rewritten vyatta_update_syslog code.
Handle multiple facilities for same target without causing duplicate
log messages.
Never restart syslog daemon, just reload it and only if the configuration
has changed.
Diffstat (limited to 'scripts/system')
-rwxr-xr-x | scripts/system/vyatta_update_syslog.pl | 114 |
1 files changed, 86 insertions, 28 deletions
diff --git a/scripts/system/vyatta_update_syslog.pl b/scripts/system/vyatta_update_syslog.pl index d02d8be0..5951875c 100755 --- a/scripts/system/vyatta_update_syslog.pl +++ b/scripts/system/vyatta_update_syslog.pl @@ -1,45 +1,103 @@ #!/usr/bin/perl +# **** License **** +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# This code was originally developed by Vyatta, Inc. +# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc. +# All Rights Reserved. +# +# **** End License **** + +# Update /etc/syslog.conf +# Exit code: 0 - update +# 1 - no change or error + use strict; +use lib "/opt/vyatta/share/perl5"; +use Vyatta::Config; +use File::Compare; + my $SYSLOG_CONF = '/etc/syslog.conf'; +my $SYSLOG_TMP = "/tmp/syslog.conf.$$"; +my $MESSAGES = '/var/log/messages'; +my $CONSOLE = '/dev/console'; +my %entries = (); + +die "$0 expects no arguments\n" if (@ARGV); +die "Must be run as root!\n" if ($EUID != 0); + +# This builds a data structure that maps from target +# to selector list for that target +sub add_entries { + my ( $config, $level, $target ) = @_; + + foreach my $facility ( $config->listNodes("$level facility") ) { + my $loglevel = $config->returnValue("$level facility $facility level"); + $facility = '*' if ( $facility eq 'all' ); + $loglevel = '*' if ( $loglevel eq 'all' ); + + $entries{$target} = [] unless $entries{$target}; + push @{ $entries{$target} }, $facility . '.' . $loglevel; + } +} -my $match1 = shift; -my $match2 = shift; -my $update_line = shift; +my $config = new Vyatta::Config; +$config->setLevel("system syslog"); -if (!defined($match1) || !defined($match2) || !defined($update_line)) { - exit 1; +add_entries( $config, 'global', $MESSAGES ); + +# Default syslog.conf if no global entry +%entries = ( $MESSAGES => { '*:notice', 'local7:*' } ) unless (%entries); + +add_entries( $config, 'console', $CONSOLE ); + +foreach my $host ( $config->listNodes('host') ) { + add_entries( $config, "host $host", "@$host" ); +} + +foreach my $file ( $config->listNodes('file') ) { + add_entries( $config, "file $file", $file ); } -if (system("touch $SYSLOG_CONF")) { - exit 2; +foreach my $user ( $config->listNodes('user') ) { + add_entries( $config, 'user $user', $user ); } -my $exp1 = ""; -my $exp2 = ""; -if ($match1 ne "") { - $exp1 = $match1; - if ($match2 ne "") { - $exp2 = $match2; - } -} elsif ($match2 ne "") { - $exp1 = $match2; +open my $in, '<', $SYSLOG_CONF + or die "Can't open $SYSLOG_CONF: $!"; + +open my $out, '>', $SYSLOG_TMP + or die "Can't open $SYSLOG_TMP: $!"; + +while (<$in>) { + chomp; + next if /# VYATTA$/; + print {$out} $_, "\n"; } +close $in; -if ($exp2 ne "") { - if (system("sed -i '/$exp1/{/$exp2/d}' $SYSLOG_CONF")) { - exit 2; - } -} elsif ($exp1 ne "") { - if (system("sed -i '/$exp1/d' $SYSLOG_CONF")) { - exit 3; - } +foreach my $target ( keys %entries ) { + print $out join( ';', @{ $entries{$target} } ), "\t$target # VYATTA\n"; } +close $out + or die "Can't output $SYSLOG_TMP: $!"; -if ($update_line ne "") { - open my $out, '>>', $SYSLOG_CONF or exit 4; - print {$out} "$update_line"; - close $out; +# Don't need to do anything, save time on boot +if ( compare( $SYSLOG_CONF, $SYSLOG_TMP ) == 0 ) { + unlink($SYSLOG_TMP); + exit 1; } +system("sudo cp $SYSLOG_TMP $SYSLOG_CONF") == 0 + or die "Can't copy $SYSLOG_TMP to $SYSLOG_CONF"; + +unlink($SYSLOG_TMP); exit 0; |