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

18 スクリプト item

概要

スクリプト item は、ユーザー定義のJavaScriptコードを実行してデータを収集するために使用されます。
HTTP/HTTPSでデータを取得する機能を持つ、ユーザー定義のJavaScriptコードを実行することによって、
データを収集することができます。スクリプトの他に、オプションでパラメータ(名前と値のペア)のリストとタイムアウトを
指定することができます。

この item タイプは、複数のステップや複雑なロジックを必要とするデータ収集シナリオに有用であると思われます。
例えば、スクリプト item は、HTTPコールを行い、最初のステップで受信したデータを何らかの方法で処理し、
変換された値を2番目のHTTPコールに渡すように設定することが可能です。

スクリプトアイテムはZabbix server または proxy ポーラによって処理されます。

設定

item configuration formTypeフィールドでScriptを選択し、必要事項を入力します。

script_item.png

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

スクリプトアイテムで特定の情報を必要とするフィールドは次のとおりです。

フィールド 説明
Key Enter a unique key that will be used to identify the item.
Parameters Specify the variables to be passed to the script as the attribute and value pairs.
Built-in macros {HOST.CONN}, {HOST.DNS}, {HOST.HOST}, {HOST.IP}, {HOST.NAME}, {ITEM.ID}, {ITEM.KEY}, {ITEM.KEY.ORIG} and user macros are supported.
Script Enter JavaScript code in the block that appears when clicking in the parameter field (or on the view/edit button next to it). This code must provide the logic for returning the metric value.
The code has access to all parameters, it may perform HTTP GET, POST, PUT and DELETE requests and has control over HTTP headers and request body.
See also: Additional JavaScript objects, JavaScript Guide.
Timeout JavaScript execution timeout (1-60s, default 3s); exceeding it will return error.
Time suffixes are supported, e.g. 30s, 1m.
Depending on the script it might take longer for the timeout to trigger.

シンプルなデータ収集

https://www.example.com/release_notes の内容を収集します。

  • タイプ "Script"の item を作成します。
  • Script 欄に、次のコードを入力します。
var request = new HttpRequest();
       return request.get("https://www.example.com/release_notes");
パラメータによるデータ収集

パラメータ値として{HOST.CONN}マクロを使用し、マクロを展開してレスポンスを取得します。

  • タイプ "Script" の item を作成します。
  • 以下のパラメータを作成します: Name: host
    Value: {HOST.CONN}
  • scriptの欄に、以下のコードを入力します:
var request = new HttpRequest();
       return request.post("https://postman-echo.com/post", JSON.parse(value));
複数のHTTPリクエスト

https://www.example.comhttps://www.example.com/release_notes の両方の内容を収集します。

  • タイプ "Script" の item を作成します。
  • Scriptの欄に、以下のコードを入力します:
var request = new HttpRequest();
       return request.get("https://www.example.com") + request.get("https://www.example.com/release_notes");
ロギング

Zabbix server ログに "Log test" エントリを追加し、アイテム値 "1" を返信します。

  • タイプ "Script" の item を作成します。
  • Script フィールドに、以下のコードを入力します。

`` {.java} Zabbix.log(3, 'Log test'); return 1; ```