Cover V05, I12
Article
Listing 1
Listing 2
Listing 3
Listing 4
Listing 5
Sidebar 1

dec96.tar


Listing 1: syslogconf

#! /usr/local/bin/perl
#
#    syslogconf:
#    @(#) create a comprehensive listing of
#    @(#) syslog.conf event logging (v.1.2)
#
#    Written 10NOV95-13NOV95 by Michael Hill
#    Lockheed Martin Astronautics
#    Denver, CO
#

#
#    Initialize a few things
#
select (STDERR); $| = 1;    # Turn off buffered I/O
select (STDOUT); $| = 1;
$ENV{'PATH'} = '/bin:/usr/bin:/usr/etc:/usr/sbin';
($progname = $0) =~ s/.*\///;
chop ($uname = `uname -nsr`);
@uname = split (' ', $uname);
$osname = $uname[0]; $hostname = $uname[1]; $osrev = $uname[2];
$ostype = $osname . $osrev; $ostype =~ s/\..*//;
if ($ostype eq 'SunOS4') {
$ENV{'PATH'} = '/usr/ucb:' . $ENV{'PATH'};
} elsif ($ostype eq 'SunOS5') {
$ENV{'PATH'} .= ':/usr/ucb';
}
$ENV{'PATH'} .= ':/usr/local/bin:/usr/local/share/bin';

#
#    Define some subroutines
#
sub usage {
local ($usage) = (@_);

printf (STDERR "usage:\t%s\n", $usage);
exit (1);
}

sub fatal {
local ($errMesg, $errCode) = (@_);

printf (STDERR "$progname:  %s\n", $errMesg);
exit ($errCode);
}

sub myGetopts {
local ($argument) = @_;
local (@args, $_, $first, $rest);
local ($errs) = 0;
local ($[) = 0;

@args = split (/ */, $argument);
while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
($first, $rest) = ($1, $2);
$pos = index ($argument, $first);
if ($pos >= $[) {
if ($args[$pos+1] eq ':') {
shift (@ARGV);
if ($rest eq '') {
++$errs unless @ARGV;
$rest = shift (@ARGV);
}
$opts{$first} = $rest;
}
else {
$opts{$first} = 1;
if ($rest eq '') {
shift (@ARGV);
}
else {
$ARGV[0] = "-$rest";
}
}
}
else {
print (STDERR "Unknown option:  -$first\n");
++$errs;
if ($rest ne '') {
$ARGV[0] = "-$rest";
}
else {
shift (@ARGV);
}
}
}
$errs == 0;
}

#
#    Define program variables
#
$optstr = 'dv?';
$syslogconf = '/etc/syslog.conf';
$loghost = '';
$ENV{'PATH'} .= ':/usr/ccs/bin';    # to find m4(1)
@facilities = ('kern', 'user', 'mail', 'daemon',
'auth', 'lpr', 'news', 'uucp', 'cron', 'local0',
'local1', 'local2', 'local3', 'local4', 'local5',
'local6', 'local7', 'mark');
@levels = ('emerg', 'alert', 'crit', 'err', 'warning',
'notice', 'info', 'debug');

#
#    Check for command-line arguments
#
&myGetopts ($optstr);
&usage ("$progname [-dv]") if ($opts{'?'});

#
#    Initialize the table of event types vs. actions
#    and list of severities
#
for $facil (@facilities) {
for $lev (@levels) {
$event_type{"$facil.$lev"} = 'no action';
}
}
for $i ($[ .. $#levels) {
$severity{$levels[$i]} = $i;
}

#
#    Find out if we're running on 'loghost' (for m4
#    macros)
#
print ("\t\tReport for $hostname:$syslogconf\n\n")
if (! $opts{'d'});
($name) = gethostbyname ('loghost');
if ($name eq $hostname) {
# we're running on 'loghost'
$loghost = '-DLOGHOST';
print ("$progname:  running on 'loghost', verbose mode on.\n")
if ($opts{'v'});
}

#
#    Open pipe from m4 to read in syslog.conf
#
if ($opts{'d'}) {
open (SYSLOGCONF, "<&STDIN") &&
print ("Enter lines for debugging.\n");
} else {
open (SYSLOGCONF, "m4 $loghost $syslogconf |") ||
&fatal ("can't open 'm4 $syslogconf'", 2);
print ("$syslogconf opened for read...\n")
if ($opts{'v'});
}

while (<SYSLOGCONF>) {
local (%eventlist);

next if (/^\s*$/ || /^\s*#/);
if ($opts{'v'}) {
print ("\nNew input line:\n");
print;
}
chop;

%eventlist = ( );
($events, $action) = split (/    +/);
print ("events:  '$events'; action = '$action'\n")
if ($opts{'v'});
next if ($action eq '');

# parse for multiple events
@events = split (';', $events);
for $event (@events) {
local ($thislev, $evt);

print ("\tevent:  $event;") if ($opts{'v'});
($facils, $level) = split ('\.', $event);
print (" level = $level;") if ($opts{'v'});
$thislev = $severity{$level};

# parse for multiple facility specifications
if ($facils eq '*') {
@facils = grep (! /mark/, @facilities);
} else {
@facils = split (',', $facils);
}
if ($level eq 'none') {
@levs = @levels;
} else {
@levs = grep ($severity{$_} <= $thislev, @levels);
}
for $facil (@facils) {
print (" facil='$facil'") if ($opts{'v'});
for $lev (@levs) {
if ($level eq 'none') {        # delete entry
delete ($eventlist{"$facil.$lev"})
if ($eventlist{"$facil.$lev"});
} else {            # add entry
$eventlist{"$facil.$lev"} = 1;
}
}
}
print ("\n") if ($opts{'v'});
}
for $evt (keys (%eventlist)) {
print ("Setting event '$evt' to ") if ($opts{'v'});
if ($event_type{$evt} eq 'no action') {
print ("$action\n") if ($opts{'v'});
$event_type{$evt} = $action;
} else {
print ("$event_type{$evt}, $action\n")
if ($opts{'v'});
$event_type{$evt} .= ", $action";
}
}
}

close (SYSLOGCONF);
print ("\n$syslogconf closed.\n")
if ($opts{'v'} && ! $opts{'d'});

print ("\n") if ($opts{'v'});

for $key (sort (keys (%event_type))) {
printf ("Event:  %-16s\tAction:  %s\n", $key,
$event_type{$key});
}

exit (0);

# End of File