{HEADER_43146526}
object script.create(object/array scripts)
此方法允许 create 新脚本。
此方法仅适用于 超级管理员 用户类型。 可以在用户角色设置中撤销调用该方法的权限。更多信息请参见 User roles
(object/array)
要创建的脚本。
该方法接受带有 脚本 的脚本。
(object)
返回一个 object,其中包含在 scriptids
属性下列出的已创建脚本的 ID。返回的 ID 顺序与传入的脚本顺序一致。
创建一个向外部服务发送HTTP请求的webhook脚本。
执行请求:
{
"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"
}
]
},
"id": 1
}
响应:
创建一个使用公钥认证的 SSH 脚本,该脚本可在 一个主机 上执行,并具有上下文菜单。
执行请求:
{
"jsonrpc": "2.0",
"method": "script.create",
"params": {
"name": "SSH script",
"command": "my script command",
"type": 2,
"authtype": 1,
"username": "John",
"publickey": "pub.key",
"privatekey": "priv.key",
"password": "secret",
"port": "12345",
"scope": 2,
"menu_path": "All scripts/SSH",
"usrgrpid": "7",
"groupid": "4"
},
"id": 1
}
响应:
创建一个自定义脚本以重启服务器。该脚本将需要写入权限到主机,并且会提示用户进行手动输入。在成功提交输入后,脚本将在前端显示确认消息。
执行请求:
{
"jsonrpc": "2.0",
"method": "script.create",
"params": {
"name": "Reboot server",
"command": "reboot server {MANUALINPUT}",
"type": 0,
"scope": 2,
"confirmation": "Are you sure you would like to reboot the server {MANUALINPUT}?",
"manualinput": 1,
"manualinput_prompt": "Which server you want to reboot?",
"manualinput_validator": "[1-9]",
"manualinput_validator_type": 0,
"manualinput_default_value": "1"
},
"id": 1
}
响应:
为 主机 范围创建一个 URL 类型脚本,该脚本在同一个窗口中打开并包含确认文本。
执行请求:
{
"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
},
"id": 1
}
响应:
为事件范围创建一个 URL 类型脚本,该脚本在新窗口中打开并具有手动输入功能。
执行请求:
{
"jsonrpc": "2.0",
"method": "script.create",
"params": {
"name": "URL script with manual input",
"type": 6,
"scope": 4,
"url": "http://zabbix/ui/zabbix.php?action={MANUALINPUT}",
"new_window": 1,
"manualinput": 1,
"manualinput_prompt": "Select a page to open:",
"manualinput_validator": "dashboard.view,script.list,actionlog.list",
"manualinput_validator_type": 1
},
"id": 1
}
响应:
CScript::create() 在 ui/include/classes/api/services/CScript.php 中。