This is a translation of the original English documentation page. Help us make it better.

14 ODBC 監視

概要

ODBC監視は、Zabbixフロントエンドの Database monitor item タイプに相当します。

ODBCはデータベース管理システム(DBMS)にアクセスするためのC言語のミドルウェアAPIです。
ODBCのコンセプトはMicrosoftによって開発され、その後他のプラットフォームにも移植されました。

ZabbixはODBCでサポートされている全てのデータベースに問い合わせをすることができます。
そのため、Zabbixはデータベースに直接接続せず、ODBCインターフェースとODBCで設定されたドライバを利用します。
この機能により、例えば、特定のデータベースキューや利用統計の確認など、複数の目的で異なるデータベースを
より効率的に監視することが可能です。
Zabbixは最も一般的に利用されているオープンソースのODBC API実装の1つであるunixODBCをサポートしています。

ODBCチェックについては、known issuesも参照してください。

unixODBC のインストール

unixODBC をインストールする推奨される方法は、Linux オペレーティングシステムのデフォルトパッケージリポジトリを
使用することです。最も人気のあるLinuxディストリビューションでは、unixODBCはデフォルトで
パッケージリポジトリに含まれています。
もし利用できない場合は、unixODBCのホームページで入手することができます。
http://www.unixodbc.org/download.html.

RedHat/Fedora ベースのシステムで yum パッケージマネージャを使用して unixODBC をインストールします。

shell> yum -y install unixODBC unixODBC-devel

SUSE ベースのシステムで zypper パッケージマネージャを使用して unixODBC をインストールします。

# zypper in unixODBC-devel

unixODBC-develパッケージは、unixODBCをサポートするZabbixをコンパイルするために必要です。

unixODBC ドライバのインストール

監視するデータベースには、unixODBCデータベースドライバをインストールする必要があります。
unixODBCには、サポートされているデータベースとドライバのリストがあります。
http://www.unixodbc.org/drivers.html
Linuxディストリビューションによっては、データベースドライバがパッケージリポジトリに含まれている場合があります。

RedHat/Fedora ベースのシステムで、yum パッケージマネージャを使用して MySQL データベースドライバをインストールする場合:

shell> yum install mysql-connector-odbc

SUSE ベースのシステムで、zypper パッケージマネージャを使用して MySQL データベースドライバをインストールする場合:

zypper で MyODBC-unixODBC
MySQL

To install the MySQL unixODBC database driver, use the package manager for the system of your choice:

# For Ubuntu/Debian systems:
       apt install odbc-mariadb
       
       # For RedHat/Fedora-based systems:
       dnf install mysql-connector-odbc
       
       # For SUSE-based systems:
       zypper install MyODBC-unixODBC

To install the database driver without a package manager, refer to MySQL documentation.

PostgreSQL

To install the PostgreSQL unixODBC database driver for RedHat/Fedora-based systems, use the dnf package manager:

dnf install postgresql-odbc

For other systems, please refer to PostgreSQL documentation.

Oracle

To install the unixODBC database driver, please refer to Oracle documentation.

MSSQL

To install the MSSQL unixODBC database driver for RedHat/Fedora-based systems, use the dnf package manager:

dnf -y install freetds unixODBC

For other systems, please refer to FreeTDS user guide.

unixODBCの設定

ODBCの設定は、odbcinst.iniodbc.ini ファイルを編集することによって行われます。
設定ファイルの場所を確認するには、次のように入力します。

shell> odbcinst -j

odbcinst.ini は、インストールされている ODBC データベースドライバーの一覧を表示するために使用されます。

[mysql]
       Description = ODBC for MySQL
       Driver      = /usr/lib/libmyodbc5.so

パラメータの詳細:

属性 説明
mysql データベースドライバ名
Description データベースドライバーの説明
Driver データベースドライバーライブラリの場所

odbc.ini は、データソースの定義に使用されます:

[test]
       Description = MySQL test database
       Driver      = mysql
       Server      = 127.0.0.1
       User        = root
       Password    =
       Port        = 3306
       Database    = zabbix

パラメータの詳細:

属性 説明
test データ ソース名 (DSN)
Description データ ソースの説明。
Driver データベース ドライバー名 - odbcinst.ini で指定されているとおり
Server データベース サーバーの IP/DNS
User 接続用のデータベース ユーザー
Password データベース ユーザーパスワード
Port データベース接続ポート
Database データベース名

ODBC接続が正常に動作しているかどうかを確認するために、データベースへの接続をテストする必要があります。
これは isql ユーティリティ(unixODBC パッケージに含まれています)で行うことができます。

shell> isql test
       +---------------------------------------+
       | Connected!                            |
       |                                       |
       | sql-statement                         |
       | help [tablename]                      |
       | quit                                  |
       |                                       |
       +---------------------------------------+
       SQL>
odbcinst.ini

The odbcinst.ini file lists the installed ODBC database drivers. If odbcinst.ini is missing, it is necessary to create it manually.

[TEST_MYSQL]
       Description=ODBC for MySQL
       Driver=/usr/lib/libmyodbc5.so
       FileUsage=1
Parameter Description
TEST_MYSQL Database driver name.
Description Database driver description.
Driver Database driver library location.
FileUsage Determines whether the database driver supports connecting to a database server without the support for accessing local files (0); supports reading data from files (1); supports writing data to files (2).
Threading Thread serialization level. Supported for PostgreSQL.
Since 1.6, if the driver manager is built with thread support, you may add another driver entry.
odbc.ini

The odbc.ini file is used to configure data sources.

[TEST_MYSQL]
       Description=MySQL Test Database
       Driver=mysql
       Server=127.0.0.1
       User=root
       Password=
       Port=3306
       Socket=
       Database=zabbix
Parameter Description
TEST_MYSQL Data source name (DSN).
Description Data source description.
Driver Database driver name (as specified in odbcinst.ini).
Server Database server IP/DNS.
User Database user for connection.
Password Database user password.
Port Database connection port.
Socket Database connection socket.
Database Database name.

For other possible configuration parameter options, see MySQL documentation.

The odbc.ini file for PostgreSQL may contain additional parameters:

[TEST_PSQL]
       Description=PostgreSQL Test Database
       Driver=postgresql
       Username=zbx_test
       Password=zabbix
       Servername=127.0.0.1
       Database=zabbix
       Port=5432
       ReadOnly=No
       Protocol=7.4+
       ShowOidColumn=No
       FakeOidIndex=No
       RowVersioning=No
       ShowSystemTables=No
       Fetch=Yes
       BoolsAsChar=Yes
       SSLmode=Require
       ConnSettings=
Parameter Description
ReadOnly Specifies whether the database connection allows only read operations (SELECT queries) and restricts modifications (INSERT, UPDATE, and DELETE statements); useful for scenarios where data should remain unchanged.
Protocol PostgreSQL backend protocol version (ignored when using SSL connections).
ShowOidColumn Specifies whether to include Object ID (OID) in SQLColumns.
FakeOidIndex Specifies whether to create a fake unique index on OID.
RowVersioning Specifies whether to enable applications to detect if data has been modified by other users while you are attempting to update a row. Note that this parameter can speed up the update process, since, to update a row, every single column does not need to be specified in the WHERE clause.
ShowSystemTables Specifies whether the database driver should treat system tables as regular tables in SQLTables; useful for accessibility, allowing visibility into system tables.
Fetch Specifies whether the driver should automatically use declare cursor/fetch to handle SELECT statements and maintain a cache of 100 rows.
BoolsAsChar Controls the mapping of Boolean types.
If set to "Yes", Bools are mapped to SQL_CHAR; otherwise, they are mapped to SQL_BIT.
SSLmode Specifies the SSL mode for the connection.
ConnSettings Additional settings sent to the backend on connection.
Testing ODBC connection

To test if the ODBC connection is working successfully, you can use the isql utility (included in the unixODBC package):

isql test
       +---------------------------------------+
       | Connected!                            |
       |                                       |
       | sql-statement                         |
       | help [tablename]                      |
       | quit                                  |
       |                                       |
       +---------------------------------------+

Zabbixフロントエンドでの item 設定

データベース監視 item を設定します。

必須入力項目には、赤いアスタリスクが付けられています。

特にデータベース監視の項目について、入力する必要があります:

タイプ ここで データベースモニタ を選択
キー サポートされている 2 つのアイテム キーのいずれかを入力します:
db.odbc.select[<一意の短い説明>,<dsn>,<接続文字列>] - このアイテムはSQL クエリ結果の最初の行の最初の列、1つの値を返すように設計されています。クエリが複数の列を返す場合、最初の列のみが読み取られます。クエリが複数の行を返す場合、最初の行のみが読み取られます。
db.odbc.get[<一意の短い説明>,<dsn>,<接続文字列>] - この項目はJSON 形式で複数の行/列を返すことができます。したがって、1 回のシステム コールですべてのデータを収集するマスター アイテムとして使用できますが、JSONPath 前処理を依存アイテムで使用して個々の値を抽出できます。詳細については、ローレベルディスカバリで使用され、返される形式の を参照してください。この項目は Zabbix 4.4 以降でサポートされています。
一意の説明は、トリガーなどで項目を識別するのに役立ちます。
dsnconnection string はオプションのパラメータですが、少なくとも 1 つが存在する必要があります。データ ソース名 (DSN) と接続文字列の両方が定義されている場合、DSN は無視されます。
データ ソース名を使用する場合は、odbc.ini で指定されているとおりに設定する必要があります。
接続文字列には、ドライバー固有の引数が含まれる場合があります。

例(MySQL ODBC driver 5の接続):
=> db.odbc.get[MySQL example,,"Driver=/usr/local/lib/libmyodbc5a.so;Database=master;Server=127.0.0.1;Port=3306"]
ユーザー名 データベースのユーザー名を入力してください
odbc.ini でユーザーが指定されている場合、このパラメーターはオプションです。
接続文字列が使用され、ユーザー名 フィールドが空でない場合、UID=<user> として接続文字列に追加されます
パスワード データベース ユーザーのパスワードを入力してください
パスワードが odbc.ini で指定されている場合、このパラメータはオプションです。
接続文字列が使用され、Password フィールドが空でない場合、接続文字列に PWD=<password> として追加されます。
SQL クエリ SQL クエリを入力します。
db.odbc.select[] アイテムを使用すると、クエリは 1 つの値のみを返す必要があることに注意してください。
データ型 ここで正しく選択できるよう、クエリによって返される情報の種類を把握しておく必要があります。 データ型が間違っていると、アイテムはサポート対象外になります。

重要な注意事項

  • server または proxy 設定でodbc pollerプロセスが起動されていない場合、データベース監視項目はサポートされなくなります。 ODBCポーラーを起動するには、Zabbix server 設定ファイルで StartODBCPollersパラメータを設定するか、proxy で実行するチェックの場合はZabbix [proxy] (/manual/appendix/config/zabbix_proxy) 設定ファイルで StartODBCPollers パラメータを設定します。
  • Zabbixはクエリの実行時間を制限していません。妥当な時間で実行できるクエリを選択することはユーザにゆだねられています。
  • Zabbix server のTimeoutパラメータの値はODBCログインタイムアウトとして 使用されます(ODBCドライバによっては、ログインタイムアウト設定が無視される場合があることに注意してください)。
  • SQL コマンドは、select ... を使用した他のクエリと同様に結果セットを返す必要があります。 問い合わせの構文は、それを処理するRDBMSに依存します。ストレージプロシージャへのリクエストの構文は call キーワードで 開始する必要があります。

Item key details

Parameters without angle brackets are mandatory. Parameters marked with angle brackets < > are optional.

db.odbc.select[<unique short description>,<dsn>,<connection string>]


Returns one value, i.e. the first column of the first row of the SQL query result. Return value: depending on the SQL query.

Parameters:

  • unique short description - a unique short description to identify the item (for use in triggers, etc);
  • dsn - the data source name (as specified in odbc.ini);
  • connection string - the connection string (may contain driver-specific arguments).

Comments:

  • Although dsn and connection string are optional parameters, at least one of them must be present. If both data source name (DSN) and connection string are defined, the DSN will be ignored.
  • If a query returns more than one column, only the first column is read. If a query returns more than one line, only the first line is read.
db.odbc.get[<unique short description>,<dsn>,<connection string>]


Transforms the SQL query result into a JSON array.
Return value: JSON object.

Parameters:

  • unique short description - a unique short description to identify the item (for use in triggers, etc);
  • dsn - the data source name (as specified in odbc.ini);
  • connection string - the connection string (may contain driver-specific arguments).

Comments:

  • Although dsn and connection string are optional parameters, at least one of them must be present. If both data source name (DSN) and connection string are defined, the DSN will be ignored.
  • Multiple rows/columns in JSON format may be returned. This item may be used as a master item that collects all data in one system call, while JSONPath preprocessing may be used in dependent items to extract individual values. For more information, see an example of the returned format, used in low-level discovery.

Example:

db.odbc.get[MySQL example,,"Driver=/usr/local/lib/libmyodbc5a.so;Database=master;Server=127.0.0.1;Port=3306"] #connection for MySQL ODBC driver 5
db.odbc.discovery[<unique short description>,<dsn>,<connection string>]


Transforms the SQL query result into a JSON array, used for low-level disovery. The column names from the query result are turned into low-level discovery macro names paired with the discovered field values. These macros can be used in creating item, trigger, etc prototypes.
Return value: JSON object.

Parameters:

  • unique short description - a unique short description to identify the item (for use in triggers, etc);
  • dsn - the data source name (as specified in odbc.ini);
  • connection string - the connection string (may contain driver-specific arguments).

Comments:

  • Although dsn and connection string are optional parameters, at least one of them must be present. If both data source name (DSN) and connection string are defined, the DSN will be ignored.

エラーメッセージ

ODBCのエラーメッセージは、詳細な情報を提供するためにフィールドに分かれて構成されています。
たとえば、以下のようなものです。

Cannot execute ODBC query: [SQL_ERROR]:[42601][7][ERROR: syntax error at or near ";"; Error while executing the query]
       └───────────┬───────────┘  └────┬────┘ └──┬──┘└┬┘└─────────────────────────────┬─────────────────────────────────────┘
                   │                   │         │    └─ Native error code            └─ Native error message
                   │                   │         └─ SQLState
                   └─ Zabbix message   └─ ODBC return code

エラーメッセージの長さは2048バイトに制限されているため、メッセージは切り捨てられる可能性があることに注意してください。
複数のODBC診断レコードがある場合、Zabbixはそれらを(|で区切って)制限された長さになるように連結しようと試みます。