script.execute

描述

object script.execute(object parameters)

该方法允许在主机或事件上运行脚本。

此方法适用于所有类型的用户。调用该方法的权限可以在用户角色设置中撤销。更多信息请参阅User roles

参数

(object) 参数包含要运行脚本的ID以及主机的ID或事件的ID。

参数 数据类型 描述
scriptid
(required)
string 要运行脚本的ID。
hostid string 运行脚本的目标主机的ID。
eventid string 运行脚本的目标事件的ID。

返回值

(object) 返回脚本执行结果。

属性 数据类型 描述
response string 脚本是否成功运行。

可能的值 - success
value string 脚本输出内容。
debug object 当执行webhook脚本时包含调试。对于其他脚本类型,包含空的object。

示例

运行 webhook 脚本

运行向外部服务发送HTTP请求的webhook脚本。

请求:

{
           "jsonrpc": "2.0",
           "method": "script.execute",
           "params": {
               "scriptid": "4",
               "hostid": "30079"
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "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"
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "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
       }

来源

位于 ui/include/classes/api/services/CScript.php 中的 CScript::execute() 方法。