[syslog-ng]Syslog.conf translator

Jon Marks j-marks@uiuc.edu
Tue, 1 May 2001 18:01:41 -0500


--zYM0uCDKw75PZbzx
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi,

In order to make the transition to syslog-ng easier for people, I thought
it might be useful to have an automated translation from the old
syslog.conf to a working syslog-ng.conf. I came up with an awk script
which works OK for me. I was targeting Solaris and AIX specifically, but
off-hand, RedHat linux syslogd looks the same and I presume other UNIX
variants' do, too. Try it, it could work ;)

In particular, there are three things to look out for
if you're interested in porting this to another UNIX platform:

1) Change the local log device if necessary, from /dev/log to whatever
   it's supposed to be.

2) Change the device type (i.e. the syslog-ng.conf directive) for the
   local log device. The script checks 'uname' for SunOS and AIX and
   sets an appropriate variable; just add to that detection mechanism. 
   A variable would do nicely for the log device, too, but they've got
   the same name in AIX and Solaris so I didn't worry about it. (I'm not
   using the "door" device).

3) The script is an awk script, but its invoked by ksh. It's easy
   to rearrange it so that it's invoked directly as awk, but for what
   I plan to do with it, I didn't need that. In either case, ksh won't
   be available on many linux distros, I think, so just change the
   shell to bash or something. (In this case, I hope the shell you pick
   is lenient about newlines within a quoted command line argument!)

Hope this is useful!

-- 
Jonathan Marks

Systems Administrator, Production Systems Group
Computing and Communication Services Office
University of Illinois at Urbana-Champaign



--zYM0uCDKw75PZbzx
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=syslog2ng

#!/bin/ksh
#
# syslog2ng
# 
# Translator from syslog.conf to syslog-ng.conf
# by Jonathan W. Marks <j-marks@uiuc.edu>

awk '
BEGIN {
	"/bin/uname -s" | getline sysname;
	close("/bin/uname -s");
	if (sysname == "SunOS") {
		LOGDEVTYPE="sun-streams";
	}  else if (sysname == "AIX") {
		LOGDEVTYPE="unix-dgram";
	} else {
		print "!!! Unsupported system: " sysname ".";
		exit 1;
	}
}

$1 !~ /^[:space:]*#/ && NF == 2 { 
	$2 in destinations ? \
		destinations[$2] = destinations[$2] " " $1 : \
		destinations[$2] = $1;
}

END {

	print \
"options { dir_perm(0755); perm(0644); chain_hostnames(no);\n" \
"          keep_hostname(yes); };\n";

	print \
"source local {\n" \
"	" LOGDEVTYPE "(\"/dev/log\");\n" \
"	udp(ip(0.0.0.0) port(514));\n" \
"	internal();\n" \
"};\n";

	for (dest in destinations) {
		make_filters(dest, destinations[dest]);
		make_destination(dest);
		make_log(dest);
	}

}
 
function make_filters(dest, filterstr) {

	split(filterstr, specentries, " ");
	for (entryNo in specentries) {
		entry = specentries[entryNo];
		split(entry, termlist, ";");
		newFilterNumbers = "";
		for (termNo in termlist) {
			newNum = make_filter(termlist[termNo]);
			if (newNum) {
				newFilterNumbers = newFilterNumbers " " \
					newNum;
			}
		}
		destinations[dest] = destination[dest] " " \
			newFilterNumbers;
	}
}

function make_filter(spec) {

	dot = index(spec, ".");
	split(substr(spec, 1, (dot - 1)), faclist, ",");
	severity = substr(spec, (dot + 1));

	negate = 0;
	if (severity == "none") { negate = 1 };
	filterID = severity;
	for (facno in faclist) {
		filterID = filterID " " faclist[facno];
	}
	if (! (filterID in all_filters)) {
		all_filters[filterID] = ++filterNum;

		printf "filter f_" filterNum " {\n\t";
		nPrinted = 0;
		if (faclist[1] != "*") {
			printf("%sfacility(", (negate ? "not " : ""));
			for (facno in faclist) {
				printf("%s" faclist[facno], \
					(nPrinted++ > 0 ? "," : ""));
			}
			printf(")%s", (severity != "none" ? " and " : ""));
		}
		if (severity != "none") {
			printf("level(" severity "%s)",
				(severity == "emerg" ? "" : "...emerg"));
		}
		printf(";\n};\n\n");
	}

	return all_filters[filterID];
}

function make_destination(d) {
	destinations[d] = ++dno " " destinations[d];
	printf "destination d_" dno " { \n";
	if (d ~ /^\//) {
		printf "\tfile(\"" d "\"\n";
		printf "\t\tcreate_dirs(yes) perm(0640));\n";
	}
	else if (d ~ /^@/) {
		printf "\tudp(\"" substr(d, 2) "\" port(514));\n";
	}
	else {
		printf "\tusertty(\"" d "\");\n";
	}		
	
	print "};\n";
}

function make_log(d) {
	
	n_entries = split(destinations[d], filters, " ");
	printf "log { source(local); " ;
	for (i = 2; i <= n_entries; i++) {
		printf "filter(f_" filters[i] "); ";
	}
	print "destination(d_" filters[1] "); };\n";
}'


--zYM0uCDKw75PZbzx--