创建

描述

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. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  25. "id": 1
  26. }

响应:

  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. "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. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  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 1",
  7. "confirmation": "Are you sure you would like to reboot the server?",
  8. "scope": 2,
  9. "type": 0
  10. },
  11. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  12. "id": 1
  13. }

响应:

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

Create an URL type script

Create an 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. "auth": "038e1d7b1735c6a5436ee9eae095879e",
  13. "id": 1
  14. }

Response:

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

源代码

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