2022 Zabbix中国峰会
2022 Zabbix中国峰会

执行

Description

描述

object script.execute(object parameters)

This method allows to run a script on a host or event. Except for URL type scripts. Those are not executable. 此方法允许在某主机或事件上运行脚本。 ::: noteclassic This method is available to users of any type. Permissions to call the method can be revoked in user role settings. See User roles for more information. ::: ::: 请注意 此方法允许任何用户使用。可以在用户角色设置中撤销调用此方法的权限。更多信息见 User roles。 :::

Parameters

参数

(object) Parameters containing the ID of the script to run, either the ID of the host or the ID of the event and manualinput value. (对象) 参数包含了运行脚本的ID,以及主机ID或者事件ID。 |参数|类型|描述| |---------|---------------------------------------------------|-----------| |scriptid
(必需)|string|运行脚本的ID。| |hostid|string|在其上运行脚本的主机ID。| |eventid|string|在其上运行脚本的事件ID。| |manualinput|string|用于运行脚本的用户提供的值,替换{MANUALPINPUT}宏。|

Return values

返回值

(object) Returns the result of script execution. (object) 返回脚本执行的结果。 |属性|类型|描述| |--|--|------| |response|string|脚本是否成功运行。

可能的值 - successfailed。| |值|字符串|脚本输出。| |debug|object|如果执行 webhook 脚本,则包含 调试对象。对于其他脚本类型,它包含空对象。|

示例

Run a webhook script

运行 webhook 脚本

Run a webhook script that sends HTTP request to external service. 运行一个向外部服务发送HTTP请求的webhook脚本。 Request: 请求

{
           "jsonrpc": "2.0",
           "method": "script.execute",
           "params": {
               "scriptid": "4",
               "hostid": "30079"
           },
           "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”脚本。

请求:

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

Run a custom script with manual input

运行自定义脚本

Run a "ping" script with command "ping -c {MANUALINPUT} {HOST.CONN}; case $? in [01]) true;; ) false;; esac" on a host. 在主机上运行“ping”脚本,在主机上使用命令“ping -c {MANUALINPUT} {HOST.CONN}; case $? in [01]) true;; ) false;; esac”。 Request: 请求

{
           "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() 在ui/include/classes/api/services/CScript.php