Syslog Server in Perl

From CT3

Jump to: navigation, search

A simple Syslog daemon script written in Perl.


Syslog Daemon in Perl

The following script can be used to retrieve Syslog messages and store them in files.

use IO::Socket;

# Variables and Constants
my $MAXLEN = 1524;
my ($lsec,$lmin,$lhour,$lmday,$lmon,$lyear,$lwday,$lyday,$lisdst)=localtime(time); $lyear+=1900; $lmon+=1;
my $perhost=1;        # Each source gets its own log file
my $daily=1;          # Create daily log files (date in file name)
my $perfacility=0;    # Each facility gets its own log file
mkdir("log");         # Create log directory if it does not exist yet

# Start Listening on UDP port 514
$sock = IO::Socket::INET->new(LocalPort => '514', Proto => 'udp')||die("Socket: $@");

my $rin = '';
my $buf;
  $sock->recv($buf, $MAXLEN);
  my ($port, $ipaddr) = sockaddr_in($sock->peername);
  my $hn = gethostbyaddr($ipaddr, AF_INET);
  my $pri=$1;
  my $head=$2;
  my $msg=$3;
  my $sev=$pri % 8;
  my $fac=($pri-$sev) / 8;

# Logs Syslog messages
sub logsys{
  my $fac=shift; my $sev=shift; my $head=shift; my $msg=shift; my $hn=shift;
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year+=1900; $mon++;
  my $pf=""; my $fn=""; my $facdiff="";
  if ($perfacility){$facdiff="-".$fact[$fac];}
  if ($daily){$facdiff.=sprintf "-%04d-%02d-%02d", $year, $mon, $mday;}
  if ($perhost){mkdir("log\\$hn"); $fn=$hn . "\\syslog".$facdiff.".log"; $pf=$hn ."\\";}else{$fn="syslog".$facdiff.".log";}
  my $p=sprintf "[%02d.%02d.%04d, %02d:%02d:%02d, %1d, %1d] %s\n", $mday, $mon, $year, $hour, $min, $sec, $fac, $sev, $msg;
  print $p;
  if (open(WW,">>log\\$fn")){print WW $p;close(WW);}


  • Download and install ActiveState Perl.
  • Create a text file called
  • Copy the above source code into the text file and save it.

Usage guidelines


Modify the following three variables inside the script to tune the operation of the Syslog daemon:

  • $perhost=1: log messages from each host are stored in a dedicated file; the filename contains the hostname of the device (default=1)
  • $daily=1: log messages are stored in daily files; the filename contains the date (default=1)
  • $perfacility=1: log messages are stored in per-facility files; the filename contains the name of the facility (default=0)


Bostjan Sustar, © 2008 NIL Data Communications

Personal tools


Main menu