script.create

Description

object script.create(object/array scripts)

This method allows to create new scripts.

This method is only available to Super admin user type. Permissions to call the method can be revoked in user role settings. See User roles for more information.

Parameters

(object/array) Scripts to create.

The method accepts scripts with the standard script properties.

Return values

(object) Returns an object containing the IDs of the created scripts under the scriptids property. The order of the returned IDs matches the order of the passed scripts.

Examples

Create a webhook script

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

Request:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "script.create",
  4. "params": {
  5. "name": "Webhook script",
  6. "command": "try {\n var request = new HttpRequest(),\n response,\n data;\n\n request.addHeader('Content-Type: application/json');\n\n response = request.post('https://localhost/post', value);\n\n try {\n response = JSON.parse(response);\n }\n catch (error) {\n response = null;\n }\n\n if (request.getStatus() !== 200 || !('data' in response)) {\n throw 'Unexpected response.';\n }\n\n data = JSON.stringify(response.data);\n\n Zabbix.log(3, '[Webhook Script] response data: ' + data);\n\n return data;\n}\ncatch (error) {\n Zabbix.log(3, '[Webhook Script] script execution failed: ' + error);\n throw 'Execution failed: ' + error + '.';\n}",
  7. "type": 5,
  8. "timeout": "40s",
  9. "parameters": [
  10. {
  11. "name": "token",
  12. "value": "{$WEBHOOK.TOKEN}"
  13. },
  14. {
  15. "name": "host",
  16. "value": "{HOST.HOST}"
  17. },
  18. {
  19. "name": "v",
  20. "value": "2.2"
  21. }
  22. ]
  23. },
  24. "id": 1
  25. }

Response:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "scriptids": [
  5. "3"
  6. ]
  7. },
  8. "id": 1
  9. }

Create an SSH script

Create an SSH script with public key authentication that can be executed on a host and has a context menu.

Request:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "script.create",
  4. "params": {
  5. "name": "SSH script",
  6. "command": "my script command",
  7. "type": 2,
  8. "username": "John",
  9. "publickey": "pub.key",
  10. "privatekey": "priv.key",
  11. "password": "secret",
  12. "port": "12345",
  13. "scope": 2,
  14. "menu_path": "All scripts/SSH",
  15. "usrgrpid": "7",
  16. "groupid": "4"
  17. },
  18. "id": 1
  19. }

Response:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "scriptids": [
  5. "5"
  6. ]
  7. },
  8. "id": 1
  9. }

Create a custom script

Create a custom script that will reboot a server. The script will require write access to the host and will display a configuration message before running in the frontend.

Request:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "script.create",
  4. "params": {
  5. "name": "Reboot server",
  6. "command": "reboot server 1",
  7. "confirmation": "Are you sure you would like to reboot the server?",
  8. "scope": 2,
  9. "type": 0
  10. },
  11. "id": 1
  12. }

Response:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "scriptids": [
  5. "4"
  6. ]
  7. },
  8. "id": 1
  9. }

Create a URL type script

Create a URL type script that for host scope and remains in same window and has confirmation text.

Request:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "script.create",
  4. "params": {
  5. "name": "URL script",
  6. "type": 6,
  7. "scope": 2,
  8. "url": "http://zabbix/ui/zabbix.php?action=host.edit&hostid={HOST.ID}",
  9. "confirmation": "Edit host {HOST.NAME}?",
  10. "new_window": 0
  11. },
  12. "id": 1
  13. }

Response:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "scriptids": [
  5. "56"
  6. ]
  7. },
  8. "id": 1
  9. }

Source

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