Skip to content

メトリックプラグイン - mackerel-plugin-postgres

mackerel-plugin-postgres は PostgreSQL の統計状態をメトリックとして投稿するプラグインです。監視対象の PostgreSQL のバージョンによっては、実行するユーザに権限を与える必要があります。詳しくは プラグインの実行に必要なユーザ権限 を参照してください。

監視できるメトリック

PostgreSQL の統計情報をメトリックとして投稿します。統計情報については PostgreSQL のドキュメントを参照してください。

[https://www.postgresql.jp/document/:embed:cite]

Postgres Connections

pg_stat_activity から取得できる情報を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Activepostgres.connections.active-active 状態のコネクション数
Active waitingpostgres.connections.active_waiting-active かつ waiting イベントが存在する状態のコネクション数
Idlepostgres.connections.idle-idle 状態のコネクション数
Idle in transactionpostgres.connections.idle_in_transaction-idle in transaction(トランザクション中で idle)状態のコネクション数
Idle in transaction (aborted)postgres.connections.idle_in_transaction_aborted_-idle in transaction 状態でトランザクション中にエラーが発生したコネクション数
fast-path function callpostgres.connections.fastpath_function_call-近道関数を実行中のコネクション数
Disabledpostgres.connections.disabled-Disabled 状態のコネクション数(track_activities が無効になっている場合)

Postgres Commits

pg_stat_database から取得できる情報を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Xact Commitpostgres.commits.xact_commit-1分あたりのデータベースでコミットされたトランザクション数
Xact Rollbackpostgres.commits.xact_rollback-1分あたりのデータベースでロールバックされたトランザクション数

Postgres Blocks

pg_stat_database から取得できる情報を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Blocks Readpostgres.blocks.blks_read-1分あたりのディスクから読み込まれたブロック数
Blocks Hitpostgres.blocks.blks_hit-1分あたりのキャッシュから読み込まれたブロック数

Postgres Rows

pg_stat_database から取得できる情報を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Returned Rowspostgres.rows.tup_returned-1分あたりのデータベース内の問い合わせで返された行数
Fetched Rowspostgres.rows.tup_fetched1分あたりのデータベース内の問い合わせで取り出された行数
Inserted Rowspostgres.rows.tup_inserted1分あたりのデータベース内の問い合わせで挿入された行数
Updated Rowspostgres.rows.tup_updated1分あたりのデータベース内の問い合わせで更新された行数
Deleted Rowspostgres.rows.tup_deleted1分あたりのデータベース内の問い合わせで削除された行数

Postgres Data Size

SELECT sum(pg_database_size(datname)) from pg_database を実行した結果を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Total Sizepostgres.size.total_size--全データベースの合計サイズ(バイト)

Postgres Dead Locks

pg_stat_database から取得できる情報を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Deadlockspostgres.deadlocks.deadlocks-1分あたりのデータベース内で検知されたデッドロック数

Postgres Block I/O time

pg_stat_database から取得できる情報を投稿します。このメトリックを投稿するためには track_io_timing を有効にする必要があります。設定が無効な場合は 0 を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Block Read Time (ms)postgres.iotime.blk_read_time-1分間あたりのデータファイルブロックの読み取り時間(ミリ秒)
Block Write Time (ms)postgres.iotime.blk_write_time-1分間あたりのデータファイルブロックの書き出し時間(ミリ秒)

Postgres Temporary file

pg_stat_database から取得できる情報を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Temporary file size (byte)postgres.tempfile.temp_bytes-1分あたりの一時ファイルに書き出されたファイルサイズ(バイト)

Postgres Amount of Transaction location change

WAL に関する情報を投稿します。このメトリックを投稿するためには pg_settings の wal_level を logical に設定する必要があります。logical 以外が設定されている場合は 0 を投稿します。

メトリック表示名メトリック名差分積み上げ表示説明
Amount of Transaction location change (byte)postgres.xlog_location.xlog_location_bytes-1分あたりのログ先行書き込みのサイズ(バイト)
  • PostgreSQL バージョン 10 以上
    • SELECT pg_wal_lsn_diff(pg_current_wal_lsn(), '0/0') の結果
  • PostgreSQL バージョン 9.2 以上
    • SELECT pg_xlog_location_diff(pg_current_xlog_location(), '0/0') の結果

指定可能なオプション

オプション必須説明デフォルト値
-hostname監視対象のホストlocalhost
-port接続先ポート5432
-userユーザ名
-passwordパスワード
-database接続先データベースpostgres
-metric-key-prefixカスタムメトリック名の接頭辞postgres
-sslmodeSSL 接続を使用するdisable
-connect_timeoutタイムアウトまでの時間5(秒)
-tempfiletempfile の保存先ファイルパスの指定
-h, -helpヘルプを表示
  • -user に postgres 以外を指定する場合、-database オプションで接続先を指定する必要があります。なお、指定したデータベースの情報のみを取得するオプションではありません。
  • tempfile には前回の結果が保存されます。デフォルトでは /var/tmp/mackerel-agen 配下に mackerel-plugin-postgres-<ハッシュ文字列> の形式で作成されます。

エージェントへの設定例

toml
[plugin.metrics.postgres]
command = ["mackerel-plugin-postgres", "-user", "username", "-password", "password"]

プラグインの実行に必要なユーザ権限

データベースへ接続するユーザに下記の権限が必要です。

  • PostgreSQL バージョン 10 以上
    • pg_monitor ロールが必要
  • PostgreSQL バージョン 9.6 以下
    • 追加のロールは不要

Tips

PostgreSQL の接続情報の記述方法

mackerel-plugin-postgres の実行に必要な接続情報を -password オプションに直接記述することを避けたい場合は、下記のような方法があります。下記の方法を利用する場合は、command の内容を配列ではなく、サンプルのように文字列で記述する必要があります。

コマンドを利用する

文字列で記述した場合 command の内容はシェル経由の実行となるので、引数にパスワードを出力するコマンドが指定できます。

mackerel-agent.conf の内容

toml
[plugin.metrics.postgres]
command = "mackerel-plugin-postgres -user username -password パスワードを出力するコマンド"

チェック監視の環境変数を利用する

チェック監視の env を利用すれば環境変数として記述できます。env についてはチェック監視の 設定項目 を参照してください。

mackerel-agent.conf の内容

toml
[plugin.metrics.postgres]
command = "mackerel-plugin-postgres -user username -password $PG_PASSWORD"
env = { "PG_PASSWORD" = "password" }

エージェントの環境変数を利用する

mackerel-agent に適用した環境変数も利用できます。詳しくは mackerel-agent仕様環境変数を適用する を参照してください。

下記は Linux 環境で設定する場合の例です。

/etc/sysconfig/mackerel-agent の内容

PG_PASSWORD=password

mackerel-agent.conf の内容

toml
[plugin.metrics.postgres]
command = "mackerel-plugin-postgres -user username -password $PG_PASSWORD"

リポジトリ

[https://github.com/mackerelio/mackerel-agent-plugins/tree/master/mackerel-plugin-postgres]