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

script.execute

説明

object script.execute(object parameters)

このメソッドで、ホストまたはイベントでスクリプトを実行できます。

このメソッドはどのタイプのユーザーでも利用可能です。このメソッドを呼び出す権限は、ユーザーロール設定で取り消すことができます。詳しくはユーザーの役割を参照してください。

パラメータ

(object) 実行するスクリプトのIDと、ホストIDまたはイベントIDのいずれかを含むパラメーター。

パラメータ タイプ 説明
scriptid
(必須)
string 実行するスクリプトのID
hostid string スクリプトを実行するホストのID
eventid string スクリプトを実行するイベントのID

戻り値

(object) スクリプトの実行結果を返します。

プロパティ タイプ 説明
response string スクリプトが正常に実行されたか。

可能な値 - success
value string スクリプトの出力
debug object Webhook スクリプトが実行される場合、デバッグ オブジェクト が含まれます。 他のスクリプト タイプの場合、空のオブジェクトが含まれます。

Webhookスクリプトを実行する

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

Request:

{  "jsonrpc": "2.0",  "method": "script.execute",  "params": {  "scriptid": "4",  "hostid": "30079"  },  "auth": "038e1d7b1735c6a5436ee9eae095879e",  "id": 1 }

Response:

{  "jsonrpc": "2.0",  "result": {  "response": "success",  "value": "{\"status\":\"sent\",\"timestamp\":\"1611235391\"}",  "debug": {  "logs": [  {  "level": 3,  "ms": 480,  "message": "[Webhook Script] HTTP status: 200."  }  ],  "ms": 495  }  },  "id": 1 }

カスタムスクリプト実行

ホストで"ping"スクリプトを実行します。

Request:

{  "jsonrpc": "2.0",  "method": "script.execute",  "params": {  "scriptid": "1",  "hostid": "30079"  },  "auth": "038e1d7b1735c6a5436ee9eae095879e",  "id": 1 }

Response:

{  "jsonrpc": "2.0",  "result": {  "response": "success",  "value": "PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.\n64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.074 ms\n64 bytes from 127.0.0.1: icmp_req=2 ttl=64 time=0.030 ms\n64 bytes from 127.0.0.1: icmp_req=3 ttl=64 time=0.030 ms\n\n--- 127.0.0.1 ping statistics ---\n3 packets transmitted, 3 received, 0% packet loss, time 1998ms\nrtt min/avg/max/mdev = 0.030/0.044/0.074/0.022 ms\n",  "debug": []  },  "id": 1 }

手動でカスタムスクリプトを実行する

ホスト上でコマンド"ping -c {MANUALINPUT} {HOST.CONN}; case $? in [01]) true;; *) false;; esac"を使用して "ping" スクリプトを実行します。

リクエスト:

{  "jsonrpc": "2.0",  "method": "script.execute",  "params": {  "scriptid": "7",  "hostid": "30079",  "manualinput": "2"  },  "id": 1 }

Response:

{  "jsonrpc": "2.0",  "result": {  "response": "success",  "value": "PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.\n64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.051 ms\n64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.025 ms\n\n--- 127.0.0.1 ping statistics ---\n2 packets transmitted, 2 received, 0% packet loss, time 1021ms\nrtt min/avg/max/mdev = 0.025/0.038/0.051/0.013 ms",  "debug": []  },  "id": 1 }

ソース

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