To create a reusable configuration snippet and reuse parts of a configuration file, you have to define the block (for example, a source) once, and reference it later. (Such reusable blocks are sometimes called a Source Configuration Library, or SCL.) Any syslog-ng object can be a block. Use the following syntax to define a block:
block type name() {<contents of the block>};
Type must be one of the following: destination, filter, log, parser, rewrite, root, source. The root blocks can be used in the "root" context of the configuration file, that is, outside any other statements.
Blocks may be nested into each other, so for example a block can be built from other blocks. Blocks are somewhat similar to C++ templates.
The type and name combination of each block must be unique, that is, two blocks can have the same name if their type is different.
To use a block in your configuration file, you have to do two things:
Include the file defining the block in the syslog-ng.conf file — or a file already included into syslog-ng.conf. Version
Reference the name of the block in your configuration file. This will insert the block into your configuration. For example, to use a block called myblock, include the following line in your configuration:
myblock()
Blocks may have parameters, but even if they do not, the reference must include opening and closing parentheses like in the previous example.
The contents of the block will be inserted into the configuration when syslog-ng OSE is started or reloaded.
Suppose you are running an application on your hosts that logs into the /opt/var/myapplication.log file. Create a file (for example, myblocks.conf) that stores a source describing this file and how it should be read:
block source myappsource() { file("/opt/var/myapplication.log" follow-freq(1) default-facility(syslog)); };
Include this file in your main syslog-ng configuration file, reference the block, and use it in a logpath:
@version: 3.16 @include "<correct/path>/myblocks.conf" source s_myappsource { myappsource(); }; ... log { source(s_myappsource); destination(...); };
To define a block that defines more than one object, use root as the type of the block, and reference the block from the main part of the syslog-ng OSE configuration file.
The following example defines a source, a destination, and a log path to connect them.
block root mylogs() { source s_file { file("/var/log/mylogs.log" follow-freq(1)); }; destination d_local { file("/var/log/messages"); }; log { source(s_file); destination(d_local); }; };
|
TIP:
Since the block is inserted into the syslog-ng OSE configuration when syslog-ng OSE is started, the block can be generated dynamically using an external script if needed. This is useful when you are running syslog-ng OSE on different hosts and you want to keep the main configuration identical. If you want to reuse more than a single configuration object, for example, a logpath and the definitions of its sources and destinations, use the include feature to reuse the entire snippet. For details, see Including configuration files. |
Configuration blocks can receive arguments as well. The parameters the block can receive must be specified when the block is defined, using the following syntax:
block type block_name(argument1(<default-value-of-the-argument>) argument2(<default-value-of-the-argument>) argument3())
If an argument does not have a default value, use empty parentheses after the name of the argument. To refer the value of the argument in the block, use the name of the argument between backticks (for example, `argument1`).
The following sample defines a file source block, which can receive the name of the file as a parameter. If no parameter is set, it reads messages from the /var/log/messages file.
block source s_logfile (filename("messages")) { file("/var/log/`filename`" ); }; source s_example { s_logfile(filename("logfile.log")); };
If you reference the block with more arguments then specified in its definition, you can use these additional arguments as a single argument-list within the block. That way, you can use a variable number of optional arguments in your block. This can be useful when passing arguments to a template, or optional arguments to an underlying driver. To reference this argument-list, insert `__VARARGS__` to the place in the block where you want to insert the argument-list. Note that you can use this only once in a block. The following definition extends the logfile block from the previous example, and passes the optional arguments (follow-freq(1) flags(no-parse)) to the file() source.
block source s_logfile (filename("messages")) { file("/var/log/`filename`" `__VARARGS__`); }; source s_example { s_logfile(filename("logfile.log") follow-freq(1) flags(no-parse)); };
The following example is the code of the pacct() source driver, which is actually a block that can optionally receive two arguments.
block source pacct(file("/var/log/account/pacct") follow-freq(1)) { @module pacctformat file("`file`" follow-freq(`follow-freq`) format("pacct") tags(".pacct") `__VARARGS__`); };
The syslog-ng OSE application can automatically execute scripts when it is started, and can include the output of such script in the configuration file. To create and use a script that generates a part of the syslog-ng OSE configuration file (actually, a configuration block), complete the following steps. The steps include examples for collecting Apache access log files (access.log) from subdirectories, but you can create any script that creates a valid syslog-ng OSE configuration snippet.
Navigate to the directory where you have installed syslog-ng OSE (for example, /opt/syslog-ng/share/include/scl/), and create a new directory, for example, apache-access-logs. The name of the directory will be used in the syslog-ng OSE configuration file as well, so use a descriptive name.
Create a file called plugin.conf in this new directory.
Edit the plugin.conf file and add the following line:
@module confgen context(source) name(<directory-name>) exec("`scl-root`/<directory-name>/<my-script>")
Replace <directory-name> with the name of the directory (for example, apache-access-logs), and <my-script> with the filename of your script (for example, apache-access-logs.sh). You can reference the script in your &abbrev; configuration file as a configuration block using the value name option.
The context option determines the type of the configuration snippet that the script generates, and must be one of the following: destination, filter, log, parser, rewrite, root, source. The root blocks can be used in the "root" context of the configuration file, that is, outside any other statements. In the example, context(source) means that the output of the script will be used within a source statement.
Write a script that generates the output you need, and formats it to a configuration snippet that syslog-ng OSE can use. The filename of the script must match with the filename used in plugin.conf, for example, apache-access-logs.sh.
The following example checks the /var/log/apache2/ directory and its subdirectories, and creates a source driver for every directory that contains an access.log file.
#!/bin/bash for i in `find /var/log/apache2/ -type d`; do echo "file(\"$i/access.log\" flags(no-parse) program-override(\"apache2\"));"; done;
The script generates an output similar to this one, where service* is the actual name of a subdirectory:
file("/var/log/apache2/service1/access.log" flags(no-parse) program-override("apache2")); file("/var/log/apache2/service2/access.log" flags(no-parse) program-override("apache2"));
Include the plugin.conf file in the syslog-ng.conf file — or a file already included into syslog-ng.conf. Version
Add the block you defined in the plugin.conf file to your syslog-ng OSE configuration file. You can reference the block using the value of the name option from the plugin.conf file, followed by parentheses, for example, apache-access-logs(). Make sure to use the block in the appropriate context of the configuration file, for example, within a source statement if the value of the context option in the plugin.conf file is source.
@include "scl.conf" ... source s_apache { file("/var/log/apache2/access.log" flags(no-parse) program-override("apache2")); file("/var/log/apache2/error.log" flags(no-parse) program-override("apache2")); file("/var/log/apache2/ssl.log" flags(no-parse) program-override("apache2")); apache-access-logs(); }; log { source(s_apache); destination(d_central); }; ...
Check if your modified syslog-ng OSE configuration file is syntactically correct using the syslog-ng --syntax-only command.
If your modified configuration is syntactically correct, load the new configuration file using the syslog-ng-ctl reload command.
default-network-drivers: Receive and parse common syslog messages
default-network-drivers() source options
internal: Collecting internal messages
file: Collecting messages from text files
Notes on reading kernel messages
wildcard-file: Collecting messages from multiple text files
wildcard-file() source options
network: Collecting messages using the RFC3164 protocol (network() driver)
nodejs: Receiving JSON messages from nodejs applications
mbox: Converting local e-mail messages to log messages
osquery: Collect and parse osquery result logs
pipe: Collecting messages from named pipes
pacct: Collecting process accounting logs on Linux
program: Receiving messages from external applications
sun-streams: Collecting messages on Sun Solaris
syslog: Collecting messages using the IETF syslog protocol (syslog() driver)
system: Collecting the system-specific log messages of a platform
systemd-journal: Collecting messages from the systemd-journal system log storage
systemd-journal() source options
systemd-syslog: Collecting systemd messages using a socket
tcp, tcp6, udp, udp6: Collecting messages from remote hosts using the BSD syslog protocol— OBSOLETE
tcp(), tcp6(), udp() and udp6() source options: OBSOLETE
unix-stream, unix-dgram: Collecting messages from UNIX domain sockets
UNIX credentials and other metadata
unix-stream() and unix-dgram() source options
A source is where syslog-ng receives log messages. Sources consist of one or more drivers, each defining where and how messages are received.
To define a source, add a source statement to the syslog-ng configuration file using the following syntax:
source <identifier> { source-driver(params); source-driver(params); ... };
The following source statement receives messages on the TCP port 1999 of the interface having the 10.1.2.3 IP address.
source s_demo_tcp { network(ip(10.1.2.3) port(1999)); };
The following source statement receives messages on the 1999 TCP port and the 1999 UDP port of the interface having the 10.1.2.3 IP address.
source s_demo_two_drivers { network(ip(10.1.2.3) port(1999)); network(ip(10.1.2.3) port(1999) transport("udp")); };
If the message received by the source does not have a proper syslog header, you can use the default-facility() and default-priority() options to set the facility and priority of the messages. Note that these values are applied only to messages that do not set these parameters in their header.
source headerless_messages { network(default-facility(syslog) default-priority(emerg)); };
Define a source only once. The same source can be used in several log paths. Duplicating sources causes syslog-ng to open the source (TCP/IP port, file, and so on) more than once, which might cause problems. For example, include the /dev/log file source only in one source statement, and use this statement in more than one log path if needed.
|
Caution:
Sources and destinations are initialized only when they are used in a log statement. For example, syslog-ng OSE starts listening on a port or starts polling a file only if the source is used in a log statement. For details on creating log statements, see log: Filter and route log messages using log paths, flags, and filters. |
To collect log messages on a specific platform, it is important to know how the native syslogd communicates on that platform. The following table summarizes the operation methods of syslogd on some of the tested platforms:
Each possible communication mechanism has a corresponding source driver in syslog-ng. For example, to open a unix socket with SOCK_DGRAM style communication use the driver unix-dgram. The same socket using the SOCK_STREAM style — as used under Linux — is called unix-stream.
The following source statement collects the following log messages:
internal(): Messages generated by syslog-ng.
network(transport("udp")): Messages arriving to the 514/UDP port of any interface of the host.
unix-dgram("/dev/log");: Messages arriving to the /dev/log socket.
source s_demo { internal(); network(transport("udp")); unix-dgram("/dev/log"); };
The following table lists the source drivers available in syslog-ng.
Name | Description |
---|---|
file() | Opens the specified file and reads messages. |
internal() | Messages generated internally in syslog-ng. |
network() | Receives messages from remote hosts using the BSD-syslog protocol over IPv4 and IPv6. Supports the TCP, UDP, and TLS network protocols. |
nodejs() | Receives JSON messages from nodejs applications. |
mbox() | Read e-mail messages from local mbox files, and convert them to multiline log messages. |
osquery() | Run osquery queries, and convert their results into log messages. |
pacct() | Reads messages from the process accounting logs on Linux. |
pipe() | Opens the specified named pipe and reads messages. |
program() | Opens the specified application and reads messages from its standard output. |
snmptrap() | Read and parse the SNMP traps of the Net-SNMP's snmptrapd application. |
sun-stream(), sun-streams() | Opens the specified STREAMS device on Solaris systems and reads incoming messages. |
syslog() | Listens for incoming messages using the new IETF-standard syslog protocol. |
system() | Automatically detects which platform syslog-ng OSE is running on, and collects the native log messages of that platform. |
systemd-journal() | Collects messages directly from the journal of platforms that use systemd. |
systemd-syslog() | Collects messages from the journal using a socket on platforms that use systemd. |
unix-dgram() | Opens the specified unix socket in SOCK_DGRAM mode and listens for incoming messages. |
unix-stream() | Opens the specified unix socket in SOCK_STREAM mode and listens for incoming messages. |
stdin() | Collects messages from the standard input stream. |
wildcard-file() | Reads messages from multiple files and directories. |
© ALL RIGHTS RESERVED. Terms of Use Privacy Cookie Preference Center