创建

描述

object script.create(object/array scripts)

此方法允许创建新脚本。 ::: 请注意 此方法只有_超级管理员_ 用户可以使用。可以在用户角色设置中撤销调用此方法的权限。更多信息见User roles。 :::

参数

(对象/数组) 要创建的脚本。

此方法接受具有标准脚本属性的脚本。

返回值

(对象) 返回一个scriptids属性包含被创建脚本ID的对象。返回的ID顺序与传入脚本的顺序一致。

示例

创建webhook脚本

创建一个webhook脚本向外部服务发送HTTP请求。 请求:

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

响应

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

创建SSH脚本

创建一个SSH脚本,通过公钥完成身份验证从而可以在主机运行,并且带有上下文菜单。 请求:

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

响应

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

创建定制脚本

创建一个重启服务器的定制脚本。该脚本会要求主机的写权限,并且在前端运行之前会显示配置信息。 请求:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "script.create",
  4. "params": {
  5. "name": "Reboot server",
  6. "command": "reboot server {MANUALINPUT}",
  7. "type": 0,
  8. "scope": 2,
  9. "confirmation": "Are you sure you would like to reboot the server {MANUALINPUT}?",
  10. "manualinput": 1,
  11. "manualinput_prompt": "Which server you want to reboot?",
  12. "manualinput_validator": "[1-9]",
  13. "manualinput_validator_type": 0,
  14. "manualinput_default_value": "1"
  15. },
  16. "id": 1
  17. }

响应

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

创建一个URL类型的脚本

为主为作用域创建一个URL类型的脚本,该脚本会要求主机的写权限,并且在前端运行之前会显示配置信息。 请求:

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

响应

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

创建一个手动输入的URL类型脚本

为在新窗口中打开并具有手动输入的事件作用域创建URL类型脚本。 请求:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "script.create",
  4. "params": {
  5. "name": "URL script with manual input",
  6. "type": 6,
  7. "scope": 4,
  8. "url": "http://zabbix/ui/zabbix.php?action={MANUALINPUT}",
  9. "new_window": 1,
  10. "manualinput": 1,
  11. "manualinput_prompt": "Select a page to open:",
  12. "manualinput_validator": "dashboard.view,script.list,actionlog.list",
  13. "manualinput_validator_type": 1
  14. },
  15. "id": 1
  16. }

响应

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

源代码

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