109.8. DBI (im_dbi)
The im_dbi module allows NXLog to pull log data from external databases. This module utilizes the libdbi database abstraction library, which supports various database engines such as MySQL, PostgreSQL, MSSQL, Sybase, Oracle, SQLite, and Firebird. A SELECT statement can be specified, which will be executed periodically to check for new records.
Note
|
The im_dbi and om_dbi modules support GNU/Linux only because of the libdbi library. The im_odbc and om_odbc modules provide native database access on Windows. |
Note
|
libdbi needs drivers to access the database engines. These are in the libdbd-* packages on Debian and Ubuntu. CentOS 5.6 has a libdbi-drivers RPM package, but this package does not contain any driver binaries under /usr/lib64/dbd. The drivers for both MySQL and PostgreSQL are in libdbi-dbd-mysql. If these are not installed, NXLog will return a libdbi driver initialization error. |
109.8.1. Configuration
The im_dbi module accepts the following directives in addition to the common module directives.
- Driver
-
This mandatory directive specifies the name of the libdbi driver which will be used to connect to the database. A DRIVER name must be provided here for which a loadable driver module exists under the name
libdbdDRIVER.so
(usually under/usr/lib/dbd/
). The MySQL driver is in thelibdbdmysql.so
file.
- SQL
-
This directive should specify the SELECT statement to be executed every PollInterval seconds. The module automatically appends a
WHERE id > ? LIMIT 10
clause to the statement. The result set returned by the SELECT statement must contain an id column which is then stored and used for the next query.
- Option
-
This directive can be used to specify additional driver options such as connection parameters. The manual of the libdbi driver should contain the options available for use here.
- PollInterval
-
This directive specifies how frequently the module will check for new records, in seconds. If this directive is not specified, the default is 1 second. Fractional seconds may be specified (
PollInterval 0.5
will check twice every second).
- SavePos
-
If this boolean directive is set to TRUE, the position will be saved when NXLog exits. The position will be read from the cache file upon startup. The default is TRUE: the position will be saved if this directive is not specified. Even if SavePos is enabled, it can be explicitly turned off with the global NoCache directive.
109.8.2. Examples
This example uses libdbi and the MySQL driver to connect to the logdb database on the local host and execute the provided statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<Input dbi>
Module im_dbi
Driver mysql
Option host 127.0.0.1
Option username mysql
Option password mysql
Option dbname logdb
SQL SELECT id, facility, severity, hostname, \
timestamp, application, message \
FROM log
</Input>
<Output file>
Module om_file
File "tmp/output"
</Output>
<Route dbi_to_file>
Path dbi => file
</Route>