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

5 ユーザパラメータ

概要

Zabbixにあらかじめ定義されていないエージェントチェックを実行したい場合があります。
このような場合、ユーザパラメータが役に立ちます。

必要なデータを取得するコマンドを作成し、そのデータをユーザパラメータとしてagent configuration file('UserParameter') という設定パラメータを作成します。

ユーザパラメータは次のような構文になります。

UserParameter=<key>,<command>

このように、ユーザパラメータにはキーも含まれます。キーは、item を設定する際に必要となります。
参照しやすい任意のキーを入力します(ホスト内で一意でなければなりません)。

agent を再起動するか、agent runtime control オプションを使用して、新しいパラメータを取得します。

zabbix_agentd -R userparameter_reload 

次に、configuring an itemの際に、実行したいユーザパラメータからコマンドを参照するためのキーを入力します。

ユーザパラメータとは、Zabbix agent が実行するコマンドのことです。item のプリプロセスステップの前に
最大512KBのデータを返すことができます。ただし、最終的にデータベースに保存できるテキスト値は、
MySQLでは64KBに制限されていることに注意してください。
(他のデータベースについては、tableを参照してください)。

UNIX オペレーティングシステムでは、コマンドラインインタプリタとして /bin/sh が使用されます。
ユーザーパラメータはエージェントチェックのタイムアウトに従います。タイムアウトに達すると
フォークされたユーザーパラメータ・プロセスが終了します。

こちらもご覧ください。

単純なユーザーパラメータの例

単純なコマンド:

UserParameter=ping,echo 1

agent は、'ping' キーを持つ item に対して常に '1' を返します。

より複雑な例:

UserParameter=mysql.ping,mysqladmin -uroot ping | grep -c alive

agent は、MySQL サーバが生きている場合は '1' を、そうでない場合は '0' を返します。

フレキシブルユーザーパラメータ

フレキシブルユーザーパラメータは、キーでパラメータを受け取ります。
このように、フレキシブルユーザーパラメータは、複数の item を作成するためのベースとなることができます。

フレキシブルユーザーパラメーターの構文は以下の通りです。

UserParameter=key[*],command
パラメータ 説明
Key Unique item key. The [*] defines that this key accepts parameters within the brackets.
Parameters are given when configuring the item.
Command Command to be executed to evaluate value of the key.
For flexible user parameters only:
You may use positional references $1…$9 in the command to refer to the respective parameter in the item key.
Zabbix parses the parameters enclosed in [ ] of the item key and substitutes $1,...,$9 in the command accordingly.
$0 will be substituted by the original command (prior to expansion of $0,...,$9) to be run.
Positional references are interpreted regardless of whether they are enclosed between double (") or single (') quotes.
To use positional references unaltered, specify a double dollar sign - for example, awk '{print $$2}'. In this case $$2 will actually turn into $2 when executing the command.

Zabbix agent では、柔軟なユーザパラメータの場合のみ、$記号を含む位置参照が検索され、置換されます。
シンプルなユーザパラメータでは、このような参照処理はスキップされるため、$記号の引用は必要ありません。

デフォルトでは、ユーザパラメータに特定のシンボルを使用することはできません。 全リストは UnsafeUserParameters ドキュメントを参照してください。

例1

とてもシンプルなもの:

UserParameter=ping[*],echo $1

ping[something]というフォーマットを持つ監視項目は無制限に定義することができる。

  • ping[0] - 常に '0' を戻す
  • ping[aaa] - 常に 'aaa' を戻す
例2

もっとセンスを磨こう!

UserParameter=mysql.ping[*],mysqladmin -u$1 -p$2 ping | grep -c alive

このパラメータは、MySQLデータベースの可用性を監視するために使用することができます。
ユーザー名とパスワードを渡すことができます:

mysql.ping[zabbix,our_password]
例3

正規表現にマッチする行はファイル中に何行あるか?

UserParameter=wc[*],grep -c "$2" $1

このパラメータは、ファイル内の行数を計算するために使用することができます。

wc[/etc/passwd,root]
       wc[/etc/services,zabbix]

コマンドの結果

コマンドの戻り値は、標準出力と標準エラー出力です。

標準エラー出力の場合、テキスト(文字、ログ、テキスト型情報)item は未サポ―トになりません。

テキスト(文字、ログ、テキスト型情報)を返すユーザーパラメータは、空白を返すことができます。
無効な結果を返した場合、そのitem は サポートされなくなります。