执行

描述

object script.execute(object parameters) 此方法允许在某主机或事件上运行脚本。

此方法允许任何用户使用。可以在用户角色设置中撤销调用此方法的权限。更多信息见 User roles

参数

(对象) 参数包含了运行脚本的ID,以及主机ID或者事件ID。

参数类型描述
scriptid
(必需)
string运行脚本的ID。
hostidstring在其上运行脚本的主机ID。
eventidstring在其上运行脚本的事件ID。

返回值

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

属性类型描述
responsestring脚本是否成功运行。

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

示例

运行 webhook 脚本

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

请求:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "script.execute",
  4. "params": {
  5. "scriptid": "4",
  6. "hostid": "30079"
  7. },
  8. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  9. "id": 1
  10. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "response": "success",
  5. "value": "{\"status\":\"sent\",\"timestamp\":\"1611235391\"}",
  6. "debug": {
  7. "logs": [
  8. {
  9. "level": 3,
  10. "ms": 480,
  11. "message": "[Webhook Script] HTTP status: 200."
  12. }
  13. ],
  14. "ms": 495
  15. }
  16. },
  17. "id": 1
  18. }

运行自定义脚本

在主机上运行“ping”脚本。

请求:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "script.execute",
  4. "params": {
  5. "scriptid": "1",
  6. "hostid": "30079"
  7. },
  8. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  9. "id": 1
  10. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "response": "success",
  5. "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",
  6. "debug": []
  7. },
  8. "id": 1
  9. }

源代码

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