script.execute

説明

object script.execute(object parameters)

このメソッドでは、ホストまたはイベントでスクリプトを実行できます。ただし、URLタイプのスクリプトは除きます。これらは実行できません。

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

パラメータ

(object) 実行するスクリプトのID、およびホストのIDまたはイベントのIDとmanualinput値を含むパラメータです。

パラメータ Type 説明
scriptid ID 実行するscriptのID。

Parameter behavior:
- required
hostid ID スクリプトを実行するホストのID。

Parameter behavior:
- eventid が設定されていない場合は required
eventid ID スクリプトを実行するeventのID。

Parameter behavior:
- hostid が設定されていない場合は required
manualinput string {MANUALINPUT} マクロを置換してスクリプトを実行するための、ユーザー指定の値。

戻り値

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

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

指定可能な値 - success
value string スクリプトの出力。
debug object webhookスクリプトが実行された場合は、debug オブジェクトを含みます。その他のスクリプトタイプでは、空のオブジェクトを含みます。

webhookスクリプトの実行

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

リクエスト:

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

レスポンス:

{
    "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" スクリプトを実行します。

リクエスト:

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

レスポンス:

{
    "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
}

レスポンス:

{
    "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\n",
        "debug": []
    },
    "id": 1
}

ソース

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