[syslog-ng] Hi everybody i'm new to the list and i have a nice gift, a little script to translate configuration :o)

Harry Rüter h.rueter at gmx.net
Sat May 13 01:18:04 CEST 2006


Hi,

i'm simply happy about syslog-ng :o)

I coded a shellscript to convert configuration from
old syslog to syslog-ng.

Works well for me on Suse LINUX 9.1

Have a look at it and enjoy.

Would be nice if it would be included
in the distribution, but it's tested with 1.6.11 only,
so it necessary to test it on different
OS and distributions isn't it ?

Greets Harry
-------------- next part --------------
#!/bin/sh
###############################################################################
#
# Translator from syslog.conf to syslog-ng.conf
# by Harry Rueter <h.rueter at gmx.net>
#
# Writes translated configuration to stdout
# 
# New file has to be checked, no warranty at all !!!!!!
# Templates are my prefered format.
# Some options and sources are commented out
# for manual configuration.
# Trailing "-" in destination file will not be translated   
#
# History: 12.05.2006: initial coding
#
###############################################################################

INFILE="$1"

if [ "a$INFILE" = "a"  ]
then
  echo "error, usage: `basename $0` syslog_configuration_file [> syslog-ng__configuration_file]"
  exit 1
fi

trenner()
{
  echo "########################################################"
}

trennerecho()
{
  trenner
  echo ""
}

trennercomment()
{
  COMMENT="$1"
  trenner
  echo "# $COMMENT"
  trenner
}


header()
{
  trenner
  echo "# Generated by `basename $0`(coded by Harry Rueter 5/2006"
  trenner 
  echo
  echo "#######################"  
  echo "# Global options      #"
  echo "#######################"
  echo "options"
  echo "{"
  echo "  sync(0);"
  echo "  time_reopen(10);"
  echo "  log_fifo_size(100);"  
  echo "  #mark(0);"
  echo "  #log_fifo_size();"
  echo "  #chain_hostnames(no);"
  echo "  #create_dirs (no);"
  echo "  #dir_perm(0755);"
  echo "  #dns_cache(yes);"
  echo "  #keep_hostname(yes);"
  echo "  #log_msg_size(8192);"
  echo "  #long_hostnames(on);"
  echo "  #perm(0644);"
  echo "  #stats(3600);"
  echo "  #use_dns(yes);"
  echo "  #use_fqdn(yes);"
  echo "};"
  trennerecho
}


downstr()
{
  echo "$1"|sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
}


mk_filter_rules()
{
  INPUT="$1"

  FAC=`echo "$INPUT"|cut -d"." -f1|sed 's/=//g'`
  LEVEL=`echo "$INPUT"|cut -d"." -f2|sed 's/=//g'`
  TMPFAC=""
  TMPLEVEL=""
  
  if [ "$FAC" != "*"  ]
  then
    TMPFAC="facility($FAC)"  
  fi

  if [ "$LEVEL" = "*" ]
  then
    TMPLEVEL="level(debug..warn)"
  elif [ "$LEVEL" != "none" ]
  then
    TMPLEVEL="level($LEVEL)"
  fi    
  
  if [ -n "$TMPLEVEL" -a -n "$TMPFAC" ]
  then
    echo "($TMPFAC and $TMPLEVEL)"
  else
    echo "($TMPFAC$TMPLEVEL)"  
  fi
}


mk_dev_log_src()
{
  trennercomment "src=/dev/log"
  echo "source dev_log_src"
  echo "{"
  echo "    unix-stream(\"/dev/log\");"
  echo "};"
  trennerecho
}

mk_unix_dgram_log_src()
{
  trennercomment "src="
  echo "#source unix_dgram_log_src"
  echo "#{"
  echo "#    unix-dgram(\"\");"
  echo "#}:"
  trennerecho
};

mk_udp_log_src()
{
  trennercomment "src=udp"
  echo "#source udp_log_src"
  echo "#{"
  echo "#   udp("
  echo "#       ip(0.0.0.0)"
  echo "#       port(5000)" 
  echo "#       max-connections(300)"
  echo "#      );"
  echo "#};"
  trennerecho
};

mk_tcp_log_src()
{
  trennercomment "src=tcp"
  echo "#source tcp_log_src"
  echo "#{"
  echo "#   tcp("
  echo "#       ip(0.0.0.0)"
  echo "#       port(5000)" 
  echo "#       max-connections(300)"
  echo "#      );"
  echo "#};"
  trennerecho
};

mk_pipe_log_src()
{
  trennercomment "src=pipe"
  echo "#source pipe_log_src"
  echo "#{"
  echo "#   pipe();"
  echo "#};"
  trennerecho
};

mk_internal_log_src()
{
  trennercomment "src=internal()"
  echo "source internal_src"
  echo "{"
  echo "   internal();"
  echo "};"
  trennerecho
}

mk_log_src()
{
  mk_dev_log_src
  mk_unix_dgram_log_src
  mk_udp_log_src
  mk_tcp_log_src
  mk_pipe_log_src
  mk_internal_log_src
  echo
}

mk_dest_file()
{
  FILE="$1"
  NAME=`basename $FILE|sed 's/\./_/g'`
  trennercomment "destination file=$FILE"    
  echo "destination d_$NAME"
  echo "{"
  echo "  file(\"$FILE\""
  echo "        owner(\"root\")"
  echo "        group(\"root\")"
  echo "        perm(0600)"
  echo "        template( \"[\$YEAR/\$MONTH/\$DAY \$HOUR:\$MIN:\$SEC] \$PROGRAM - \$PRIORITY : \$MSGONLY\n\")"
  echo "        #template_escape(no)"
  echo "        #sync_freq(1000)"	
  echo "        #create_dirs(yes/no)"
  echo "        #dir_perm(755)"
  echo "        #dir_owner(root)"
  echo "        #dir_group(root)"
  echo "      );"
  echo "};"
}	


mk_dest_pipe()
{
  FILE="$1" 
  NAME=`basename $FILE|sed 's/\./_/g'`
  trennercomment "destination pipe=$FILE"
  echo "destination d_$NAME"
  echo "{"
  echo "  pipe(\"$FILE\""
  echo "        owner(\"root\")"
  echo "        group(\"root\")"
  echo "        perm(0600)"
  echo "        template( \"[\$YEAR/\$MONTH/\$DAY \$HOUR:\$MIN:\$SEC] \$PROGRAM - \$PRIORITY : \$MSGONLY\n\")"
  echo "        #template_escape(no)"
  echo "       );"
  echo "};"
}	

mk_dest_filter()
{
  NAME="$1"  
  FILTER="$2"
  echo "filter f_$NAME"
  echo "{"
  echo "  $FILTER;"
  echo "};"
  trenner
}				
	
mk_log_entry()
{
  FILTER="$1"
  DEST="$2"
  LINE="$3"
  echo "log"
  echo "{"
  echo "  source(dev_log_src);"
  echo "  filter($FILTER);"
  echo "  destination($DEST);"
  echo "};"
  trennerecho
}				      


mk_internal_entry()
{
  DEST="/var/log/syslog-ng-internal.log"
  trennercomment "internal syslog-ng logging"
  mk_dest_file $DEST   
  echo "log"
  echo "{"
  echo "  source(internal_src);"
  echo "  destination(d_syslog-ng-internal_log};"
  echo "};"
  trennerecho
}				      


mk_dest()
{
  DEST="$1"
  
  echo $DEST|grep "^|" >/dev/null
  if [ $? -eq 0  ]
  then
    DEST=`echo "$DEST"|sed 's/^|//g'`
    mk_dest_pipe $DEST 
  else
    mk_dest_file $DEST 
  fi
}  

proceedline()
{
  LINE="$1"
  DEST="$2"
  CNT=1
  
  THEFILTER=""
  
  trenner
  echo  "# syslogd: $LINE"   
  echo  "# log entry for $DEST"  
  trennerecho
  
  mk_dest "$DEST"
  trenner
  
  echo "$LINE"|grep ";" >/dev/null 2>&1

  if [ $? -eq 0 ]
  then
    while [ 0 -eq 0  ]
    do
      PART=`echo "$LINE"|cut -d";" -f$CNT`
      CNT=`expr $CNT + 1`

      if [ "a$PART" = "a" ]
      then
        NAME="`basename $DEST|sed 's/\./_/g'`"
	mk_dest_filter "$NAME" "$THEFILTER"
        break
      else
	TMPFILTER=`mk_filter_rules "$PART"`
	if [ -n "$THEFILTER"  ]
	then
	  THEFILTER="$THEFILTER or $TMPFILTER"
	else
	  THEFILTER="$TMPFILTER"	
	fi
      fi  
    done
  else
    NAME=`basename $DEST|sed 's/\./_/g'`
    TMPFILTER=`mk_filter_rules "$LINE"`
    mk_dest_filter "$NAME" "$TMPFILTER"
  fi     
}

proceed()
{
  LINE="$1"
  RULES=`echo "$LINE"|cut -d" " -f1`
  DEST=`echo "$LINE"|cut -d" " -f2-10|sed 's/^-//g'`
  
  if [ "$DEST" = "*"  ]
  then
    DEST="/var/log/everything.log"
  fi
  
  proceedline "$RULES" "$DEST"
  WHAT=`basename $DEST|sed 's/\./_/g'`
  mk_log_entry "f_$WHAT" "d_$WHAT" $LINE
}

#############################################
# main program
#############################################

# statical entrys
header
echo
mk_log_src
echo
mk_internal_entry
echo


# dynamical entries 
cat "$INFILE" |grep -v "^#"|grep -v "^$" |expand|sed 's/  */ /'| \
while read LINE
do
  proceed "$LINE"
done


More information about the syslog-ng mailing list