Skip to content
Florian Forster edited this page Nov 20, 2023 · 1 revision
Name: DBI plugin
Type: read
Callbacks: config, init, read, shutdown
Status: supported
FirstVersion: 4.6
Copyright: 2008–2015 Florian Forster
License: MIT License
Manpage: collectd.conf(5)
See also: List of Plugins

The DBI plugin uses libdbi, a database abstraction library, to execute SQL statements on a database and read back the result. Depending on the configuration, the returned values are then converted into collectd "value-lists" (the data structure used internally to pass statistics around). This plugin is a generic plugin, i.e. it cannot work without configuration, because there is no reasonable default behavior. Please read the Plugin dbi section of the collectd.conf(5) manual page for an in-depth description of the plugin's configuration.

Being an abstraction layer, libdbi can connect to and talk with a variety of database systems. Database specific code is encapsulated in "database drivers" (DBD), which are developed as the separate libdbi-drivers project. Currently, drivers for the following databases are available:

  • Firebird / Interbase
  • FreeTDS (provides access to MS SQL Server and Sybase)
  • MariaDB
  • MySQL
  • PostgreSQL
  • SQLite / SQLite3

The configuration syntax of the DBI, Oracle, and PostgreSQL plugins is very similar, because the configuration of those plugins is handled by the same module. Also, we tried to keep the syntax similar to that of the SNMP plugin. So if you use any of those plugins already, most of the following will look familiar.

Synopsis

 <Plugin dbi>
   <Query "out_of_stock">
     Statement "SELECT category, COUNT(*) AS value FROM products WHERE in_stock = 0 GROUP BY category"
     # Use with MySQL 5.0.0 or later
     MinVersion 50000
     <Result>
       Type "gauge"
       InstancePrefix "out_of_stock"
       InstancesFrom "category"
       ValuesFrom "value"
     </Result>
   </Query>
   <Database "product_information">
     Driver "mysql"
     DriverOption "host" "localhost"
     # You may connect via socket as well:
     # DriverOption "mysql_unix_socket" "/var/tmp/mysql.sock"
     DriverOption "username" "collectd"
     DriverOption "password" "aZo6daiw"
     DriverOption "dbname" "prod_info"
     SelectDB "prod_info"
     Query "out_of_stock"
   </Database>
 </Plugin>
  LoadPlugin dbi
  <Plugin dbi>
    ## Query sections must be before Database sections
    <Query "global_status">
      Statement "SHOW GLOBAL STATUS WHERE Value REGEXP '^-?[.0-9]+$';"
      <Result>
        Type "gauge"
        InstancesFrom "Variable_name"
        ValuesFrom "Value"
      </Result>
    </Query>
    <Database "mariadb_global_status">
      Driver "mysql"
      DriverOption "host" "127.0.0.1"
      DriverOption "username" "collectd"
      DriverOption "password" "collectd"
      DriverOption "dbname" "information_schema"
      Query "global_status"
    </Database>
  </Plugin>

Example graphs

Collectd.dbi.png

Dependencies

See also

Clone this wiki locally