26. Forwarding and Storing Logs
This chapter discusses the configuration of NXLog outputs, including:
-
converting log messages to various formats,
-
forwarding logs over the network,
-
writing logs to files and sockets,
-
storing logs in databases, and
-
sending logs to an executable.
26.1. Generating Various Formats
The data format used in an outgoing log message must be considered in addition to the transport protocol. If the message cannot be parsed by the receiver, it may be discarded or improperly processed. See also Parsing Various Formats.
- Syslog
-
There are two Syslog formats, the older BSD Syslog (RFC 3164) and the newer IETF Syslog (RFC 5424). The transport protocol in Syslog can be UDP, TCP, or SSL. The xm_syslog module provides procedures for generating Syslog messages. For more information, see Generating Syslog.
Example 129. Generating Syslog and Sending via TCPThis configuration uses the to_syslog_ietf() procedure to convert the corresponding fields in the event record to a Syslog message in IETF format. The result is forwarded via TCP by the om_tcp module.
- Syslog Snare
-
The Snare agent format is a special format on top of BSD Syslog which is used and understood by several tools and log analyzer frontends. This format is most useful when forwarding Windows EventLog data in conjunction with im_mseventlog and/or im_msvistalog. The to_syslog_snare() procedure can construct Syslog Snare formatted messages. For more information, see Generating Snare.
Example 130. Generating Syslog Snare and Sending via UDPIn this example, the to_syslog_snare() procedure converts the corresponding fields in the event record to Snare format. The messages are then forwarded via UDP by the om_udp module.
- NXLog Binary format
-
The Binary format is only understood by NXLog. All the fields are preserved when the data is sent in this format, so there is no need to parse it again. The output module instance must contain
OutputType Binary
. The receiver NXLog module instance can be set toInputType Binary
. - Graylog Extended Log Format (GELF)
-
The xm_gelf module can be used to generate GELF output.
26.2. Forwarding over the Network
The following network protocols can be used. There is a trade-off between speed, reliability, compatibility, and security.
- UDP
-
To send logs in UDP datagrams, use the om_udp module.
WarningUDP packets can be dropped by the operating system because the protocol does not guarantee reliable message delivery. It is recommended to use TCP or TLS/SSL instead if message loss is a concern. - TCP
-
To send logs over TCP, use the om_tcp module.
- SSL/TLS
-
To send logs over a trusted secure SSL connection, use the om_ssl module.
Example 134. Using the om_ssl ModuleThis example provides nearly identical behavior to the TCP example above, but in this case SSL is used to securely transmit the data.
- HTTP(S)
-
To send logs over a HTTP or HTTPS connection, use the om_http and im_http modules (available only in NXLog Enterprise Edition).
26.3. Sending to Files and Sockets
- Files
-
To store logs in local files, use the om_file module. See also Writing Syslog to File.
- Unix Domain Socket
-
To send logs to a Unix domain socket, use the om_uds module. See also Sending Syslog to the Local Syslog Daemon via /dev/log.
26.4. Storing in Databases
The om_dbi and om_odbc modules can be used to store logs in databases. The om_dbi module can be used on POSIX systems where libdbi is available. The om_odbc module, available in NXLog Enterprise Edition, can be used with ODBC compatible databases on Windows, Linux, and Unix.
This configuration uses libdbi and the pgsql
driver to insert events
into the specified database. The SQL statement references fields in
the event record to be added to the database.
1
2
3
4
5
6
7
8
9
10
11
12
<Output out>
Module om_dbi
SQL INSERT INTO log (facility, severity, hostname, timestamp, application, \
message) \
VALUES ($SyslogFacility, $SyslogSeverity, $Hostname, '$EventTime', \
$SourceName, $Message)
Driver pgsql
Option host 127.0.0.1
Option username dbuser
Option password secret
Option dbname logdb
</Output>
This example inserts events into the database specified by the ODBC connection string. In this case, the sql_exec() and sql_fetch() functions are used to interact with the database.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<Output out>
Module om_odbc
ConnectionString DSN=mysql_ds;username=mysql;password=mysql;database=logdb;
<Exec>
if ( sql_exec("INSERT INTO log (facility, severity, hostname, timestamp,
application, message) VALUES (?, ?, ?, ?, ?, ?)",
1, 2, "host", now(), "app", $raw_event) == TRUE )
{
if ( sql_fetch("SELECT max(id) as id from log") == TRUE )
{
log_info("ID: " + $id);
if ( sql_fetch("SELECT message from log WHERE id=?", $id) == TRUE )
log_info($message);
}
}
</Exec>
</Output>
26.5. Sending to Executables
Using the om_exec module, all messages can be piped to an external program or script which will run until the module (or NXLog) is stopped.