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

script.create

説明

object script.create(object/array scripts)

このメソッドで新しいスクリプトを作成できます

このメソッドはSuper adminユーザータイプでのみ使用できます。 メソッドを呼び出す権限は、ユーザーロール設定で取り消すことができます。 詳細についてはユーザーの役割を参照してください。

パラメータ

(object/array) 作成するスクリプト

このメソッドは、標準のスクリプトプロパティを持つスクリプトを受け入れます

戻り値

(object) scriptidsプロパティの下で作成されたスクリプトのIDを含むオブジェクトを返します。 返されるIDの順序は、渡されたスクリプトの順序と一致します。

Webhookスクリプトを作成する

HTTPリクエストを外部サービスに送信するWebhookスクリプトを作成します。

Request:

{  "jsonrpc": "2.0",  "method": "script.create",  "params": {  "name": "Webhook script",  "command": "try {\n var request = new HttpRequest(),\n response,\n data;\n\n request.addHeader('Content-Type: application/json');\n\n response = request.post('https://localhost/post', value);\n\n try {\n response = JSON.parse(response);\n }\n catch (error) {\n response = null;\n }\n\n if (request.getStatus() !== 200 || !('data' in response)) {\n throw 'Unexpected response.';\n }\n\n data = JSON.stringify(response.data);\n\n Zabbix.Log(3, '[Webhook Script] response data: ' + data);\n\n return data;\n}\ncatch (error) {\n Zabbix.Log(3, '[Webhook Script] script execution failed: ' + error);\n throw 'Execution failed: ' + error + '.';\n}",  "type": 5,  "timeout": "40s",  "parameters": [  {  "name": "token",  "value": "{$WEBHOOK.TOKEN}"  },  {  "name": "host",  "value": "{HOST.HOST}"  },  {  "name": "v",  "value": "2.2"  }  ]  },  "auth": "038e1d7b1735c6a5436ee9eae095879e",  "id": 1 }

Response:

{  "jsonrpc": "2.0",  "result": {  "scriptids": [  "3"  ]  },  "id": 1 }

SSHスクリプトを作成する

コンテキストメニューを備えた公開鍵認証を使用してホスト上で実行できるSSHスクリプトを作成します

Request:

{  "jsonrpc": "2.0",  "method": "script.create",  "params": {  "name": "SSH script",  "command": "my script command",  "type": 2,  "username": "John",  "publickey": "pub.key",  "privatekey": "priv.key",  "password": "secret",  "port": "12345",  "scope": 2,  "menu_path": "All scripts/SSH",  "usrgrpid": "7",  "groupid": "4"  },  "auth": "038e1d7b1735c6a5436ee9eae095879e",  "id": 1 }

Response:

{  "jsonrpc": "2.0",  "result": {  "scriptids": [  "5"  ]  },  "id": 1 }

カスタムスクリプトを作成する

サーバーを再起動するカスタムスクリプトを作成します。 スクリプトはホストへの書き込みアクセスを必要とし、フロントエンドで実行する前に設定メッセージを表示します。

Request:

{  "jsonrpc": "2.0",  "method": "script.create",  "params": {  "name": "Reboot server",  "command": "reboot server 1",  "confirmation": "Are you sure you would like to reboot the server?",  "scope": 2,  "type": 0  },  "auth": "038e1d7b1735c6a5436ee9eae095879e",  "id": 1 }

Response:

{  "jsonrpc": "2.0",  "result": {  "scriptids": [  "4"  ]  },  "id": 1 }

URL タイプのスクリプト作成

同じウィンドウに表示され、確認テキストを含む、ホスト スコープ用の URL タイプのスクリプトを作成します。

リクエスト:

{  "jsonrpc": "2.0",  "method": "script.create",  "params": {  "name": "URL script",  "type": 6,  "scope": 2,  "url": "http://zabbix/ui/zabbix.php?action=host.edit&hostid={HOST.ID}",  "confirmation": "Edit host {HOST.NAME}?",  "new_window": 0  },  "id": 1 }

Response:

{  "jsonrpc": "2.0",  "result": {  "scriptids": [  "56"  ]  },  "id": 1 }

手動入力が可能な URL 型スクリプト作成

新しいウィンドウで開き、手動入力が可能なイベントスコープ用の URL 型スクリプトを作成します。

リクエスト:

{  "jsonrpc": "2.0",  "method": "script.create",  "params": {  "name": "URL script with manual input",  "type": 6,  "scope": 4,  "url": "http://zabbix/ui/zabbix.php?action={MANUALINPUT}",  "new_window": 1,  "manualinput": 1,  "manualinput_prompt": "Select a page to open:",  "manualinput_validator": "dashboard.view,script.list,actionlog.list",  "manualinput_validator_type": 1  },  "id": 1 }

Response:

{  "jsonrpc": "2.0",  "result": {  "scriptids": [  "57"  ]  },  "id": 1 }

ソース

CScript::create() in ui/include/classes/api/services/CScript.php.