这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.
2022 Zabbix中国峰会
2022 Zabbix中国峰会

创建

描述

object script.create(object/array scripts)

此方法允许创建新脚本。

此方法只有超级管理员 用户可以使用。可以在用户角色设置中撤销调用此方法的权限。更多信息见User roles

参数

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

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

返回值

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

示例

创建webhook脚本

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

请求:

{
           "jsonrpc": "2.0",
           "method": "script.create",
           "params": {
               "name": "Webhook script",
               "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}",
               "type": 5,
               "timeout": "40s",
               "parameters": [
                   {
                       "name": "token",
                       "value": "{$WEBHOOK.TOKEN}"
                   },
                   {
                       "name": "host",
                       "value": "{HOST.HOST}"
                   },
                   {
                       "name": "v",
                       "value": "2.2"
                   }
               ]
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "scriptids": [
                   "3"
               ]
           },
           "id": 1
       }

创建SSH脚本

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

请求:

{
           "jsonrpc": "2.0",
           "method": "script.create",
           "params": {
               "name": "SSH script",
               "command": "my script command",
               "type": 2,
               "username": "John",
               "publickey": "pub.key",
               "privatekey": "priv.key",
               "password": "secret",
               "port": "12345",
               "scope": 2,
               "menu_path": "All scripts/SSH",
               "usrgrpid": "7",
               "groupid": "4"
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "scriptids": [
                   "5"
               ]
           },
           "id": 1
       }

创建定制脚本

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

请求:

{
           "jsonrpc": "2.0",
           "method": "script.create",
           "params": {
               "name": "Reboot server",
               "command": "reboot server 1",
               "confirmation": "Are you sure you would like to reboot the server?",
               "scope": 2,
               "type": 0
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "scriptids": [
                   "4"
               ]
           },
           "id": 1
       }

Create an URL type script

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

Request:

{
           "jsonrpc": "2.0",
           "method": "script.create",
           "params": {
               "name": "URL script",
               "type": 6,
               "scope": 2,
               "url": "http://zabbix/ui/zabbix.php?action=host.edit&hostid={HOST.ID}",
               "confirmation": "Edit host {HOST.NAME}?",
               "new_window": 0
           },
           "auth": "038e1d7b1735c6a5436ee9eae095879e",
           "id": 1
       }

Response:

{
           "jsonrpc": "2.0",
           "result": {
               "scriptids": [
                   "56"
               ]
           },
           "id": 1
       }

源代码

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