script.execute

Description

object script.execute(object parameters)

This method allows to run a script on a host or event.

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.

Parameters

(object) Parameters containing the ID of the script to run and either the ID of the host or the ID of the event.

ParameterTypeDescription
scriptid
(required)
stringID of the script to run.
hostidstringID of the host to run the script on.
eventidstringID of the event to run the script on.

Return values

(object) Returns the result of script execution.

PropertyTypeDescription
responsestringWhether the script was run successfully.

Possible values: success or failed.
valuestringScript output.
debugobjectContains a debug object if a webhook script is executed. For other script types, it contains empty object.

Examples

Run a webhook script

Run a webhook script that sends HTTP request to external service.

Request:

  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. }

Response:

  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. }

Run a custom script

Run a “ping” script on a host.

Request:

  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. }

Response:

  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. }

Source

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