You can extend and customize syslog-ng OSE easily by writing destinations, parsers, template functions, and sources in Python.
Instead of writing Python code into your syslog-ng OSE configuration file, you can store the Python code for your Python object in an external file. That way, it is easier to write, maintain, and debug the code. You can store the Python code in any directory in your system, but make sure to include it in your Python path.
When referencing a Python class from an external file in the class() option of a Python block in the syslog-ng OSE configuration file, the class name must include the name of the Python file containing the class, without the path and the .py extension. For example, if the MyDestination class is available in the /etc/syslog-ng/etc/pythonexample.py file, use class("pythonexample.MyDestination"):
destination d_python_to_file { python( class("pythonexample.MyDestination") ); }; log { source(src); destination(d_python_to_file); };
If you store the Python code in a separate Python file and only include it in the syslog-ng OSE configuration file, make sure that the PYTHON_PATH environment variable includes the path to the Python file, and export the PYTHON_PATH environment variable. For example, if you start syslog-ng OSE manually from a terminal and you store your Python files in the /opt/syslog-ng/etc directory, use the following command: export PYTHONPATH=/opt/syslog-ng/etc
In production, when syslog-ng OSE starts on boot, you must configure your startup script to include the Python path. The exact method depends on your operating system. For recent Red Hat Enterprise Linux, Fedora, and CentOS distributions that use systemd, the systemctl command sources the /etc/sysconfig/syslog-ng file before starting syslog-ng OSE. (On openSUSE and SLES, /etc/sysconfig/syslog file.) Append the following line to the end of this file: PYTHONPATH="<path-to-your-python-file>", for example, PYTHONPATH="/opt/syslog-ng/etc"
To help debugging and troubleshooting your Python code, you can send log messages to the internal() source of syslog-ng OSE. For details, see Logging from your Python code.
You can extend and customize syslog-ng OSE easily by writing destinations, parsers, template functions, and sources in Python.
To debug and troubleshoot your Python code, syslog-ng OSE allows you to use the logger() method to send log messages to the internal() source of syslog-ng OSE. That way the diagnostic messages of your Python code are treated the same way as other such log messages of syslog-ng OSE. This has the following benefits:
The logger() method respects the log level settings of syslog-ng OSE. You can write error, warning, info, debug, and trace level messages.
You can follow what your Python code is doing even if syslog-ng OSE is running as a daemon in the background.
Logging to the internal() source is available in syslog-ng OSE version
To send log messages to the internal() source from Python
Add the following import to your Python code:
import syslogng
Create a logger object:
logger = syslogng.Logger()
Use the logger object in your Python code, for example:
logger.info("This is a sample log message send from the Python code.")
You can use the following log levels: logger.error, logger.warning, logger.info, logger.debug, logger.trace
Make sure that your syslog-ng OSE configuration includes the internal() source, for example:
source s_internal { internal(); }; destination d_internal { file("/var/log/internal.txt"); }; log {source(s_internal); destination(d_internal); };
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
linux-audit: Collecting messages from Linux audit logs
network: Collecting messages using the RFC3164 protocol (network() driver)
nodejs: Receiving JSON messages from nodejs applications
mbox: Converting local email 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
python: writing server-style Python sources
python() and python-fetcher() source options
python-fetcher: writing fetcher-style Python sources
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 email 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. |
python() and python-fetcher() | Receive or fetch messages using a custom source written in Python. |
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. |
© 2024 One Identity LLC. ALL RIGHTS RESERVED. Terms of Use Privacy Cookie Preference Center