Syslog-ng i bazy danych

Możliwość przeglądania logów systemowych w bazie danych daje duże możliwości analityczne. Tylko jak je tam umieścić?

Contents

konfiguracja Syslog-ng

Należy do komputera logującego do bazy danych dodać dwie linie określające iż chcemy logować do potoku:

destination d_logpipe { pipe("/tmp/pipe" owner("someone") template("\( '$HOST', '$ISODATE', '$FACILITY', '$PRIORITY', '$MESSAGE' \)\n") ); };
log { source(src); destination(d_logpipe); };

Logowanie do bazy danych przy pomocy skryptu Perla

W celu logowania do bazy danych można wykorzystać prosty skrypt:

#!/usr/bin/perl -w
# We read line after line from a "pipe" and from them we generate SQL
# sentences to insert the log entries in the remote database.
#         José Luis Domingo López 

## Overview
# "syslog-ng" is configured to log all system events to a named pipe
# previously created via "mkfifo". Each log entre is formated through a
# "syslog-ng" "template" to generate a pseudo-SQL insert on the remote
# database. But we are using a SSH tunnel (-L mode), so we connect to
# the local IP address, not the remote server's.
#
## Implementation details
# - First, open an SQL connection to the remote MySQL database (via the
#   local tunnel endpoint), and keep it open while running
# - Open read-only the pipe where syslog-ng logs system events, and
#   keep reading line by line until EOF or program end
# - Each line read from the pipe is the base of a SQL insertion sentence
#   into the remote MySQL database.

# NOTE: libdbd-mysql-perl is necessary for this script
use strict;
use Mysql;

my $host="127.0.0.1";
my $database="logs";
my $user="joseluis";
my $password="joseluis";
my $query="";
my $error=0;
my $errmsg;
my $sth;

my $dbh = Mysql->connect($host, $database, $user, $password);
if ( ! $dbh ) {
    $errmsg= Mysql->errmsg();
    die "Unable to connect with the remote database, error: $errmsg\n";
};

open ( LOGPIPE, "< /tmp/pipe" );
while ( my $log =    ) {
    $query="INSERT INTO logs.testbox (host,time,facility,priority,message) VALUES ";
    $query=$query . $log ;
    $sth = $dbh->query($query);
    $error = Mysql->errno;
    $errmsg= Mysql->errmsg();
    #if ( $error != 0 ) {
    #    die "Insertion failed: $errmsg\n";
    #};
};
close ( LOGPIPE );

Tekst powstał na podstawie listu José Luis Domingo López skierowanego na listę debian-security@debian.org wysłanego 24 kwietnia 2003 roku.

Dodaj komentarz