[comment]: # translation:outdated

[comment]: # ({1a1e2756-1a1e2756})
# 18 スクリプト

[comment]: # ({/1a1e2756-1a1e2756})

[comment]: # ({0f769f40-cb89bc38})
#### 概要

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

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

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

[comment]: # ({/0f769f40-cb89bc38})

[comment]: # ({af9dfa85-7ed55e87})
#### 設定

[item configuration form](/manual/config/items/item)の*Type*フィールドでScriptを選択し、必要事項を入力します。

![script\_item.png](../../../../../assets/en/manual/config/items/itemtypes/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.<br>[Built-in macros](/manual/config/macros) {HOST.CONN}, {HOST.DNS}, {HOST.HOST}, {HOST.IP}, {HOST.NAME}, {ITEM.ID}, {ITEM.KEY}, {ITEM.KEY.ORIG} and [user macros](/manual/config/macros/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.<br>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.<br>See also: [Additional JavaScript objects](/manual/config/items/preprocessing/javascript/javascript_objects), [JavaScript Guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide).|
|Timeout|JavaScript execution timeout (1-60s, default 3s); exceeding it will return error.<br>Time suffixes are supported, e.g. 30s, 1m.<br>Depending on the script it might take longer for the timeout to trigger. |

[comment]: # ({/af9dfa85-7ed55e87})

[comment]: # ({4c477658-4c860844})
#### 例

[comment]: # ({/4c477658-4c860844})

[comment]: # ({6f34fd12-9c6c78ee})
##### シンプルなデータ収集

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

- タイプ "Script"の item を作成します。
- *Script* 欄に、次のコードを入力します。

``` {.java}
var request = new HttpRequest();
return request.get("https://www.example.com/release_notes");
```

[comment]: # ({/6f34fd12-9c6c78ee})

[comment]: # ({7e716947-7115c5ef})
##### パラメータによるデータ収集

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

-   タイプ "Script" の item を作成します。
-   以下のパラメータを作成します:
    *Name:* host\
    *Value:* {HOST.CONN}\
-   *script*の欄に、以下のコードを入力します:

``` {.java}
var request = new HttpRequest();
return request.post("https://postman-echo.com/post", JSON.parse(value));
```

[comment]: # ({/7e716947-7115c5ef})

[comment]: # ({565bf58a-18a089f4})
##### 複数のHTTPリクエスト

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

-   タイプ "Script" の item を作成します。
-   *Script*の欄に、以下のコードを入力します:

``` {.java}
var request = new HttpRequest();
return request.get("https://www.example.com") + request.get("https://www.example.com/release_notes");
```

[comment]: # ({/565bf58a-18a089f4})

[comment]: # ({0a711007-d837bcdd})
##### ロギング

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

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

```javascript
Zabbix.log(3, 'Log test');
return 1;
```

[comment]: # ({/0a711007-d837bcdd})
